在RealityKit中可视化检测到的平面问题



伙计们我想使用下面的代码在RealityKit中可视化检测到的平面,但结果是检测到的飞机随着相机的移动而浮动(不是完全浮动,有点,但很明显(,所以,我的问题是如何解决这个问题?

有身体能帮上忙吗?

struct ARViewContainer: UIViewRepresentable {
func makeUIView(context: Context) -> ARView {
let arView = ARView(frame: .zero)
let config = ARWorldTrackingConfiguration()
config.planeDetection = .horizontal
arView.debugOptions = [.showFeaturePoints, .showWorldOrigin]
arView.session.run(config, options:[ ])
arView.session.delegate = arView
arView.CreatePlane()
return arView
}
func updateUIView(_ uiView: ARView, context: Context) {
}
}
var planeMesh = MeshResource.generatePlane(width: 0, depth: 0)
var planeEntity = ModelEntity(mesh:planeMesh)
extension ARView : ARSessionDelegate{
func CreatePlane(){
let planeAnchor = AnchorEntity(plane:.horizontal)
//planeEntity.transform.translation = SIMD3(0,0,0)
planeAnchor.addChild(planeEntity)
self.scene.addAnchor(planeAnchor)
}
public func session(_ session: ARSession, didUpdate anchors: [ARAnchor]){
guard let planeAnchor = anchors[0] as? ARPlaneAnchor else {
return
}
DispatchQueue.main.async {
let position = planeAnchor.transform.toTranslation()
let orientation = planeAnchor.transform.toQuaternion()
let rotatedCenter = orientation.act(planeAnchor.center)
planeEntity.model?.mesh = MeshResource.generatePlane(
width: planeAnchor.extent.x,
depth: planeAnchor.extent.z
)
planeEntity.transform.translation = position  + rotatedCenter
planeEntity.transform.rotation = orientation
planeEntity.model?.materials = [SimpleMaterial(color:UIColor.white.withAlphaComponent(0.5),isMetallic: false)]
}

也许我还没有弄清楚,我用上面的代码在RealityKit中可视化了检测到的平面,是的,它起作用了,我可以看到平面,以及ARAnchor更新时更新的平面,也就是说,当explorer打开时,平面的位置、方向、大小都更新了。但有一个问题:渲染的平面没有固定在空间中,在我扫描了桌子之后,渲染的平面并不总是固定在桌子上,当我将相机向左、向右、移动到桌子下面时,它可以在桌子的左边、右边、下面浮动,尤其是在Y轴上所以,我的问题是这是怎么发生的?以及如何解决?

当您的ARPlane已经有锚并且位于您满意的位置时,您可以尝试关闭planeDetectionARKit将停止更新锚点,这样你的平面锚点就不会再调整了,它应该更好地固定在曲面上。

你可以通过添加按钮来停止更新,或者检查你的飞机是否已经有了锚:

planeAnchor.anchor!.isAnchored == true

在任何一种情况下,只需更改ARWorldTrackingConfiguration的配置,而不使用planeDetection

let config = ARWorldTrackingConfiguration()
config.planeDetection = []
arView.session.run(config, options:[ ])

最新更新