在ARSCNView中显示“锚”

时间:2018-09-05 15:53:17

标签: swift scenekit augmented-reality arkit

在ARKit中,我们可以通过Feature Points' Cloud类型属性可视化在ARSession中检测到的.showFeaturePoints

self.sceneView.debugOptions = [ARSCNDebugOptions.showFeaturePoints]

此外,我们可以显示坐标轴可视化效果,指示AR世界坐标系的位置和方向:

static let showWorldOrigin: SCNDebugOptions

但是可以在ARSCNView中显示ARAnchors吗?

如果,我们怎么做?

2 个答案:

答案 0 :(得分:4)

ARAnchor仅表示“位置和方向”。您可以看到的是SCNNode个。

您可以通过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
}

在添加锚点之后(或在系统添加锚点时(例如,当您启用了平面检测或图像/对象检测时))调用此方法

sceneView.session.add(anchor:)

答案 1 :(得分:4)

只需跟进@ sj-r和@Rickster的评论即可。

@Rickster在谈论coordinateOrigin.scn时所涉及的示例代码位于:Creating Face Based Experiences

这是我之前用来可视化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") }
}

可以这样初始化:

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {

        let anchorVizualizer = BMOriginVisualizer(scale: 0.5)
        node.addChildNode(anchorVizualizer)

}

希望这将有助于扩展@ sj-r提供的答案。