2017-07-21 19 views
0

我想在我的ARKit代碼中添加多個帶有陰影的3d對象。SCNNode clone()爲節點添加更多光線,因爲節點被克隆了

創建節點這樣

func loadNodaA() 
{ 

    isObjectCoordinatesAtCentre = false 

    let scaleFactor = 0.005 

    let dragonScene = SCNScene(named:"art.scnassets/Lowpoly_tree_sample.dae")! 

    let queenNode = dragonScene.rootNode.childNode(withName: "Tree_lp_11", recursively: true) 

    let geometry : SCNGeometry = (queenNode?.geometry)! 

    geometry.materials = [self.CreateMaterialWithName("streakedmetal")]; 

    nodeA = dragonScene.rootNode 
    nodeA.position = SCNVector3Zero 
    nodeA.scale = SCNVector3(x: Float(scaleFactor), y: Float(scaleFactor), z: Float(scaleFactor)) 
    nodeA.position = SCNVector3Make(0,0,0) 

} 

而且一旦ARKIT檢測到飛機,我創建這樣一個平面:

func createPlaneNode(anchor: ARPlaneAnchor) -> SCNNode { 

    let floatwidth = anchor.extent.x 
    let floatlength = anchor.extent.z 
    let floatPlaneHeight : Float = 0.001 

    let planeGeometry = SCNBox(width: CGFloat(floatwidth), height: CGFloat(floatPlaneHeight), length: CGFloat(floatlength), chamferRadius:0) 

    planesGeometries[anchor.identifier] = planeGeometry 

    let planeNode = SCNNode(geometry: planeGeometry) 

    let transparentMaterial : SCNMaterial = SCNMaterial() 
    //transparentMaterial.diffuse.contents = UIImage.init(named: "art.scnassets/GridtextureGrayLines.png") 

    transparentMaterial.diffuse.contents = UIImage.init(named: "art.scnassets/sunmica.png") 

     //planeGeometry.materials = [transparentMaterial, transparentMaterial, transparentMaterial, transparentMaterial,self.CreateMaterialWithName("redbricks"), transparentMaterial]; 
    //planeGeometry.materials = [transparentMaterial] 

    planeGeometry.firstMaterial = transparentMaterial 

    transparentMaterial.diffuse.contentsTransform = SCNMatrix4MakeScale(2,2, 0) 

    planeNode.geometry?.firstMaterial?.diffuse.wrapS = .repeat 
    planeNode.geometry?.firstMaterial?.diffuse.wrapT = .repeat 
    planeNode.position = SCNVector3Make(0, -(floatPlaneHeight/2), 0) 

    planeNode.physicsBody = SCNPhysicsBody(type: .kinematic, shape: SCNPhysicsShape(geometry: planeGeometry, options: nil)) 

    //planeNode.castsShadow = true 


    self.setTextureScale(plane: planeNode, planeGeometry: planeGeometry) 

    return planeNode 

} 

而且在我加入樹對象與陰影面的水龍頭到它。但問題是,當我克隆這些對象一次又一次,樹下的平面獲得更多的亮度,我認爲我添加的光節點不斷爲每個節點添加,儘管我試圖按名稱刪除它。

func insertGeometry(_ anchor : ARHitTestResult) { 



    let h = CGFloat(selectedNode.boundingBox.max.y - selectedNode.boundingBox.min.y) 



    var insertionYOffset: Float = Float(h)/2.0; 

    if isObjectCoordinatesAtCentre == false 
    { 
     insertionYOffset = 0 
    } 



    let hitPointPosition = SCNVector3Make(
     anchor.worldTransform.columns.3.x, 
     anchor.worldTransform.columns.3.y + insertionYOffset, 
     anchor.worldTransform.columns.3.z 
    ); 

    var nodeClone = SCNNode.init() 

    if CurrentNodeChoise == 1 
    { 
     //This means draw tree 
     nodeClone = nodeA.clone() 
    } 

    nodeClone.position = hitPointPosition 
    sceneView.scene.rootNode.addChildNode(nodeClone) 



    guard let spotlightNode : SCNNode = (sceneView.scene.rootNode.childNode(withName: "LightForShadows", recursively: true)) else { 


     let spot = SCNLight() 
     spot.type = .spot 
     spot.castsShadow = true 
     spot.color = UIColor.darkGray 
     spot.shadowMode = .forward 
     spot.intensity = 2000 
     spot.shadowRadius = 5; 
     spot.shadowSampleCount = 20; 
     spot.shadowMapSize = CGSize(width: 4000, height: 4000) 
     spot.orthographicScale=50; 
     spot.zNear=1; 
     spot.zFar=1000; 
     spot.name = "myLight" 
     let spotNode = SCNNode() 
     spotNode.light = spot 
     spotNode.position = SCNVector3(x:4, y: 6, z: 0)    
     let lookAt = SCNLookAtConstraint.init(target: nodeClone) 
     constraintsArray.append(lookAt) 
     spotNode.constraints = constraintsArray    
     spotNode.name = "LightForShadows"   
     sceneView.scene.rootNode.addChildNode(spotNode) 

     return 
    } 

    let lookAt = SCNLookAtConstraint.init(target: nodeClone) 

    constraintsArray.append(lookAt) 

    spotlightNode.constraints = constraintsArray 
} 

我也嘗試只給第一個節點添加光,然後通過更新第一個光節點爲其餘節點添加光。但是這也不起作用。我希望樹木能夠在平面下面添加陰影並保持不變。此外,陰影並不完美,有沒有辦法讓它們變得更好?

查看截圖樹下的平面如何逐個添加樹。

First Image

Second Image

Third Image

回答

2

對於每次添加添加光樹,當然,讓你的飛機亮。

堅持你在場景中創建的一盞燈,並更新它的約束,指向飛機。添加其他節點時,您不需要對閃電做任何事情。當你的場景有一個創建陰影的燈光時,這會影響你添加的每個節點。

爲了讓你的陰影「看起來更好」 - 無論這意味着什麼 - 你需要玩弄不同的閃電模式和燈光參數。我建議在Xcode編輯器中創建一個場景並使用這些屬性。

+0

非常感謝你,我現在添加了光節點到我的平面節點,它現在可以工作。 – AKumar

+0

還有一個問題:如果我只用線條使用透明網格圖像,我看不到陰影。正如你在第一張圖片中看到的棕色平面陰影是可見的,但是當我用透明網格開關時,陰影變得不可見。 – AKumar

+0

如果您使飛機基本不可見,則SceneKit可以渲染陰影。查看[SceneKit:WWDC 17的新功能](https://developer.apple.com/videos/play/wwdc2017/604/?time=2569)以查看解決方法。 – orangenkopf