ARKit - 如何在sceneview中获取特定节点?

时间:2018-03-31 13:40:09

标签: ios swift arkit

我是ARKit的新人。我想让sceneview中的特定节点与该节点进行交互。这是我的代码:

@objc func moveNode(_ gesture: UIPanGestureRecognizer) {

    if !isRotating{

        //1. Get The Current Touch Point
        let currentTouchPoint = gesture.location(in: self.sceneView)

        //2. Get The Next Feature Point Etc
        guard let hitTest = self.sceneView.hitTest(currentTouchPoint, types: .existingPlane).first else { return }

        //3. Convert To World Coordinates
        let worldTransform = hitTest.worldTransform

        //4. Set The New Position
        let newPosition = SCNVector3(worldTransform.columns.3.x, worldTransform.columns.3.y, worldTransform.columns.3.z)

        //5. Apply To The Node
        self.sceneView.scene.rootNode.enumerateChildNodes{ (node, _) in
            //**********************************************************
            // how to detect which node is active to interactive here???
            node.simdPosition = float3(newPosition.x, newPosition.y, newPosition.z)
            //**********************************************************
        }
    }
}

如何在**代码块中检测哪个节点对交互活动?谢谢。

1 个答案:

答案 0 :(得分:3)

为了获得对特定SCNNode的引用,您需要使用SCNHitTest:

  

沿您指定的光线查找SCNGeometry对象。对于每一个   在射线和几何体之间的交集,SceneKit创建了一个   命中测试结果,提供有关SCNNode对象的信息   包含几何和交叉点的位置   几何的表面。

因此,假设您在场景中有两个SCNode变量,例如:

var nodeOne: SCNNode!
var nodeTwo: SCNNode!

这些是使用唯一的name属性初始化的:

let nodeOneGeometry = SCNSphere(radius: 0.2)
nodeOneGeometry.firstMaterial?.diffuse.contents = UIColor.cyan
nodeOne = SCNNode(geometry: nodeOneGeometry)
nodeOne.name = "Node One"
nodeOne.position = SCNVector3(0, 0, -1.5)
augmentedRealityView.scene.rootNode.addChildNode(nodeOne)

let nodeTwoGeometry = SCNBox(width: 0.2, height: 0.2, length: 0.2, chamferRadius: 0)
nodeTwoGeometry.firstMaterial?.diffuse.contents = UIColor.cyan
nodeTwo = SCNNode(geometry: nodeTwoGeometry)
nodeTwo.name = "Node Two"
nodeTwo.position = SCNVector3(0.5, 0, -1.5)
augmentedRealityView.scene.rootNode.addChildNode(nodeTwo)

我们还需要创建另一个变量来存储当前节点,例如:

var currentNode: SCNNode?

我们将在您现有的功能中使用,如下所示:

@objc func moveNode(_ gesture: UIPanGestureRecognizer) {

    //1. Get The Current Touch Point
    let currentTouchPoint = gesture.location(in: self.augmentedRealityView)

    //2. If The Gesture State Has Begun Perform A Hit Test To Get The SCNNode At The Touch Location
    if gesture.state == .began{

        //2a. Perform An SCNHitTest To Detect If An SCNNode Has Been Touched
        guard let nodeHitTest = self.augmentedRealityView.hitTest(currentTouchPoint, options: nil).first else { return }

        //2b. Get The SCNNode Result
        let nodeHit = nodeHitTest.node

        //2c. Get The Namt Of The Node & Set As The Current Node
        if let nodeName = nodeHit.name{

            if nodeName == "Node One"{

                print("Node One Hit")
                currentNode = nodeHit

            }else if nodeName == "Node Two"{

                print("Node Two Hit")
                currentNode = nodeHit

            }else{
                return
            }
        }
    }

    //3. If The Gesture State Has Changed Then Perform An ARSCNHitTest To Detect Any Existing Planes
    if gesture.state == .changed{

        //3b. Get The Next Feature Point Etc
        guard let hitTest = self.augmentedRealityView.hitTest(currentTouchPoint, types: .existingPlane).first else { return }

        //3c. Convert To World Coordinates
        let worldTransform = hitTest.worldTransform

        //3d. Set The New Position
        let newPosition = SCNVector3(worldTransform.columns.3.x, worldTransform.columns.3.y, worldTransform.columns.3.z)

        //3e. Apply To The Node
        currentNode?.simdPosition = float3(newPosition.x, newPosition.y, newPosition.z)

    }

    //4. If The Gesture State Has Ended Remove The Reference To The Current Node
    if gesture.state == .ended{
        currentNode = nil
    }
}

请注意,有许多不同的方法可以实现您的需求,而这只是其中之一。希望它有所帮助...

<强>更新

如果您没有SCNNode的名称或者它们具有相同的名称(不知道为什么会这样),您可以简单地修改这个功能:

@objc func moveNode(_ gesture: UIPanGestureRecognizer) {

//1. Get The Current Touch Point
let currentTouchPoint = gesture.location(in: self.augmentedRealityView)

//2. If The Gesture State Has Begun Perform A Hit Test To Get The SCNNode At The Touch Location
if gesture.state == .began{

    //2a. Perform An SCNHitTest To Detect If An SCNNode Has Been Touched
    guard let nodeHitTest = self.augmentedRealityView.hitTest(currentTouchPoint, options: nil).first else { return }

    //2b. Get The SCNNode Result
    let nodeHit = nodeHitTest.node

    //2c. Set As The Current Node
    currentNode = nodeHit

}

//3. If The Gesture State Has Changed Then Perform An ARSCNHitTest To Detect Any Existing Planes
if gesture.state == .changed{

    //3b. Get The Next Feature Point Etc
    guard let hitTest = self.augmentedRealityView.hitTest(currentTouchPoint, types: .existingPlane).first else { return }

    //3c. Convert To World Coordinates
    let worldTransform = hitTest.worldTransform

    //3d. Set The New Position
    let newPosition = SCNVector3(worldTransform.columns.3.x, worldTransform.columns.3.y, worldTransform.columns.3.z)

    //3e. Apply To The Node
    currentNode?.simdPosition = float3(newPosition.x, newPosition.y, newPosition.z)

}

//4. If The Gesture State Has Ended Remove The Reference To The Current Node
if gesture.state == .ended{
    currentNode = nil
   }
}