如何解决如何将实体与 RealityKit/ARkit 中的 3 个独立锚点对齐?
我正在使用 ARKit 和 RealityKit 开发应用。
我想在场景中放置一个虚拟框,并将它与三个平面(x,y,z)中的物理对象对齐
我能够将盒子放在场景中(在地板上)。
框已经与 z 轴对齐 我认为在放置盒子后,您应该选择要对齐的平面。
- 首先触摸物理对象的 x 轴平面
- 第二次触摸物理对象的 z 轴平面
下面是我使用的代码
var box: ModelEntity!
var numberOfTouches: Int = 0
@objc func handleTap(sender: UITapGestureRecognizer) {
print("tap")
let point:CGPoint = sender.location(in: nil)
guard let queryFirst = self.sceneView.makeRaycastQuery(
from: point,allowing: .existingPlaneInfinite,//.estimatedPlane,alignment: self.box == nil ? .horizontal : .vertical
)
else { return }
let result = self.sceneView.session.raycast(queryFirst)
guard let raycastResult = result.first
else { return }
let anchor = AnchorEntity(raycastResult: raycastResult)
//If there is no box,then put it in the scene
if(self.box == nil){
var material = SimpleMaterial()
material.tintColor = UIColor.init(red: 0.0,green: 0.0,blue: 1.0,alpha: 1)
material.baseColor = MaterialColorParameter.color(UIColor.white)
self.box = ModelEntity(
mesh: MeshResource.generateBox(size: 0.25),materials: [material]
)
//is already aligned to the z-axis
anchor.addChild(self.box)
sceneView.scene.anchors.append(anchor)
}else{
//If there is a box in the scene,then align to the current anchor:
//1) first touch on plane of the physical object - align on the x-axis
//2) second touch on the other plane of the physical object - align on the z-axis
numberOfTouches += 1
if(numberOfTouches == 1){
// How align self.box on x-axis anchor ?
}
if(numberOfTouches == 2){
// How align self.box on y-axis anchor ?
}
}
}
如何与其他轴对齐? 而为了让盒子适合物体的位置?
我可能应该这样做:
let orientation: simd_quatf = anchor.orientation(relativeTo: self.box)
self.box.setOrientation(orientation,relativeTo: anchor)
但这对我不起作用。请帮帮我。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。