簡體   English   中英

Scenekit - 將子節點(Plane 節點)添加到攝像機前的父節點(球體節點)

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

我正在嘗試將子節點添加到擁有自己的相機的父節點。 在用戶點擊現場時,使用命中測試我將子節點(即平面節點)添加到父節點(球體節點)。 但我不知道為什么子節點沒有正確面對相機。 它在不同的位置看起來不同。 我想讓它解決看着相機的問題。

代碼 :

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

結果我使用上面的代碼:

添加的平面節點看起來很奇怪

在此處輸入圖片說明

我不知道您是否找到了答案,但是可以使用 SCNBillboardConstraint 釋放節點軸:)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM