Andy Jazz
Andy Jazz

Reputation: 58053

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?

Upvotes: 3

Views: 1436

Answers (2)

PongBongoSaurus
PongBongoSaurus

Reputation: 7385

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.

Upvotes: 6

sj-r
sj-r

Reputation: 571

ARAnchor only represents 'position and orientation'. Things you can see are SCNNodes.

You can attach a node for each anchor you add via a method in ARSCNViewDelegate

func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {
    //create a node so you can visualize the location.
    let sphereNode = SCNNode(geometry: SCNSphere(radius: 0.5))
    sphereNode.geometry?.firstMaterial?.diffuse.contents = UIColor.blue
    return sphereNode
}

This is called after you add an anchor (or when the system adds anchors such as when you have plane detection or image/object detection turned on)

sceneView.session.add(anchor:)

Upvotes: 5

Related Questions