【问题标题】:How to move multiple nodes in ARSCNView如何在 ARSCNView 中移动多个节点
【发布时间】:2021-05-30 16:46:49
【问题描述】:

我的目标是能够使用手势识别器移动/旋转 AR 对象。虽然我让它适用于单个 AR 立方体,但我无法让它适用于多个立方体/对象。

viewDidLoad 的主要部分:

        let boxNode1 = addCube(position: SCNVector3(0,0,0), name: "box")
        let boxNode2 = addCube(position: SCNVector3(0,-0.1,-0.1), name: "box2")

        sceneView.scene.rootNode.addChildNode(boxNode1)
        sceneView.scene.rootNode.addChildNode(boxNode2)
        
        var nodes: [SCNNode] = getMyNodes()

        var parentNode = SCNNode()
        parentNode.name = "motherNode"

        for node in nodes {
            parentNode.addChildNode(node)
        }
        sceneView.scene.rootNode.addChildNode(parentNode)

//        sceneView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(ViewController.handleTap(_:))))
        sceneView.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(ViewController.handleMove(_:))))
        sceneView.addGestureRecognizer(UIRotationGestureRecognizer(target: self, action: #selector(ViewController.handleRotate(_:))))
        
        let configuration = ARWorldTrackingConfiguration()
        sceneView.session.run(configuration)

panGesture 的一部分(适用于每个立方体,但如果我更改为 nodeHit.Parent 则不起作用!)正确检测到父节点,但未对其进行任何更改:

    @objc func handleMove(_ gesture: UIPanGestureRecognizer) {

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

    //2. Get The Next Feature Point Etc
    guard let nodeHitTest = self.sceneView.hitTest(location, options: nil).first else { print("no node"); return }

    var nodeHit = nodeHitTest.node

//    nodeHit = nodeHit.parent!
    //3. Convert To World Coordinates
    let worldTransform = nodeHitTest.simdWorldCoordinates
    //4. Apply To The Node
        nodeHit.position = SCNVector3(worldTransform.x, worldTransform.y, 0)
        
    }

我想要做的是能够同时移动两个立方体(所以它们都经历了相同的平移)。从这篇文章听起来可能: How to join multiple nodes to one node in iOS scene 然而,同时这篇文章也说我不能这样做,原因我还不明白: SceneKit nodes aren't changing position with scene's root node 在最坏的情况下,我认为可以手动将转换应用于每个子节点,但是将转换应用于一个父节点似乎是一种非常优雅的方式。

编辑:我尝试过这种方式,可以让两个移动节点移动,但是有时位置会颠倒(有时立方体会在不应该出现的情况下出现在另一个之上):

    @objc func handleMove(_ gesture: UIPanGestureRecognizer) {

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

    //2. Get The Next Feature Point Etc
    guard let nodeHitTest = self.sceneView.hitTest(location, options: nil).first else { print("no node"); return }

//    var nodeHit = nodeHitTest.node

    let nodeHit = nodeHitTest.node
    let original_x = nodeHitTest.node.position.x
    let original_y = nodeHitTest.node.position.y
    print(original_x, original_y)
//    let nodeHit = sceneView.scene.rootNode.childNode(withName: "motherNode2", recursively: true)
    //3. Convert To World Coordinates
    let worldTransform = nodeHitTest.simdWorldCoordinates
    //4. Apply To The Node
////    nodeHit.position = SCNVector3(worldTransform.x, worldTransform.y, 0)
    nodeHit.position = SCNVector3(worldTransform.x, worldTransform.y, 0)

    for node in nodeHit.parent!.childNodes {
        if node.name != nil{
            if node.name != nodeHit.name {
                let old_x = node.position.x
                let old_y = node.position.y
                print(old_x, old_y)
                node.position = SCNVector3((nodeHit.simdPosition.x + original_x - old_x), (nodeHit.simdPosition.y + original_y - old_y), 0)
            }
        }
    }

有什么想法吗?

【问题讨论】:

    标签: ios swift scenekit arkit


    【解决方案1】:

    我交换了加减号,现在它可以正常工作了。代码如下:

        /// - Parameter gesture: UIPanGestureRecognizer
        @objc func handleMove(_ gesture: UIPanGestureRecognizer) {
    
        //1. Get The Current Touch Point
        let location = gesture.location(in: self.sceneView)
    
        //2. Get The Next Feature Point Etc
        guard let nodeHitTest = self.sceneView.hitTest(location, options: nil).first else { print("no node"); return }
    
    //    var nodeHit = nodeHitTest.node
    
        let nodeHit = nodeHitTest.node
        let original_x = nodeHitTest.node.position.x
        let original_y = nodeHitTest.node.position.y
    //    let nodeHit = sceneView.scene.rootNode.childNode(withName: "motherNode2", recursively: true)
        //3. Convert To World Coordinates
        let worldTransform = nodeHitTest.simdWorldCoordinates
        //4. Apply To The Node
    ////    nodeHit.position = SCNVector3(worldTransform.x, worldTransform.y, 0)
        nodeHit.position = SCNVector3(worldTransform.x, worldTransform.y, 0)
    
        for node in nodeHit.parent!.childNodes {
            if node.name != nil{
                if node.name != nodeHit.name {
                    let old_x = node.position.x
                    let old_y = node.position.y
                    node.position = SCNVector3((nodeHit.simdPosition.x - original_x + old_x), (nodeHit.simdPosition.y - original_y + old_y), 0)
                }
            }
        }
    

    这个想法是即使没有将所有内容分组到一个新节点中,我也可以使用 nodeHit.parent!.childNodes 访问所有节点。这还包含默认创建的其他节点,例如相机或光源,因此我添加了条件以确保它仅选择具有我创建的名称的节点。理想情况下,您只需要使用一些内置方法来移动场景中的所有节点,但我找不到这样的方法。

    所以我的方法在移动之前首先跟踪旧位置,然后如果它是命中测试命中的节点,则像以前一样移动它。但是,如果它不是命中测试命中的节点,则通过 2 个节点之间的差异重新定位它。无论您将节点移动到何处,相对位置差异都应该相同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-25
      • 2020-10-04
      • 2017-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-15
      • 1970-01-01
      相关资源
      最近更新 更多