Skip to content

Commit

Permalink
[Port] [2023.2] Added custom light data shadow and light layer sync w…
Browse files Browse the repository at this point in the history
…ith the main light.

Fixes - Setting shadow rendering layer is not changing shadow when using “shadowRenderingLayers” in the script
https://jira.unity3d.com/browse/UUM-28061

Added logic similar to UI. Where we replace base light component renderingLayerMask with either shadow or light layers. Which later used for shadow drawing.
  • Loading branch information
svc-reach-platform-support authored and Evergreen committed Apr 18, 2024
1 parent a297a97 commit a0dbcb4
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,25 @@ public bool usePipelineSettings
/// </summary>
public static readonly int AdditionalLightsShadowDefaultCustomResolution = 128;

[NonSerialized] private Light m_Light;

/// <summary>
/// Returns the cached light component associated with the game object that owns this light data.
/// </summary>
#if UNITY_EDITOR
internal new Light light
#else
internal Light light
#endif
{
get
{
if (!m_Light)
TryGetComponent(out m_Light);
return m_Light;
}
}

/// <summary>
/// The minimum shadow resolution for additional lights.
/// </summary>
Expand Down Expand Up @@ -146,8 +165,18 @@ public LightLayerEnum lightLayerMask
/// </summary>
public uint renderingLayers
{
get { return m_RenderingLayers; }
set { m_RenderingLayers = value; }
get
{
return m_RenderingLayers;
}
set
{
if (m_RenderingLayers != value)
{
m_RenderingLayers = value;
SyncLightAndShadowLayers();
}
}
}

[SerializeField] bool m_CustomShadowLayers = false;
Expand All @@ -158,8 +187,18 @@ public uint renderingLayers
/// </summary>
public bool customShadowLayers
{
get { return m_CustomShadowLayers; }
set { m_CustomShadowLayers = value; }
get
{
return m_CustomShadowLayers;
}
set
{
if (m_CustomShadowLayers != value)
{
m_CustomShadowLayers = value;
SyncLightAndShadowLayers();
}
}
}

// The layer(s) used for shadow casting.
Expand All @@ -181,8 +220,18 @@ public LightLayerEnum shadowLayerMask
/// </summary>
public uint shadowRenderingLayers
{
get { return m_ShadowRenderingLayers; }
set { m_ShadowRenderingLayers = value; }
get
{
return m_ShadowRenderingLayers;
}
set
{
if (value != m_ShadowRenderingLayers)
{
m_ShadowRenderingLayers = value;
SyncLightAndShadowLayers();
}
}
}

/// <summary>
Expand Down Expand Up @@ -242,5 +291,11 @@ public void OnAfterDeserialize()
m_Version = 3;
}
}

private void SyncLightAndShadowLayers()
{
if (light)
light.renderingLayerMask = m_CustomShadowLayers ? (int)m_ShadowRenderingLayers : (int)m_RenderingLayers;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using NUnit.Framework;

namespace UnityEngine.Rendering.Universal.Tests
{
[TestFixture]
class LightTests
{
[Test]
public void TestMainLightRenderingLayerMaskSyncWithUniversalLightAndShadowLayers()
{
var lightObject = new GameObject("Light");
var light = lightObject.AddComponent<Light>();
var lightData = light.GetUniversalAdditionalLightData();

lightData.renderingLayers = (1 << 1);
lightData.shadowRenderingLayers = (1 << 2);

lightData.customShadowLayers = false;
Assert.AreEqual(light.renderingLayerMask, lightData.renderingLayers);

lightData.customShadowLayers = true;
Assert.AreEqual(light.renderingLayerMask, lightData.shadowRenderingLayers);

lightData.customShadowLayers = false;
lightData.renderingLayers = (1 << 3);
lightData.shadowRenderingLayers = (1 << 4);
Assert.AreEqual(light.renderingLayerMask, lightData.renderingLayers);

lightData.customShadowLayers = true;
lightData.renderingLayers = (1 << 5);
lightData.shadowRenderingLayers = (1 << 6);
Assert.AreEqual(light.renderingLayerMask, lightData.shadowRenderingLayers);

Object.DestroyImmediate(lightObject);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a0dbcb4

Please sign in to comment.