Skip to content

Commit a3edaa5

Browse files
Fix light flare for pbr sky (#6278)
* Fix Template directional light * Fix when using multiple directional lights with flare * restore default flare on template Co-authored-by: sebastienlagarde <sebastien@unity3d.com>
1 parent 7eda375 commit a3edaa5

File tree

7 files changed

+19
-25
lines changed

7 files changed

+19
-25
lines changed

com.unity.render-pipelines.high-definition/Runtime/Lighting/Light/HDGpuLightsBuilder.Jobs.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,12 @@ private void ConvertDirectionalLightToGPUFormat(
683683

684684
lightData.flareSize = Mathf.Max(lightRenderData.flareSize * Mathf.Deg2Rad, 5.960464478e-8f);
685685
lightData.flareFalloff = lightRenderData.flareFalloff;
686+
687+
// On some vendors trigonometry has very bad precision, so we precompute what we can on CPU to avoid precision issues (case 1369376).
688+
float radInner = 0.5f * lightData.angularDiameter;
689+
lightData.flareCosInner = Mathf.Cos(radInner);
690+
lightData.flareCosOuter = Mathf.Cos(radInner + lightData.flareSize);
691+
686692
lightData.flareTint = (Vector3)(Vector4)lightRenderData.flareTint;
687693
lightData.surfaceTint = (Vector3)(Vector4)lightRenderData.surfaceTint;
688694

com.unity.render-pipelines.high-definition/Runtime/Lighting/LightDefinition.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,16 @@ struct DirectionalLightData
9999

100100
public float diffuseDimmer;
101101
public float specularDimmer;
102+
102103
public float penumbraTint;
103104
public float isRayTracedContactShadow;
104105

105106
public float distanceFromCamera; // -1 -> no sky interaction
106107
public float angularDiameter; // Units: radians
108+
107109
public float flareFalloff;
110+
public float flareCosInner;
111+
public float flareCosOuter;
108112
public float __unused__;
109113

110114
public Vector3 flareTint;

com.unity.render-pipelines.high-definition/Runtime/Lighting/LightDefinition.cs.hlsl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ struct DirectionalLightData
8080
float distanceFromCamera;
8181
float angularDiameter;
8282
float flareFalloff;
83+
float flareCosInner;
84+
float flareCosOuter;
8385
float __unused__;
8486
float3 flareTint;
8587
float flareSize;

com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDStringConstants.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,6 @@ static class HDShaderIDs
581581
public static readonly int _SpaceEmissionMultiplier = Shader.PropertyToID("_SpaceEmissionMultiplier");
582582

583583
public static readonly int _RenderSunDisk = Shader.PropertyToID("_RenderSunDisk");
584-
public static readonly int _SunDiskCosines = Shader.PropertyToID("_SunDiskCosines");
585584

586585
public static readonly int _ColorSaturation = Shader.PropertyToID("_ColorSaturation");
587586
public static readonly int _AlphaSaturation = Shader.PropertyToID("_AlphaSaturation");

com.unity.render-pipelines.high-definition/Runtime/Sky/PhysicallyBasedSky/PhysicallyBasedSky.shader

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Shader "Hidden/HDRP/Sky/PbrSky"
44

55
#pragma vertex Vert
66

7+
// #pragma enable_d3d11_debug_symbols
78
#pragma editor_sync_compilation
89
#pragma target 4.5
910
#pragma only_renderers d3d11 playstation xboxone xboxseries vulkan metal switch
@@ -25,13 +26,6 @@ Shader "Hidden/HDRP/Sky/PbrSky"
2526
float _GroundEmissionMultiplier;
2627
float _SpaceEmissionMultiplier;
2728

28-
// Inner and outer cosine computed as:
29-
// float radInner = 0.5 * light.angularDiameter
30-
// float cosInner = cos(radInner); // (In _SunDiskCosines.x)
31-
// float cosOuter = cos(radInner + light.flareSize); // (In _SunDiskCosines.y)
32-
// We need to pass it over instead of computing it here because on some vendors trigonometry has very bad precision, so we precompute what we can on CPU to have better precision.
33-
float4 _SunDiskCosines;
34-
3529
// Sky framework does not set up global shader variables (even per-view ones),
3630
// so they can contain garbage. It's very difficult to not include them, however,
3731
// since the sky framework includes them internally in many header files.
@@ -118,19 +112,16 @@ Shader "Hidden/HDRP/Sky/PbrSky"
118112
float rad = acos(LdotV);
119113
float radInner = 0.5 * light.angularDiameter;
120114

121-
float cosInner = _SunDiskCosines.x;
122-
float cosOuter = _SunDiskCosines.y;
123-
124-
float solidAngle = TWO_PI * (1 - cosInner);
115+
float solidAngle = TWO_PI * (1 - light.flareCosInner);
125116

126-
if (LdotV >= cosOuter)
117+
if (LdotV >= light.flareCosOuter)
127118
{
128119
// Sun flare is visible. Sun disk may or may not be visible.
129120
// Assume uniform emission.
130121
float3 color = light.color.rgb;
131122
float scale = rcp(solidAngle);
132123

133-
if (LdotV >= cosInner) // Sun disk.
124+
if (LdotV >= light.flareCosInner) // Sun disk.
134125
{
135126
tFrag = lightDist;
136127

com.unity.render-pipelines.high-definition/Runtime/Sky/PhysicallyBasedSky/PhysicallyBasedSkyRenderer.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -500,14 +500,6 @@ public override void RenderSky(BuiltinSkyParameters builtinParams, bool renderFo
500500
}
501501
s_PbrSkyMaterialProperties.SetInt(HDShaderIDs._HasSpaceEmissionTexture, hasSpaceEmissionTexture);
502502

503-
// We need to pass it over instead of computing it here because on some vendors trigonometry has very bad precision, so we precompute what we can on CPU to have better precision.
504-
// We can safely retrieve HDAdditionalLightData as for PBR sky the sunlight is always going to be an HDRP light.
505-
var lightData = builtinParams.sunLight.gameObject.GetComponent<HDAdditionalLightData>();
506-
float radInner = 0.5f * lightData.angularDiameter * Mathf.Deg2Rad;
507-
float cosInner = Mathf.Cos(radInner);
508-
float cosOuter = Mathf.Cos(radInner + lightData.flareSize);
509-
s_PbrSkyMaterialProperties.SetVector(HDShaderIDs._SunDiskCosines, new Vector4(cosInner, cosOuter, 0, 0));
510-
511503
s_PbrSkyMaterialProperties.SetInt(HDShaderIDs._RenderSunDisk, renderSunDisk ? 1 : 0);
512504

513505
int pass = (renderForCubemap ? 0 : 2);

com.unity.template-hd/Assets/Scenes/SampleScene.unity

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28899,9 +28899,9 @@ MonoBehaviour:
2889928899
m_UseScreenSpaceShadows: 0
2890028900
m_InteractsWithSky: 1
2890128901
m_AngularDiameter: 0.53
28902-
m_FlareSize: 0
28903-
m_FlareTint: {r: 0, g: 0, b: 0, a: 1}
28904-
m_FlareFalloff: 0
28902+
m_FlareSize: 2
28903+
m_FlareTint: {r: 1, g: 1, b: 1, a: 1}
28904+
m_FlareFalloff: 4
2890528905
m_SurfaceTexture: {fileID: 0}
2890628906
m_SurfaceTint: {r: 1, g: 1, b: 1, a: 1}
2890728907
m_Distance: 150000000

0 commit comments

Comments
 (0)