ARImageAnchor在与SCNNode的方向关系中。

huangapple go评论67阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年4月10日 23:10:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75978284.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定