如何判断检测到哪个参照物?

时间:2019-05-16 03:41:56

标签: ios swift augmented-reality arkit

我有一个ARKit应用程序,它可以检测大量对象,然后将3d对象放置在它们之上。我想更改显示在检测到的对象上方的3d对象的文本,但是无法弄清楚如果检测到y对象如何执行x功能。

我的AR扫描/对象集合位于一个名为“画廊”的组中,我的代码如下:

class ViewController: UIViewController, ARSCNViewDelegate {
    @IBOutlet var sceneView: ARSCNView!
    var arrowAnchor:ARAnchor?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Set the view's delegate
        sceneView.delegate = self
        sceneView.showsStatistics = true
        sceneView.autoenablesDefaultLighting = true
        sceneView.debugOptions = [ARSCNDebugOptions.showFeaturePoints]
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        //Create ARWorldTrackingConfiguration object and load your garrey asset group.
        //You can add multiple ARObject file into ar asset group,
        let configuration = ARWorldTrackingConfiguration()
        guard let referenceObjects = ARReferenceObject.referenceObjects(inGroupNamed: "gallery", bundle: nil) else {
            fatalError("Missing expected asset catalog resources.")
        }
        configuration.detectionObjects = referenceObjects
        sceneView.session.run(configuration)

    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

        // Pause the view's session
        sceneView.session.pause()
    }

    func session(_ session: ARSession, didFailWithError error: Error) {
        // Present an error message to the user
    }

    func sessionWasInterrupted(_ session: ARSession) {
        // Inform the user that the session has been interrupted, for example, by presenting an overlay
    }

    func sessionInterruptionEnded(_ session: ARSession) {
        // Reset tracking and/or remove existing anchors if consistent tracking is required
    }

    func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
        if let objectAnchor = anchor as? ARObjectAnchor {
            let translation = objectAnchor.transform.columns.3
            let pos = float3(translation.x, translation.y, translation.z)
            let nodeArrow = getArrowNode()
            nodeArrow.position = SCNVector3(pos)
            nodeArrow.constraints = [SCNBillboardConstraint()]
            sceneView.scene.rootNode.addChildNode(nodeArrow)

            let text = SCNText(string: "Asset", extrusionDepth: 1)
            let material = SCNMaterial()
            material.diffuse.contents = UIColor.white
            text.materials = [material]
            let node = SCNNode()
            let pos2 = float3(translation.x-0.01, translation.y+0.22, translation.z-0.0)
            node.position = SCNVector3(pos2)
            node.scale = SCNVector3(x: 0.003, y: 0.003, z: 0.003)
            node.geometry = text
            node.constraints = [SCNBillboardConstraint()]
            sceneView.scene.rootNode.addChildNode(node)

        }
    }

    func getArrowNode() -> SCNNode {
        let sceneURL = Bundle.main.url(forResource: "arrow_yellow", withExtension: "scn", subdirectory: "art.scnassets")!
        let referenceNode = SCNReferenceNode(url: sceneURL)!
        referenceNode.load()
        return referenceNode
    }

    func getTextNode() -> SCNNode {
        let sceneURL = Bundle.main.url(forResource: "text", withExtension: "scn", subdirectory: "art.scnassets")!
        let referenceNode = SCNReferenceNode(url: sceneURL)!
        referenceNode.load()
        return referenceNode
    }

}

如何根据检测到的参考对象执行某些代码块?

1 个答案:

答案 0 :(得分:0)

这个神奇的咒语恰好满足了我的需求:

    let objectName = objectAnchor.referenceObject.name

我这样使用它:

func renderer(_ renderer:SCNSceneRenderer,didAdd节点:SCNNode,锚点:ARAnchor){         如果让objectAnchor =锚定为? ARObjectAnchor {

        let objectName = objectAnchor.referenceObject.name

        let translation = objectAnchor.transform.columns.3
        let pos = float3(translation.x, translation.y, translation.z)
        let nodeArrow = getArrowNode()
        nodeArrow.position = SCNVector3(pos)
        nodeArrow.constraints = [SCNBillboardConstraint()]
        sceneView.scene.rootNode.addChildNode(nodeArrow)

        let text = SCNText(string: objectName, extrusionDepth: 1)
        let material = SCNMaterial()
        material.diffuse.contents = UIColor.white
        text.materials = [material]
        let node = SCNNode()
        let pos2 = float3(translation.x-0.01, translation.y+0.22, translation.z-0.0)
        node.position = SCNVector3(pos2)
        node.scale = SCNVector3(x: 0.003, y: 0.003, z: 0.003)
        node.geometry = text
        node.constraints = [SCNBillboardConstraint()]
        sceneView.scene.rootNode.addChildNode(node)

    }
}