reze-engine 0.10.2 → 0.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/README.md +90 -13
  2. package/dist/engine.d.ts +177 -34
  3. package/dist/engine.d.ts.map +1 -1
  4. package/dist/engine.js +1204 -318
  5. package/dist/index.d.ts +2 -1
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +1 -1
  8. package/dist/shaders/body.d.ts +2 -0
  9. package/dist/shaders/body.d.ts.map +1 -0
  10. package/dist/shaders/body.js +232 -0
  11. package/dist/shaders/classify.d.ts +4 -0
  12. package/dist/shaders/classify.d.ts.map +1 -0
  13. package/dist/shaders/classify.js +12 -0
  14. package/dist/shaders/cloth_rough.d.ts +2 -0
  15. package/dist/shaders/cloth_rough.d.ts.map +1 -0
  16. package/dist/shaders/cloth_rough.js +190 -0
  17. package/dist/shaders/cloth_smooth.d.ts +2 -0
  18. package/dist/shaders/cloth_smooth.d.ts.map +1 -0
  19. package/dist/shaders/cloth_smooth.js +186 -0
  20. package/dist/shaders/default.d.ts +2 -0
  21. package/dist/shaders/default.d.ts.map +1 -0
  22. package/dist/shaders/default.js +185 -0
  23. package/dist/shaders/dfg_lut.d.ts +3 -0
  24. package/dist/shaders/dfg_lut.d.ts.map +1 -0
  25. package/dist/shaders/dfg_lut.js +129 -0
  26. package/dist/shaders/eye.d.ts +2 -0
  27. package/dist/shaders/eye.d.ts.map +1 -0
  28. package/dist/shaders/eye.js +159 -0
  29. package/dist/shaders/face.d.ts +2 -0
  30. package/dist/shaders/face.d.ts.map +1 -0
  31. package/dist/shaders/face.js +235 -0
  32. package/dist/shaders/hair.d.ts +2 -0
  33. package/dist/shaders/hair.d.ts.map +1 -0
  34. package/dist/shaders/hair.js +196 -0
  35. package/dist/shaders/ltc_mag_lut.d.ts +3 -0
  36. package/dist/shaders/ltc_mag_lut.d.ts.map +1 -0
  37. package/dist/shaders/ltc_mag_lut.js +1033 -0
  38. package/dist/shaders/metal.d.ts +2 -0
  39. package/dist/shaders/metal.d.ts.map +1 -0
  40. package/dist/shaders/metal.js +187 -0
  41. package/dist/shaders/nodes.d.ts +2 -0
  42. package/dist/shaders/nodes.d.ts.map +1 -0
  43. package/dist/shaders/nodes.js +465 -0
  44. package/dist/shaders/stockings.d.ts +2 -0
  45. package/dist/shaders/stockings.d.ts.map +1 -0
  46. package/dist/shaders/stockings.js +244 -0
  47. package/package.json +1 -1
  48. package/src/engine.ts +1412 -385
  49. package/src/index.ts +12 -2
  50. package/src/shaders/body.ts +234 -0
  51. package/src/shaders/classify.ts +25 -0
  52. package/src/shaders/cloth_rough.ts +192 -0
  53. package/src/shaders/cloth_smooth.ts +188 -0
  54. package/src/shaders/default.ts +186 -0
  55. package/src/shaders/dfg_lut.ts +131 -0
  56. package/src/shaders/eye.ts +160 -0
  57. package/src/shaders/face.ts +237 -0
  58. package/src/shaders/hair.ts +198 -0
  59. package/src/shaders/ltc_mag_lut.ts +1035 -0
  60. package/src/shaders/metal.ts +189 -0
  61. package/src/shaders/nodes.ts +466 -0
  62. package/src/shaders/stockings.ts +246 -0
