英文:
ARImageAnchor orientation in relation to a SCNNode
问题
我的当前代码库提供了与世界相关的ARImageAnchor
的方向(俯仰
,偏航
,翻滚
)。我想要实现的目标是获取ARImageAnchor
方向数据,相对于在启动会话后的某个时间添加的节点。
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
guard let _ = anchor as? ARImageAnchor else { return }
DispatchQueue.main.async {
let cylinderNode = SCNNode()
cylinderNode.geometry = SCNCylinder(radius: 0.05, height: 0.4)
node.addChildNode(cylinderNode)
let ea: SCNVector3 = node.eulerAngles
print(self.convert(ea.x), self.convert(ea.y), self.convert(ea.z))
}
}
希望能提供帮助!
英文:
My current code base provides the orientation (pitch
, yaw
, roll
) of an ARImageAnchor
in relation to the world. What I want to achieve is to get the ARImageAnchor
orientation data in relation to a node that has been added sometime after starting the session.
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode,
for anchor: ARAnchor) {
guard let _ = anchor as? ARImageAnchor else { return }
DispatchQueue.main.async {
let cylinderNode = SCNNode()
cylinderNode.geometry = SCNCylinder(radius: 0.05, height: 0.4)
node.addChildNode(cylinderNode)
let ea: SCNVector3 = node.eulerAngles
print(self.convert(ea.x), self.convert(ea.y), self.convert(ea.z))
}
}
Any help would be much appreciated!
答案1
得分: 1
你可以将锚点的 orientation part
部分从 simd_float4x4 transform 传递给你的模型(请注意,这些数据存储在当前会话的每个 ARFrame 中),以恢复位置:
import ARKit
import SceneKit
class ViewController: UIViewController, ARSCNViewDelegate {
@IBOutlet var sceneView: ARSCNView!
var model = SCNNode()
override func viewDidLoad() {
super.viewDidLoad()
sceneView.delegate = self
sceneView.scene = SCNScene(named: "ARScene")!
model = sceneView.scene.rootNode.childNode(withName: "model",
recursively: true)!
}
@IBAction func passTransformButton(_ sender: UIButton) {
if let trans = sceneView.session.currentFrame?.anchors[0].transform {
model.simdTransform = trans // Full Transform
model.simdTransform.columns.3.x = 0 // 恢复位置 X
model.simdTransform.columns.3.y = 0 // 恢复位置 Y
model.simdTransform.columns.3.z = 0 // 恢复位置 Z
}
}
}
英文:
You can pass anchor's orientation part
of the simd_float4x4 transform to your model (note that this data is stored in each ARFrame of the current session) restoring position:
import ARKit
import SceneKit
class ViewController : UIViewController, ARSCNViewDelegate {
@IBOutlet var sceneView: ARSCNView!
var model = SCNNode()
override func viewDidLoad() {
super.viewDidLoad()
sceneView.delegate = self
sceneView.scene = SCNScene(named: "ARScene")!
model = sceneView.scene.rootNode.childNode(withName: "model",
recursively: true)!
}
@IBAction func passTransformButton(_ sender: UIButton) {
if let trans = sceneView.session.currentFrame?.anchors[0].transform {
model.simdTransform = trans // Full Transform
model.simdTransform.columns.3.x = 0 // restore position X
model.simdTransform.columns.3.y = 0 // restore position Y
model.simdTransform.columns.3.z = 0 // restore position Z
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论