From fd5eb748e256bfb77d673b7985d26e0d0cbc0249 Mon Sep 17 00:00:00 2001 From: DeMuenu <96650288+DeMuenu@users.noreply.github.com> Date: Tue, 30 Sep 2025 12:59:56 +0200 Subject: [PATCH] Add support for multiple shadowcaster planes Extended ShadowcasterUpdater and shaders to handle multiple shadowcaster planes by introducing indexed properties and logic for two shadowcaster sets. Updated property names and shader logic to support per-index shadowcaster textures, colors, and brightness, enabling more flexible shadow casting in scenes. --- Scripts/ShadowcasterUpdater.cs | 25 +++++++++++++++++++++++-- Shader/BlendinShader.shader | 32 +++++++++++++++++++++++++------- Shader/Includes/Variables.hlsl | 8 -------- Shader/LitParticles.shader | 34 ++++++++++++++++++++++++++-------- 4 files changed, 74 insertions(+), 25 deletions(-) diff --git a/Scripts/ShadowcasterUpdater.cs b/Scripts/ShadowcasterUpdater.cs index a1fee8a..f688a92 100644 --- a/Scripts/ShadowcasterUpdater.cs +++ b/Scripts/ShadowcasterUpdater.cs @@ -9,15 +9,36 @@ public class ShadowcasterUpdater : UdonSharpBehaviour { public Renderer[] rendererTargets; + public Texture2D ShadowcasterTexture; + public Color OutsideColor = Color.white; + public Color TextureColor = Color.white; + public float MinBrightness = 0.0f; + + public int shadowcasterIndex = 1; + public string propertyName = "_Udon_WorldToLocal"; private MaterialPropertyBlock _mpb; + void Start() { _mpb = new MaterialPropertyBlock(); + ApplyTextureData(); } - + public void ApplyTextureData() + { + foreach (Renderer mat in rendererTargets) + { + if (mat == null) continue; + mat.GetPropertyBlock(_mpb); + _mpb.SetTexture("_Udon_ShadowcasterTex" + "_" + (string)shadowcasterIndex, ShadowcasterTexture); + _mpb.SetColor("_Udon_shadowCasterColor" + "_" + (string)shadowcasterIndex, TextureColor); + _mpb.SetColor("_Udon_OutSideColor" + "_" + (string)shadowcasterIndex, OutsideColor); + _mpb.SetFloat("_Udon_MinBrightnessShadow" + "_" + (string)shadowcasterIndex, MinBrightness); + mat.SetPropertyBlock(_mpb); + } + } void LateUpdate() { var w2l = transform.worldToLocalMatrix; @@ -27,7 +48,7 @@ public class ShadowcasterUpdater : UdonSharpBehaviour { if (mat == null) continue; mat.GetPropertyBlock(_mpb); - _mpb.SetMatrix(propertyName, w2l); + _mpb.SetMatrix(propertyName + "_" + (string)shadowcasterIndex, w2l); mat.SetPropertyBlock(_mpb); } diff --git a/Shader/BlendinShader.shader b/Shader/BlendinShader.shader index ec85d5e..89d642e 100644 --- a/Shader/BlendinShader.shader +++ b/Shader/BlendinShader.shader @@ -92,6 +92,17 @@ Shader "DeMuenu/World/Hoppou/RevealStandart" MoonlightGlobalVariables + float4x4 _Udon_WorldToLocal_1; + sampler2D _Udon_shadowCasterTex_1; + float4 _Udon_shadowCasterColor_1; + float4 _Udon_OutSideColor_1; + float _Udon_MinBrightnessShadow_1; + + float4x4 _Udon_WorldToLocal_2; + sampler2D _Udon_shadowCasterTex_2; + float4 _Udon_shadowCasterColor_2; + float4 _Udon_OutSideColor_2; + float _Udon_MinBrightnessShadow_2; v2f vert (appdata v) { @@ -148,15 +159,22 @@ Shader "DeMuenu/World/Hoppou/RevealStandart" LightTypeCalculations(_Udon_LightColors, LightCounter, i, NdotL, dIntensity, _Udon_LightPositions[LightCounter].a, _Udon_LightPositions[LightCounter].xyz); - float4 ShadowCasterMult = float4(1,1,1,1); - if (_Udon_ShadowMapIndex[LightCounter] > 0.5) { - ShadowCasterMult = SampleShadowcasterPlane(_Udon_WorldToLocal, _shadowCasterTex, _Udon_LightPositions[LightCounter].xyz, i.worldPos, _OutSideColor); - ShadowCasterMult *= _shadowCasterColor; - ShadowCasterMult = float4(ShadowCasterMult.rgb * (1-ShadowCasterMult.a), 1); + float4 ShadowCasterMult_1 = float4(1,1,1,1); + float4 ShadowCasterMult_2 = float4(1,1,1,1); + if (((_Udon_ShadowMapIndex[LightCounter] > 0.5) && (_Udon_ShadowMapIndex[LightCounter] < 1.5)) || (_Udon_ShadowMapIndex[LightCounter] > 2.5)) { + ShadowCasterMult_1 = SampleShadowcasterPlane(_Udon_WorldToLocal_1, _Udon_shadowCasterTex_1, _Udon_LightPositions[LightCounter].xyz, i.worldPos, _Udon_OutSideColor_1); + ShadowCasterMult_1 *= _Udon_shadowCasterColor_1; + ShadowCasterMult_1 = float4(ShadowCasterMult_1.rgb * (1-ShadowCasterMult_1.a), 1); + ShadowCasterMult_1 = max(ShadowCasterMult_1, _Udon_MinBrightnessShadow_1) + } + if (_Udon_ShadowMapIndex[LightCounter] > 1.5) { + ShadowCasterMult_2 = SampleShadowcasterPlane(_Udon_WorldToLocal_2, _Udon_shadowCasterTex_2, _Udon_LightPositions[LightCounter].xyz, i.worldPos, _Udon_OutSideColor_2); + ShadowCasterMult_2 *= _Udon_shadowCasterColor_2; + ShadowCasterMult_2 = float4(ShadowCasterMult_2.rgb * (1-ShadowCasterMult_2.a), 1); + ShadowCasterMult_2 = max(ShadowCasterMult_2, _Udon_MinBrightnessShadow_2) } - - dmax = dmax + contrib * float4(LightColor, 1) * NdotL * max(ShadowCasterMult, _MinBrightnessShadow); + dmax = dmax + contrib * float4(LightColor, 1) * ShadowCasterMult_1 * ShadowCasterMult_2; } diff --git a/Shader/Includes/Variables.hlsl b/Shader/Includes/Variables.hlsl index bb89d05..95e74a8 100644 --- a/Shader/Includes/Variables.hlsl +++ b/Shader/Includes/Variables.hlsl @@ -11,12 +11,4 @@ float _Udon_ShadowMapIndex[MAX_LIGHTS];\ float _Udon_PlayerCount; /* set via SetFloat */ \ - - float4x4 _Udon_WorldToLocal; \ - sampler2D _shadowCasterTex; \ - float4 _shadowCasterColor; \ - float4 _OutSideColor; \ - float _MinBrightnessShadow; \ - - #endif \ No newline at end of file diff --git a/Shader/LitParticles.shader b/Shader/LitParticles.shader index ba6d357..f4ea031 100644 --- a/Shader/LitParticles.shader +++ b/Shader/LitParticles.shader @@ -85,6 +85,17 @@ Shader "DeMuenu/World/Hoppou/Particles/LitParticles" MoonlightGlobalVariables + float4x4 _Udon_WorldToLocal_1; + sampler2D _Udon_shadowCasterTex_1; + float4 _Udon_shadowCasterColor_1; + float4 _Udon_OutSideColor_1; + float _Udon_MinBrightnessShadow_1; + + float4x4 _Udon_WorldToLocal_2; + sampler2D _Udon_shadowCasterTex_2; + float4 _Udon_shadowCasterColor_2; + float4 _Udon_OutSideColor_2; + float _Udon_MinBrightnessShadow_2; v2f vert (appdata v) { @@ -121,18 +132,25 @@ Shader "DeMuenu/World/Hoppou/Particles/LitParticles" InLoopSetup(_Udon_LightPositions, LightCounter, count, i); //defines distanceFromLight, contrib - Lambert(_Udon_LightPositions[LightCounter].xyz ,i, N); //defines NdotL - LightTypeCalculations(_Udon_LightColors, LightCounter, i, NdotL, dIntensity, _Udon_LightPositions[LightCounter].a, _Udon_LightPositions[LightCounter].xyz); + LightTypeCalculations(_Udon_LightColors, LightCounter, i, 1, dIntensity, _Udon_LightPositions[LightCounter].a, _Udon_LightPositions[LightCounter].xyz); - float4 ShadowCasterMult = float4(1,1,1,1); - if (_Udon_ShadowMapIndex[LightCounter] > 0.5) { - ShadowCasterMult = SampleShadowcasterPlane(_Udon_WorldToLocal, _shadowCasterTex, _Udon_LightPositions[LightCounter].xyz, i.worldPos, _OutSideColor); - ShadowCasterMult *= _shadowCasterColor; - ShadowCasterMult = float4(ShadowCasterMult.rgb * (1-ShadowCasterMult.a), 1); + float4 ShadowCasterMult_1 = float4(1,1,1,1); + float4 ShadowCasterMult_2 = float4(1,1,1,1); + if (((_Udon_ShadowMapIndex[LightCounter] > 0.5) && (_Udon_ShadowMapIndex[LightCounter] < 1.5)) || (_Udon_ShadowMapIndex[LightCounter] > 2.5)) { + ShadowCasterMult_1 = SampleShadowcasterPlane(_Udon_WorldToLocal_1, _Udon_shadowCasterTex_1, _Udon_LightPositions[LightCounter].xyz, i.worldPos, _Udon_OutSideColor_1); + ShadowCasterMult_1 *= _Udon_shadowCasterColor_1; + ShadowCasterMult_1 = float4(ShadowCasterMult_1.rgb * (1-ShadowCasterMult_1.a), 1); + ShadowCasterMult_1 = max(ShadowCasterMult_1, _Udon_MinBrightnessShadow_1) + } + if (_Udon_ShadowMapIndex[LightCounter] > 1.5) { + ShadowCasterMult_2 = SampleShadowcasterPlane(_Udon_WorldToLocal_2, _Udon_shadowCasterTex_2, _Udon_LightPositions[LightCounter].xyz, i.worldPos, _Udon_OutSideColor_2); + ShadowCasterMult_2 *= _Udon_shadowCasterColor_2; + ShadowCasterMult_2 = float4(ShadowCasterMult_2.rgb * (1-ShadowCasterMult_2.a), 1); + ShadowCasterMult_2 = max(ShadowCasterMult_2, _Udon_MinBrightnessShadow_2) } - dmax = dmax + contrib * float4(LightColor, 1) * 1 * max(ShadowCasterMult, _MinBrightnessShadow); + dmax = dmax + contrib * float4(LightColor, 1) * ShadowCasterMult1 * ShadowCasterMult_2; }