【问题标题】:How to add Shadow on surface plane?如何在表面平面上添加阴影?
【发布时间】:2019-04-20 11:53:59
【问题描述】:

我有一个问题,在ARView中显示物体上的阴影但没有显示在表面上,我附上了尝试代码?它正在工作 在对象上显示阴影但不显示在表面上。 Show Image

代码:

var light2 = SCNLight()
var lightNodeb2 = SCNNode()
light2.castsShadow = true
light2.automaticallyAdjustsShadowProjection = true
light2.maximumShadowDistance = 40.0
light2.orthographicScale = 1
light2.type = .directional
light2.shadowMapSize = CGSize(width: 2048, height: 2048)
light2.shadowMode = .forward
light2.shadowSampleCount = 128
light2.shadowRadius = 3
light2.shadowBias  = 32
light2.zNear=1;
light2.zFar=1000;
lightNodeb2.light = light2
lightNodeb2.position = SCNVector3(x: -1, y: 10, z: 1)
lightNodeb2.rotation = SCNVector4Make(2, 0, 0, -Float.pi / 3) 
self.sceneView.scene.rootNode.addChildNode(lightNodeb2)

【问题讨论】:

    标签: ios swift scenekit augmented-reality arkit


    【解决方案1】:

    我在这里看到 2 个可能的问题:

    您的灯设置正常。我认为第一个问题如下:您在场景图中将程序化灯光与“非程序化”3D 对象一起使用。

    检查一下。在这段代码中,所有对象都是程序化的:

    let sphereNode1 = SCNNode(geometry: SCNSphere(radius: 1))
    sphereNode1.position = SCNVector3(x: 0, y: 5, z: 3)
    scene.rootNode.addChildNode(sphereNode1)
    
    let sphereNode2 = SCNNode(geometry: SCNSphere(radius: 3))
    sphereNode2.position = SCNVector3(x: 0, y: 1, z: 0)
    scene.rootNode.addChildNode(sphereNode2)
    
    let boxNode = SCNNode(geometry: SCNBox(width: 20, height: 0.1, length: 20, chamferRadius: 0))
    boxNode.position = SCNVector3(x: 0, y: -3, z: 0)
    scene.rootNode.addChildNode(boxNode)
    
    let light2 = SCNLight()
    let lightNodeb2 = SCNNode()
    light2.castsShadow = true
    light2.automaticallyAdjustsShadowProjection = true
    light2.maximumShadowDistance = 40.0
    light2.orthographicScale = 1
    light2.type = .directional
    light2.shadowMapSize = CGSize(width: 2048, height: 2048)
    
    light2.shadowMode = .deferred    // Renders shadows in a postprocessing pass
    
    light2.shadowSampleCount = 128
    light2.shadowRadius = 3
    light2.shadowBias = 32
    light2.zNear = 1
    light2.zFar = 1000
    lightNodeb2.light = light2
    
    // DIRECTIONAL LIGHT POSITION doesn't matter. Matters only its direction.
    // lightNodeb2.position = SCNVector3(x: -1, y: 10, z: 1)
    
    lightNodeb2.rotation = SCNVector4(2, 0, 0, -Float.pi/3)
    scene.rootNode.addChildNode(lightNodeb2)
    

    所有对象Shadow castings默认为On

    第一个问题的解决方案:

    要使您的 3D 模型可供虚拟编程灯访问,请使用以下方法

    func childNode(withName name: String, recursively: Bool) -> SCNNode? {
        return SCNNode()
    }  
    
    let geometryNode = childNode(withName: "art.scnassets/your3Dmodel", 
                                 recursively: true)!
    
    scene.rootNode.addChildNode(geometryNode)
    

    第二个问题的解决方案:

    如果你想要一个带有阴影的隐藏平面,请使用以下代码

    hiddenPlaneNode.castsShadow = false
    hiddenPlaneNode.geometry?.firstMaterial?.lightingModel = .constant
    hiddenPlaneNode.geometry?.firstMaterial?.isDoubleSided = true
    hiddenPlaneNode.geometry?.firstMaterial?.readsFromDepthBuffer = true
    hiddenPlaneNode.geometry?.firstMaterial?.writesToDepthBuffer = true
    hiddenPlaneNode.geometry?.firstMaterial?.colorBufferWriteMask = []
    
    light2.shadowMode = .deferred
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-21
      • 2017-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-11
      • 1970-01-01
      • 2017-12-12
      相关资源
      最近更新 更多