简体   繁体   English

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

[英]Scenekit - Add child node (Plane node) to the parent node (sphere node) in front of camera

I am trying to add child node to the parent node which have it's own camera.我正在尝试将子节点添加到拥有自己的相机的父节点。 on tap of the user on scene, using hit test I am adding child node (ie plane node) to the the parent node (sphere node).在用户点击现场时,使用命中测试我将子节点(即平面节点)添加到父节点(球体节点)。 But I don't know why the child node is not facing properly to the camera.但我不知道为什么子节点没有正确面对相机。 it's looking different on different location.它在不同的位置看起来不同。 I wanted it to fix looking at the camera.我想让它解决看着相机的问题。

Code :代码 :

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

Results I am getting using above code:结果我使用上面的代码:

Added Plane node is looking weird添加的平面节点看起来很奇怪

在此处输入图片说明

I don't know if you ever found your answer but, it would be done freeing the node axes with SCNBillboardConstraint :)我不知道您是否找到了答案,但是可以使用 SCNBillboardConstraint 释放节点轴:)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM