summaryrefslogtreecommitdiffstats
path: root/lighting.cginc
diff options
context:
space:
mode:
authoryum <yum.food.vr@gmail.com>2026-02-24 18:11:03 -0800
committeryum <yum.food.vr@gmail.com>2026-02-24 18:11:03 -0800
commitaacad28954ceb792ecc7ebd90338466b9f70d456 (patch)
tree8a9a7b793caa594728d322646d4f3417f9b532bb /lighting.cginc
parent142805d05c999ab1c36cdecffcc305c66dd15feb (diff)
Implement wrapped lighting (direct & IBL diffuse)
Diffstat (limited to 'lighting.cginc')
-rwxr-xr-xlighting.cginc33
1 files changed, 29 insertions, 4 deletions
diff --git a/lighting.cginc b/lighting.cginc
index 500141b..cae24d0 100755
--- a/lighting.cginc
+++ b/lighting.cginc
@@ -147,19 +147,40 @@ float3 yumSH9(float4 n, float3 worldPos, inout LightIndirect light) {
//
// 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 L1 = float3(dot(unity_SHAr, n), dot(unity_SHAg, n), dot(unity_SHAb, n));
+ float4 vB = n.xyzz * n.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);
+#if defined(_WRAPPED_LIGHTING)
+ // 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 wrap_amount = _Wrapped_Lighting_Amount;
+ float l1_wrap = 1.0f - wrap_amount * 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_amount);
+ l2_wrap *= l2_wrap;
+ L2 *= l2_wrap;
+#else
+ float l1_wrap = 1.0f;
+#endif // _WRAPPED_LIGHTING
+
light.L00 = L0;
light.L01r = unity_SHAr.xyz;
light.L01g = unity_SHAg.xyz;
light.L01b = unity_SHAb.xyz;
- return L01 + L2;
+ return L0 + L1 + L2;
}
float4 getIndirectDiffuse(v2f i, Pbr pbr, inout LightIndirect light) {
@@ -234,7 +255,11 @@ void GetLighting(v2f i, Pbr pbr, out LightData data) {
// Direct lighting
data.direct.dir = getDirectLightDirection(i);
data.direct.H = normalize(data.common.V + data.direct.dir);
+#if defined(_WRAPPED_LIGHTING)
+ data.direct.NoL = wrapNoL(saturate(dot(pbr.normal, data.direct.dir)), _Wrapped_Lighting_Amount);
+#else
data.direct.NoL = saturate(dot(pbr.normal, data.direct.dir));
+#endif
data.direct.NoH = saturate(dot(pbr.normal, data.direct.H));
data.direct.LoH = saturate(dot(data.direct.dir, data.direct.H));
#if defined(_CLEARCOAT)