reze-engine 0.40.0 → 0.41.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 (49) hide show
  1. package/README.md +8 -5
  2. package/dist/engine.d.ts +87 -31
  3. package/dist/engine.d.ts.map +1 -1
  4. package/dist/engine.js +264 -77
  5. package/dist/graph/presets/body.js +2 -2
  6. package/dist/graph/presets/cloth_rough.js +2 -2
  7. package/dist/graph/presets/cloth_smooth.js +2 -2
  8. package/dist/graph/presets/default.js +2 -2
  9. package/dist/graph/presets/eye.js +2 -2
  10. package/dist/graph/presets/face.js +2 -2
  11. package/dist/graph/presets/hair.js +2 -2
  12. package/dist/graph/presets/metal.js +2 -2
  13. package/dist/graph/presets/stockings.js +3 -3
  14. package/dist/graph/registry.d.ts.map +1 -1
  15. package/dist/graph/registry.js +436 -10
  16. package/dist/graph/style-group.d.ts +46 -0
  17. package/dist/graph/style-group.d.ts.map +1 -1
  18. package/dist/index.d.ts +2 -2
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/shaders/agx-lut.d.ts +11 -0
  21. package/dist/shaders/agx-lut.d.ts.map +1 -0
  22. package/dist/shaders/agx-lut.js +29 -0
  23. package/dist/shaders/materials/common.d.ts +1 -1
  24. package/dist/shaders/materials/common.d.ts.map +1 -1
  25. package/dist/shaders/materials/common.js +10 -0
  26. package/dist/shaders/materials/nodes.d.ts +1 -1
  27. package/dist/shaders/materials/nodes.d.ts.map +1 -1
  28. package/dist/shaders/materials/nodes.js +957 -555
  29. package/dist/shaders/passes/composite.d.ts +27 -12
  30. package/dist/shaders/passes/composite.d.ts.map +1 -1
  31. package/dist/shaders/passes/composite.js +155 -48
  32. package/package.json +1 -1
  33. package/src/engine.ts +304 -84
  34. package/src/graph/presets/body.ts +2 -2
  35. package/src/graph/presets/cloth_rough.ts +2 -2
  36. package/src/graph/presets/cloth_smooth.ts +2 -2
  37. package/src/graph/presets/default.ts +2 -2
  38. package/src/graph/presets/eye.ts +2 -2
  39. package/src/graph/presets/face.ts +2 -2
  40. package/src/graph/presets/hair.ts +2 -2
  41. package/src/graph/presets/metal.ts +2 -2
  42. package/src/graph/presets/stockings.ts +3 -3
  43. package/src/graph/registry.ts +450 -10
  44. package/src/graph/style-group.ts +44 -0
  45. package/src/index.ts +4 -2
  46. package/src/shaders/agx-lut.ts +30 -0
  47. package/src/shaders/materials/common.ts +10 -0
  48. package/src/shaders/materials/nodes.ts +962 -560
  49. package/src/shaders/passes/composite.ts +182 -64
@@ -1,22 +1,37 @@
1
- /** What a user background effect must define, documented once:
1
+ /** What user effect WGSL may define, documented once. A file declares its own
2
+ * mounts by which of these it defines — defining both is how one file is one
3
+ * weather system (dark sky behind, rain in front):
2
4
  *
3
5
  * fn background(ray: vec3f, uv: vec2f, time: f32) -> vec4f
6
+ * fn foreground(ray: vec3f, uv: vec2f, time: f32, depth: f32) -> vec4f
4
7
  *
5
- * - `ray` — normalized world-space view direction of this pixel (left-handed,
6
- * +Z forward; identical to what the 360 skybox samples by).
7
- * - `uv` — 0..1 across the canvas, origin bottom-left (shadertoy-style).
8
- * - `time` — seconds since the effect was applied.
8
+ * - `ray` — normalized world-space view direction of this pixel (left-handed,
9
+ * +Z forward; identical to what the 360 skybox samples by).
10
+ * - `uv` — 0..1 across the canvas, origin bottom-left (shadertoy-style).
11
+ * - `time` — seconds since the effect was applied.
12
+ * - `depth` — FOREGROUND ONLY. Camera-space distance in metres of whatever the
13
+ * scene drew at this pixel, the far plane where it drew nothing.
14
+ * Compare a particle's own distance against it and the model
15
+ * occludes it; fog needs no comparison at all, its alpha simply IS
16
+ * a function of distance.
9
17
  * - `bgResolution()` — canvas size in pixels, for aspect correction.
10
- * - declared params arrive as `params.<name>` (f32 or vec3f).
11
- * Return display-space sRGB + alpha, 0..1. The effect is a LAYER: it is
12
- * over-composited onto the base background (solid color / 360 equirect /
13
- * transparent) and sits behind the scene — alpha 0 lets the base show
14
- * through, so e.g. a starfield returns stars with a transparent sky. */
18
+ * - declared params arrive as `params.<name>` (f32 or vec3f), shared by both.
19
+ *
20
+ * Return display-space sRGB + alpha, 0..1. Both mounts are alpha-composited
21
+ * LAYERS, so alpha is what decides how much they replace: a background effect
22
+ * at alpha 1 covers the base (solid color / 360 equirect / transparent) and at
23
+ * 0 lets it through, which is how a starfield is stars over the user's color;
24
+ * a foreground at alpha 1 covers the frame. No mode flag anywhere — the alpha
25
+ * channel already says it. */
15
26
  export type CompositeEffectSource = {
16
- /** User WGSL defining `background(...)` (plus any helpers it wants). */
27
+ /** The user's WGSL verbatim: helpers plus whichever entry points it defines. */
17
28
  wgsl: string;
18
- /** Codegen'd `struct BgParams {...}` + binding decl; empty when no params. */
29
+ /** Codegen'd `struct EffectParams {...}` + binding decl; empty when no params. */
19
30
  paramsDecl: string;
31
+ /** Defines `fn background(...)` — mount under the scene. */
32
+ hasBackground: boolean;
33
+ /** Defines `fn foreground(...)` — mount over the finished frame. */
34
+ hasForeground: boolean;
20
35
  };
21
36
  export declare function buildCompositeShader(effect?: CompositeEffectSource | null): string;
22
37
  /** Kept for compatibility with existing imports (the base, no-effect shader). */
@@ -1 +1 @@
1
- {"version":3,"file":"composite.d.ts","sourceRoot":"","sources":["../../../src/shaders/passes/composite.ts"],"names":[],"mappings":"AASA;;;;;;;;;;;;;yEAayE;AACzE,MAAM,MAAM,qBAAqB,GAAG;IAClC,wEAAwE;IACxE,IAAI,EAAE,MAAM,CAAA;IACZ,8EAA8E;IAC9E,UAAU,EAAE,MAAM,CAAA;CACnB,CAAA;AAwQD,wBAAgB,oBAAoB,CAAC,MAAM,CAAC,EAAE,qBAAqB,GAAG,IAAI,GAAG,MAAM,CAelF;AAED,iFAAiF;AACjF,eAAO,MAAM,qBAAqB,QAA6B,CAAA"}
1
+ {"version":3,"file":"composite.d.ts","sourceRoot":"","sources":["../../../src/shaders/passes/composite.ts"],"names":[],"mappings":"AAeA;;;;;;;;;;;;;;;;;;;;;;;;+BAwB+B;AAC/B,MAAM,MAAM,qBAAqB,GAAG;IAClC,gFAAgF;IAChF,IAAI,EAAE,MAAM,CAAA;IACZ,kFAAkF;IAClF,UAAU,EAAE,MAAM,CAAA;IAClB,4DAA4D;IAC5D,aAAa,EAAE,OAAO,CAAA;IACtB,oEAAoE;IACpE,aAAa,EAAE,OAAO,CAAA;CACvB,CAAA;AA0WD,wBAAgB,oBAAoB,CAAC,MAAM,CAAC,EAAE,qBAAqB,GAAG,IAAI,GAAG,MAAM,CAclF;AAED,iFAAiF;AACjF,eAAO,MAAM,qBAAqB,QAA6B,CAAA"}
@@ -2,10 +2,16 @@
2
2
  // Bloom tint/intensity applied at combine (EEVEE treats them as combine-stage params, not prefilter).
3
3
  //
4
4
  // The shader is a TEMPLATE: buildCompositeShader() emits either the base pass or
5
- // a variant with a user background effect injected (setBackgroundEffect). The
6
- // effect is background mode 3, a sibling of the 360 equirect (mode 2) — it reuses
7
- // the same per-pixel view-ray reconstruction and composites in display space
8
- // under the scene, so it never affects lighting, bloom, or tonemapping.
5
+ // a variant with user WGSL injected at one or both effect MOUNTS (setEffect).
6
+ // The two mounts are the same idea on either side of the scene:
7
+ //
8
+ // background(...) under the scene — a sibling of the 360 equirect (mode 2),
9
+ // reusing the same per-pixel view-ray reconstruction.
10
+ // foreground(...) over the finished frame, handed the scene's depth in metres
11
+ // so it can be occluded by whatever it passes behind.
12
+ //
13
+ // Both composite in display space, so neither affects lighting, bloom, or
14
+ // tonemapping, and both are captured by offline export like any background.
9
15
  const COMPOSITE_HEAD = /* wgsl */ `
10
16
  // Pipeline-override constant: the engine creates two composite pipelines, one
11
17
  // with APPLY_GAMMA=false (gamma=1 fast path) and one with APPLY_GAMMA=true.
@@ -17,7 +23,7 @@ override APPLY_GAMMA: bool = true;
17
23
  @group(0) @binding(0) var hdrTex: texture_2d<f32>;
18
24
  @group(0) @binding(1) var bloomTex: texture_2d<f32>; // bloomUpTexture mip 0 (full pyramid top)
19
25
  @group(0) @binding(2) var bloomSamp: sampler;
20
- @group(0) @binding(3) var<uniform> viewU: array<vec4<f32>, 10>;
26
+ @group(0) @binding(3) var<uniform> viewU: array<vec4<f32>, 11>;
21
27
  // Aux mask/alpha texture. .r = bloom mask (unused here; bloom blit uses it).
22
28
  // .g = accumulated canvas alpha (what hdr.a carried before the HDR format
23
29
  // became rg11b10ufloat). We unpremultiply HDR by this alpha for tonemap, then
@@ -36,19 +42,23 @@ override APPLY_GAMMA: bool = true;
36
42
  // viewU[2] = (background.rgb, mode) — display-space sRGB, composited UNDER the
37
43
  // scene post-tonemap. BASE-layer mode: 0 transparent (DOM shows),
38
44
  // 1 solid color, 2 = 360 equirect skybox sampled by view ray. A user
39
- // WGSL effect is a separate LAYER over the base (viewU[6].y flag).
45
+ // WGSL effect is a separate LAYER over the base — no mode of its own,
46
+ // and no on/off uniform either: the pipeline is REBUILT per effect, so
47
+ // the compiled variant IS the flag.
40
48
  // viewU[3] = (camera right, tanHalfFov·aspect); viewU[4] = (camera up, tanHalfFov);
41
49
  // viewU[5] = (camera forward, _) — refreshed per frame while skybox/effect active.
42
- // viewU[6] = (time seconds, effect on/off, canvas width, canvas height).
50
+ // viewU[6] = (time seconds, view transform id, canvas width, canvas height).
43
51
  // viewU[7] = (grade offset.rgb, contrast); viewU[8] = (grade power.rgb, saturation);
44
52
  // viewU[9] = (grade slope.rgb, grade on/off) — see grade() below.
53
+ // viewU[10] = (camera world position, _) — refreshed with the basis above.
45
54
  // invGamma = 1/gamma precomputed on CPU — avoids a per-pixel divide.
46
55
  @group(0) @binding(6) var bgEquirect: texture_2d<f32>;
47
56
  // The scene pass's own MSAA depth buffer, bound depth-only. NOT an extra
48
- // render target: when depth of field is off the scene pass discards depth
49
- // (TBDR tile memory never spills) and this binding is never read — the whole
50
- // feature costs one uniform branch. When on, the pass stores depth and the
51
- // gather below reads sample 0.
57
+ // render target: with neither depth of field nor a foreground effect active the
58
+ // scene pass discards depth (TBDR tile memory never spills) and this binding is
59
+ // never read. Either feature makes the pass store it instead, and both read
60
+ // sample 0 — the DoF gather, and linearDepth() for the depth handed to
61
+ // foreground().
52
62
  @group(0) @binding(8) var depthTex: texture_depth_multisampled_2d;
53
63
  // dofU[0] = (enabled, focusDistance, focusRange, aperture)
54
64
  // dofU[1] = (maxBlurRadiusPx, bladeCount, sampleCount, anamorphicRatio)
@@ -57,6 +67,9 @@ override APPLY_GAMMA: bool = true;
57
67
  // track the camera radius. Cleared depth (1.0) inverts to the far
58
68
  // plane, so empty sky reads as maximally defocused background.
59
69
  @group(0) @binding(9) var<uniform> dofU: array<vec4<f32>, 3>;
70
+ // Blender's AgX, as the 57³ lookup it ships as rather than a reconstruction of
71
+ // it. Sampled in the log-encoded E-Gamut space the cube expects — see agxTransform.
72
+ @group(0) @binding(10) var agxLut: texture_3d<f32>;
60
73
 
61
74
  // Must match FILMIC_LUT_WIDTH in engine.ts (bakeFilmicLut).
62
75
  const FILMIC_LUT_W: f32 = 256.0;
@@ -100,9 +113,78 @@ fn filmic(x: f32) -> f32 {
100
113
  return textureSampleLevel(filmicLut, bloomSamp, vec2f(u, 0.5), 0.0).r;
101
114
  }
102
115
 
103
- /** Canvas size in pixels — for user background effects (aspect correction). */
116
+ /** The sRGB display encoding — Blender's "Standard" view transform, which is what
117
+ * NPR work uses: no film curve at all, so the colours a graph computes are the
118
+ * colours that land. Not a 2.2 power law; sRGB has a linear toe. */
119
+ fn srgb_encode(x: f32) -> f32 {
120
+ let c = max(x, 0.0);
121
+ return select(1.055 * pow(c, 1.0 / 2.4) - 0.055, c * 12.92, c <= 0.0031308);
122
+ }
123
+
124
+ /**
125
+ * AgX, the chain config.ocio states for "AgX Base Rec.1886" on an sRGB display:
126
+ *
127
+ * scene-linear Rec.709 → 3x3 into FilmLight E-Gamut
128
+ * → log2 across 25 stops, [-12.47393, +12.5261] → 0..1
129
+ * → the 57³ cube
130
+ * → Rec.1886 (pure γ2.4) decoded, then re-encoded as sRGB for the swapchain
131
+ *
132
+ * The last step is not pedantry: Rec.1886 is a pure power law and sRGB has a
133
+ * linear toe, so skipping it lifts the darkest few code values.
134
+ *
135
+ * Blender interpolates the cube tetrahedrally and a GPU sampler is trilinear.
136
+ * The difference is small and confined to saturated gradients; if it ever shows,
137
+ * tetrahedral is a dozen lines here.
138
+ */
139
+ fn agxTransform(c: vec3f) -> vec3f {
140
+ let m = mat3x3<f32>(
141
+ vec3f(0.55937113, 0.07622071, 0.06552670),
142
+ vec3f(0.30478326, 0.78797183, 0.16454675),
143
+ vec3f(0.13584556, 0.13580748, 0.76992653),
144
+ );
145
+ let e = m * max(c, vec3f(0.0));
146
+ // The cube's domain: an unlit pixel would take log2 to -inf, so floor it at
147
+ // the bottom stop rather than sampling outside the LUT.
148
+ let t = clamp((log2(max(e, vec3f(1e-10))) + vec3f(12.47393)) / 25.0, vec3f(0.0), vec3f(1.0));
149
+ // Half-texel inset, so the end stops are the cube's own values and not a blend
150
+ // with the clamp-to-edge border.
151
+ let n = 57.0;
152
+ let uvw = t * ((n - 1.0) / n) + (0.5 / n);
153
+ let formed = textureSampleLevel(agxLut, bloomSamp, uvw, 0.0).rgb;
154
+ let linear = pow(max(formed, vec3f(0.0)), vec3f(2.4));
155
+ return vec3f(srgb_encode(linear.r), srgb_encode(linear.g), srgb_encode(linear.b));
156
+ }
157
+
158
+ /** Which display transform, chosen per frame at viewU[6].y (0 filmic, 1 standard, 2 agx).
159
+ * A uniform branch rather than a pipeline variant: switching is rare, and both
160
+ * arms are cheap enough that specialising the pipeline would buy nothing. */
161
+ fn viewTransform(c: vec3f) -> vec3f {
162
+ let mode = viewU[6].y;
163
+ if (mode > 1.5) { return agxTransform(c); }
164
+ if (mode > 0.5) { return vec3f(srgb_encode(c.r), srgb_encode(c.g), srgb_encode(c.b)); }
165
+ return vec3f(filmic(c.r), filmic(c.g), filmic(c.b));
166
+ }
167
+
168
+ /** Canvas size in pixels — for user effects (aspect correction). */
104
169
  fn bgResolution() -> vec2f { return viewU[6].zw; }
105
170
 
171
+ /** The camera's world position. */
172
+ fn bgCameraPos() -> vec3f { return viewU[10].xyz; }
173
+
174
+ /** Where in the WORLD the scene drew this pixel — the depth handed to
175
+ * foreground() turned into a place. Without it an effect can only think in
176
+ * distances from the lens, which is no use to anything that belongs somewhere:
177
+ * fog lying on the ground has to know where the ground is.
178
+ *
179
+ * depth measures along the VIEW AXIS, not along the ray, so it is divided by
180
+ * the ray's projection onto camera-forward before being walked out. At the far
181
+ * plane (nothing drawn) this lands a very long way off, which is what a sky
182
+ * should do to anything reading it. */
183
+ fn bgWorldPos(ray: vec3f, depth: f32) -> vec3f {
184
+ let axis = max(dot(normalize(ray), viewU[5].xyz), 1e-4);
185
+ return bgCameraPos() + normalize(ray) * (depth / axis);
186
+ }
187
+
106
188
  /** Color grading, applied to the tonemapped SCENE (not the background — see the
107
189
  * call site). The core is ASC CDL, the film-industry interchange standard:
108
190
  *
@@ -195,8 +277,7 @@ const COMPOSITE_BODY = /* wgsl */ `
195
277
  let sceneStraight = scenePm.rgb / max(sceneAlpha, 1e-6);
