移动时在设备屏幕中心附加球体

问题描述

我试图在设备屏幕的中心附加一个球体,当我在球体周围移动设备时,该球体应该保持在屏幕的中心(如十字准线)

我已经附加了一个球体实体,并在 makeUIView 函数中像这样将它添加到 sphere_anchor 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 的锚点会自动跟踪 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 设备时有效。

enter image description here

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...