Skip to content

Commit 1f86495

Browse files
elliomanEvergreen
authored andcommitted
[2023.3][URP] Fixing an issue with incorrect matrix data when Scene and Game windows are open. (UUM-63267)
Fixes UUM-63267. In non-RenderGraph, the main and additional light shadow passes didn't have the correct data in the WorldToCamera matrix. This is due to it being set after they've been run. This PR fixes that.
1 parent a8c1898 commit 1f86495

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

Packages/com.unity.render-pipelines.universal/Runtime/Passes/AdditionalLightsShadowCasterPass.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,11 @@ void RenderAdditionalShadowmapAtlas(RasterCommandBuffer cmd, ref PassData data,
711711

712712
using (new ProfilingScope(cmd, ProfilingSampler.Get(URPProfileId.AdditionalLightsShadow)))
713713
{
714+
// For non-RG, need set the worldToCamera Matrix as that is not set for passes executed before normal rendering,
715+
// otherwise shadows will behave incorrectly when Scene and Game windows are open at the same time (UUM-63267).
716+
if (!useRenderGraph)
717+
ShadowUtils.SetWorldToCameraMatrix(cmd, data.viewMatrix);
718+
714719
bool anyShadowSliceRenderer = false;
715720
int shadowSlicesCount = m_ShadowSliceToAdditionalLightIndex.Count;
716721
if (shadowSlicesCount > 0)
@@ -826,6 +831,7 @@ private class PassData
826831
{
827832
internal UniversalLightData lightData;
828833
internal UniversalShadowData shadowData;
834+
internal Matrix4x4 viewMatrix;
829835
internal bool stripShadowsOffVariants;
830836

831837
internal AdditionalLightsShadowCasterPass pass;
@@ -846,6 +852,7 @@ private void InitPassData(ref PassData passData, UniversalCameraData cameraData,
846852

847853
passData.lightData = lightData;
848854
passData.shadowData = shadowData;
855+
passData.viewMatrix = cameraData.GetViewMatrix();
849856
passData.stripShadowsOffVariants = cameraData.renderer.stripShadowsOffVariants;
850857

851858
passData.emptyShadowmap = m_CreateEmptyShadowmap;

Packages/com.unity.render-pipelines.universal/Runtime/Passes/MainLightShadowCasterPass.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,13 @@ void RenderMainLightCascadeShadowmap(RasterCommandBuffer cmd, ref PassData data,
268268

269269
using (new ProfilingScope(cmd, ProfilingSampler.Get(URPProfileId.MainLightShadow)))
270270
{
271-
// Need to start by setting the Camera position as that is not set for passes executed before normal rendering
272-
cmd.SetGlobalVector(ShaderPropertyId.worldSpaceCameraPos, data.cameraData.worldSpaceCameraPos);
271+
// Need to start by setting the Camera position and worldToCamera Matrix as that is not set for passes executed before normal rendering
272+
ShadowUtils.SetCameraPosition(cmd, data.cameraData.worldSpaceCameraPos);
273+
274+
// For non-RG, need set the worldToCamera Matrix as that is not set for passes executed before normal rendering,
275+
// otherwise shadows will behave incorrectly when Scene and Game windows are open at the same time (UUM-63267).
276+
if (!isRenderGraph)
277+
ShadowUtils.SetWorldToCameraMatrix(cmd, data.cameraData.GetViewMatrix());
273278

274279
for (int cascadeIndex = 0; cascadeIndex < m_ShadowCasterCascadesCount; ++cascadeIndex)
275280
{

Packages/com.unity.render-pipelines.universal/Runtime/ShadowUtils.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -489,10 +489,6 @@ public static void SetupShadowCasterConstantBuffer(CommandBuffer cmd, ref Visibl
489489
SetupShadowCasterConstantBuffer(CommandBufferHelpers.GetRasterCommandBuffer(cmd), ref shadowLight, shadowBias);
490490
}
491491

492-
private static int _ShadowBias = Shader.PropertyToID("_ShadowBias");
493-
private static int _LightDirection = Shader.PropertyToID("_LightDirection");
494-
private static int _LightPosition = Shader.PropertyToID("_LightPosition");
495-
496492
internal static void SetupShadowCasterConstantBuffer(RasterCommandBuffer cmd, ref VisibleLight shadowLight, Vector4 shadowBias)
497493
{
498494
SetShadowBias(cmd, shadowBias);
@@ -508,17 +504,31 @@ internal static void SetupShadowCasterConstantBuffer(RasterCommandBuffer cmd, re
508504

509505
internal static void SetShadowBias(RasterCommandBuffer cmd, Vector4 shadowBias)
510506
{
511-
cmd.SetGlobalVector(_ShadowBias, shadowBias);
507+
cmd.SetGlobalVector(ShaderPropertyId.shadowBias, shadowBias);
512508
}
513509

514510
internal static void SetLightDirection(RasterCommandBuffer cmd, Vector3 lightDirection)
515511
{
516-
cmd.SetGlobalVector(_LightDirection, new Vector4(lightDirection.x, lightDirection.y, lightDirection.z, 0.0f));
512+
cmd.SetGlobalVector(ShaderPropertyId.lightDirection, new Vector4(lightDirection.x, lightDirection.y, lightDirection.z, 0.0f));
517513
}
518514

519515
internal static void SetLightPosition(RasterCommandBuffer cmd, Vector3 lightPosition)
520516
{
521-
cmd.SetGlobalVector(_LightPosition, new Vector4(lightPosition.x, lightPosition.y, lightPosition.z, 1.0f));
517+
cmd.SetGlobalVector(ShaderPropertyId.lightPosition, new Vector4(lightPosition.x, lightPosition.y, lightPosition.z, 1.0f));
518+
}
519+
520+
internal static void SetCameraPosition(RasterCommandBuffer cmd, Vector3 worldSpaceCameraPos)
521+
{
522+
cmd.SetGlobalVector(ShaderPropertyId.worldSpaceCameraPos, worldSpaceCameraPos);
523+
}
524+
525+
internal static void SetWorldToCameraMatrix(RasterCommandBuffer cmd, Matrix4x4 viewMatrix)
526+
{
527+
// There's an inconsistency in handedness between unity_matrixV and unity_WorldToCamera
528+
// Unity changes the handedness of unity_WorldToCamera (see Camera::CalculateMatrixShaderProps)
529+
// we will also change it here to avoid breaking existing shaders. (case 1257518)
530+
Matrix4x4 worldToCameraMatrix = Matrix4x4.Scale(new Vector3(1.0f, 1.0f, -1.0f)) * viewMatrix;
531+
cmd.SetGlobalMatrix(ShaderPropertyId.worldToCameraMatrix, worldToCameraMatrix);
522532
}
523533

524534
private static RenderTextureDescriptor GetTemporaryShadowTextureDescriptor(int width, int height, int bits)

Packages/com.unity.render-pipelines.universal/Runtime/UniversalRenderPipelineCore.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,10 @@ internal static class ShaderPropertyId
849849
public static readonly int worldToCameraMatrix = Shader.PropertyToID("unity_WorldToCamera");
850850
public static readonly int cameraToWorldMatrix = Shader.PropertyToID("unity_CameraToWorld");
851851

852+
public static readonly int shadowBias = Shader.PropertyToID("_ShadowBias");
853+
public static readonly int lightDirection = Shader.PropertyToID("_LightDirection");
854+
public static readonly int lightPosition = Shader.PropertyToID("_LightPosition");
855+
852856
public static readonly int cameraWorldClipPlanes = Shader.PropertyToID("unity_CameraWorldClipPlanes");
853857

854858
public static readonly int billboardNormal = Shader.PropertyToID("unity_BillboardNormal");

0 commit comments

Comments
 (0)