2017-07-21 21 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