Refactor shadowcaster to use explicit plane basis

Replaces the use of world-to-local matrices for shadowcaster planes with explicit plane origin, basis, and normal vectors in both C# and shader code. Updates property setting in ShadowcasterUpdater and its editor preview, and rewrites the shadow sampling function in the shader include to use the new basis. Adjusts all affected shaders to use the new properties and sampling function for improved clarity and flexibility.
This commit is contained in:
DeMuenu
2025-10-01 13:26:33 +02:00
parent 841d99ad0e
commit 0e633983cf
5 changed files with 225 additions and 191 deletions

View File

@@ -16,7 +16,6 @@ public class ShadowcasterUpdater : UdonSharpBehaviour
public int shadowcasterIndex = 1;
public string propertyName = "_Udon_WorldToLocal";
private MaterialPropertyBlock _mpb;
@@ -32,23 +31,44 @@ public class ShadowcasterUpdater : UdonSharpBehaviour
{
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);
_mpb.SetTexture("_Udon_shadowCasterTex" + "_" + shadowcasterIndex.ToString(), ShadowcasterTexture);
_mpb.SetColor("_Udon_shadowCasterColor" + "_" + shadowcasterIndex.ToString(), TextureColor);
_mpb.SetColor("_Udon_OutSideColor" + "_" + shadowcasterIndex.ToString(), OutsideColor);
_mpb.SetFloat("_Udon_MinBrightnessShadow" + "_" + shadowcasterIndex.ToString(), MinBrightness);
mat.SetPropertyBlock(_mpb);
}
}
void LateUpdate()
{
var w2l = transform.worldToLocalMatrix;
foreach (Renderer mat in rendererTargets)
{
if (mat == null) continue;
mat.GetPropertyBlock(_mpb);
_mpb.SetMatrix(propertyName + "_" + (string)shadowcasterIndex, w2l);
float quadHalfWidth = 0.5f;
float quadHalfHeight = 0.5f;
// World-space basis directions from transform
Vector3 Udir = transform.rotation * Vector3.right; // plane local +X
Vector3 Vdir = transform.rotation * Vector3.up; // plane local +Y
// World-space half extents after non-uniform scaling
float halfW = quadHalfWidth * transform.lossyScale.x;
float halfH = quadHalfHeight * transform.lossyScale.y;
// Reciprocal axes so dot(r, Uinv/Vinv) -> [-0.5, 0.5]
Vector3 Uinv = Udir / (2.0f * Mathf.Max(halfW, 1e-6f));
Vector3 Vinv = Vdir / (2.0f * Mathf.Max(halfH, 1e-6f));
// Unit normal
Vector3 N = Vector3.Normalize(Vector3.Cross(Udir, Vdir));
_mpb.SetVector("_Udon_Plane_Origin_" + shadowcasterIndex.ToString(), new Vector4(transform.position.x, transform.position.y, transform.position.z, 0));
_mpb.SetVector("_Udon_Plane_Uinv_" + shadowcasterIndex.ToString(), new Vector4(Uinv.x, Uinv.y, Uinv.z, 0));
_mpb.SetVector("_Udon_Plane_Vinv_" + shadowcasterIndex.ToString(), new Vector4(Vinv.x, Vinv.y, Vinv.z, 0));
_mpb.SetVector("_Udon_Plane_Normal_" + shadowcasterIndex.ToString(), new Vector4(N.x, N.y, N.z, 0));
mat.SetPropertyBlock(_mpb);
}