From 409c74bf1791bc9c12a7b93d0566a6b35612c7f5 Mon Sep 17 00:00:00 2001 From: Erik Hakala Date: Tue, 28 Sep 2021 18:40:37 +0300 Subject: [PATCH] Fix stale data for main light directional cookie. --- .../Runtime/LightCookieManager.cs | 9 +++++++++ .../ShaderLibrary/LightCookie/LightCookie.hlsl | 5 ++++- .../ShaderLibrary/LightCookie/LightCookieInput.hlsl | 4 ++++ .../ShaderLibrary/LightCookie/LightCookieTypes.hlsl | 1 + 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/com.unity.render-pipelines.universal/Runtime/LightCookieManager.cs b/com.unity.render-pipelines.universal/Runtime/LightCookieManager.cs index 45ff002f048..73a10e181c1 100644 --- a/com.unity.render-pipelines.universal/Runtime/LightCookieManager.cs +++ b/com.unity.render-pipelines.universal/Runtime/LightCookieManager.cs @@ -31,6 +31,8 @@ static class ShaderProperty private enum LightCookieShaderFormat { + None = -1, + RGB = 0, Alpha = 1, Red = 2 @@ -580,6 +582,13 @@ bool SetupMainLight(CommandBuffer cmd, ref VisibleLight visibleMainLight) cmd.SetGlobalMatrix(ShaderProperty.mainLightWorldToLight, cookieMatrix); cmd.SetGlobalFloat(ShaderProperty.mainLightCookieTextureFormat, cookieFormat); } + else + { + // Make sure we erase stale data in case the main light is disabled but cookie system is enabled (for additional lights). + cmd.SetGlobalTexture(ShaderProperty.mainLightTexture, Texture2D.whiteTexture); + cmd.SetGlobalMatrix(ShaderProperty.mainLightWorldToLight, Matrix4x4.identity); + cmd.SetGlobalFloat(ShaderProperty.mainLightCookieTextureFormat, (float)LightCookieShaderFormat.None); + } return isMainLightCookieEnabled; } diff --git a/com.unity.render-pipelines.universal/ShaderLibrary/LightCookie/LightCookie.hlsl b/com.unity.render-pipelines.universal/ShaderLibrary/LightCookie/LightCookie.hlsl index 920b2adf4d0..8aefa0bd50a 100644 --- a/com.unity.render-pipelines.universal/ShaderLibrary/LightCookie/LightCookie.hlsl +++ b/com.unity.render-pipelines.universal/ShaderLibrary/LightCookie/LightCookie.hlsl @@ -17,7 +17,7 @@ float2 ComputeLightCookieUVDirectional(float4x4 worldToLight, float3 samplePosit { // Translate and rotate 'positionWS' into the light space. // Project point to light "view" plane, i.e. discard Z. - float2 positionLS = mul(worldToLight, float4(samplePositionWS, 1)).xy; + float2 positionLS = mul(worldToLight, float4(samplePositionWS, 1)).xy; // Remap [-1, 1] to [0, 1] // (implies the transform has ortho projection mapping world space box to [-1, 1]) @@ -70,6 +70,9 @@ float2 ComputeLightCookieUVPoint(float4x4 worldToLight, float3 samplePositionWS, real3 SampleMainLightCookie(float3 samplePositionWS) { + if(!IsMainLightCookieEnabled()) + return real3(1,1,1); + float2 uv = ComputeLightCookieUVDirectional(_MainLightWorldToLight, samplePositionWS, float4(1, 1, 0, 0), URP_TEXTURE_WRAP_MODE_NONE); real4 color = SampleMainLightCookieTexture(uv); diff --git a/com.unity.render-pipelines.universal/ShaderLibrary/LightCookie/LightCookieInput.hlsl b/com.unity.render-pipelines.universal/ShaderLibrary/LightCookie/LightCookieInput.hlsl index 4cba98d33cb..3c4e85132e9 100644 --- a/com.unity.render-pipelines.universal/ShaderLibrary/LightCookie/LightCookieInput.hlsl +++ b/com.unity.render-pipelines.universal/ShaderLibrary/LightCookie/LightCookieInput.hlsl @@ -103,6 +103,10 @@ real4 SampleAdditionalLightsCookieAtlasTexture(float2 uv) } // Helpers +bool IsMainLightCookieEnabled() +{ + return _MainLightCookieTextureFormat != URP_LIGHT_COOKIE_FORMAT_NONE; +} bool IsLightCookieEnabled(int lightBufferIndex) { diff --git a/com.unity.render-pipelines.universal/ShaderLibrary/LightCookie/LightCookieTypes.hlsl b/com.unity.render-pipelines.universal/ShaderLibrary/LightCookie/LightCookieTypes.hlsl index dcc5933fa66..be2d3d477e3 100644 --- a/com.unity.render-pipelines.universal/ShaderLibrary/LightCookie/LightCookieTypes.hlsl +++ b/com.unity.render-pipelines.universal/ShaderLibrary/LightCookie/LightCookieTypes.hlsl @@ -5,6 +5,7 @@ // Types +#define URP_LIGHT_COOKIE_FORMAT_NONE (-1) #define URP_LIGHT_COOKIE_FORMAT_RGB (0) #define URP_LIGHT_COOKIE_FORMAT_ALPHA (1) #define URP_LIGHT_COOKIE_FORMAT_RED (2)