Skip to content
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
94e008a
In progress shadow refactor
unity-cchu Jan 16, 2021
553f400
Fixed code to support single channel shadows
unity-cchu Jan 19, 2021
9f87928
Fixed shadows on the alpha channel
unity-cchu Jan 19, 2021
81e6084
Remaining issue with single color channel, and removed added vertex
unity-cchu Jan 19, 2021
36459ce
Initial Checkin
unity-cchu Jan 20, 2021
91f7f80
Fixed a number of issues with shadow rendering
unity-cchu Jan 22, 2021
b439562
Added versioning code for ShadowCaster2D
unity-cchu Jan 22, 2021
f19d02f
in progress work..
unity-cchu Jan 26, 2021
a0e1aff
Merge branch 'master' into 2d/shadow-refactor-jan
unity-cchu Mar 7, 2021
81a6e83
Revert "Added versioning code for ShadowCaster2D"
unity-cchu Mar 7, 2021
9341272
Revert "Revert "Added versioning code for ShadowCaster2D""
unity-cchu Mar 7, 2021
e2b0d06
Fixed orthographic shadow projection for arbitrary rotation
unity-cchu Mar 7, 2021
9429cf4
Fixed perspective issue for shadow
unity-cchu Mar 7, 2021
e391623
Fixed another perspective issue with shadows
unity-cchu Mar 7, 2021
9590864
Updated a comment
unity-cchu Mar 7, 2021
d76f961
Fixed a few more issues. Seems to be working pretty well again
unity-cchu Mar 7, 2021
3738fc2
Fixed some issues with culling
unity-cchu Mar 10, 2021
a403ab6
Removed debug pragma
unity-cchu Mar 10, 2021
00aec05
Can now use channels when prerendering shadow casting lights
unity-cchu Mar 10, 2021
4528b71
Fixed problem with not creating shadow texture
unity-cchu Mar 11, 2021
86af676
Fixed versioning
unity-cchu Mar 11, 2021
93099e4
Fixed formatting
unity-cchu Mar 11, 2021
52e58f0
Merge branch 'master' into 2d/shadow-refactor-jan
unity-cchu Mar 11, 2021
dde29cf
Added profiling sampling per channel to Shadow Rendering
unity-cchu Mar 12, 2021
42d565e
Fixed a bug in ShadowProjectVertex.hlsl
unity-cchu Mar 12, 2021
77af365
In progress Shadow Prerendering Test.
unity-cchu Mar 14, 2021
7482e8c
Update prererendering test
unity-cchu Mar 15, 2021
a3477b1
Merge branch 'master' into 2d/shadow-refactor-jan
unity-cchu Apr 21, 2021
74e5e31
Merge branch 'master' into 2d/shadow-refactor-jan
unity-cchu Apr 21, 2021
4d0710a
Compilation Fix
unity-cchu Apr 22, 2021
8313550
Reverted back to the old shadow casting mesh
unity-cchu Apr 22, 2021
88d1997
Fixed up bounding sphere
unity-cchu Apr 22, 2021
9eb22e8
Fixes to creation of shadow materials
unity-cchu Apr 22, 2021
8077c81
Fixed small math issue
unity-cchu Apr 22, 2021
66edeb0
Merge branch 'master' into 2d/shadow-refactor-jan
unity-cchu Apr 22, 2021
57def38
Fixed formatting, Added changelog
unity-cchu Apr 23, 2021
b153ff2
Merge branch 'master' into 2d/shadow-refactor-jan
unity-cchu Apr 24, 2021
553eacb
Updated shadow prerendering test
unity-cchu Apr 24, 2021
26dd035
Merge branch 'master' into 2d/shadow-refactor-jan
unity-cchu Apr 26, 2021
f7d2d91
Merge branch 'master' into 2d/shadow-refactor-jan
unity-cchu Apr 26, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,11 @@ public static void DrawPointLight(CommandBuffer cmd, Light2D light, Mesh lightMe

private static void RenderLightSet(IRenderPass2D pass, RenderingData renderingData, int blendStyleIndex, CommandBuffer cmd, int layerToRender, RenderTargetIdentifier renderTexture, List<Light2D> lights)
{
var maxShadowTextureCount = ShadowRendering.maxTextureCount;
var maxShadowLightCount = ShadowRendering.maxTextureCount * 4;
var requiresRTInit = true;

// This case should never happen, but if it does it may cause an infinite loop later.
if (maxShadowTextureCount < 1)
if (maxShadowLightCount < 1)
{
Debug.LogError("maxShadowTextureCount cannot be less than 1");
return;
Expand All @@ -193,12 +193,11 @@ private static void RenderLightSet(IRenderPass2D pass, RenderingData renderingDa

// Add lights to our batch until the number of shadow textures reach the maxShadowTextureCount
var shadowLightCount = 0;
while (batchedLights < remainingLights && shadowLightCount < maxShadowTextureCount)
while (batchedLights < remainingLights && shadowLightCount < maxShadowLightCount)
{
var light = lights[lightIndex + batchedLights];
if (light.shadowsEnabled && light.shadowIntensity > 0)
{
ShadowRendering.CreateShadowRenderTexture(pass, renderingData, cmd, shadowLightCount);
ShadowRendering.PrerenderShadows(pass, renderingData, cmd, layerToRender, light, shadowLightCount, light.shadowIntensity);
shadowLightCount++;
}
Expand Down Expand Up @@ -269,11 +268,11 @@ private static void RenderLightSet(IRenderPass2D pass, RenderingData renderingDa

public static void RenderLightVolumes(this IRenderPass2D pass, RenderingData renderingData, CommandBuffer cmd, int layerToRender, int endLayerValue, RenderTargetIdentifier renderTexture, RenderTargetIdentifier depthTexture, List<Light2D> lights)
{
var maxShadowTextureCount = ShadowRendering.maxTextureCount;
var maxShadowLightCount = ShadowRendering.maxTextureCount * 4; // Now encodes shadows into RGBA as well as seperate textures
var requiresRTInit = true;

// This case should never happen, but if it does it may cause an infinite loop later.
if (maxShadowTextureCount < 1)
if (maxShadowLightCount < 1)
{
Debug.LogError("maxShadowTextureCount cannot be less than 1");
return;
Expand All @@ -288,12 +287,11 @@ public static void RenderLightVolumes(this IRenderPass2D pass, RenderingData ren

// Add lights to our batch until the number of shadow textures reach the maxShadowTextureCount
var shadowLightCount = 0;
while (batchedLights < remainingLights && shadowLightCount < maxShadowTextureCount)
while (batchedLights < remainingLights && shadowLightCount < maxShadowLightCount)
{
var light = lights[lightIndex + batchedLights];
if (light.volumetricShadowsEnabled && light.shadowVolumeIntensity > 0)
{
ShadowRendering.CreateShadowRenderTexture(pass, renderingData, cmd, shadowLightCount);
ShadowRendering.PrerenderShadows(pass, renderingData, cmd, layerToRender, light, shadowLightCount, light.shadowVolumeIntensity);
shadowLightCount++;
}
Expand Down Expand Up @@ -325,7 +323,7 @@ public static void RenderLightVolumes(this IRenderPass2D pass, RenderingData ren
var lightVolumeMaterial = pass.rendererData.GetLightMaterial(light, true);
var lightMesh = light.lightMesh;

// Set the shadow texture to read from
// Set the shadow texture to read from.
if (light.volumetricShadowsEnabled && light.shadowVolumeIntensity > 0)
ShadowRendering.SetGlobalShadowTexture(cmd, light, shadowLightCount++);
else
Expand Down
28 changes: 14 additions & 14 deletions com.unity.render-pipelines.universal/Runtime/2D/Renderer2DData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,14 @@ public enum Renderer2DDefaultMaterialType
[SerializeField, Reload("Shaders/Utils/Sampling.shader")]
Shader m_SamplingShader = null;

[SerializeField, Reload("Shaders/2D/ShadowGroup2D.shader")]
Shader m_ShadowGroupShader = null;
[SerializeField, Reload("Shaders/2D/Shadow2D-Projected.shader")]
Shader m_ProjectedShadowShader = null;

[SerializeField, Reload("Shaders/2D/Shadow2DRemoveSelf.shader")]
Shader m_RemoveSelfShadowShader = null;
[SerializeField, Reload("Shaders/2D/Shadow2D-Shadow-Sprite.shader")]
Shader m_SpriteShadowShader = null;

[SerializeField, Reload("Shaders/2D/Shadow2D-Unshadow-Sprite.shader")]
Shader m_SpriteUnshadowShader = null;

[SerializeField, Reload("Shaders/Utils/FallbackError.shader")]
Shader m_FallbackErrorShader;
Expand All @@ -102,9 +105,10 @@ public enum Renderer2DDefaultMaterialType
internal Shader pointLightVolumeShader => m_PointLightVolumeShader;
internal Shader blitShader => m_BlitShader;
internal Shader samplingShader => m_SamplingShader;
internal Shader shadowGroupShader => m_ShadowGroupShader;
internal Shader removeSelfShadowShader => m_RemoveSelfShadowShader;
internal PostProcessData postProcessData { get => m_PostProcessData; set { m_PostProcessData = value; } }
internal Shader spriteShadowShader => m_SpriteShadowShader;
internal Shader spriteUnshadowShader => m_SpriteUnshadowShader;
internal Shader projectedShadowShader => m_ProjectedShadowShader;
internal TransparencySortMode transparencySortMode => m_TransparencySortMode;
internal Vector3 transparencySortAxis => m_TransparencySortAxis;
internal uint lightRenderTextureMemoryBudget => m_MaxLightRenderTextureCount;
Expand Down Expand Up @@ -135,18 +139,14 @@ protected override void OnEnable()

normalsRenderTarget.Init("_NormalMap");
shadowsRenderTarget.Init("_ShadowTex");

const int totalMaterials = 256;
if (shadowMaterials == null || shadowMaterials.Length == 0)
shadowMaterials = new Material[totalMaterials];
if (removeSelfShadowMaterials == null || removeSelfShadowMaterials.Length == 0)
removeSelfShadowMaterials = new Material[totalMaterials];
}

// transient data
internal Dictionary<uint, Material> lightMaterials { get; } = new Dictionary<uint, Material>();
internal Material[] shadowMaterials { get; private set; }
internal Material[] removeSelfShadowMaterials { get; private set; }
internal Material spriteSelfShadowMaterial { get; set; }
internal Material spriteUnshadowMaterial { get; set; }
internal Material projectedShadowMaterial { get; set; }
internal Material stencilOnlyShadowMaterial { get; set; }

internal bool isNormalsRenderTargetValid { get; set; }
internal float normalsRenderTargetScale { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,16 @@ namespace UnityEngine.Experimental.Rendering.Universal
[ExecuteInEditMode]
[DisallowMultipleComponent]
[AddComponentMenu("Rendering/2D/Shadow Caster 2D (Experimental)")]
public class ShadowCaster2D : ShadowCasterGroup2D
public class ShadowCaster2D : ShadowCasterGroup2D, ISerializationCallbackReceiver
{
public enum ComponentVersions
{
Version_Unserialized = 0,
Version_1 = 1
}
const ComponentVersions k_CurrentComponentVersion = ComponentVersions.Version_1;
[SerializeField] ComponentVersions m_ComponentVersion = ComponentVersions.Version_Unserialized;

[SerializeField] bool m_HasRenderer = false;
[SerializeField] bool m_UseRendererSilhouette = true;
[SerializeField] bool m_CastsShadows = true;
Expand All @@ -26,15 +34,17 @@ public class ShadowCaster2D : ShadowCasterGroup2D
internal ShadowCasterGroup2D m_ShadowCasterGroup = null;
internal ShadowCasterGroup2D m_PreviousShadowCasterGroup = null;

internal Mesh mesh => m_Mesh;
internal Vector3[] shapePath => m_ShapePath;
[SerializeField]
internal BoundingSphere m_ProjectedBoundingSphere;

public Mesh mesh => m_Mesh;
public Vector3[] shapePath => m_ShapePath;
internal int shapePathHash { get { return m_ShapePathHash; } set { m_ShapePathHash = value; } }

int m_PreviousShadowGroup = 0;
bool m_PreviousCastsShadows = true;
int m_PreviousPathHash = 0;


/// <summary>
/// If selfShadows is true, useRendererSilhoutte specifies that the renderer's sihouette should be considered part of the shadow. If selfShadows is false, useRendererSilhoutte specifies that the renderer's sihouette should be excluded from the shadow
/// </summary>
Expand Down Expand Up @@ -75,6 +85,17 @@ static int[] SetDefaultSortingLayers()
return allLayers;
}

internal bool IsLit(Light2D light)
{
Vector3 deltaPos = light.transform.position - (m_ProjectedBoundingSphere.position + transform.position);
float distanceSq = Vector3.SqrMagnitude(deltaPos);

float lightRadiusSq = light.boundingSphere.radius * light.boundingSphere.radius;
float projectedRadiusSq = m_ProjectedBoundingSphere.radius * m_ProjectedBoundingSphere.radius;

return distanceSq <= (lightRadiusSq + projectedRadiusSq);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this correct? I guess a^2 <= b^2 + c^2 is not the same as a <= b+c

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch

}

internal bool IsShadowedLayer(int layer)
{
return m_ApplyToSortingLayers != null ? Array.IndexOf(m_ApplyToSortingLayers, layer) >= 0 : false;
Expand Down Expand Up @@ -126,7 +147,7 @@ protected void OnEnable()
if (m_Mesh == null || m_InstanceId != GetInstanceID())
{
m_Mesh = new Mesh();
ShadowUtility.GenerateShadowMesh(m_Mesh, m_ShapePath);
m_ProjectedBoundingSphere = ShadowUtility.GenerateShadowMesh(m_Mesh, m_ShapePath);
m_InstanceId = GetInstanceID();
}

Expand All @@ -145,7 +166,9 @@ public void Update()

bool rebuildMesh = LightUtility.CheckForChange(m_ShapePathHash, ref m_PreviousPathHash);
if (rebuildMesh)
ShadowUtility.GenerateShadowMesh(m_Mesh, m_ShapePath);
{
m_ProjectedBoundingSphere = ShadowUtility.GenerateShadowMesh(m_Mesh, m_ShapePath);
}

m_PreviousShadowCasterGroup = m_ShadowCasterGroup;
bool addedToNewGroup = ShadowCasterGroup2DManager.AddToShadowCasterGroup(this, ref m_ShadowCasterGroup);
Expand Down Expand Up @@ -174,6 +197,23 @@ public void Update()
}
}

public void OnBeforeSerialize()
{
m_ComponentVersion = k_CurrentComponentVersion;
}

public void OnAfterDeserialize()
{
// Upgrade from no serialized version
if (m_ComponentVersion == ComponentVersions.Version_Unserialized)
{
// Regenerate the shadow mesh
m_Mesh = new Mesh();
m_ProjectedBoundingSphere = ShadowUtility.GenerateShadowMesh(m_Mesh, m_ShapePath);
m_ComponentVersion = ComponentVersions.Version_1;
}
}

#if UNITY_EDITOR
void Reset()
{
Expand Down
Loading