From 155a8ceecfe64773eb21ba55a2ac286d52589141 Mon Sep 17 00:00:00 2001 From: yum Date: Tue, 24 Feb 2026 01:37:59 -0800 Subject: Rewrite SH9 core, more concise --- lighting.cginc | 35 ++++++++++++----------------------- 1 file changed, 12 insertions(+), 23 deletions(-) (limited to 'lighting.cginc') diff --git a/lighting.cginc b/lighting.cginc index 2ae7d0d..9a66703 100755 --- a/lighting.cginc +++ b/lighting.cginc @@ -139,36 +139,25 @@ float3 yumSH9(float4 n, float3 worldPos, inout LightIndirect light) { // unity_SHB*: first four of the L2 coefficients // unity_SHC: last L2 coefficient - // Parse out coefficients into a simpler but less efficient format. - float3 L00 = float3(unity_SHAr.w, unity_SHAg.w, unity_SHAb.w); - float3 L1_1 = float3(unity_SHAr.x, unity_SHAg.x, unity_SHAb.x); - float3 L10 = float3(unity_SHAr.y, unity_SHAg.y, unity_SHAb.y); - float3 L11 = float3(unity_SHAr.z, unity_SHAg.z, unity_SHAb.z); - float3 L2_2 = float3(unity_SHBr.x, unity_SHBg.x, unity_SHBb.x); - float3 L2_1 = float3(unity_SHBr.y, unity_SHBg.y, unity_SHBb.y); - float3 L20 = float3(unity_SHBr.z, unity_SHBg.z, unity_SHBb.z); - float3 L21 = float3(unity_SHBr.w, unity_SHBg.w, unity_SHBb.w); - float3 L22 = unity_SHC; - // Equation 13 from "An Efficient Representation for Irradiance Environment - // Maps" by Ramamoorthi and Hanrahan. Note that the order of some - // coefficients is different, and normalization constants have been - // premultiplied by Unity. - float3 L0 = L00; - float3 L1 = L1_1 * n.x + L10 * n.y + L11 * n.z; - float3 L2 = - L2_2 * n.x * n.y + - L2_1 * n.y * n.z + - L20 * n.z * n.z + - L21 * n.x * n.z + - L22 * (n.x * n.x - n.y * n.y); + // Maps" by Ramamoorthi and Hanrahan. Normalization constants have been + // premultiplied by Unity into the coefficient buffers. + // + // L0+L1: dot4 per channel (n.w=1 picks up the L0 term from SHA*.w) + // L2: four quadratic terms packed into vB via swizzle multiply, plus L22 + float4 n4 = float4(n.xyz, 1.0); + float3 L0 = float3(unity_SHAr.w, unity_SHAg.w, unity_SHAb.w); + float3 L01 = float3(dot(unity_SHAr, n4), dot(unity_SHAg, n4), dot(unity_SHAb, n4)); + float4 vB = n4.xyzz * n4.yzzx; + float3 L2 = float3(dot(unity_SHBr, vB), dot(unity_SHBg, vB), dot(unity_SHBb, vB)) + + unity_SHC * (n.x * n.x - n.y * n.y); light.L00 = L0; light.L01r = unity_SHAr.xyz; light.L01g = unity_SHAg.xyz; light.L01b = unity_SHAb.xyz; - return L0 + L1 + L2; + return L01 + L2; } float4 getIndirectDiffuse(v2f i, Pbr pbr, inout LightIndirect light) { -- cgit v1.2.3