Add Entity to ARMeshAnchor

I try to set entity with custom material to ARMeshAnchor using RealityKit, like this examplehttps://www.dropbox.com/s/b0u9mbsxqaobnrf/Screen%20Shot%202021-10-29%20at%205.25.57%20PM.png?dl=0  I found mini solution how to make it via sceneKit Geometry, but can't something how to make it via RealityKit. Tell me is it possible to do it? If this is possible, how? I tried to do this, but it looks like I'm doing something wrong. I'm also interested in understanding how to stretch mesh entities along the boundaries of the space that captures the camera, since it seems to be done through .generatePlane(width: 1, height: 1) complete


    private lazy var arView = ARView().do {
        $0.frame = view.bounds
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        MetalLibLoader.initializeMetal()
        setupSubviews()
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        arView.session.delegate = self
        configureWorldTracking()
    }

    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        arView.session.pause()
    }

    private func setupSubviews() {
        view.addSubview(arView)
    }

    private func configureWorldTracking() {
        let configuration = ARWorldTrackingConfiguration()
        let personSegmentation: ARWorldTrackingConfiguration.FrameSemantics = .personSegmentationWithDepth
        if ARWorldTrackingConfiguration.supportsFrameSemantics(personSegmentation) {
      configuration.frameSemantics.insert(personSegmentation)
        }
        let sceneReconstruction: ARWorldTrackingConfiguration.SceneReconstruction = .mesh
        if ARWorldTrackingConfiguration.supportsSceneReconstruction(sceneReconstruction) {
            configuration.sceneReconstruction = sceneReconstruction
        }
        configuration.planeDetection.insert(.horizontal)
        arView.renderOptions.insert(.disableMotionBlur)
        arView.session.run(configuration)
    }

    private func updateAnchors(anchors: [ARAnchor]) {
        for anchor in anchors.compactMap({ $0 as? ARMeshAnchor }) {
            let anchorEntity = AnchorEntity(anchor: anchor)
            anchorEntity.addChild(plasmaEntity())
            arView.scene.addAnchor(anchorEntity)
        }
    }

    private func plasmaEntity() -> ModelEntity {
        let customMaterial: CustomMaterial
        let surfaceShader = CustomMaterial.SurfaceShader(named: "plasma", in: MetalLibLoader.library)
        do {
            try customMaterial = CustomMaterial(surfaceShader: surfaceShader, lightingModel: .lit)
        } catch {
            fatalError(error.localizedDescription)
        }
        return ModelEntity(mesh: .generatePlane(width: 1, height: 1), materials: [customMaterial])
    }
}

extension ARViewController: ARSessionDelegate {
    func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
        updateAnchors(anchors: anchors)
    }

    func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
        updateAnchors(anchors: anchors)
    }
}


is there a reason why you are using compactMap in updateAnchors? Have you verified that there is at least one anchor in the compactMap list? I would try for anchor in anchors if you haven't already. The rest of your custom material code looks correct though.

The image that you linked to looks more like an effect based on sceneDepth data, as opposed to a material being applied to the sceneReconstruction geometry.

You should take a look at the Underwater sample code from WWDC, which uses the sceneDepth data in its PostProcess.

Add Entity to ARMeshAnchor
 
 
Q