Search code examples
swiftscenekitaugmented-realityarkit

Displaying an "ARAnchor" in ARSCNView


In ARKit we can visualise Feature Points' Cloud detected in a ARSession via .showFeaturePoints Type Property:

self.sceneView.debugOptions = [ARSCNDebugOptions.showFeaturePoints]

Also, we can display a coordinate axis visualization indicating the position and orientation of the AR World Coordinate System:

static let showWorldOrigin: SCNDebugOptions

But is it possible to show ARAnchors in ARSCNView?

And if yes, how could we do it?


Solution

  • Just to follow up on @sj-r and @Rickster's comments.

    The example code that @Rickster was talking about in regard to the coordinateOrigin.scn is found here: Creating Face Based Experiences

    And here is a little snippet I have used before to visualize Axis:

    class BMOriginVisualizer: SCNNode {
    
        //----------------------
        //MARK: - Initialization
        //---------------------
    
        /// Creates An AxisNode To Vizualize ARAnchors
        ///
        /// - Parameter scale: CGFloat
        init(scale: CGFloat = 1) {
    
            super.init()
    
            //1. Create The X Axis
            let xNode = SCNNode()
            let xNodeGeometry = SCNBox(width: 1, height: 0.01, length: 0.01, chamferRadius: 0)
            xNode.geometry = xNodeGeometry
            xNodeGeometry.firstMaterial?.diffuse.contents = UIColor.red
            xNode.position = SCNVector3(0.5, 0, 0)
            self.addChildNode(xNode)
    
            //2. Create The Y Axis
            let yNode = SCNNode()
            let yNodeGeometry = SCNBox(width: 0.01, height: 1, length: 0.01, chamferRadius: 0)
            yNode.geometry = yNodeGeometry
            yNode.position = SCNVector3(0, 0.5, 0)
            yNodeGeometry.firstMaterial?.diffuse.contents = UIColor.green
            self.addChildNode(yNode)
    
            //3. Create The Z Axis
            let zNode = SCNNode()
            let zNodeGeometry = SCNBox(width: 0.01, height: 0.01, length: 1, chamferRadius: 0)
            zNode.geometry = zNodeGeometry
            zNodeGeometry.firstMaterial?.diffuse.contents = UIColor.blue
            zNode.position = SCNVector3(0, 0, 0.5)
            self.addChildNode(zNode)
    
            //4. Scale Our Axis
            self.scale = SCNVector3(scale, scale, scale)
    
        }
    
    
        required init?(coder aDecoder: NSCoder) { fatalError("Vizualizer Coder Not Implemented") }
    }
    

    Which can be initialised like so:

    func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
    
            let anchorVizualizer = BMOriginVisualizer(scale: 0.5)
            node.addChildNode(anchorVizualizer)
    
    }
    

    Hopefully this will provide useful as an expansion to the answer provided by @sj-r.