2

I am using WorldTracking in ARKit and converting ARAnchors to SCNNodes to display it later using SceneView. Here is the code for adding new anchor and I am adding new node on each anchor added.

func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {
        guard let anchor = anchor as? ARMeshAnchor ,
              let frame = sceneView.session.currentFrame else { return nil }

        let node = SCNNode()
        let geometry = scanGeometory(frame: frame, anchor: anchor, node: node, needTexture: true, cameraImage: captureCamera())
        node.geometry = geometry
        return node
    }

Till this point everything is working fine. Now when Anchors are updated and geometry object is reconstructed it is causing issue while applying texture. Here is the anchor update call back code.

func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {
        guard let frame = self.sceneView.session.currentFrame else { return }
        guard let anchor = anchor as? ARMeshAnchor else { return }
        let geometry = self.scanUpdatedGeometory(frame: frame, anchor: anchor, node: node, needTexture: true, cameraImage: captureCamera())
        node.geometry = geometry
    }

Here is the scanGeometry code where image texture is being applied on Geometry.

func scanGeometory(frame: ARFrame, anchor: ARMeshAnchor, node: SCNNode, needTexture: Bool = false, cameraImage: UIImage? = nil) -> SCNGeometry {

        let camera = frame.camera
        
        let geometry = SCNGeometry(geometry: anchor.geometry, camera: camera, modelMatrix: anchor.transform, needTexture: needTexture)

        if let image = cameraImage, needTexture {
            geometry.firstMaterial?.diffuse.contents = image
        } else {
            geometry.firstMaterial?.diffuse.contents = UIColor(red: 0.5, green: 1.0, blue: 0.0, alpha: 0.7)
        }
        node.geometry = geometry

        return geometry
    }

When scan geometry is called on didUpdate anchor at that time it is trying to apply current image on updated anchor doesn't matter that anchor is within current frame or or not. This is causing stretched texture outside current frame/view. Here is the reference image. Left side stretched area was not in view when I stopped scan.

enter image description here

What can be the solution for applying current view texture only and leave old one as is. Or is there any another way to apply texture for world tracking?

Zia ur Rehman
  • 331
  • 1
  • 2
  • 20

0 Answers0