【问题标题】:Scenekit - Add child node (Plane node) to the parent node (sphere node) in front of cameraScenekit - 将子节点(平面节点)添加到相机前面的父节点(球体节点)
【发布时间】: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)
}

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

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

【问题讨论】:

    标签: ios swift scenekit scnnode scnscene


    【解决方案1】:

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

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

    【讨论】:

    • 将试用并回复您。
    猜你喜欢
    • 1970-01-01
    • 2021-11-23
    • 1970-01-01
    • 2016-06-16
    • 2017-12-29
    • 1970-01-01
    • 1970-01-01
    • 2015-03-20
    • 2020-08-25
    相关资源
    最近更新 更多