A DirectionalShadowMap lets the directional light of a PBREnvironment cast shadows. The shadow casters are rendered into the shadow map first and then the scene is rendered as usual.
Shadow Map
Creates a DirectionalShadowMap and passes it to the environment. More cascades keep shadows sharp over a longer shadowDistance. cascadeBlend blends the far end of each cascade into the next to hide the seam, 0 disables it.
val shadowMap = DirectionalShadowMap(device, cascadeCount = 3)
shadowMap.shadowDistance = 40f
shadowMap.cascadeBlend = 0.1f
val lightDirection = MutableVec3f(0.5f, 0.8f, 0.3f).norm()
val environment =
PBREnvironment(device, shadowMap = shadowMap).apply {
setDirectionalLight(DirectionalLight(direction = lightDirection, intensity = 2.5f))
}
Shadow Batch
A second ModelBatch renders the shadow casters using a ShadowMaterialPipelineProvider.
val shadowBatch =
ModelBatch(device).apply {
defaultPipelineProvider = ShadowMaterialPipelineProvider()
depthFormat = shadowMap.format
}
Rendering
Update and render the shadow map before the main render pass.
onUpdate { dt ->
camera.update()
shadowMap.update(lightDirection, camera)
renderFrame { frame, commandEncoder ->
shadowMap.render(commandEncoder, shadowBatch, models, dt)
val renderPass = ... // main render pass
models.forEach { modelBatch.render(it, camera, environment) }
modelBatch.flush(renderPass, camera, dt)
renderPass.end()
}
}
onRelease {
shadowBatch.release()
shadowMap.release()
}
Shadow Casters
Only materials with castShadows enabled are rendered into the shadow map. Materials with an alphaCutoff cast cut out shadows.
val floorMaterial = PBRMaterial(device, baseColorTexture = checkered, castShadows = false)
val fox =
resourcesVfs["models/Fox.glb"].readGltfModel(
config = GltfLoaderPbrConfig(GltfModelConfig(castShadows = true, skinned = true))
)
Spot Light Shadows
A SpotShadowMap gives up to count spot lights with castShadows their own shadow. Lights past count still shine but don’t cast shadows.
val spotShadowMap = SpotShadowMap(device, count = 3)
spotShadowMap.pcfRadius = 2
val environment =
PBREnvironment(device, spotShadowMap = spotShadowMap).apply {
addSpotLight(SpotLight(intensity = 25f, range = 25f, castShadows = true))
}
val shadowBatch =
ModelBatch(device).apply {
defaultPipelineProvider = ShadowMaterialPipelineProvider()
depthFormat = spotShadowMap.format
}
Update it after the lights move and render it before the main render pass.
renderFrame { frame, commandEncoder ->
spotShadowMap.update(environment.spotLights)
spotShadowMap.render(commandEncoder, shadowBatch, models, dt)
val renderPass = ... // main render pass
}
depthBias and normalOffset reduce shadow acne and near skips casters too close to the light.