如何解决如何在 ARSCNView 中移动多个节点
我的目标是能够使用gestureRecognizer 移动/旋转AR 对象。虽然我让它适用于单个 AR 立方体,但我无法让它适用于多个立方体/对象。
viewDidLoad 的主要部分:
let boxNode1 = addCube(position: SCNVector3(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,0)
nodeHit.position = SCNVector3(worldTransform.x,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)
}
}
}
有什么想法吗?
解决方法
我换了加减号,现在它可以正常工作了。代码如下:
; WITH XMLNAMESPACES ('http://report_xml.org/dataSet/201006' AS DS)
select @xmlData.query('/Envelope/Body/getReport/return/rapor/DS:dataSet/DS:dataTable/DS:id')
or
select @xmlData.query('/Envelope/Body/getReport/return/rapor/DS:dataSet/DS:dataTable/DS:row')
这个想法是即使不将所有内容都分组到一个新节点中,我也可以使用 nodeHit.parent!.childNodes 访问所有节点。这还包含默认创建的其他节点,例如相机或光源,因此我添加了条件以确保它仅选择具有我创建的名称的节点。理想情况下,您只需要使用一些内置方法来移动场景中的所有节点,但我找不到这样的方法。
所以我的方法在移动之前首先跟踪旧位置,然后如果它是命中测试命中的节点,则像以前一样移动它。但是,如果它不是命中测试命中的节点,则通过 2 个节点之间的差异重新定位它。无论您将节点移动到何处,相对位置差异都应该相同。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。