diff options
Diffstat (limited to 'lighting.cginc')
| -rw-r--r-- | lighting.cginc | 51 |
1 files changed, 18 insertions, 33 deletions
diff --git a/lighting.cginc b/lighting.cginc index 48d95a1..6668459 100644 --- a/lighting.cginc +++ b/lighting.cginc @@ -114,50 +114,35 @@ 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; + // L0 band + float3 L0 = float3(unity_SHAr.w, unity_SHAg.w, unity_SHAb.w); + + // L1 band + float3 L1 = float3( + dot(unity_SHAr.xyz, n.xyz), + dot(unity_SHAg.xyz, n.xyz), + dot(unity_SHAb.xyz, n.xyz) + ); + + // L2 band + float4 v = float4(n.x * n.y, n.y * n.z, n.z * n.z, n.x * 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); + float3(dot(unity_SHBr.xyzw, v), dot(unity_SHBg.xyzw, v), dot(unity_SHBb.xyzw, v)) + + unity_SHC.xyz * (n.x * n.x - n.y * n.y); // TODO expose this as a parameter float wrap_term = 0.0f; + // Original coefficients: 1, 2/3, 1/4. // Wrapped coefficients: 1, (2-w)/3, ((1-w)^2)/4. - - // Setting w=0, the l1 band is: - // (2-w)/3 = 2/3 - // 2-w = 2 - // 1-w/2 = 1 float l1_wrap = 1.0f - wrap_term * 0.75f; L1 *= l1_wrap; - // The l2 band is: - // ((1-w)^2)/4 = 1/4 - // (1-w)^2 = 1 - float l2_wrap = (1.0f-wrap_term); - l2_wrap *= l2_wrap; + float l2_wrap_base = 1.0f - wrap_term; + float l2_wrap = l2_wrap_base * l2_wrap_base; L2 *= l2_wrap; - light.L00 = L00; + light.L00 = L0; light.L01r = unity_SHAr.xyz * l1_wrap; light.L01g = unity_SHAg.xyz * l1_wrap; light.L01b = unity_SHAb.xyz * l1_wrap; |