@@ -0,0 +1,189 @@
1
+ // M_Metal — Metallic Principled (Metallic=1.0, Specular=1.0, Specular Tint=0.114, Roughness=0.3)
2
+ // + NPR toon/AO emission stack (Strength=8.1), MixShader Fac=0.6967.
3
+ // Base color uses a Voronoi pattern sampled in reflection-coord space (Blender 纹理坐标.Reflection)
4
+ // to add subtle metallic sparkle variation. No Normal link in the graph.
5
+
6
+ import { NODES_WGSL } from "./nodes"
7
+
8
+ export const METAL_SHADER_WGSL = /* wgsl */ `
9
+
10
+ ${NODES_WGSL}
11
+
12
+ struct CameraUniforms {
13
+ view: mat4x4f,
14
+ projection: mat4x4f,
15
+ viewPos: vec3f,
16
+ _padding: f32,
17
+ };
18
+
19
+ struct Light {
20
+ direction: vec4f,
21
+ color: vec4f,
22
+ };
23
+
24
+ struct LightUniforms {
25
+ ambientColor: vec4f,
26
+ lights: array<Light, 4>,
27
+ };
28
+
29
+ struct MaterialUniforms {
30
+ diffuseColor: vec3f,
31
+ alpha: f32,
32
+ };
33
+
34
+ struct VertexOutput {
35
+ @builtin(position) position: vec4f,
36
+ @location(0) normal: vec3f,
37
+ @location(1) uv: vec2f,
38
+ @location(2) worldPos: vec3f,
39
+ };
40
+
41
+ struct LightVP { viewProj: mat4x4f, };
42
+
43
+ @group(0) @binding(0) var<uniform> camera: CameraUniforms;
44
+ @group(0) @binding(1) var<uniform> light: LightUniforms;
45
+ @group(0) @binding(2) var diffuseSampler: sampler;
46
+ @group(0) @binding(3) var shadowMap: texture_depth_2d;
47
+ @group(0) @binding(4) var shadowSampler: sampler_comparison;
48
+ @group(0) @binding(5) var<uniform> lightVP: LightVP;
49
+ @group(1) @binding(0) var<storage, read> skinMats: array<mat4x4f>;
50
+ @group(2) @binding(0) var diffuseTexture: texture_2d<f32>;
51
+ @group(2) @binding(1) var<uniform> material: MaterialUniforms;
52
+
53
+ fn sampleShadow(worldPos: vec3f, n: vec3f) -> f32 {
54
+ // Back-facing to key light: direct contribution is zero anyway, skip 9 texture samples.
55
+ if (dot(n, -light.lights[0].direction.xyz) <= 0.0) { return 0.0; }
56
+ let biasedPos = worldPos + n * 0.08;
57
+ let lclip = lightVP.viewProj * vec4f(biasedPos, 1.0);
58
+ let ndc = lclip.xyz / max(lclip.w, 1e-6);
59
+ let suv = vec2f(ndc.x * 0.5 + 0.5, 0.5 - ndc.y * 0.5);
60
+ let cmpZ = ndc.z - 0.001;
61
+ let ts = 1.0 / 2048.0;
62
+ // 3x3 PCF unrolled — Safari's Metal backend doesn't unroll nested shadow loops reliably.
63
+ let s00 = textureSampleCompareLevel(shadowMap, shadowSampler, suv + vec2f(-ts, -ts), cmpZ);
64
+ let s10 = textureSampleCompareLevel(shadowMap, shadowSampler, suv + vec2f(0.0, -ts), cmpZ);
65
+ let s20 = textureSampleCompareLevel(shadowMap, shadowSampler, suv + vec2f( ts, -ts), cmpZ);
66
+ let s01 = textureSampleCompareLevel(shadowMap, shadowSampler, suv + vec2f(-ts, 0.0), cmpZ);
67
+ let s11 = textureSampleCompareLevel(shadowMap, shadowSampler, suv, cmpZ);
68
+ let s21 = textureSampleCompareLevel(shadowMap, shadowSampler, suv + vec2f( ts, 0.0), cmpZ);
69
+ let s02 = textureSampleCompareLevel(shadowMap, shadowSampler, suv + vec2f(-ts, ts), cmpZ);
70
+ let s12 = textureSampleCompareLevel(shadowMap, shadowSampler, suv + vec2f(0.0, ts), cmpZ);
71
+ let s22 = textureSampleCompareLevel(shadowMap, shadowSampler, suv + vec2f( ts, ts), cmpZ);
72
+ return (s00 + s10 + s20 + s01 + s11 + s21 + s02 + s12 + s22) * (1.0 / 9.0);
73
+ }
74
+
75
+ const PI_M: f32 = 3.141592653589793;
76
+ const METAL_SPECULAR: f32 = 1.0;
77
+ const METAL_METALLIC: f32 = 1.0;
78
+ const METAL_ROUGHNESS: f32 = 0.3;
79
+ const METAL_SPECULAR_TINT: f32 = 0.114;
80
+ const METAL_TOON_EDGE: f32 = 0.2966;
81
+ const METAL_MIX04_MUL: f32 = 0.5;
82
+ const METAL_EMIT_STR: f32 = 8.100000381469727;
83
+ const METAL_MIX_SHADER_FAC: f32 = 0.6967;
84
+ const METAL_VORONOI_SCALE: f32 = 4.3;
85
+
86
+ @vertex fn vs(
87
+ @location(0) position: vec3f,
88
+ @location(1) normal: vec3f,
89
+ @location(2) uv: vec2f,
90
+ @location(3) joints0: vec4<u32>,
91
+ @location(4) weights0: vec4<f32>
92
+ ) -> VertexOutput {
93
+ var output: VertexOutput;
94
+ let pos4 = vec4f(position, 1.0);
95
+ let weightSum = weights0.x + weights0.y + weights0.z + weights0.w;
96
+ let invWeightSum = select(1.0, 1.0 / weightSum, weightSum > 0.0001);
97
+ let nw = select(vec4f(1.0, 0.0, 0.0, 0.0), weights0 * invWeightSum, weightSum > 0.0001);
98
+ var skinnedPos = vec4f(0.0);
99
+ var skinnedNrm = vec3f(0.0);
100
+ for (var i = 0u; i < 4u; i++) {
101
+ let m = skinMats[joints0[i]];
102
+ let w = nw[i];
103
+ skinnedPos += (m * pos4) * w;
104
+ skinnedNrm += (mat3x3f(m[0].xyz, m[1].xyz, m[2].xyz) * normal) * w;
105
+ }
106
+ output.position = camera.projection * camera.view * vec4f(skinnedPos.xyz, 1.0);
107
+ // Skip VS normalize — interpolation denormalizes anyway, and FS always does normalize(input.normal).
108
+ output.normal = skinnedNrm;
109
+ output.uv = uv;
110
+ output.worldPos = skinnedPos.xyz;
111
+ return output;
112
+ }
113
+
114
+ struct FSOut {
115
+ @location(0) color: vec4f,
116
+ @location(1) mask: f32,
117
+ };
118
+
119
+ @fragment fn fs(input: VertexOutput) -> FSOut {
120
+ let n = normalize(input.normal);
121
+ let v = normalize(camera.viewPos - input.worldPos);
122
+ let l = -light.lights[0].direction.xyz;
123
+ let sun = light.lights[0].color.xyz * light.lights[0].color.w;
124
+ let amb = light.ambientColor.xyz;
125
+ let shadow = sampleShadow(input.worldPos, n);
126
+
127
+ let tex_s = textureSample(diffuseTexture, diffuseSampler, input.uv);
128
+ let tex_rgb = tex_s.rgb;
129
+ let out_alpha = material.alpha * tex_s.a;
130
+ if (out_alpha < 0.001) { discard; }
131
+
132
+ // ═══ NPR toon stack (图像 → HSV.007 Val=0.8 → 转接点.001) ═══
133
+ let tex_tint = hue_sat_id(1.0, 0.800000011920929, 1.0, tex_rgb);
134
+ let lum_shade = shader_to_rgb_diffuse(n, l, sun, amb, shadow);
135
+ let ramp008 = ramp_constant_edge_aa(lum_shade, METAL_TOON_EDGE, vec4f(0,0,0,1), vec4f(1,1,1,1));
136
+ let mix04_fac = math_multiply(ramp008.r, METAL_MIX04_MUL);
137
+
138
+ // 混合.004: A=HSV.002(Val=0.2 dark), B=tex_tint
139
+ let dark_tex = hue_sat_id(1.0, 0.19999998807907104, 1.0, tex_tint);
140
+ let mix04 = mix_blend(mix04_fac, dark_tex, tex_tint);
141
+
142
+ // AO white/black ramp → 混合.002 factor
143
+ let ao = 1.0; // ao_fake(n, v) — no SSAO yet; inline 1.0 so the ramp/mix chain folds at compile time.
144
+ let ao_ramp_c = ramp_linear(ao, 0.0, vec4f(1,1,1,1), 0.8808, vec4f(0,0,0,1));
145
+ let overlay_fac = mix(1.0, 0.0, ao_ramp_c.r);
146
+
147
+ // 混合.002 OVERLAY: A=HSV.008(Val=1.0 identity) ← mix04, B=HSV.004(Val=2.0 bright) ← mix04
148
+ let hue008 = mix04; // identity HSV
149
+ let hue004 = hue_sat_id(1.0, 2.0, 1.0, mix04);
150
+ let npr_rgb = mix_overlay(overlay_fac, hue008, hue004);
151
+ let npr_emission = npr_rgb * METAL_EMIT_STR;
152
+
153
+ // ═══ Metallic Principled base color ═══
154
+ // Reflection-coord Voronoi for metallic sparkle:
155
+ // 纹理坐标.Reflection → 矢量运算 → 沃罗诺伊(Scale=4.3) → 颜色渐变 → 混合.005
156
+ let refl_dir = reflect(-v, n);
157
+ let voro = tex_voronoi_f1(refl_dir, METAL_VORONOI_SCALE);
158
+ let voro_ramp = ramp_linear(voro, 0.0, vec4f(0,0,0,1), 1.0, vec4f(1,1,1,1)).r;
159
+ // 混合.005: Fac=voro_ramp, A=voro_color(grayscale), B=HSV.006(Hue=0.5 Sat=1.5 Val=1.3)
160
+ let hue006 = hue_sat_id(1.5, 1.2999999523162842, 1.0, tex_tint);
161
+ let albedo = mix_blend(voro_ramp, vec3f(voro_ramp), hue006);
162
+
163
+ // 原理化BSDF (EEVEE port): metallic=1.0, specular=1.0, roughness=0.3.
164
+ // Per Blender principled wiring: f0 = mix((0.08*spec)*dielectric, albedo, metallic) → with
165
+ // metallic=1 this is just albedo (specular_tint is dielectric-only and ignored here).
166
+ let f0 = albedo;
167
+ let f90 = mix(f0, vec3f(1.0), sqrt(METAL_SPECULAR));
168
+ let NL = max(dot(n, l), 0.0);
169
+ let NV = max(dot(n, v), 1e-4);
170
+ let brdf_lut = brdf_lut_sample(NV, METAL_ROUGHNESS);
171
+ let reflection_color = F_brdf_multi_scatter(f0, f90, brdf_lut.xy);
172
+
173
+ let spec_direct = bsdf_ggx(n, l, v, NL, NV, METAL_ROUGHNESS) * sun * shadow * ltc_brdf_scale_from_lut(brdf_lut);
174
+ let spec_indirect = amb;
175
+ let spec_radiance = (spec_direct + spec_indirect) * reflection_color;
176
+
177
+ // Pure metal — no diffuse lobe (diffuse_weight = (1 - metallic) = 0).
178
+ let principled = spec_radiance;
179
+
180
+ // 混合着色器.001 Fac=0.6967: Shader=npr_emission, Shader_001=principled
181
+ let final_color = mix(npr_emission, principled, METAL_MIX_SHADER_FAC);
182
+
183
+ var out: FSOut;
184
+ out.color = vec4f(final_color, out_alpha);
185
+ out.mask = 1.0;
186
+ return out;
187
+ }
188
+
189
+ `
@@ -0,0 +1,466 @@
1
+ // Shared WGSL primitives for Blender-style NPR material nodes.
2
+ // Every function here maps 1:1 to a Blender shader node type used in the preset JSONs.
3
+ // Hand-ported material shaders concatenate this block before their own code.
4
+
5
+ export const NODES_WGSL = /* wgsl */ `
6
+
7
+ // Baked 64×64 rgba8unorm combined BRDF LUT — created once at engine init by dfg_lut.ts.
8
+ // .rg = split-sum DFG (Karis: tint = f0·x + f90·y) → F_brdf_*_scatter
9
+ // .ba = Heitz 2016 LTC magnitude (ltc_mag_ggx) → ltc_brdf_scale_from_lut
10
+ // Paired with group(0) binding(2) diffuseSampler (linear filter). Sample once per
11
+ // fragment via brdf_lut_sample() — callers feed .rg and the whole vec4 into the
12
+ // helpers below, halving LUT taps on the default Principled path.
13
+ @group(0) @binding(9) var brdfLut: texture_2d<f32>;
14
+
15
+ // ─── RGB ↔ HSV ──────────────────────────────────────────────────────
16
+
17
+ fn rgb_to_hsv(rgb: vec3f) -> vec3f {
18
+ let c_max = max(rgb.r, max(rgb.g, rgb.b));
19
+ let c_min = min(rgb.r, min(rgb.g, rgb.b));
20
+ let delta = c_max - c_min;
21
+
22
+ var h = 0.0;
23
+ if (delta > 1e-6) {
24
+ if (c_max == rgb.r) {
25
+ h = (rgb.g - rgb.b) / delta;
26
+ if (h < 0.0) { h += 6.0; }
27
+ } else if (c_max == rgb.g) {
28
+ h = 2.0 + (rgb.b - rgb.r) / delta;
29
+ } else {
30
+ h = 4.0 + (rgb.r - rgb.g) / delta;
31
+ }
32
+ h /= 6.0;
33
+ }
34
+ let s = select(0.0, delta / c_max, c_max > 1e-6);
35
+ return vec3f(h, s, c_max);
36
+ }
37
+
38
+ fn hsv_to_rgb(hsv: vec3f) -> vec3f {
39
+ let h = hsv.x;
40
+ let s = hsv.y;
41
+ let v = hsv.z;
42
+ if (s < 1e-6) { return vec3f(v); }
43
+
44
+ let hh = fract(h) * 6.0;
45
+ let sector = u32(hh);
46
+ let f = hh - f32(sector);
47
+ let p = v * (1.0 - s);
48
+ let q = v * (1.0 - s * f);
49
+ let t = v * (1.0 - s * (1.0 - f));
50
+
51
+ switch (sector) {
52
+ case 0u: { return vec3f(v, t, p); }
53
+ case 1u: { return vec3f(q, v, p); }
54
+ case 2u: { return vec3f(p, v, t); }
55
+ case 3u: { return vec3f(p, q, v); }
56
+ case 4u: { return vec3f(t, p, v); }
57
+ default: { return vec3f(v, p, q); }
58
+ }
59
+ }
60
+
61
+ // ─── HUE_SAT node ───────────────────────────────────────────────────
62
+
63
+ fn hue_sat(hue: f32, saturation: f32, value: f32, fac: f32, color: vec3f) -> vec3f {
64
+ var hsv = rgb_to_hsv(color);
65
+ hsv.x = fract(hsv.x + hue - 0.5);
66
+ hsv.y = clamp(hsv.y * saturation, 0.0, 1.0);
67
+ hsv.z *= value;
68
+ return mix(color, hsv_to_rgb(hsv), fac);
69
+ }
70
+
71
+ // hue_sat specialization for hue=0.5 (identity hue shift — fract(h + 0.5 - 0.5) = h).
72
+ // Branchless equivalent that skips the rgb_to_hsv → hsv_to_rgb roundtrip: WebKit's
73
+ // Metal backend serializes the 3-way if chain in rgb_to_hsv and the 6-way switch in
74
+ // hsv_to_rgb, where this form compiles to linear SIMD ops + a single select.
75
+ fn hue_sat_id(saturation: f32, value: f32, fac: f32, color: vec3f) -> vec3f {
76
+ let m = max(max(color.r, color.g), color.b);
77
+ let n = min(min(color.r, color.g), color.b);
78
+ // Unclamped (sat*old_s ≤ 1): reproj = mix(vec3f(m), color, saturation).
79
+ // Clamped (saturated to 1): reproj = (color - n) * m / (m - n).
80
+ let range = max(m - n, 1e-6);
81
+ let unclamped = mix(vec3f(m), color, saturation);
82
+ let clamped = (color - vec3f(n)) * m / range;
83
+ let needs_clamp = (m - n) * saturation >= m;
84
+ let reproj = select(unclamped, clamped, needs_clamp);
85
+ return mix(color, reproj * value, fac);
86
+ }
87
+
88
+ // ─── BRIGHTCONTRAST node ────────────────────────────────────────────
89
+
90
+ fn bright_contrast(color: vec3f, bright: f32, contrast: f32) -> vec3f {
91
+ let a = 1.0 + contrast;
92
+ let b = bright - contrast * 0.5;
93
+ return max(vec3f(0.0), color * a + vec3f(b));
94
+ }
95
+
96
+ // ─── INVERT node ────────────────────────────────────────────────────
97
+
98
+ fn invert(fac: f32, color: vec3f) -> vec3f {
99
+ return mix(color, vec3f(1.0) - color, fac);
100
+ }
101
+
102
+ fn invert_f(fac: f32, val: f32) -> f32 {
103
+ return mix(val, 1.0 - val, fac);
104
+ }
105
+
106
+ // ─── Color ramp (VALTORGB) — 2-stop variants ───────────────────────
107
+ // All 7 presets use exclusively 2-stop ramps.
108
+
109
+ fn ramp_constant(f: f32, p0: f32, c0: vec4f, p1: f32, c1: vec4f) -> vec4f {
110
+ return select(c0, c1, f >= p1);
111
+ }
112
+
113
+ // CONSTANT ramp with screen-space edge AA — kills sparkle where fwidth(f) straddles a hard step (NPR terminator)
114
+ fn ramp_constant_edge_aa(f: f32, edge: f32, c0: vec4f, c1: vec4f) -> vec4f {
115
+ let w = max(fwidth(f) * 1.75, 6e-6);
116
+ let t = smoothstep(edge - w, edge + w, f);
117
+ return mix(c0, c1, t);
118
+ }
119
+
120
+ fn ramp_linear(f: f32, p0: f32, c0: vec4f, p1: f32, c1: vec4f) -> vec4f {
121
+ let t = saturate((f - p0) / max(p1 - p0, 1e-6));
122
+ return mix(c0, c1, t);
123
+ }
124
+
125
+ fn ramp_cardinal(f: f32, p0: f32, c0: vec4f, p1: f32, c1: vec4f) -> vec4f {
126
+ // cardinal spline with 2 stops degrades to smoothstep
127
+ let t = saturate((f - p0) / max(p1 - p0, 1e-6));
128
+ let ss = t * t * (3.0 - 2.0 * t);
129
+ return mix(c0, c1, ss);
130
+ }
131
+
132
+ // ─── MATH node operations ───────────────────────────────────────────
133
+
134
+ fn math_add(a: f32, b: f32) -> f32 { return a + b; }
135
+ fn math_multiply(a: f32, b: f32) -> f32 { return a * b; }
136
+ fn math_power(a: f32, b: f32) -> f32 { return pow(max(a, 0.0), b); }
137
+ fn math_greater_than(a: f32, b: f32) -> f32 { return select(0.0, 1.0, a > b); }
138
+
139
+ // Blender's implicit Color → Float socket conversion uses BT.601 grayscale
140
+ // (rgb_to_grayscale in blenkernel/intern/node.cc). When a material graph plugs a
141
+ // Color output into a Math node's Value input, this is the scalar it actually sees.
142
+ fn color_to_value(c: vec3f) -> f32 {
143
+ return 0.299 * c.r + 0.587 * c.g + 0.114 * c.b;
144
+ }
145
+
146
+ // ─── MIX node (blend_type variants) ────────────────────────────────
147
+
148
+ fn mix_blend(fac: f32, a: vec3f, b: vec3f) -> vec3f {
149
+ return mix(a, b, fac);
150
+ }
151
+
152
+ fn mix_overlay(fac: f32, a: vec3f, b: vec3f) -> vec3f {
153
+ let lo = 2.0 * a * b;
154
+ let hi = vec3f(1.0) - 2.0 * (vec3f(1.0) - a) * (vec3f(1.0) - b);
155
+ let overlay = select(hi, lo, a < vec3f(0.5));
156
+ return mix(a, overlay, fac);
157
+ }
158
+
159
+ fn mix_multiply(fac: f32, a: vec3f, b: vec3f) -> vec3f {
160
+ return mix(a, a * b, fac);
161
+ }
162
+
163
+ fn mix_lighten(fac: f32, a: vec3f, b: vec3f) -> vec3f {
164
+ return mix(a, max(a, b), fac);
165
+ }
166
+
167
+ // Blender Mix (Color) blend LINEAR_LIGHT: result = mix(A, A + 2*B - 1, Fac)
168
+ fn mix_linear_light(fac: f32, a: vec3f, b: vec3f) -> vec3f {
169
+ return mix(a, a + 2.0 * b - vec3f(1.0), fac);
170
+ }
171
+
172
+ // Luminance for Shader→RGB scalar gates (linear RGB, Rec.709 weights)
173
+ fn luminance_rec709_linear(c: vec3f) -> f32 {
174
+ return dot(max(c, vec3f(0.0)), vec3f(0.2126, 0.7152, 0.0722));
175
+ }
176
+
177
+ // ─── FRESNEL node ───────────────────────────────────────────────────
178
+ // Schlick approximation matching Blender's Fresnel node
179
+
180
+ fn fresnel(ior: f32, n: vec3f, v: vec3f) -> f32 {
181
+ let r = (ior - 1.0) / (ior + 1.0);
182
+ let f0 = r * r;
183
+ let cos_theta = clamp(dot(n, v), 0.0, 1.0);
184
+ let m = 1.0 - cos_theta;
185
+ let m2 = m * m;
186
+ let m5 = m2 * m2 * m;
187
+ return f0 + (1.0 - f0) * m5;
188
+ }
189
+
190
+ // ─── LAYER_WEIGHT node ──────────────────────────────────────────────
191
+
192
+ fn layer_weight_fresnel(blend: f32, n: vec3f, v: vec3f) -> f32 {
193
+ let eta = max(1.0 - blend, 1e-4);
194
+ let r = (1.0 - eta) / (1.0 + eta);
195
+ let f0 = r * r;
196
+ let cos_theta = clamp(abs(dot(n, v)), 0.0, 1.0);
197
+ let m = 1.0 - cos_theta;
198
+ let m2 = m * m;
199
+ let m5 = m2 * m2 * m;
200
+ return f0 + (1.0 - f0) * m5;
201
+ }
202
+
203
+ fn layer_weight_facing(blend: f32, n: vec3f, v: vec3f) -> f32 {
204
+ var facing = abs(dot(n, v));
205
+ let b = clamp(blend, 0.0, 0.99999);
206
+ if (b != 0.5) {
207
+ let exponent = select(2.0 * b, 0.5 / (1.0 - b), b >= 0.5);
208
+ facing = pow(facing, exponent);
209
+ }
210
+ return 1.0 - facing;
211
+ }
212
+
213
+ // ─── SHADER_TO_RGB (white DiffuseBSDF) ──────────────────────────────
214
+ // Eevee captures lit diffuse: (albedo/π)*sun*N·L*shadow + ambient (linear). Albedo=1.
215
+ // Matches default.ts direct term scale so VALTORGB thresholds from Blender JSON stay valid.
216
+
217
+ fn shader_to_rgb_diffuse(n: vec3f, l: vec3f, sun_rgb: vec3f, ambient_rgb: vec3f, shadow: f32) -> f32 {
218
+ const PI_S: f32 = 3.141592653589793;
219
+ let ndotl = max(dot(n, l), 0.0);
220
+ let rgb = sun_rgb * (ndotl * shadow / PI_S) + ambient_rgb;
221
+ return luminance_rec709_linear(rgb);
222
+ }
223
+
224
+ // ─── AMBIENT_OCCLUSION node (faked) ─────────────────────────────────
225
+ // Real SSAO is a non-goal. We approximate: use the "inside" value from
226
+ // concavity heuristic: 1.0 = fully lit, lower = occluded.
227
+ // For now returns 1.0 (no darkening). Individual presets can override.
228
+
229
+ fn ao_fake(n: vec3f, v: vec3f) -> f32 {
230
+ return 1.0;
231
+ }
232
+
233
+ // ─── BUMP node ──────────────────────────────────────────────────────
234
+ // Screen-space bump from a scalar height field. Needs dFdx/dFdy which
235
+ // WGSL provides as dpdx/dpdy.
236
+
237
+ fn bump(strength: f32, height: f32, normal: vec3f, world_pos: vec3f) -> vec3f {
238
+ let dhdx = dpdx(height);
239
+ let dhdy = dpdy(height);
240
+ let dpdx_pos = dpdx(world_pos);
241
+ let dpdy_pos = dpdy(world_pos);
242
+ let perturbed = normalize(normal) - strength * (dhdx * normalize(cross(dpdy_pos, normal)) + dhdy * normalize(cross(normal, dpdx_pos)));
243
+ return normalize(perturbed);
244
+ }
245
+
246
+ // LH engine + WebGPU fragment Y: flip dhdy contribution so height peaks read as outward bumps vs Blender reference
247
+ fn bump_lh(strength: f32, height: f32, normal: vec3f, world_pos: vec3f) -> vec3f {
248
+ let dhdx = dpdx(height);
249
+ let dhdy = dpdy(height);
250
+ let dpdx_pos = dpdx(world_pos);
251
+ let dpdy_pos = dpdy(world_pos);
252
+ let perturbed = normalize(normal) - strength * (dhdx * normalize(cross(dpdy_pos, normal)) - dhdy * normalize(cross(normal, dpdx_pos)));
253
+ return normalize(perturbed);
254
+ }
255
+
256
+ // ─── NOISE texture (Perlin-style) ───────────────────────────────────
257
+ // Simplified gradient noise matching Blender's default noise output.
258
+
259
+ // PCG-style integer hash. Replaces the classic 'fract(sin(q) * LARGE)' trick because
260
+ // WebKit's Metal backend compiles 'sin' to a full transcendental op (slow), while
261
+ // Safari's Apple-GPU scalar ALU handles int muls/xors near free. Inputs arrive as
262
+ // integer-valued floats (floor(p) + unit offsets) from _noise3, so vec3i cast is exact.
263
+ fn _hash33(p: vec3f) -> vec3f {
264
+ var h = vec3u(vec3i(p) + vec3i(32768));
265
+ h = h * vec3u(1664525u, 1013904223u, 2654435761u);
266
+ h = (h.yzx ^ h) * vec3u(2246822519u, 3266489917u, 668265263u);
267
+ h = h ^ (h >> vec3u(16u));
268
+ // Mask to 24 bits — above that f32 loses precision on the u32→f32 convert.
269
+ let hm = h & vec3u(16777215u);
270
+ return vec3f(hm) * (2.0 / 16777216.0) - 1.0;
271
+ }
272
+
273
+ fn _noise3(p: vec3f) -> f32 {
274
+ let i = floor(p);
275
+ let f = fract(p);
276
+ let u = f * f * (3.0 - 2.0 * f);
277
+
278
+ return mix(
279
+ mix(
280
+ mix(dot(_hash33(i + vec3f(0,0,0)), f - vec3f(0,0,0)),
281
+ dot(_hash33(i + vec3f(1,0,0)), f - vec3f(1,0,0)), u.x),
282
+ mix(dot(_hash33(i + vec3f(0,1,0)), f - vec3f(0,1,0)),
283
+ dot(_hash33(i + vec3f(1,1,0)), f - vec3f(1,1,0)), u.x), u.y),
284
+ mix(
285
+ mix(dot(_hash33(i + vec3f(0,0,1)), f - vec3f(0,0,1)),
286
+ dot(_hash33(i + vec3f(1,0,1)), f - vec3f(1,0,1)), u.x),
287
+ mix(dot(_hash33(i + vec3f(0,1,1)), f - vec3f(0,1,1)),
288
+ dot(_hash33(i + vec3f(1,1,1)), f - vec3f(1,1,1)), u.x), u.y),
289
+ u.z);
290
+ }
291
+
292
+ fn tex_noise(p: vec3f, scale: f32, detail: f32, roughness: f32, distortion: f32) -> f32 {
293
+ var q = p;
294
+ if (abs(distortion) > 1e-6) {
295
+ let w = _noise3(p * scale * 1.37 + vec3f(2.31, 5.17, 8.09));
296
+ q = p + (w * 2.0 - 1.0) * distortion;
297
+ }
298
+ let coords = q * scale;
299
+ var value = 0.0;
300
+ var amplitude = 1.0;
301
+ var frequency = 1.0;
302
+ var total_amp = 0.0;
303
+ let octaves = i32(clamp(detail, 0.0, 15.0)) + 1;
304
+ for (var i = 0; i < octaves; i++) {
305
+ value += amplitude * _noise3(coords * frequency);
306
+ total_amp += amplitude;
307
+ amplitude *= roughness;
308
+ frequency *= 2.0;
309
+ }
310
+ return value / max(total_amp, 1e-6) * 0.5 + 0.5;
311
+ }
312
+
313
+ // tex_noise specialization: detail=2.0 (3 octaves), roughness=0.5, distortion=0.
314
+ // WebKit can't unroll tex_noise's for-loop because 'octaves' is a runtime value;
315
+ // this variant is fully unrolled with constants folded (total_amp = 1.75).
316
+ fn tex_noise_d2(p: vec3f, scale: f32) -> f32 {
317
+ let c = p * scale;
318
+ let v = _noise3(c) + 0.5 * _noise3(c * 2.0) + 0.25 * _noise3(c * 4.0);
319
+ return v * (1.0 / 1.75) * 0.5 + 0.5;
320
+ }
321
+
322
+ // ─── TEX_GRADIENT (linear) ──────────────────────────────────────────
323
+ // Used by Stockings preset. Maps the input vector's X to a 0–1 gradient.
324
+
325
+ fn tex_gradient_linear(uv: vec3f) -> f32 {
326
+ return clamp(uv.x, 0.0, 1.0);
327
+ }
328
+
329
+ // ─── TEX_VORONOI (distance only) ────────────────────────────────────
330
+ // Used by Metal preset. Simplified F1 cell noise.
331
+
332
+ fn tex_voronoi_f1(p: vec3f, scale: f32) -> f32 {
333
+ let coords = p * scale;
334
+ let i = floor(coords);
335
+ let f = fract(coords);
336
+ var min_dist = 1e10;
337
+ for (var z = -1; z <= 1; z++) {
338
+ for (var y = -1; y <= 1; y++) {
339
+ for (var x = -1; x <= 1; x++) {
340
+ let neighbor = vec3f(f32(x), f32(y), f32(z));
341
+ let point = _hash33(i + neighbor) * 0.5 + 0.5;
342
+ let diff = neighbor + point - f;
343
+ min_dist = min(min_dist, dot(diff, diff));
344
+ }
345
+ }
346
+ }
347
+ return sqrt(min_dist);
348
+ }
349
+
350
+ // ─── SEPXYZ node ────────────────────────────────────────────────────
351
+
352
+ fn separate_xyz(v: vec3f) -> vec3f { return v; }
353
+
354
+ // ─── VECT_MATH (cross product) ──────────────────────────────────────
355
+
356
+ fn vect_math_cross(a: vec3f, b: vec3f) -> vec3f { return cross(a, b); }
357
+
358
+ // ─── MAPPING node ───────────────────────────────────────────────────
359
+ // Point-type mapping: scale, rotate (euler XYZ), translate.
360
+
361
+ fn mapping_point(v: vec3f, loc: vec3f, rot: vec3f, scl: vec3f) -> vec3f {
362
+ var p = v * scl;
363
+ // simplified: skip rotation when all angles are zero (common case)
364
+ if (abs(rot.x) + abs(rot.y) + abs(rot.z) > 1e-6) {
365
+ let cx = cos(rot.x); let sx = sin(rot.x);
366
+ let cy = cos(rot.y); let sy = sin(rot.y);
367
+ let cz = cos(rot.z); let sz = sin(rot.z);
368
+ let rx = vec3f(p.x, cx*p.y - sx*p.z, sx*p.y + cx*p.z);
369
+ let ry = vec3f(cy*rx.x + sy*rx.z, rx.y, -sy*rx.x + cy*rx.z);
370
+ p = vec3f(cz*ry.x - sz*ry.y, sz*ry.x + cz*ry.y, ry.z);
371
+ }
372
+ return p + loc;
373
+ }
374
+
375
+ // ─── NORMAL_MAP node (tangent-space) ────────────────────────────────
376
+ // Applies a tangent-space normal map. Requires TBN from vertex stage.
377
+
378
+ fn normal_map(strength: f32, map_color: vec3f, normal: vec3f, tangent: vec3f, bitangent: vec3f) -> vec3f {
379
+ let ts = map_color * 2.0 - 1.0;
380
+ let perturbed = normalize(tangent * ts.x + bitangent * ts.y + normal * ts.z);
381
+ return normalize(mix(normal, perturbed, strength));
382
+ }
383
+
384
+ // ─── EEVEE Principled BSDF primitives ───────────────────────────────
385
+ // Ports from Blender 3.6 source/blender/draw/engines/eevee/shaders/
386
+ // bsdf_common_lib.glsl + gpu_shader_material_principled.glsl.
387
+ // Usage pattern (see material shaders): direct spec = bsdf_ggx × sun × shadow
388
+ // (NL baked in, no F yet); ambient spec = probe_radiance; tint both with
389
+ // reflection_color = F_brdf_multi_scatter(f0, f90, split_sum) AFTER summing.
390
+
391
+ const EEVEE_PI: f32 = 3.141592653589793;
392
+
393
+ // Fused analytic GGX specular (direct lights). Returns BRDF × NL.
394
+ // 4·NL·NV is cancelled via G1_Smith reciprocal form — see bsdf_common_lib.glsl:115.
395
+ // Caller passes NL, NV (already computed for diffuse + brdf_lut_sample) so WebKit
396
+ // can reuse them instead of recomputing dot products across the function boundary.
397
+ fn bsdf_ggx(N: vec3f, L: vec3f, V: vec3f, NL_in: f32, NV_in: f32, roughness: f32) -> f32 {
398
+ let a = max(roughness, 1e-4);
399
+ let a2 = a * a;
400
+ let H = normalize(L + V);
401
+ let NH = max(dot(N, H), 1e-8);
402
+ let NL = max(NL_in, 1e-8);
403
+ let NV = max(NV_in, 1e-8);
404
+ // G1_Smith_GGX_opti reciprocal form — denominator piece only.
405
+ let G1L = NL + sqrt(NL * (NL - NL * a2) + a2);
406
+ let G1V = NV + sqrt(NV * (NV - NV * a2) + a2);
407
+ let G = G1L * G1V;
408
+ // D_ggx_opti = pi * denom² — reciprocal D × a².
409
+ let tmp = (NH * a2 - NH) * NH + 1.0;
410
+ let D_opti = EEVEE_PI * tmp * tmp;
411
+ return NL * a2 / (D_opti * G);
412
+ }
413
+
414
+ // Split-sum DFG LUT — Karis 2013 curve fit stand-in for the 64×64 baked LUT.
415
+ // Returns (lut.x, lut.y) in Blender convention: tint = f0·lut.x + f90·lut.y.
416
+ fn brdf_lut_approx(NV: f32, roughness: f32) -> vec2f {
417
+ let c0 = vec4f(-1.0, -0.0275, -0.572, 0.022);
418
+ let c1 = vec4f(1.0, 0.0425, 1.04, -0.04);
419
+ let r = roughness * c0 + c1;
420
+ let a004 = min(r.x * r.x, exp2(-9.28 * NV)) * r.x + r.y;
421
+ return vec2f(-1.04, 1.04) * a004 + r.zw;
422
+ }
423
+
424
+ // Baked combined BRDF LUT — exact port of Blender bsdf_lut_frag.glsl packed with
425
+ // ltc_mag_ggx from eevee_lut.c. Single sample returns DFG (.rg) and LTC mag (.ba).
426
+ // Addressed as Blender's common_utiltex_lib.glsl:lut_coords:
427
+ // coords = (roughness, sqrt(1 - NV)), then half-texel bias for filtering.
428
+ // Requires group(0) binding(9) brdfLut + binding(2) diffuseSampler in the host shader.
429
+ fn brdf_lut_sample(NV: f32, roughness: f32) -> vec4f {
430
+ let LUT_SIZE: f32 = 64.0;
431
+ var uv = vec2f(saturate(roughness), sqrt(saturate(1.0 - NV)));
432
+ uv = uv * ((LUT_SIZE - 1.0) / LUT_SIZE) + 0.5 / LUT_SIZE;
433
+ return textureSampleLevel(brdfLut, diffuseSampler, uv, 0.0);
434
+ }
435
+
436
+ fn F_brdf_single_scatter(f0: vec3f, f90: vec3f, lut: vec2f) -> vec3f {
437
+ return lut.y * f90 + lut.x * f0;
438
+ }
439
+
440
+ // Fdez-Agüera 2019 multi-scatter compensation (EEVEE do_multiscatter=1).
441
+ fn F_brdf_multi_scatter(f0: vec3f, f90: vec3f, lut: vec2f) -> vec3f {
442
+ let FssEss = lut.y * f90 + lut.x * f0;
443
+ let Ess = lut.x + lut.y;
444
+ let Ems = 1.0 - Ess;
445
+ let Favg = f0 + (1.0 - f0) / 21.0;
446
+ let Fms = FssEss * Favg / (1.0 - (1.0 - Ess) * Favg);
447
+ return FssEss + Fms * Ems;
448
+ }
449
+
450
+ // EEVEE direct-specular energy compensation factor — closure_eval_glossy_lib.glsl:79-81:
451
+ // ltc_brdf_scale = (ltc.x + ltc.y) / (split_sum.x + split_sum.y)
452
+ // Blender evaluates direct lights via LTC (Heitz 2016) but indirect via split-sum;
453
+ // direct radiance is rescaled so total-energy matches the split-sum LUT.
454
+ // Takes a pre-sampled vec4f from brdf_lut_sample() to share the fetch with
455
+ // F_brdf_multi_scatter on the same fragment.
456
+ fn ltc_brdf_scale_from_lut(lut: vec4f) -> f32 {
457
+ return (lut.z + lut.w) / max(lut.x + lut.y, 1e-6);
458
+ }
459
+
460
+ // Luminance-normalized hue extraction — Blender tint_from_color (isolates hue+sat).
461
+ fn tint_from_color(color: vec3f) -> vec3f {
462
+ let lum = dot(color, vec3f(0.3, 0.6, 0.1));
463
+ return select(vec3f(1.0), color / lum, lum > 0.0);
464
+ }
465
+
466
+ `;