添加 SCNBillboardConstraint 使节点消失 [英] Adding a SCNBillboardConstraint makes the node dissapear

查看:28
本文介绍了添加 SCNBillboardConstraint 使节点消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我阅读文档和互联网上的内容之后,SCNBillboardConstraint 将旋转一个节点以始终面向 pointOfView 节点 - 在 ARKit 的情况下,用户的相机.

After what I've read in the documentation and on the internet a SCNBillboardConstraint would rotate a node to always face the pointOfView node - in the case of ARKit, the user's camera.

问题是,当我将 SCNBillboardConstraint 添加到子节点时,它会消失.节点只是作为更复杂模型的子子添加的一些 SCNTexts.层次结构如下所示:RootNode -> Text 节点(其中两个).

The thing is, when I add a SCNBillboardConstraint to a child node, it dissapears. The nodes are just some SCNTexts added as a subchild of a more complex model. The hierarchy looks something like this: RootNode - > Text node (two of them).

就在我将根节点添加到场景的根节点之后,我通过以下方式添加了这个约束:

Just after I added the root node to the scene's root node, I add this constraint in the following way:

updateQueue.async {
    self.sceneView.scene.rootNode.addChildNode(virtualObject)
    self.sceneView.addOrUpdateAnchor(for: virtualObject)
    self.addBillboardContraintsToText(object: virtualObject)
}

func addBillboardContraintsToText(object: VirtualObject) {
    guard let storeNode = object.childNodes.first else {
           return
   }

   for node in storeNode.childNodes {
        if let geometry = node.geometry, geometry.isKind(of: SCNText.self) {
            let billboard = SCNBillboardConstraint()
            node.constraints = [billboard]
        }
    }
}

文本节点相对于它们的根节点的位置设置正确,所以没有问题.但是,当我添加 SCNLookAtConstraint 时,它工作得很好.

The text nodes have their position set properly relative to their root node, so there's no problem with that. When I add a SCNLookAtConstraint though, it works just fine.

node.pivot = SCNMatrix4Rotate(node.pivot, Float.pi, 0, 1, 0)
let lookAt = SCNLookAtConstraint(target: sceneView.pointOfView)
lookAt.isGimbalLockEnabled = true
node.constraints = [lookAt]

任何想法为什么 SCNBillboardConstraint 可能不起作用?我做错了什么吗?

Any ideas why the SCNBillboardConstraint might not work? Am I doing something wrong?

推荐答案

此代码(使用苹果 CupScn)对我来说效果很好:

This Code (with apples CupScn) works just fine for me:

cupNode.position = SCNVector3(0.5,0,-0.5)
    guard let virtualObjectScene = SCNScene(named: "cup.scn", inDirectory: "Models.scnassets/cup") else {
        return
    }
    let wrapperNode = SCNNode()
    for child in virtualObjectScene.rootNode.childNodes {
        child.geometry?.firstMaterial?.lightingModel = .physicallyBased
        wrapperNode.addChildNode(child)
    }
    cupNode.addChildNode(wrapperNode)
    scene.rootNode.addChildNode(cupNode)

    let billboardConstraint = SCNBillboardConstraint()
    billboardConstraint.freeAxes = SCNBillboardAxis.Y
    cupNode.constraints = [billboardConstraint]

这篇关于添加 SCNBillboardConstraint 使节点消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