【发布时间】: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)
}
}
}
有什么想法吗?
【问题讨论】: