|
|
|
|
|
|
|
|
|
|
struct ShadowSamplingData |
|
|
|
{ |
|
|
|
half shadowStrength; |
|
|
|
half4 shadowOffset0; |
|
|
|
half4 shadowOffset1; |
|
|
|
half4 shadowOffset2; |
|
|
|
|
|
|
ShadowSamplingData GetMainLightShadowSamplingData() |
|
|
|
{ |
|
|
|
ShadowSamplingData shadowSamplingData; |
|
|
|
shadowSamplingData.shadowStrength = _ShadowData.x; |
|
|
|
shadowSamplingData.shadowOffset0 = _ShadowOffset0; |
|
|
|
shadowSamplingData.shadowOffset1 = _ShadowOffset1; |
|
|
|
shadowSamplingData.shadowOffset2 = _ShadowOffset2; |
|
|
|
|
|
|
ShadowSamplingData GetLocalLightShadowSamplingData() |
|
|
|
{ |
|
|
|
ShadowSamplingData shadowSamplingData; |
|
|
|
shadowSamplingData.shadowStrength = _LocalShadowData.x; |
|
|
|
shadowSamplingData.shadowOffset0 = _LocalShadowOffset0; |
|
|
|
shadowSamplingData.shadowOffset1 = _LocalShadowOffset1; |
|
|
|
shadowSamplingData.shadowOffset2 = _LocalShadowOffset2; |
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
inline half SampleScreenSpaceShadowMap(float4 shadowCoord) |
|
|
|
half GetMainLightShadowStrength() |
|
|
|
{ |
|
|
|
return _ShadowData.x; |
|
|
|
} |
|
|
|
|
|
|
|
half GetLocalLightShadowStrenth(int lightIndex) |
|
|
|
{ |
|
|
|
return _LocalShadowData[lightIndex]; |
|
|
|
} |
|
|
|
|
|
|
|
half SampleScreenSpaceShadowMap(float4 shadowCoord) |
|
|
|
{ |
|
|
|
shadowCoord.xy /= shadowCoord.w; |
|
|
|
|
|
|
|
|
|
|
return attenuation; |
|
|
|
} |
|
|
|
|
|
|
|
inline real SampleShadowmap(float4 shadowCoord, TEXTURE2D_SHADOW_ARGS(ShadowMap, sampler_ShadowMap), ShadowSamplingData samplingData, float isMainLight = 0.0) |
|
|
|
real SampleShadowmap(float4 shadowCoord, TEXTURE2D_SHADOW_ARGS(ShadowMap, sampler_ShadowMap), ShadowSamplingData samplingData, half shadowStrength) |
|
|
|
if (isMainLight == 0.0) |
|
|
|
shadowCoord.xyz /= shadowCoord.w; |
|
|
|
shadowCoord.xyz /= shadowCoord.w; |
|
|
|
|
|
|
|
real attenuation; |
|
|
|
|
|
|
|
|
|
|
attenuation = SAMPLE_TEXTURE2D_SHADOW(ShadowMap, sampler_ShadowMap, shadowCoord.xyz); |
|
|
|
#endif |
|
|
|
|
|
|
|
// Apply shadow strength |
|
|
|
attenuation = LerpWhiteTo(attenuation, samplingData.shadowStrength); |
|
|
|
attenuation = LerpWhiteTo(attenuation, shadowStrength); |
|
|
|
inline half ComputeCascadeIndex(float3 positionWS) |
|
|
|
half ComputeCascadeIndex(float3 positionWS) |
|
|
|
{ |
|
|
|
// TODO: profile if there's a performance improvement if we avoid indexing here |
|
|
|
float3 fromCenter0 = positionWS.xyz - _DirShadowSplitSpheres[0].xyz; |
|
|
|
|
|
|
return SampleScreenSpaceShadowMap(shadowCoord); |
|
|
|
#else |
|
|
|
ShadowSamplingData shadowSamplingData = GetMainLightShadowSamplingData(); |
|
|
|
return SampleShadowmap(shadowCoord, TEXTURE2D_PARAM(_ShadowMap, sampler_ShadowMap), shadowSamplingData, 1.0); |
|
|
|
half shadowStrength = GetMainLightShadowStrength(); |
|
|
|
return SampleShadowmap(shadowCoord, TEXTURE2D_PARAM(_ShadowMap, sampler_ShadowMap), shadowSamplingData, shadowStrength); |
|
|
|
#endif |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
#else |
|
|
|
float4 shadowCoord = mul(_LocalWorldToShadowAtlas[lightIndex], float4(positionWS, 1.0)); |
|
|
|
ShadowSamplingData shadowSamplingData = GetLocalLightShadowSamplingData(); |
|
|
|
return SampleShadowmap(shadowCoord, TEXTURE2D_PARAM(_LocalShadowMapAtlas, sampler_LocalShadowMapAtlas), shadowSamplingData, 0.0); |
|
|
|
half shadowStrength = GetLocalLightShadowStrenth(lightIndex); |
|
|
|
return SampleShadowmap(shadowCoord, TEXTURE2D_PARAM(_LocalShadowMapAtlas, sampler_LocalShadowMapAtlas), shadowSamplingData, shadowStrength); |
|
|
|
#endif |
|
|
|
} |
|
|
|
|