reze-engine 0.40.0 → 0.41.0
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.
- package/README.md +8 -5
- package/dist/engine.d.ts +56 -30
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +124 -63
- package/dist/graph/registry.d.ts.map +1 -1
- package/dist/graph/registry.js +10 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/shaders/materials/nodes.d.ts +1 -1
- package/dist/shaders/materials/nodes.d.ts.map +1 -1
- package/dist/shaders/materials/nodes.js +21 -0
- package/dist/shaders/passes/composite.d.ts +27 -12
- package/dist/shaders/passes/composite.d.ts.map +1 -1
- package/dist/shaders/passes/composite.js +99 -46
- package/package.json +1 -1
- package/src/engine.ts +140 -71
- package/src/graph/registry.ts +11 -0
- package/src/index.ts +2 -2
- package/src/shaders/materials/nodes.ts +21 -0
- package/src/shaders/passes/composite.ts +126 -62
|
@@ -2,30 +2,51 @@
|
|
|
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
|
|
6
|
-
//
|
|
7
|
-
//
|
|
8
|
-
//
|
|
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
|
|
|
10
|
-
/** What
|
|
16
|
+
/** What user effect WGSL may define, documented once. A file declares its own
|
|
17
|
+
* mounts by which of these it defines — defining both is how one file is one
|
|
18
|
+
* weather system (dark sky behind, rain in front):
|
|
11
19
|
*
|
|
12
20
|
* fn background(ray: vec3f, uv: vec2f, time: f32) -> vec4f
|
|
21
|
+
* fn foreground(ray: vec3f, uv: vec2f, time: f32, depth: f32) -> vec4f
|
|
13
22
|
*
|
|
14
|
-
* - `ray`
|
|
15
|
-
*
|
|
16
|
-
* - `uv`
|
|
17
|
-
* - `time`
|
|
23
|
+
* - `ray` — normalized world-space view direction of this pixel (left-handed,
|
|
24
|
+
* +Z forward; identical to what the 360 skybox samples by).
|
|
25
|
+
* - `uv` — 0..1 across the canvas, origin bottom-left (shadertoy-style).
|
|
26
|
+
* - `time` — seconds since the effect was applied.
|
|
27
|
+
* - `depth` — FOREGROUND ONLY. Camera-space distance in metres of whatever the
|
|
28
|
+
* scene drew at this pixel, the far plane where it drew nothing.
|
|
29
|
+
* Compare a particle's own distance against it and the model
|
|
30
|
+
* occludes it; fog needs no comparison at all, its alpha simply IS
|
|
31
|
+
* a function of distance.
|
|
18
32
|
* - `bgResolution()` — canvas size in pixels, for aspect correction.
|
|
19
|
-
* - declared params arrive as `params.<name>` (f32 or vec3f).
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
33
|
+
* - declared params arrive as `params.<name>` (f32 or vec3f), shared by both.
|
|
34
|
+
*
|
|
35
|
+
* Return display-space sRGB + alpha, 0..1. Both mounts are alpha-composited
|
|
36
|
+
* LAYERS, so alpha is what decides how much they replace: a background effect
|
|
37
|
+
* at alpha 1 covers the base (solid color / 360 equirect / transparent) and at
|
|
38
|
+
* 0 lets it through, which is how a starfield is stars over the user's color;
|
|
39
|
+
* a foreground at alpha 1 covers the frame. No mode flag anywhere — the alpha
|
|
40
|
+
* channel already says it. */
|
|
24
41
|
export type CompositeEffectSource = {
|
|
25
|
-
/**
|
|
42
|
+
/** The user's WGSL verbatim: helpers plus whichever entry points it defines. */
|
|
26
43
|
wgsl: string
|
|
27
|
-
/** Codegen'd `struct
|
|
44
|
+
/** Codegen'd `struct EffectParams {...}` + binding decl; empty when no params. */
|
|
28
45
|
paramsDecl: string
|
|
46
|
+
/** Defines `fn background(...)` — mount under the scene. */
|
|
47
|
+
hasBackground: boolean
|
|
48
|
+
/** Defines `fn foreground(...)` — mount over the finished frame. */
|
|
49
|
+
hasForeground: boolean
|
|
29
50
|
}
|
|
30
51
|
|
|
31
52
|
const COMPOSITE_HEAD = /* wgsl */ `
|
|
@@ -39,7 +60,7 @@ override APPLY_GAMMA: bool = true;
|
|
|
39
60
|
@group(0) @binding(0) var hdrTex: texture_2d<f32>;
|
|
40
61
|
@group(0) @binding(1) var bloomTex: texture_2d<f32>; // bloomUpTexture mip 0 (full pyramid top)
|
|
41
62
|
@group(0) @binding(2) var bloomSamp: sampler;
|
|
42
|
-
@group(0) @binding(3) var<uniform> viewU: array<vec4<f32>,
|
|
63
|
+
@group(0) @binding(3) var<uniform> viewU: array<vec4<f32>, 11>;
|
|
43
64
|
// Aux mask/alpha texture. .r = bloom mask (unused here; bloom blit uses it).
|
|
44
65
|
// .g = accumulated canvas alpha (what hdr.a carried before the HDR format
|
|
45
66
|
// became rg11b10ufloat). We unpremultiply HDR by this alpha for tonemap, then
|
|
@@ -58,19 +79,23 @@ override APPLY_GAMMA: bool = true;
|
|
|
58
79
|
// viewU[2] = (background.rgb, mode) — display-space sRGB, composited UNDER the
|
|
59
80
|
// scene post-tonemap. BASE-layer mode: 0 transparent (DOM shows),
|
|
60
81
|
// 1 solid color, 2 = 360 equirect skybox sampled by view ray. A user
|
|
61
|
-
// WGSL effect is a separate LAYER over the base
|
|
82
|
+
// WGSL effect is a separate LAYER over the base — no mode of its own,
|
|
83
|
+
// and no on/off uniform either: the pipeline is REBUILT per effect, so
|
|
84
|
+
// the compiled variant IS the flag.
|
|
62
85
|
// viewU[3] = (camera right, tanHalfFov·aspect); viewU[4] = (camera up, tanHalfFov);
|
|
63
86
|
// viewU[5] = (camera forward, _) — refreshed per frame while skybox/effect active.
|
|
64
|
-
// viewU[6] = (time seconds,
|
|
87
|
+
// viewU[6] = (time seconds, _, canvas width, canvas height).
|
|
65
88
|
// viewU[7] = (grade offset.rgb, contrast); viewU[8] = (grade power.rgb, saturation);
|
|
66
89
|
// viewU[9] = (grade slope.rgb, grade on/off) — see grade() below.
|
|
90
|
+
// viewU[10] = (camera world position, _) — refreshed with the basis above.
|
|
67
91
|
// invGamma = 1/gamma precomputed on CPU — avoids a per-pixel divide.
|
|
68
92
|
@group(0) @binding(6) var bgEquirect: texture_2d<f32>;
|
|
69
93
|
// The scene pass's own MSAA depth buffer, bound depth-only. NOT an extra
|
|
70
|
-
// render target:
|
|
71
|
-
// (TBDR tile memory never spills) and this binding is
|
|
72
|
-
//
|
|
73
|
-
// gather
|
|
94
|
+
// render target: with neither depth of field nor a foreground effect active the
|
|
95
|
+
// scene pass discards depth (TBDR tile memory never spills) and this binding is
|
|
96
|
+
// never read. Either feature makes the pass store it instead, and both read
|
|
97
|
+
// sample 0 — the DoF gather, and linearDepth() for the depth handed to
|
|
98
|
+
// foreground().
|
|
74
99
|
@group(0) @binding(8) var depthTex: texture_depth_multisampled_2d;
|
|
75
100
|
// dofU[0] = (enabled, focusDistance, focusRange, aperture)
|
|
76
101
|
// dofU[1] = (maxBlurRadiusPx, bladeCount, sampleCount, anamorphicRatio)
|
|
@@ -122,9 +147,26 @@ fn filmic(x: f32) -> f32 {
|
|
|
122
147
|
return textureSampleLevel(filmicLut, bloomSamp, vec2f(u, 0.5), 0.0).r;
|
|
123
148
|
}
|
|
124
149
|
|
|
125
|
-
/** Canvas size in pixels — for user
|
|
150
|
+
/** Canvas size in pixels — for user effects (aspect correction). */
|
|
126
151
|
fn bgResolution() -> vec2f { return viewU[6].zw; }
|
|
127
152
|
|
|
153
|
+
/** The camera's world position. */
|
|
154
|
+
fn bgCameraPos() -> vec3f { return viewU[10].xyz; }
|
|
155
|
+
|
|
156
|
+
/** Where in the WORLD the scene drew this pixel — the depth handed to
|
|
157
|
+
* foreground() turned into a place. Without it an effect can only think in
|
|
158
|
+
* distances from the lens, which is no use to anything that belongs somewhere:
|
|
159
|
+
* fog lying on the ground has to know where the ground is.
|
|
160
|
+
*
|
|
161
|
+
* depth measures along the VIEW AXIS, not along the ray, so it is divided by
|
|
162
|
+
* the ray's projection onto camera-forward before being walked out. At the far
|
|
163
|
+
* plane (nothing drawn) this lands a very long way off, which is what a sky
|
|
164
|
+
* should do to anything reading it. */
|
|
165
|
+
fn bgWorldPos(ray: vec3f, depth: f32) -> vec3f {
|
|
166
|
+
let axis = max(dot(normalize(ray), viewU[5].xyz), 1e-4);
|
|
167
|
+
return bgCameraPos() + normalize(ray) * (depth / axis);
|
|
168
|
+
}
|
|
169
|
+
|
|
128
170
|
/** Color grading, applied to the tonemapped SCENE (not the background — see the
|
|
129
171
|
* call site). The core is ASC CDL, the film-industry interchange standard:
|
|
130
172
|
*
|
|
@@ -236,40 +278,54 @@ const COMPOSITE_BODY = /* wgsl */ `
|
|
|
236
278
|
let bg = viewU[2];
|
|
237
279
|
var bgA = select(0.0, 1.0, bg.w > 0.5);
|
|
238
280
|
var bgPm = bg.rgb * bgA; // premultiplied accumulator
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
281
|
+
// This pixel's world-space view ray, rebuilt from the camera basis — what the
|
|
282
|
+
// equirect samples by, and what both effect mounts navigate by. The dome sits
|
|
283
|
+
// at infinity (no parallax): PhotoDome-style, display-only. Hoisted out of the
|
|
284
|
+
// branch below because the foreground mount is past the end of it; it is pure
|
|
285
|
+
// arithmetic on uniforms, which every backend sinks into whatever reads it.
|
|
286
|
+
let ndc = vec2f(fragCoord.x / fullSz.x * 2.0 - 1.0, 1.0 - fragCoord.y / fullSz.y * 2.0);
|
|
287
|
+
let dir = normalize(viewU[5].xyz + ndc.x * viewU[3].w * viewU[3].xyz + ndc.y * viewU[4].w * viewU[4].xyz);
|
|
288
|
+
if (BACKGROUND_COND) {
|
|
246
289
|
if (bg.w > 1.5) {
|
|
247
290
|
// LH world (+Z forward): longitude = atan2(x, z), Babylon-PhotoDome convention.
|
|
248
291
|
let su = 0.5 + atan2(dir.x, dir.z) * 0.15915494309; // 1/(2π)
|
|
249
292
|
let sv = 0.5 - asin(clamp(dir.y, -1.0, 1.0)) * 0.31830988618; // 1/π
|
|
250
293
|
bgPm = textureSampleLevel(bgEquirect, bloomSamp, vec2f(su, sv), 0.0).rgb;
|
|
251
294
|
}
|
|
252
|
-
|
|
295
|
+
BACKGROUND_CALL
|
|
253
296
|
}
|
|
254
|
-
|
|
297
|
+
// The frame, premultiplied: scene over background. A var, not the return
|
|
298
|
+
// expression, because the foreground mount composites onto it.
|
|
299
|
+
var outRgb = disp * sceneAlpha + bgPm * (1.0 - sceneAlpha);
|
|
300
|
+
var outA = sceneAlpha + bgA * (1.0 - sceneAlpha);
|
|
301
|
+
FOREGROUND_CALL
|
|
302
|
+
return vec4f(outRgb, outA);
|
|
255
303
|
}
|
|
256
304
|
`
|
|
257
305
|
|
|
258
|
-
// Base variant: no effect installed, the flag is never set — the ray block only
|
|
259
|
-
// runs for the equirect, and there is nothing to add. (`dir` may go unused when
|
|
260
|
-
// this compiles with mode<2 shaders; WGSL is fine with an unused let.)
|
|
261
|
-
const NO_EFFECT_CALL = `_ = dir;`
|
|
262
|
-
|
|
263
306
|
// uv flipped to bottom-left origin (shadertoy convention); clamped so a stray
|
|
264
307
|
// effect can't push negatives/NaN into the premultiplied composite. Standard
|
|
265
|
-
// OVER onto the base layer.
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
308
|
+
// OVER onto the base layer. No `if` around it: the pipeline is rebuilt per
|
|
309
|
+
// effect, so this text only exists in variants whose WGSL defines background().
|
|
310
|
+
const BACKGROUND_CALL = /* wgsl */ `
|
|
311
|
+
let bgUv = vec2f(fragCoord.x / fullSz.x, 1.0 - fragCoord.y / fullSz.y);
|
|
312
|
+
let bgFx = clamp(background(dir, bgUv, viewU[6].x), vec4f(0.0), vec4f(1.0));
|
|
313
|
+
bgPm = bgFx.rgb * bgFx.a + bgPm * (1.0 - bgFx.a);
|
|
314
|
+
bgA = bgFx.a + bgA * (1.0 - bgFx.a);
|
|
315
|
+
`
|
|
316
|
+
|
|
317
|
+
// Same OVER, one layer later — onto the finished frame rather than onto the
|
|
318
|
+
// base. Ungated by design: a foreground runs at every pixel, including the ones
|
|
319
|
+
// the model covers, because covering them is the point.
|
|
320
|
+
const FOREGROUND_CALL = /* wgsl */ `
|
|
321
|
+
let fgUv = vec2f(fragCoord.x / fullSz.x, 1.0 - fragCoord.y / fullSz.y);
|
|
322
|
+
// The scene's own depth, so the effect can tell what is in front of it: a
|
|
323
|
+
// petal compares its distance against this and lets the model take the pixel,
|
|
324
|
+
// and fog's alpha is nothing but a function of it. Pixels the scene never drew
|
|
325
|
+
// read the far plane, so distance fog closes over the backdrop too.
|
|
326
|
+
let fgFx = clamp(foreground(dir, fgUv, viewU[6].x, linearDepth(coord)), vec4f(0.0), vec4f(1.0));
|
|
327
|
+
outRgb = fgFx.rgb * fgFx.a + outRgb * (1.0 - fgFx.a);
|
|
328
|
+
outA = fgFx.a + outA * (1.0 - fgFx.a);
|
|
273
329
|
`
|
|
274
330
|
|
|
275
331
|
// Derivative builtins are illegal in non-uniform control flow (WGSL uniformity
|
|
@@ -277,33 +333,41 @@ const EFFECT_CALL = /* wgsl */ `
|
|
|
277
333
|
// effect code that doesn't use them. Checked textually at build time.
|
|
278
334
|
const USES_DERIVATIVES = /\b(?:fwidth|dpdx|dpdy)(?:Fine|Coarse)?\s*\(/
|
|
279
335
|
|
|
280
|
-
/**
|
|
281
|
-
*
|
|
282
|
-
*
|
|
283
|
-
*
|
|
284
|
-
*
|
|
285
|
-
*
|
|
286
|
-
|
|
287
|
-
|
|
336
|
+
/** The condition on the background block (equirect sample + background effect).
|
|
337
|
+
*
|
|
338
|
+
* Two jobs. It skips the block behind pixels the model fully covers — the
|
|
339
|
+
* composite multiplies the result by (1 - alpha) = 0 there anyway, and on a
|
|
340
|
+
* full-screen effect that's a third or more of the frame (the cost Safari feels
|
|
341
|
+
* most). And with no background effect compiled in, it also skips the block
|
|
342
|
+
* entirely unless the equirect needs it.
|
|
343
|
+
*
|
|
344
|
+
* The equirect uses explicit-LOD sampling, which is always legal in non-uniform
|
|
345
|
+
* flow; only derivative-using effects must keep uniform control flow and forgo
|
|
346
|
+
* the coverage half. The test is textual over the whole file, so a foreground
|
|
347
|
+
* that uses fwidth costs the background its gate — conservative, and only ever
|
|
348
|
+
* in the direction of correctness. (The foreground mount itself sits in uniform
|
|
349
|
+
* flow, so derivatives are always legal there.) */
|
|
350
|
+
function backgroundCondition(effect?: CompositeEffectSource | null): string {
|
|
288
351
|
// sceneAlpha, not alpha: the bokeh gather spreads coverage, so a pixel the
|
|
289
352
|
// sharp scene fully covered can end up needing background behind its blur.
|
|
290
|
-
|
|
353
|
+
const coverage = "sceneAlpha < 0.999"
|
|
354
|
+
if (!effect?.hasBackground) return `bg.w > 1.5 && ${coverage}`
|
|
355
|
+
return USES_DERIVATIVES.test(effect.wgsl) ? "true" : coverage
|
|
291
356
|
}
|
|
292
357
|
|
|
293
358
|
export function buildCompositeShader(effect?: CompositeEffectSource | null): string {
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
)
|
|
359
|
+
const body = COMPOSITE_BODY.replace("BACKGROUND_COND", backgroundCondition(effect))
|
|
360
|
+
.replace("BACKGROUND_CALL", effect?.hasBackground ? BACKGROUND_CALL.trim() : "")
|
|
361
|
+
.replace("FOREGROUND_CALL", effect?.hasForeground ? FOREGROUND_CALL.trim() : "")
|
|
362
|
+
if (!effect) return COMPOSITE_HEAD + body
|
|
299
363
|
return (
|
|
300
364
|
COMPOSITE_HEAD +
|
|
301
|
-
"\n// ── user
|
|
365
|
+
"\n// ── user effect (setEffect) ──\n" +
|
|
302
366
|
effect.paramsDecl +
|
|
303
367
|
"\n" +
|
|
304
368
|
effect.wgsl +
|
|
305
369
|
"\n" +
|
|
306
|
-
|
|
370
|
+
body
|
|
307
371
|
)
|
|
308
372
|
}
|
|
309
373
|
|