节点在场景中的位置
Position of node in scene
好的,我目前正在创建一个应用程序,允许您下载某些对象,然后使用 Apple 的 ARkit 真实地显示这些对象 space。我已经能够下载该项目并将其真实显示 space 但位置全部关闭并且由于某种原因我无法获得根节点子节点。这是我目前所拥有的一部分:
@objc func tapped(sender: UITapGestureRecognizer){
let sceneView = sender.view as! ARSCNView
let tapLocation = sender.location(in: sceneView)
let hitTest = sceneView.hitTest(tapLocation, types: .existingPlaneUsingExtent)
print(hitTest)
if !hitTest.isEmpty{
print("in tapped part")
self.sceneView.scene.rootNode.enumerateChildNodes({ (node, stop) in
node.removeFromParentNode()
})
self.addItem(hitTestResult: hitTest.first!)
}else{
print("not in tapped")
}
}
func addItem(hitTestResult: ARHitTestResult){
print("in add item part")
self.node = scene!.rootNode
let transform = hitTestResult.worldTransform
let thirdColumn = transform.columns.3
self.node!.position = SCNVector3Make(0, 0, 0)
self.sceneView.scene.rootNode.addChildNode(self.node!)
}
我试过这样做:
self.node = scene!.rootNode.childNode(withName: "Cow", recursively: false)
但我一直没有得到结果。目标文件的名称是 Cow,我什至尝试将其命名为 Cow.obj 但这也不起作用。我很确定这就是我的定位不正确的原因。它与我在 childNode 方法中使用的名称有关吗?如果需要,我可以 post 更多代码。
这就是我要接收到的他们接触到的位置。
guard let touch = touches.first else {return} //Get's touch on screen
let result = sceneView.hitTest(touch.location(in: sceneView), types: ARHitTestResult.ResultType.featurePoint) //searches for real world objects
guard let pointResult = result.last else {return} //a hit test result (where the point was clicked)
let pointTransform = SCNMatrix4(pointResult.worldTransform) //turns the point into a point on the world grid
let pointVector = SCNVector3Make(pointTransform.m41, pointTransform.m42, pointTransform.m43) //the X, Y, and Z of the clicked cordinate
看来您需要将 hitTestResult 转换为 SCNMatrix4 位置,然后使用 SCNMatrix4.m41、m42 和 m43 制作 SCNVector3Make。然后,使用那个 scnvector3make 来确定节点的位置。
好的,我目前正在创建一个应用程序,允许您下载某些对象,然后使用 Apple 的 ARkit 真实地显示这些对象 space。我已经能够下载该项目并将其真实显示 space 但位置全部关闭并且由于某种原因我无法获得根节点子节点。这是我目前所拥有的一部分:
@objc func tapped(sender: UITapGestureRecognizer){
let sceneView = sender.view as! ARSCNView
let tapLocation = sender.location(in: sceneView)
let hitTest = sceneView.hitTest(tapLocation, types: .existingPlaneUsingExtent)
print(hitTest)
if !hitTest.isEmpty{
print("in tapped part")
self.sceneView.scene.rootNode.enumerateChildNodes({ (node, stop) in
node.removeFromParentNode()
})
self.addItem(hitTestResult: hitTest.first!)
}else{
print("not in tapped")
}
}
func addItem(hitTestResult: ARHitTestResult){
print("in add item part")
self.node = scene!.rootNode
let transform = hitTestResult.worldTransform
let thirdColumn = transform.columns.3
self.node!.position = SCNVector3Make(0, 0, 0)
self.sceneView.scene.rootNode.addChildNode(self.node!)
}
我试过这样做:
self.node = scene!.rootNode.childNode(withName: "Cow", recursively: false)
但我一直没有得到结果。目标文件的名称是 Cow,我什至尝试将其命名为 Cow.obj 但这也不起作用。我很确定这就是我的定位不正确的原因。它与我在 childNode 方法中使用的名称有关吗?如果需要,我可以 post 更多代码。
这就是我要接收到的他们接触到的位置。
guard let touch = touches.first else {return} //Get's touch on screen
let result = sceneView.hitTest(touch.location(in: sceneView), types: ARHitTestResult.ResultType.featurePoint) //searches for real world objects
guard let pointResult = result.last else {return} //a hit test result (where the point was clicked)
let pointTransform = SCNMatrix4(pointResult.worldTransform) //turns the point into a point on the world grid
let pointVector = SCNVector3Make(pointTransform.m41, pointTransform.m42, pointTransform.m43) //the X, Y, and Z of the clicked cordinate
看来您需要将 hitTestResult 转换为 SCNMatrix4 位置,然后使用 SCNMatrix4.m41、m42 和 m43 制作 SCNVector3Make。然后,使用那个 scnvector3make 来确定节点的位置。