【问题标题】:SceneKit / ARKit updating a node every frameSceneKit / ARKit 每帧更新一个节点
【发布时间】:2021-08-17 01:08:05
【问题描述】:

我正在使用 ARKit / SceneKit,我试图让箭头指向我在世界中设置的任意位置,但我遇到了一些麻烦。在我的场景视图中,我设置了一个场景以加载到我的箭头中。

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Set the view's delegate
        sceneView.delegate = self
        guard let arrowScene = SCNScene(named: "art.scnassets/arrowScene.scn") else {
            fatalError("Scene arrow.scn not found")
        }
        
        let worldAnchor = ARAnchor(name: "World Anchor", transform: simd_float4x4(1))
        sceneView.session.add(anchor: worldAnchor)
        
        sceneView.scene = (arrowScene)
    }

然后我希望使用SCNNode.look(at:) 将我的箭头指向指定的锚点,但是,我不确定如何让每一帧都发生这种情况。我知道我可以访问ARSCNView 提供的特殊代表,例如renderer(willUpdate node:),但是当锚没有改变位置而箭头改变位置时,我不确定如何使用它们。

感谢您的帮助!

【问题讨论】:

    标签: swift augmented-reality scenekit arkit


    【解决方案1】:

    您可以使用updateAtTime 委托函数来做到这一点,但我强烈建议您使用 SCNConstraint。

    let lookAtConstraint = SCNLookAtConstraint(target: myLookAtNode)
    lookAtConstraint.isGimbalLockEnabled = true // useful for cameras, probably also for your arrow.
    
    // in addition you can use a position constraint
    let positionConstraint = SCNReplicatorConstraint(target: myLookAtNode)
    positionConstraint.positionOffset           = yourOffsetSCNVector3
    positionConstraint.replicatesOrientation    = false
    positionConstraint.replicatesScale          = false
    positionConstraint.replicatesPosition       = true
        
    // then add the constraints to your arrow node
    arrowNode.constraints = [lookAtConstraint, positionConstraint ]
    

    这将为每个渲染帧自动调整您的节点。

    【讨论】:

    • 我什至不知道其中任何一个存在 - 非常感谢!
    猜你喜欢
    • 2020-04-01
    • 2018-09-09
    • 2018-12-01
    • 1970-01-01
    • 2021-01-04
    • 2017-12-25
    • 2016-07-22
    • 2019-10-25
    • 2017-12-11
    相关资源
    最近更新 更多