196
278
 
197
279
  let exposed = sceneStraight * exp2(viewU[0].x);
198
- let tm = vec3f(filmic(exposed.r), filmic(exposed.g), filmic(exposed.b));
199
- var disp = max(tm, vec3f(0.0));
280
+ var disp = max(viewTransform(exposed), vec3f(0.0));
200
281
  // Grade the SCENE only, before the display gamma. Deliberately not applied to
201
282
  // the background: it keeps a picked background color exactly as picked, and —
202
283
  // load-bearing — leaves green-screen mode's key color unshifted so chroma
@@ -213,66 +294,92 @@ const COMPOSITE_BODY = /* wgsl */ `
213
294
  let bg = viewU[2];
214
295
  var bgA = select(0.0, 1.0, bg.w > 0.5);
215
296
  var bgPm = bg.rgb * bgA; // premultiplied accumulator
216
- let fxOn = viewU[6].y > 0.5;
217
- if ((bg.w > 1.5 || fxOn) COVERAGE_GATE) {
218
- // The equirect and any effect both need this pixel's world-space view ray,
219
- // rebuilt from the camera basis. The dome sits at infinity (no parallax) —
220
- // PhotoDome-style, display-only.
221
- let ndc = vec2f(fragCoord.x / fullSz.x * 2.0 - 1.0, 1.0 - fragCoord.y / fullSz.y * 2.0);
222
- let dir = normalize(viewU[5].xyz + ndc.x * viewU[3].w * viewU[3].xyz + ndc.y * viewU[4].w * viewU[4].xyz);
297
+ // This pixel's world-space view ray, rebuilt from the camera basis — what the
298
+ // equirect samples by, and what both effect mounts navigate by. The dome sits
299
+ // at infinity (no parallax): PhotoDome-style, display-only. Hoisted out of the
300
+ // branch below because the foreground mount is past the end of it; it is pure
301
+ // arithmetic on uniforms, which every backend sinks into whatever reads it.
302
+ let ndc = vec2f(fragCoord.x / fullSz.x * 2.0 - 1.0, 1.0 - fragCoord.y / fullSz.y * 2.0);
303
+ let dir = normalize(viewU[5].xyz + ndc.x * viewU[3].w * viewU[3].xyz + ndc.y * viewU[4].w * viewU[4].xyz);
304
+ if (BACKGROUND_COND) {
223
305
  if (bg.w > 1.5) {
224
306
  // LH world (+Z forward): longitude = atan2(x, z), Babylon-PhotoDome convention.
225
307
  let su = 0.5 + atan2(dir.x, dir.z) * 0.15915494309; // 1/(2π)
226
308
  let sv = 0.5 - asin(clamp(dir.y, -1.0, 1.0)) * 0.31830988618; // 1/π
227
309
  bgPm = textureSampleLevel(bgEquirect, bloomSamp, vec2f(su, sv), 0.0).rgb;
228
310
  }
229
- BG_EFFECT_CALL
311
+ BACKGROUND_CALL
230
312
  }
231
- return vec4f(disp * sceneAlpha + bgPm * (1.0 - sceneAlpha), sceneAlpha + bgA * (1.0 - sceneAlpha));
313
+ // The frame, premultiplied: scene over background. A var, not the return
314
+ // expression, because the foreground mount composites onto it.
315
+ var outRgb = disp * sceneAlpha + bgPm * (1.0 - sceneAlpha);
316
+ var outA = sceneAlpha + bgA * (1.0 - sceneAlpha);
317
+ FOREGROUND_CALL
318
+ return vec4f(outRgb, outA);
232
319
  }
233
320
  `;
