@@ -1351,6 +1351,8 @@ pl__renderer_generate_cascaded_shadow_map(plRenderEncoder* ptEncoder, plCommandB
13511351
13521352 // TODO: we shouldn't have to check all rects, optimize this
13531353
1354+ float fShadowFarZ = pl_min (ptSceneCamera -> fFarZ , gptData -> tRuntimeOptions .fMaxShadowRange );
1355+
13541356 const uint32_t uAtlasRectCount = pl_sb_size (ptScene -> sbtShadowRects );
13551357 for (uint32_t uRectIndex = 0 ; uRectIndex < uAtlasRectCount ; uRectIndex ++ )
13561358 {
@@ -1363,7 +1365,7 @@ pl__renderer_generate_cascaded_shadow_map(plRenderEncoder* ptEncoder, plCommandB
13631365 {
13641366 continue ;
13651367 }
1366- const plLightComponent * ptLight = gptECS -> get_component (ptScene -> ptComponentLibrary , gptData -> tLightComponentType , ptScene -> sbtDirectionLights [ptData -> uLightIndex ].tEntity );
1368+ plLightComponent * ptLight = gptECS -> get_component (ptScene -> ptComponentLibrary , gptData -> tLightComponentType , ptScene -> sbtDirectionLights [ptData -> uLightIndex ].tEntity );
13671369
13681370
13691371 int iShadowIndex = ptScene -> sbtDirectionLightData [ptData -> uLightIndex ].iShadowIndex ;
@@ -1381,13 +1383,34 @@ pl__renderer_generate_cascaded_shadow_map(plRenderEncoder* ptEncoder, plCommandB
13811383 const plVec3 tDirection = pl_norm_vec3 (ptLight -> tDirection );
13821384 const uint32_t uCascadeCount = tInfo .bAltMode ? 1 : ptLight -> uCascadeCount ; // probe only needs single cascade
13831385
1384- const float afCascadeSplits [4 ] = {
1386+ float afCascadeSplits [4 ] = {
13851387 tInfo .bAltMode ? 1.0f : ptLight -> afCascadeSplits [0 ], // use whole frustum for environment probes
13861388 ptLight -> afCascadeSplits [1 ],
13871389 ptLight -> afCascadeSplits [2 ],
13881390 ptLight -> afCascadeSplits [3 ]
13891391 };
13901392
1393+ if (!tInfo .bAltMode && ptLight -> fShadowLambda > 0.0f )
1394+ {
1395+ for (uint32_t i = 1 ; i <= uCascadeCount ; ++ i )
1396+ {
1397+ float p = (float )i / (float )uCascadeCount ;
1398+
1399+ float logSplit =
1400+ ptSceneCamera -> fNearZ * powf (fShadowFarZ / ptSceneCamera -> fNearZ , p );
1401+
1402+ float linSplit =
1403+ ptSceneCamera -> fNearZ + (fShadowFarZ - ptSceneCamera -> fNearZ ) * p ;
1404+
1405+ float di =
1406+ ptLight -> fShadowLambda * logSplit +
1407+ (1.0f - ptLight -> fShadowLambda ) * linSplit ;
1408+
1409+ afCascadeSplits [i - 1 ] = (di - ptSceneCamera -> fNearZ )/(ptSceneCamera -> fFarZ - ptSceneCamera -> fNearZ );
1410+ ptLight -> afCascadeSplits [i - 1 ] = afCascadeSplits [i - 1 ];
1411+ }
1412+ }
1413+
13911414 //-------------------------------------------------------------------------
13921415 // stable light basis from direction only
13931416 //-------------------------------------------------------------------------
@@ -1513,7 +1536,7 @@ pl__renderer_generate_cascaded_shadow_map(plRenderEncoder* ptEncoder, plCommandB
15131536 const float fCenterZ = 0.5f * (fZMin + fZMax );
15141537
15151538 // optional z padding for off-frustum casters
1516- const float fDepthPadding = 200 .0f ; // TODO: make option
1539+ const float fDepthPadding = 100 .0f ; // TODO: make option
15171540 const float fNearZ = fZMax + fDepthPadding ;
15181541 const float fFarZ = fZMin - fDepthPadding ;
15191542
@@ -1580,7 +1603,7 @@ pl__renderer_generate_cascaded_shadow_map(plRenderEncoder* ptEncoder, plCommandB
15801603 };
15811604 gptGfx -> set_viewport (ptEncoder , & tViewport );
15821605 gptGfx -> set_scissor_region (ptEncoder , & tScissor );
1583- gptGfx -> set_depth_bias (ptEncoder , gptData -> tRuntimeOptions .fShadowConstantDepthBias , 0.0f , gptData -> tRuntimeOptions .fShadowSlopeDepthBias );
1606+ gptGfx -> set_depth_bias (ptEncoder , gptData -> tRuntimeOptions .fTerrainShadowConstantDepthBias , 0.0f , gptData -> tRuntimeOptions .fTerrainShadowSlopeDepthBias );
15841607 gptGfx -> bind_shader (ptEncoder , ptScene -> tTerrainShadowShader );
15851608 gptGfx -> bind_vertex_buffer (ptEncoder , ptScene -> ptTerrain -> tVertexBuffer );
15861609 plBindGroupHandle atBindGroups [] = {ptScene -> atSceneBindGroups [uFrameIdx ], tInfo .tBindGroup };
0 commit comments