Skip to content

Fix shadow mask fade and optimize it at same time #5911

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions com.unity.render-pipelines.high-definition/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Fixed ambient occlusion strenght incorrectly using GTAOMultiBounce
- Fixed range compression factor being clamped. (case 1365707)
- Fixed tooltip not showing on labels in ShaderGraphs (1358483).
- Fixed and optimize distance shadowmask fade.

## [13.0.0] - 2021-09-01

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ struct DirectionalLightData
[SurfaceDataAttributes(precision = FieldPrecision.Real)]
public Vector4 shadowMaskSelector; // Used with ShadowMask feature

public Vector2 cascadesBorderFadeScaleBias;

public float diffuseDimmer;
public float specularDimmer;
public float penumbraTint;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ struct DirectionalLightData
real minRoughness;
int screenSpaceShadowIndex;
real4 shadowMaskSelector;
float2 cascadesBorderFadeScaleBias;
float diffuseDimmer;
float specularDimmer;
float penumbraTint;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,19 +269,9 @@ SHADOW_TYPE EvaluateShadow_Directional( LightLoopContext lightLoopContext, Posit
shadow = lightLoopContext.shadowValue;

#ifdef SHADOWS_SHADOWMASK
// TODO: Optimize this code! Currently it is a bit like brute force to get the last transistion and fade to shadow mask, but there is
// certainly more efficient to do
// We reuse the transition from the cascade system to fade between shadow mask at max distance
uint payloadOffset;
real fade;
int cascadeCount;
int shadowSplitIndex = 0;

shadowSplitIndex = EvalShadow_GetSplitIndex(lightLoopContext.shadowContext, light.shadowIndex, posInput.positionWS, fade, cascadeCount);

// we have a fade caclulation for each cascade but we must lerp with shadow mask only for the last one
// if shadowSplitIndex is -1 it mean we are outside cascade and should return 1.0 to use shadowmask: saturate(-shadowSplitIndex) return 0 for >= 0 and 1 for -1
fade = ((shadowSplitIndex + 1) == cascadeCount) ? fade : saturate(-shadowSplitIndex);
float3 camToPixel = posInput.positionWS - GetPrimaryCameraPosition();
float distanceCamToPixel2 = dot(camToPixel, camToPixel);
float fade = saturate(distanceCamToPixel2 * light.cascadesBorderFadeScaleBias.x + light.cascadesBorderFadeScaleBias.y);

// In the transition code (both dithering and blend) we use shadow = lerp( shadow, 1.0, fade ) for last transition
// mean if we expend the code we have (shadow * (1 - fade) + fade). Here to make transition with shadow mask
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,22 @@ internal void GetDirectionalLightData(CommandBuffer cmd, HDCamera hdCamera, Visi
{
lightData.shadowMaskSelector[lightComponent.bakingOutput.occlusionMaskChannel] = 1.0f;
lightData.nonLightMappedOnly = lightComponent.lightShadowCasterMode == LightShadowCasterMode.NonLightmappedOnly ? 1 : 0;
// Get shadow info from the volume stack.
var shadowSettings = hdCamera.volumeStack.GetComponent<HDShadowSettings>();
float maxDistanceSq = shadowSettings.maxShadowDistance.value * shadowSettings.maxShadowDistance.value;
float outBorderDistance = shadowSettings.cascadeShadowBorders[shadowSettings.cascadeShadowSplitCount.value - 1];
if (outBorderDistance < 1e-4f)
{
lightData.cascadesBorderFadeScaleBias = new Vector2(1e6f, -maxDistanceSq * 1e6f);
}
else
{
outBorderDistance = 1.0f - outBorderDistance;
outBorderDistance *= outBorderDistance;
float distanceFadeNear = outBorderDistance * maxDistanceSq;
lightData.cascadesBorderFadeScaleBias.x = 1.0f / (maxDistanceSq - distanceFadeNear);
lightData.cascadesBorderFadeScaleBias.y = -distanceFadeNear / (maxDistanceSq - distanceFadeNear);
}
}
else
{
Expand Down