234
- // Base variant: no effect installed, the flag is never set — the ray block only
235
- // runs for the equirect, and there is nothing to add. (`dir` may go unused when
236
- // this compiles with mode<2 shaders; WGSL is fine with an unused let.)
237
- const NO_EFFECT_CALL = `_ = dir;`;
238
321
  // uv flipped to bottom-left origin (shadertoy convention); clamped so a stray
239
322
  // effect can't push negatives/NaN into the premultiplied composite. Standard
240
- // OVER onto the base layer.
241
- const EFFECT_CALL = /* wgsl */ `
242
- if (fxOn) {
243
- let bgUv = vec2f(fragCoord.x / fullSz.x, 1.0 - fragCoord.y / fullSz.y);
244
- let fx = clamp(background(dir, bgUv, viewU[6].x), vec4f(0.0), vec4f(1.0));
245
- bgPm = fx.rgb * fx.a + bgPm * (1.0 - fx.a);
246
- bgA = fx.a + bgA * (1.0 - fx.a);
247
- }
323
+ // OVER onto the base layer. No `if` around it: the pipeline is rebuilt per
324
+ // effect, so this text only exists in variants whose WGSL defines background().
325
+ const BACKGROUND_CALL = /* wgsl */ `
326
+ let bgUv = vec2f(fragCoord.x / fullSz.x, 1.0 - fragCoord.y / fullSz.y);
327
+ let bgFx = clamp(background(dir, bgUv, viewU[6].x), vec4f(0.0), vec4f(1.0));
328
+ bgPm = bgFx.rgb * bgFx.a + bgPm * (1.0 - bgFx.a);
329
+ bgA = bgFx.a + bgA * (1.0 - bgFx.a);
330
+ `;
331
+ // Same OVER, one layer later — onto the finished frame rather than onto the
332
+ // base. Ungated by design: a foreground runs at every pixel, including the ones
333
+ // the model covers, because covering them is the point.
334
+ const FOREGROUND_CALL = /* wgsl */ `
335
+ let fgUv = vec2f(fragCoord.x / fullSz.x, 1.0 - fragCoord.y / fullSz.y);
336
+ // The scene's own depth, so the effect can tell what is in front of it: a
337
+ // petal compares its distance against this and lets the model take the pixel,
338
+ // and fog's alpha is nothing but a function of it. Pixels the scene never drew
339
+ // read the far plane, so distance fog closes over the backdrop too.
340
+ let fgFx = clamp(foreground(dir, fgUv, viewU[6].x, linearDepth(coord)), vec4f(0.0), vec4f(1.0));
341
+ outRgb = fgFx.rgb * fgFx.a + outRgb * (1.0 - fgFx.a);
342
+ outA = fgFx.a + outA * (1.0 - fgFx.a);
248
343
  `;
249
344
  // Derivative builtins are illegal in non-uniform control flow (WGSL uniformity
250
345
  // analysis rejects the pipeline), so the coverage gate below can only wrap
251
346
  // effect code that doesn't use them. Checked textually at build time.
252
347
  const USES_DERIVATIVES = /\b(?:fwidth|dpdx|dpdy)(?:Fine|Coarse)?\s*\(/;
253
- /** Skip the whole background block (equirect sample + effect) behind pixels the
254
- * model fully covers — the composite multiplies the result by (1 - alpha) = 0
255
- * there anyway, and on a full-screen effect that's a third or more of the frame
256
- * (the cost Safari feels most). The equirect uses explicit-LOD sampling, which
257
- * is always legal in non-uniform flow; only derivative-using effects must keep
258
- * uniform control flow and forgo the gate. */
259
- function coverageGate(effect) {
260
- const gated = !effect || !USES_DERIVATIVES.test(effect.wgsl);
348
+ /** The condition on the background block (equirect sample + background effect).
349
+ *
350
+ * Two jobs. It skips the block behind pixels the model fully covers — the
351
+ * composite multiplies the result by (1 - alpha) = 0 there anyway, and on a
352
+ * full-screen effect that's a third or more of the frame (the cost Safari feels
353
+ * most). And with no background effect compiled in, it also skips the block
354
+ * entirely unless the equirect needs it.
355
+ *
356
+ * The equirect uses explicit-LOD sampling, which is always legal in non-uniform
357
+ * flow; only derivative-using effects must keep uniform control flow and forgo
358
+ * the coverage half. The test is textual over the whole file, so a foreground
359
+ * that uses fwidth costs the background its gate — conservative, and only ever
360
+ * in the direction of correctness. (The foreground mount itself sits in uniform
361
+ * flow, so derivatives are always legal there.) */
362
+ function backgroundCondition(effect) {
261
363
  // sceneAlpha, not alpha: the bokeh gather spreads coverage, so a pixel the
262
364
  // sharp scene fully covered can end up needing background behind its blur.
263
- return gated ? "&& sceneAlpha < 0.999" : "";
365
+ const coverage = "sceneAlpha < 0.999";
366
+ if (!effect?.hasBackground)
367
+ return `bg.w > 1.5 && ${coverage}`;
368
+ return USES_DERIVATIVES.test(effect.wgsl) ? "true" : coverage;
264
369
  }
265
370
  export function buildCompositeShader(effect) {
371
+ const body = COMPOSITE_BODY.replace("BACKGROUND_COND", backgroundCondition(effect))
372
+ .replace("BACKGROUND_CALL", effect?.hasBackground ? BACKGROUND_CALL.trim() : "")
373
+ .replace("FOREGROUND_CALL", effect?.hasForeground ? FOREGROUND_CALL.trim() : "");
266
374
  if (!effect)
267
- return (COMPOSITE_HEAD +
268
- COMPOSITE_BODY.replace("BG_EFFECT_CALL", NO_EFFECT_CALL).replace("COVERAGE_GATE", coverageGate(null)));
375
+ return COMPOSITE_HEAD + body;
269
376
  return (COMPOSITE_HEAD +
270
- "\n// ── user background effect (setBackgroundEffect) ──\n" +
377
+ "\n// ── user effect (setEffect) ──\n" +
271
378
  effect.paramsDecl +
272
379
  "\n" +
273
380
  effect.wgsl +
274
381
  "\n" +
275
- COMPOSITE_BODY.replace("BG_EFFECT_CALL", EFFECT_CALL.trim()).replace("COVERAGE_GATE", coverageGate(effect)));
382
+ body);
276
383
  }
277
384
  /** Kept for compatibility with existing imports (the base, no-effect shader). */
278
385
  export const COMPOSITE_SHADER_WGSL = buildCompositeShader(null);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "reze-engine",
3
- "version": "0.40.0",
3
+ "version": "0.41.1",
4
4
  "description": "A lightweight WebGPU engine for real-time 3D MMD/PMX model rendering",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",