从远处的光线投下阴影

Cast shadows from distant light

我有一个 AR 应用程序,我在其中计算了用户当前的太阳位置。我将这个位置缩小到 10,000 meters 的距离,所以当我在场景中移动时它有点 "stays" 到位。现在我想从这个节点投射阴影到我场景中的其他节点上。我尝试了几种闪电,但 none 成功了。有些根本没有投下阴影,有些则表现得很奇怪。从如此遥远的节点创建光源以在不可见 floor node 上投射阴影的最佳方法是什么?我还应该向场景添加 ambient 灯光吗?它可以添加到相机节点,还是应该在其他地方?

使用 directional 光模拟太阳(太阳有平行光线,对我们来说是来自非常远的光源的初级光线)并使用 ambient 光模拟假二次光线(以防你这样做不使用全局照明)。

// DIRECTIONAL LIGHT for `primary light rays` simulation
let lightNode = SCNNode()
lightNode.light = SCNLight()
lightNode.light!.type = .directional
lightNode.light!.castsShadow = true
lightNode.light!.shadowMode = .deferred
lightNode.light!.categoryBitMask = -1
lightNode.light!.automaticallyAdjustsShadowProjection = true
//lightNode.light!.maximumShadowDistance = 11000
lightNode.position = SCNVector3(x: 0, y: -5000, z: 0)
lightNode.rotation = SCNVector4(x: -1, y: 0, z: 0, w: .pi/2)
scene.rootNode.addChildNode(lightNode)

Tip: The position of directional light could be any (in the following code it is even under the plane y:-5000), the most important thing is direction of directional light!

Also directional light has no falloff parameter or, so called, quadratic light decay.

// AMBIENT LIGHT for `fake secondary light rays` simulation
let ambientLightNode = SCNNode()
ambientLightNode.light = SCNLight()
ambientLightNode.light!.type = .ambient
ambientLightNode.light!.intensity = 1000
ambientLightNode.light!.color = NSColor.white
scene.rootNode.addChildNode(ambientLightNode)

不要错误地使用这个布尔值:

lightNode.castsShadow = true

而不是这个:

lightNode.light!.castsShadow = true

布尔值lightNode.castsShadow决定SceneKit是否将节点的内容渲染成阴影贴图。

如果您希望在手动创建的 directional 灯光中启用阴影,这里有一个屏幕截图:

有时候,给相机装上一盏灯会有一些好处。在这种情况下,法线与光线平行的物体表面会被照亮。