Skip to content

Commit 190c6d8

Browse files
remi-chapelainEvergreen
authored andcommitted
[HDRP] Fix Transmission not being affected by shadow from Volumetric Clouds.
While developping arctic demo, we noticed that the transmission in our icy moutains was still glowing eventhough they were in the volumetric clouds shadows, this was a huge discrepancy in lighting which is now eaisly fixed by this PR thanks to @adrien-tocqueville snippet. This wasn't the case for cloud layer because it's using the cookie of the directionnal light. ![c5784cf2073607debfbe678b852b052e](https://media.github.cds.internal.unity3d.com/user/1764/files/7819d363-b832-4a2c-856b-e9d0d4599b8e) Fix hard earth shadow ![image](https://media.github.cds.internal.unity3d.com/user/2154/files/5adb6416-6dff-4ad4-9afc-bd154048475d) ![image](https://media.github.cds.internal.unity3d.com/user/2154/files/64573645-fe94-48e1-bfa2-de8aa97af159)
1 parent b18ef86 commit 190c6d8

File tree

6 files changed

+30
-25
lines changed

6 files changed

+30
-25
lines changed

Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/LightLoop.hlsl

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,6 @@ void LightLoop( float3 V, PositionInputs posInput, PreLightData preLightData, BS
232232
context.shadowValue = GetDirectionalShadowAttenuation(context.shadowContext,
233233
posInput.positionSS, posInput.positionWS, GetNormalForShadowBias(bsdfData),
234234
light.shadowIndex, L);
235-
236-
#ifndef LIGHT_EVALUATION_NO_CLOUDS_SHADOWS
237-
// Apply the volumetric cloud shadow if relevant
238-
if (_VolumetricCloudsShadowOriginToggle.w == 1.0)
239-
context.shadowValue *= EvaluateVolumetricCloudsShadows(light, posInput.positionWS);
240-
#endif
241235
}
242236
}
243237
}

Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/SurfaceShading.hlsl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,24 @@ DirectLighting ShadeSurface_Directional(LightLoopContext lightLoopContext,
8282
float4 lightColor = EvaluateLight_Directional(lightLoopContext, posInput, light);
8383
lightColor.rgb *= lightColor.a; // Composite
8484

85+
float cloudShadow = 1.0f;
86+
#ifndef LIGHT_EVALUATION_NO_CLOUDS_SHADOWS
87+
// Apply the volumetric cloud shadow if relevant
88+
// We evaluate them here instead of inside EvaluateLight_Directional to be able to apply it on objects
89+
// with transmission and still benefit from shadow dimmer and colored shadows
90+
if (light.shadowIndex >= 0 && _VolumetricCloudsShadowOriginToggle.w == 1.0)
91+
{
92+
cloudShadow = EvaluateVolumetricCloudsShadows(light, posInput.positionWS);
93+
lightLoopContext.shadowValue *= cloudShadow;
94+
}
95+
#endif
96+
8597
#ifdef MATERIAL_INCLUDE_TRANSMISSION
8698
if (ShouldEvaluateThickObjectTransmission(V, L, preLightData, bsdfData, 0))
8799
{
88100
// Transmission through thick objects does not support shadowing
89101
// from directional lights. It will use the 'baked' transmittance value.
90-
lightColor *= _DirectionalTransmissionMultiplier;
102+
lightColor *= _DirectionalTransmissionMultiplier * cloudShadow;
91103
}
92104
else
93105
#endif

Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/VolumetricClouds/HDRenderPipeline.VolumetricCloudsShadows.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,11 @@ void ReleaseVolumetricCloudsShadows()
4545

4646
bool HasVolumetricCloudsShadows(HDCamera hdCamera, in VolumetricClouds settings)
4747
{
48-
return (HasVolumetricClouds(hdCamera, in settings)
49-
&& GetMainLight() != null
50-
&& settings.shadows.value);
51-
}
52-
53-
bool HasVolumetricCloudsShadows(HDCamera hdCamera)
54-
{
55-
VolumetricClouds settings = hdCamera.volumeStack.GetComponent<VolumetricClouds>();
56-
return HasVolumetricCloudsShadows(hdCamera, settings);
48+
return HasVolumetricClouds(hdCamera, settings) &&
49+
settings.shadows.value &&
50+
m_CurrentSunLight != null &&
51+
m_CurrentSunLight.shadows != LightShadows.None &&
52+
m_CurrentSunLightAdditionalLightData.shadowDimmer != 0.0f;
5753
}
5854

5955
void EvaluateShadowRegionData(HDCamera hdCamera, CommandBuffer cmd)
@@ -63,15 +59,15 @@ void EvaluateShadowRegionData(HDCamera hdCamera, CommandBuffer cmd)
6359
VolumetricClouds settings = hdCamera.volumeStack.GetComponent<VolumetricClouds>();
6460

6561
// Grab the light and make sure it is valid
66-
Light targetLight = GetMainLight();
67-
if (!HasVolumetricClouds(hdCamera, in settings) || targetLight == null || !settings.shadows.value)
62+
if (!HasVolumetricCloudsShadows(hdCamera, in settings))
6863
{
6964
// Bind the invalid volumetric clouds shadow texture
7065
cmd.SetGlobalTexture(HDShaderIDs._VolumetricCloudsShadowsTexture, Texture2D.blackTexture);
7166
return;
7267
}
7368

7469
// Grab the volume profile of the volumetric clouds
70+
Light targetLight = GetMainLight();
7571
Matrix4x4 wsToLSMat = targetLight.transform.worldToLocalMatrix;
7672
Matrix4x4 lsToWSMat = targetLight.transform.localToWorldMatrix;
7773

Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/VolumetricClouds/VolumetricCloudsUtilities.hlsl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ EnvironmentLighting EvaluateEnvironmentLighting(CloudRay ray, float3 entryEvalua
153153

154154
#ifdef PHYSICALLY_BASED_SUN
155155
// evaluate the attenuation at both points (entrance and exit of the cloud layer)
156-
lighting.sunColor0 *= EvaluateSunColorAttenuation(entryEvaluationPointPS, lighting.sunDirection);
157-
lighting.sunColor1 *= EvaluateSunColorAttenuation(exitEvaluationPointPS, lighting.sunDirection);
156+
lighting.sunColor0 *= EvaluateSunColorAttenuation(entryEvaluationPointPS, lighting.sunDirection, true);
157+
lighting.sunColor1 *= EvaluateSunColorAttenuation(exitEvaluationPointPS, lighting.sunDirection, false);
158158
#endif
159159

160160
// Evaluate cos of the theta angle between the view and light vectors
@@ -405,7 +405,7 @@ float3 EvaluateSunLuminance(float3 positionWS, float3 sunDirection, float3 sunCo
405405
float3 luminance = float3(0.0, 0.0, 0.0);
406406

407407
// If we early out, this means we've hit the earth itself
408-
if (ExitCloudVolume(ConvertToPS (positionWS), sunDirection, _HighestCloudAltitude, totalLightDistance))
408+
if (ExitCloudVolume(ConvertToPS(positionWS), sunDirection, _HighestCloudAltitude, totalLightDistance))
409409
{
410410
// Because of the very limited numebr of light steps and the potential humongous distance to cover, we decide to potnetially cover less and make it more useful
411411
totalLightDistance = clamp(totalLightDistance, 0, _NumLightSteps * LIGHT_STEP_MAXIMAL_SIZE);

Packages/com.unity.render-pipelines.high-definition/Runtime/Sky/CloudSystem/CloudLayer/CloudLayerCommon.hlsl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,11 @@ float4 GetCloudLayerColor(float3 dir, int index)
117117
cloud = SampleCloudMap(dir, index);
118118

119119
float3 lightColor = _SunLightColor(index);
120-
float3 ambient = SampleSH9(_AmbientProbeBuffer, float3(0, -1, 0)) * _AmbientDimmer(index);
120+
float3 ambient = max(SampleSH9(_AmbientProbeBuffer, float3(0, -1, 0)), 0) * _AmbientDimmer(index);
121121

122122
#ifdef PHYSICALLY_BASED_SUN
123-
lightColor *= EvaluateSunColorAttenuation(position + float3(0,_PlanetaryRadius,0), _SunDirection);
123+
float3 positionPS = position + float3(0,_PlanetaryRadius,0);
124+
lightColor *= EvaluateSunColorAttenuation(positionPS, _SunDirection, true);
124125
#endif
125126

126127
return float4(cloud.x * lightColor + ambient * cloud.y, cloud.y) * _Opacity;

Packages/com.unity.render-pipelines.high-definition/Runtime/Sky/PhysicallyBasedSky/PhysicallyBasedSkyCommon.hlsl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ float3 EvaluateSunColorAttenuation(float cosTheta, float r)
494494
}
495495

496496
// This function evaluates the sun color attenuation from the physically based sky
497-
float3 EvaluateSunColorAttenuation(float3 positionPS, float3 sunDirection)
497+
float3 EvaluateSunColorAttenuation(float3 positionPS, float3 sunDirection, bool estimatePenumbra = false)
498498
{
499499
float r = length(positionPS);
500500
float cosTheta = dot(positionPS, sunDirection) * rcp(r); // Normalize
@@ -507,7 +507,9 @@ float3 EvaluateSunColorAttenuation(float3 positionPS, float3 sunDirection)
507507
{
508508
float3 oDepth = ComputeAtmosphericOpticalDepth(r, cosTheta, true);
509509
float3 opacity = 1 - TransmittanceFromOpticalDepth(oDepth);
510-
return 1 - (Desaturate(opacity, _AlphaSaturation) * _AlphaMultiplier);
510+
float penumbra = saturate((cosTheta - cosHoriz) / 0.0019f); // very scientific value
511+
float3 attenuation = 1 - (Desaturate(opacity, _AlphaSaturation) * _AlphaMultiplier);
512+
return estimatePenumbra ? attenuation * penumbra : attenuation;
511513
}
512514
else
513515
{

0 commit comments

Comments
 (0)