如何在ARSCNView中移动多个节点



我的目标是能够使用gestureRecognizer移动/旋转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)

}

我想做的是能够同时移动两个立方体(所以它们都经历了相同的转换)。从这篇文章看来,这是可能的:iOS场景中如何将多个节点连接到一个节点然而,与此同时,这篇文章也说我不能这样做,因为我还不明白:SceneKit节点不随场景的根节点改变位置在最坏的情况下,我认为有可能手动将转换应用于每个子节点,但是将转换应用于一个父节点似乎是一种更优雅的方法。

编辑:我尝试过这种方式,可以得到两个移动节点移动,但有时位置是颠倒的(有时立方体出现在另一个当他们不应该):

@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)
}
}
}

任何想法?

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

/// - 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访问所有节点。这还包含其他默认创建的节点,如相机或光源,所以我添加了条件,以确保它只选择具有我创建的名称的节点。理想情况下,你只需要使用一些内置的方法来移动场景中的所有节点,但我找不到这样的方法。

所以我的方法在移动之前首先跟踪旧的位置,然后如果它是节点命中命中测试,像以前一样移动它。但是,如果它不是hit test命中的节点,则根据两个节点之间的差重新定位它。无论将节点移动到哪里,相对位置差都应该是相同的。

最新更新