我试图在设备屏幕的中心附加一个球体,当我围绕球体移动设备时,它应该保持在屏幕的中心(如十字准线)
我已经附加了一个球体实体并将其添加到 sphere_anchor 中,就像在 makeUIView 函数中一样 sphere_anchor.addChild(modelEntity)
但是当我移动设备时,球体只会在初始帧中移动,当我移动设备时,实体会附加到该实体。希望有人能指出我执行此操作的正确方法
//Implement ARSession didUpdate session delegate method
public func session(_ session: ARSession, didUpdate frame: ARFrame) {
let trasnform = frame.camera.transform
if ((self.scene.findEntity(named: "sphere")) != nil) {
let position = simd_make_float3(trasnform.columns.3)
//print(position)
sphere_anchor.position = position
sphere_anchor.orientation = Transform(matrix: trasnform).rotation
}
}
最佳答案
尝试AnchorEntity(.camera)
。如果您实现它,则不需要 session(_:didUpdate:)
实例方法,因为 RealityKit 的 anchor 会自动跟踪 ARCamera 位置。
@IBOutlet var arView: ARView!
override func viewDidLoad() {
super.viewDidLoad()
let mesh = MeshResource.generateSphere(radius: 0.1)
let sphere = ModelEntity(mesh: mesh)
let anchor = AnchorEntity(.camera)
sphere.setParent(anchor)
arView.scene.addAnchor(anchor)
sphere.transform.translation.z = -0.75
}
AnchorEntity(.camera)
仅当选择 Active Scheme
中的真实 iOS 设备时才有效。
关于swift - 移动时将球体附加到设备屏幕的中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65793876/