Adding a SCNBillboardConstraint makes the node dissapear

只愿长相守 提交于 2019-12-04 00:29:10

问题


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.

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

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]

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


回答1:


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]


来源:https://stackoverflow.com/questions/49194597/adding-a-scnbillboardconstraint-makes-the-node-dissapear

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!