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

时间:2019-06-11 07:05:09

标签: ios swift scenekit scnnode scnscene

我正在尝试将子节点添加到拥有它自己的摄像机的父节点中。当用户在场景中点击时,使用命中测试我将子节点(即平面节点)添加到父节点(球体节点)。但是我不知道为什么子节点没有正确面对相机。在不同的位置看起来有所不同。我希望它可以解决相机问题。

代码:

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

结果我正在使用上面的代码:

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

enter image description here

1 个答案:

答案 0 :(得分:0)

我不知道您是否找到答案,但是可以通过SCNBillboardConstraint来释放节点轴:)

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