0

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

代码 :

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

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

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

在此处输入图像描述

4

1 回答 1

0

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

let billboardConstraint = SCNBillboardConstraint()
billboardConstraint.freeAxes = [.all]
node.constraints = [billboardConstraint]
于 2020-01-13T06:03:21.290 回答