繁体   English   中英

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

[英]ARKit - How to get the specific node in sceneview?

我是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)
            //**********************************************************
        }
    }
}

如何在**代码块中检测哪个节点处于活动状态以进行交互? 谢谢。

为了获得对特定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
   }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM