diff options
| author | yum <yum.food.vr@gmail.com> | 2026-03-30 13:11:29 -0700 |
|---|---|---|
| committer | yum <yum.food.vr@gmail.com> | 2026-03-30 13:11:29 -0700 |
| commit | 146b1e287e606b6ce3ebc4f60a1719f43c755916 (patch) | |
| tree | 914758325caffc2a1e2408119c9fd81e48f4520e /glitter.cginc | |
| parent | b5197bed4cad2a8452bcbfa8e116497760edf1ba (diff) | |
Glitter: use micro normal for IBL
Diffstat (limited to 'glitter.cginc')
| -rw-r--r-- | glitter.cginc | 113 |
1 files changed, 88 insertions, 25 deletions
diff --git a/glitter.cginc b/glitter.cginc index 2ed0ef4..34cd17c 100644 --- a/glitter.cginc +++ b/glitter.cginc @@ -13,6 +13,10 @@ * I have made changes to this code. They are: * 1. Syntax changes required to translate GLSL to HLSL. * 2. Stylistic preferences, like using "1" or "1.0" instead of "1.". + * 3. The `GetGlitterLighting` function, which populates data required for + * IBL. The original paper only discusses analytic lighting. For IBL, you + * also need the micro-normal to figure out which part of the cubemap to + * sample. * * @article{KPT:2025:Glinty, * title = {Evaluating and Sampling Glinty NDFs in Constant Time}, @@ -150,7 +154,16 @@ float compensation(float2 x_a, float2x2 sigma_a, float res_a) { return containing - explicitly_evaluated; } -float D_Kemppinen(float3 h, float alpha, float glint_alpha, float2 uv, float2x2 uv_J, float N, float filter_size) { +float3 disk_to_ndf_ggx(float2 v_disk, float alpha) { + float2 p = v_disk * 2.0f - 1.0f; + float r2 = saturate(dot(p, p)); + float3 hemi = float3(p * sqrt(max(1e-6f, 2.0f - r2)), 1.0f - r2); + float alpha2 = alpha * alpha; + float denom = sqrt(max(1e-6f, alpha2 * dot(hemi.xy, hemi.xy) + hemi.z * hemi.z)); + return float3(alpha * hemi.xy, hemi.z) / denom; +} + +float D_Kemppinen(float3 h, float alpha, float glint_alpha, float2 uv, float2x2 uv_J, float N, float filter_size, out float3 micro_normal) { float res = sqrt(N); float2 x_s = uv; float3 x_a_and_d = ndf_to_disk_ggx(h, alpha); @@ -160,44 +173,94 @@ float D_Kemppinen(float3 h, float alpha, float glint_alpha, float2 uv, float2x2 float lambda = QueryLod(res * uv_J, filter_size); float D_filter = 0; + float best_weight = 0; + float2 best_g_a = x_a; [loop] - for (float m = 0; m < 2; m += 1) { - float l = floor(lambda) + m; + for (float m = 0; m < 2; m += 1) { + float l = floor(lambda) + m; - float w_lambda = 1.0 - abs(lambda - l); - float res_s = res * pow(2, -l); - float res_a = pow(2, l); + float w_lambda = 1.0 - abs(lambda - l); + float res_s = res * pow(2, -l); + float res_a = pow(2, l); - float2x2 uv_J2 = filter_size * uv_J; - float2x2 sigma_s = mul(uv_J2, transpose(uv_J2)); + float2x2 uv_J2 = filter_size * uv_J; + float2x2 sigma_s = mul(uv_J2, transpose(uv_J2)); - float2x2 sigma_a = d * pow(glint_alpha, 2) * float2x2(1, 0, 0, 1); + float2x2 sigma_a = d * pow(glint_alpha, 2) * float2x2(1, 0, 0, 1); - float2 base_i_a = clamp(round(x_a * res_a), 1, res_a-1); - [loop] - for (uint j_a = 0; j_a < 4; ++j_a) { - float2 i_a = base_i_a + float2(int2(j_a, j_a/2)%2)-.5; + float2 base_i_a = clamp(round(x_a * res_a), 1, res_a-1); + [loop] + for (uint j_a = 0; j_a < 4; ++j_a) { + float2 i_a = base_i_a + float2(int2(j_a, j_a/2)%2)-.5; - float2 base_i_s = round(x_s * res_s); - [loop] - for (uint j_s = 0; j_s < 4; ++j_s) { - float2 i_s = base_i_s + float2(int2(j_s, j_s/2)%2)-.5; + float2 base_i_s = round(x_s * res_s); + [loop] + for (uint j_s = 0; j_s < 4; ++j_s) { + float2 i_s = base_i_s + float2(int2(j_s, j_s/2)%2)-.5; - float2 g_s = (i_s + Rand2D(i_s, i_a, l, 1u) - .5) / res_s; - float2 g_a = (i_a + Rand2D(i_s, i_a, l, 2u) - .5) / res_a; + float2 g_s = (i_s + Rand2D(i_s, i_a, l, 1u) - .5) / res_s; + float2 g_a = (i_a + Rand2D(i_s, i_a, l, 2u) - .5) / res_a; - float r = Rand1D(i_s, i_a, l, 4u); - float roulette = smoothstep(max(.0, r-.1), min(1.0, r+.1), w_lambda); + float r = Rand1D(i_s, i_a, l, 4u); + float roulette = smoothstep(max(.0, r-.1), min(1.0, r+.1), w_lambda); - D_filter += roulette * normal(sigma_a, x_a - g_a) * normal(sigma_s, x_s - g_s) / N; - } + float w = roulette * normal(sigma_a, x_a - g_a) * normal(sigma_s, x_s - g_s) / N; + D_filter += w; + if (w > best_weight) { + best_weight = w; + best_g_a = g_a; } - D_filter += w_lambda * compensation(x_a, sigma_a, res_a); + } } + D_filter += w_lambda * compensation(x_a, sigma_a, res_a); + } + micro_normal = normalize(disk_to_ndf_ggx(best_g_a, alpha)); return D_filter * d / PI; } -#endif // __GLITTER_INC +#if defined(_GLITTER) +struct LightGlitter { + float direct_D; + + float indirect_D; + float3 indirect_specular; + float3 indirect_dir; + float3 indirect_H; + float indirect_NoL; + float indirect_LoH; +}; + +// Glitter data getter to be run from lighting code. +LightGlitter GetGlitterLighting( + float glitter_amount, float glitter_roughness, + float2 uv, float3x3 tbn, float roughness, + float3 normal, float3 V, float3 direct_H, float3 indirect_H) { + LightGlitter g; + const float glitter_filter_size = 0.7f; + float2x2 uv_J = uv_ellipsoid(transpose(float2x2(ddx(uv), ddy(uv)))); + float N = 8.0e5f * pow(10.0f, glitter_amount * 6.0f - 2.0f); + + // Direct + float3 direct_H_tangent = mul(direct_H, transpose(tbn)); + float3 direct_micro_normal; // unused + g.direct_D = D_Kemppinen(direct_H_tangent, roughness, glitter_roughness, + uv, uv_J, N, glitter_filter_size, direct_micro_normal); + + // Indirect + float3 indirect_H_tangent = mul(indirect_H, transpose(tbn)); + float3 indirect_micro_normal; // used to sample cubemap + g.indirect_D = D_Kemppinen(indirect_H_tangent, roughness, glitter_roughness, + uv, uv_J, N, glitter_filter_size, indirect_micro_normal); + // Normal vector is the halfway vector in IBL. + g.indirect_H = normalize(mul(indirect_micro_normal, tbn)); + g.indirect_dir = reflect(-V, g.indirect_H); + g.indirect_NoL = max(1e-4, dot(normal, g.indirect_dir)); + g.indirect_LoH = max(1e-4, dot(g.indirect_dir, g.indirect_H)); + + return g; +} +#endif +#endif // __GLITTER_INC |
