Scenekit - 将子节点(平面节点)添加到相机前面的父节点(球体节点)

Posted

技术标签:

【中文标题】Scenekit - 将子节点(平面节点)添加到相机前面的父节点(球体节点)【英文标题】:Scenekit - Add child node (Plane node) to the parent node (sphere node) in front of camera 【发布时间】:2019-10-25 13:18:08 【问题描述】:

我正在尝试将子节点添加到拥有自己相机的父节点。在场景中点击用户,使用命中测试我将子节点(即平面节点)添加到父节点(球体节点)。但我不知道为什么子节点没有正确面对相机。它在不同的位置看起来不同。我希望它修复看着相机。

代码:

// Creating sphere node and adding camera to it 

func setup() 

        let sphere = SCNSphere(radius: 10)
        sphere.segmentCount = 360
        let material = getTextureMaterial()
        material.diffuse.contents = UIImage(named: "sample")
        sphere.firstMaterial = material
        let sphereNode = SCNNode()
        sphereNode.geometry = sphere
        sceneView.scene?.rootNode.addChildNode(sphereNode)


 //Creating texture material to show 360 degree image...

func getTextureMaterial() -> SCNMaterial 
        let material = SCNMaterial()
        material.diffuse.mipFilter = .nearest
        material.diffuse.magnificationFilter = .linear
        material.diffuse.contentsTransform = SCNMatrix4MakeScale(-1, 1, 1)
        material.diffuse.wrapS = .repeat
        material.cullMode = .front
        material.isDoubleSided = true
        return material
    




 @objc func handleTap(rec: UITapGestureRecognizer)
            if rec.state == .ended 
                let location: CGPoint = rec.location(in: sceneView)
                let hits = sceneView.hitTest(location, options: nil)

                if !hits.isEmpty 
                    let result: SCNHitTestResult = hits[0]
                     createPlaneNode(result: result)
                

        


func createPlaneNode(result : SCNHitTestResult) 
    let plane = SCNPlane(width: 5, height: 5)
    plane.firstMaterial?.diffuse.contents = UIColor.red
    plane.firstMaterial?.isDoubleSided = true
    let planeNode = SCNNode()
    planeNode.name = "B"
    planeNode.geometry = plane
    planeNode.position = result.worldCoordinates
    result.node.addChildNode(planeNode)

我使用上面的代码得到的结果:

添加的平面节点看起来很奇怪

【问题讨论】:

【参考方案1】:

我不知道您是否找到了答案,但是可以使用 SCNBillboardConstraint 释放节点轴:)

let billboardConstraint = SCNBillboardConstraint()
billboardConstraint.freeAxes = [.all]
node.constraints = [billboardConstraint]

【讨论】:

将试用并回复您。

以上是关于Scenekit - 将子节点(平面节点)添加到相机前面的父节点(球体节点)的主要内容,如果未能解决你的问题,请参考以下文章

SceneKit:关于定位相机节点的数学问题

在 SceneKit 节点周围添加边框

SceneKit:向相机节点添加定向光没有效果

VB.NET 如何将子节点添加到树视图中的特定节点

ExtJS 将子节点添加到一个空的异步树节点

有没有办法链接 SceneKit 动画?