visualize detected plane problem in RealityKit

Viewed 845

guys I want to visualize the detected plane in RealityKit use the code below, but the result gives that the detected plane float as the camera move (not totally float, a bit, but obviously ), so, my question is how to solve this problem ?

can any body help ?

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)]
        }

2 Answers

maybe I have not make myself clear, I used the code above to visualize the detected plane in RealityKit, Yes, it works, I can see the plane, and the plane updated when the ARAnchor update, that is to say, the plane's position ,orientation, size updated when explorer goes on. but there's a problem: the rendered plane does not fixed in the space, that is to say, after I scanned the table ,the rendered plane not always fixed on the table ,it can float left、 right、below the table when I move the Camera left、right、below the table, especially in the Y axis 。 so ,my question is how this happen ? and how to solve it ?

You can try turning off planeDetection when your ARPlane already have an Anchor and is in the position that satisfies you. ARKit will stop updating anchors so your plane anchor won't be adjusted anymore and it should stay fixed better to the surface.

You can do it by adding button to stop updates or checking if your plane already has an anchor:

planeAnchor.anchor!.isAnchored == true

In either case just change configuration for ARWorldTrackingConfiguration without planeDetection

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