3

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?

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220

2 Answers2

6

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.

BlackMirrorz
  • 7,217
  • 2
  • 20
  • 31
  • There's also a +50 bounty for this question: https://stackoverflow.com/questions/52025727/does-arkit-2-0-consider-lens-distortion-in-iphone – Andy Jazz Sep 06 '18 at 07:03
  • That’s beyond me :) Rickster will probably know :) – BlackMirrorz Sep 06 '18 at 07:49
  • I'd like to ask you a question on another topic: How long does it take for you to write a code for a AR game like Apple's SwiftShot (from scratch)? 4-5 months? – Andy Jazz Sep 06 '18 at 14:26
  • Hard to say to be honest. That timeframe sounds about write although as you know it could well be less or could be more :) since there is an example it might take less :) – BlackMirrorz Sep 08 '18 at 03:12
5

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:)
sj-r
  • 561
  • 2
  • 10
  • What is about orientation of these spheres? Maybe it's better to place a cubes with extruded faces (and texture three positive directions x/y/z with red/green/blue colours)? – Andy Jazz Sep 05 '18 at 16:19
  • 2
    Yeah sure you can make any node you want. orientation and position are the same as the anchor. Think of them(ARAnchor and the Node) as a pair – sj-r Sep 05 '18 at 16:23
  • Thanks a lot, sj-r. – Andy Jazz Sep 05 '18 at 16:24
  • 2
    If you look inside some of the sample code projects on the [Apple ARKit documentation](http://developer.apple.com/documentation/arkit) pages, you'll find a `coordinateOrigin.scn` file that draws x/y/z coordinate axes, which can be a handy way to visualize an anchor transform. – rickster Sep 05 '18 at 17:18
  • Thanks a lot, rickster! – Andy Jazz Sep 05 '18 at 20:15
  • How can I stop tracking real world objects & keep this sphereNode in scene in the appear position!? – Ahmadreza May 14 '21 at 06:23