reze-engine 0.41.4 → 0.42.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.
@@ -29,7 +29,15 @@
29
29
  * Compare a particle's own distance against it and the model
30
30
  * occludes it; fog needs no comparison at all, its alpha simply IS
31
31
  * a function of distance.
32
- * - `bgResolution()` — canvas size in pixels, for aspect correction.
32
+ * - `rzResolution()` — canvas size in pixels, for aspect correction.
33
+ * - `rzCameraPos()`, `rzWorldPos(ray, depth)` — the lens, and the place a pixel
34
+ * was drawn.
35
+ * - `rzSubjectCount()`, `rzSubjectHip(i)` — the cast, at HIP height (see the
36
+ * function; it is not the floor, and reading it as the floor is a
37
+ * mistake this API's own comment used to invite).
38
+ * - `rzProject(p)` — a world point as uv + view-axis distance; the cheap way to
39
+ * anchor anything, and `z` compares directly against `depth`.
40
+ * - the bg* spellings of all of the above still resolve, permanently.
33
41
  * - declared params arrive as `params.<name>` (f32 or vec3f), shared by both.
34
42
  *
35
43
  * Return display-space sRGB + alpha, 0..1. Both mounts are alpha-composited
@@ -38,6 +46,46 @@
38
46
  * 0 lets it through, which is how a starfield is stars over the user's color;
39
47
  * a foreground at alpha 1 covers the frame. No mode flag anywhere — the alpha
40
48
  * channel already says it. */
49
+ /**
50
+ * The bones an effect asked for, in declaration order — the slots rzAnchor reads.
51
+ *
52
+ * // @anchor 左手首 trail
53
+ * // @anchor 頭
54
+ *
55
+ * A declaration in the source, like the mounts: what a file names is what gets
56
+ * resolved and uploaded, so naming none costs nothing and nobody pays for a
57
+ * rig's other five hundred bones. Anchored to the start of a line so that
58
+ * writing the word @anchor in ordinary prose does not silently add a slot —
59
+ * which would shift every slot after it.
60
+ *
61
+ * `trail` additionally keeps that bone's recent PATH, for rzTrail. Opt-in
62
+ * because a path is two orders of magnitude more data than a point, and most
63
+ * anchors want a point.
64
+ *
65
+ * Names are passed through verbatim: any bone the rig has works, and one it does
66
+ * not have simply reports invalid.
67
+ */
68
+ export function parseEffectAnchors(wgsl: string, max: number): { bone: string; trail: boolean }[] {
69
+ return [...wgsl.matchAll(/^[ \t]*\/\/[ \t]*@anchor[ \t]+(\S+)([ \t]+trail)?[ \t]*$/gm)]
70
+ .map((m) => ({ bone: m[1], trail: m[2] !== undefined }))
71
+ .slice(0, max)
72
+ }
73
+
74
+ /**
75
+ * The caps the cast buffer is built to, shared by the shader below and by the
76
+ * engine that fills it. Interpolated into the WGSL rather than written twice:
77
+ * the layout arithmetic on both sides has to agree exactly, and two literals
78
+ * that must match are two literals that eventually will not.
79
+ *
80
+ * All three are MINIMUMS. Raising one breaks nothing, because effects read
81
+ * through accessors and loop to the count functions; lowering one does.
82
+ */
83
+ export const EFFECT_SUBJECTS = 4
84
+ export const EFFECT_ANCHORS = 8
85
+ export const EFFECT_TRAIL_SAMPLES = 128
86
+ /** vec4 slot where the trails begin — after the subjects and the anchors. */
87
+ export const EFFECT_TRAIL_BASE = EFFECT_SUBJECTS * 3 + EFFECT_ANCHORS * EFFECT_SUBJECTS * 3
88
+
41
89
  export type CompositeEffectSource = {
42
90
  /** The user's WGSL verbatim: helpers plus whichever entry points it defines. */
43
91
  wgsl: string
@@ -107,6 +155,13 @@ override APPLY_GAMMA: bool = true;
107
155
  // Blender's AgX, as the 57³ lookup it ships as rather than a reconstruction of
108
156
  // it. Sampled in the log-encoded E-Gamut space the cube expects — see agxTransform.
109
157
  @group(0) @binding(10) var agxLut: texture_3d<f32>;
158
+ // The cast, as data. Read through rzSubject/rzAnchor below — the LAYOUT IS NOT
159
+ // STABLE and never will be, because it depends on what each effect declared.
160
+ // Reading it directly is the one thing that would freeze it forever.
161
+ //
162
+ // vec4 slots: [0 .. 11] four subjects, three each (root+valid, hip, bounds);
163
+ // then MAX_ANCHORS × four subjects, three each (pos+valid, vel, fwd).
164
+ @group(0) @binding(11) var<storage, read> _rzCast: array<vec4f>;
110
165
 
111
166
  // Must match FILMIC_LUT_WIDTH in engine.ts (bakeFilmicLut).
112
167
  const FILMIC_LUT_W: f32 = 256.0;
@@ -202,29 +257,179 @@ fn viewTransform(c: vec3f) -> vec3f {
202
257
  return vec3f(filmic(c.r), filmic(c.g), filmic(c.b));
203
258
  }
204
259
 
205
- /** Canvas size in pixels — for user effects (aspect correction). */
206
- fn bgResolution() -> vec2f { return viewU[6].zw; }
260
+ // ── The effect API ────────────────────────────────────────────────────────────
261
+ //
262
+ // Named rz*, for the engine. The prefix earns its place twice: user code is
263
+ // concatenated into THIS module, so an unprefixed rzAnchor() would collide with
264
+ // exactly the helper an author would write, and the old bg* prefix stopped being
265
+ // true in 0.41.0 when effects gained a mount over the finished frame.
266
+ //
267
+ // The bg* names below are permanent aliases, not a deprecation with an end date.
268
+ // A published link is immutable, so a scene pinned to a bg* effect has to keep
269
+ // compiling forever. They are one-line and inlined; no new function gets one.
270
+
271
+ /** Canvas size in pixels — for aspect correction. */
272
+ fn rzResolution() -> vec2f { return viewU[6].zw; }
207
273
 
208
274
  /** The camera's world position. */
209
- fn bgCameraPos() -> vec3f { return viewU[10].xyz; }
275
+ fn rzCameraPos() -> vec3f { return viewU[10].xyz; }
210
276
 
211
277
  /** How many characters are in the scene, up to four. */
212
- fn bgSubjectCount() -> i32 { return i32(viewU[10].w); }
278
+ fn rzSubjectCount() -> i32 { return i32(viewU[10].w); }
213
279
 
214
280
  /**
215
- * Where a character is standing, in world space.
281
+ * A world point as the camera sees it: xy the uv it lands on, z its distance
282
+ * along the VIEW AXIS in metres.
216
283
  *
217
- * An effect that wants to RESPOND to the cast ripples under the feet, a glow
218
- * that follows someone, dust kicked up where they are needs to know where
219
- * they are, and the ray and the depth cannot tell it: they describe the pixel,
220
- * not the scene. This is the model's root, which for a PMX is between the feet
221
- * on the floor, so it is already the contact point a ripple wants.
284
+ * The exact inverse of the ray this pass builds per pixel, so it is the cheap way
285
+ * to work with anything anchored in the world. Marching a curve or a trail in 3D
286
+ * costs a distance evaluation per sample per pixel; projecting its points once
287
+ * and measuring in 2D costs a subtraction, which is the difference between a
288
+ * ribbon that runs at 4K and one that does not.
289
+ *
290
+ * z is directly comparable to the depth handed to foreground(), so occlusion is
291
+ * a single test: draw where your z is nearer than the scene's. It is returned
292
+ * SIGNED and unclamped — behind the camera is negative, and worth rejecting
293
+ * before you use the uv, which is meaningless there.
294
+ */
295
+ fn rzProject(p: vec3f) -> vec3f {
296
+ let d = p - viewU[10].xyz;
297
+ let z = dot(d, viewU[5].xyz);
298
+ // Guard only the divide. z itself is returned as it is, so the caller can see
299
+ // the sign; clamping it here would put points behind the lens on the horizon.
300
+ let inv = 1.0 / select(z, 1e-4, z < 1e-4);
301
+ let ndc = vec2f(dot(d, viewU[3].xyz) * inv / viewU[3].w, dot(d, viewU[4].xyz) * inv / viewU[4].w);
302
+ return vec3f(ndc * 0.5 + 0.5, z);
303
+ }
304
+
305
+ /** A character, as much of one as a shader needs. */
306
+ struct RzSubject {
307
+ /** On the FLOOR, under the body — where a ring or a magic circle belongs. */
308
+ root: vec3f,
309
+ /** At the hips, the middle of the body — where an aura belongs. */
310
+ center: vec3f,
311
+ /** Bounding sphere: xyz centre, w radius. Deliberately generous — cull with it. */
312
+ bounds: vec4f,
313
+ /** False past the end of the cast, and every field is then zero. */
314
+ valid: bool,
315
+ }
316
+
317
+ /** One bone an effect asked for, by name, at the top of its own source. */
318
+ struct RzAnchor {
319
+ pos: vec3f,
320
+ /** World units per second, from the previous frame. Direction for a trail,
321
+ * magnitude for anything that should react to how hard someone is moving. */
322
+ vel: vec3f,
323
+ /** The bone's forward axis — which way a foot points, where a head looks. */
324
+ fwd: vec3f,
325
+ /** False when this rig has no such bone. Check it: the alternative is drawing
326
+ * a hand effect at the world origin on every model that spells it differently. */
327
+ valid: bool,
328
+ }
329
+
330
+ const RZ_MAX_ANCHORS: i32 = ${EFFECT_ANCHORS};
331
+
332
+ /**
333
+ * Character i. Loop to rzSubjectCount(), never to a constant — the caps here are
334
+ * MINIMUMS and are free to grow, which is only true while nobody hardcodes them.
335
+ */
336
+ fn rzSubject(i: i32) -> RzSubject {
337
+ var s: RzSubject;
338
+ s.valid = i >= 0 && i < rzSubjectCount();
339
+ if (!s.valid) { return s; }
340
+ let b = i * 3;
341
+ s.root = _rzCast[b].xyz;
342
+ s.center = _rzCast[b + 1].xyz;
343
+ s.bounds = _rzCast[b + 2];
344
+ return s;
345
+ }
346
+
347
+ /**
348
+ * The slot-th bone this effect declared, on character subject.
349
+ *
350
+ * Slots are the order of the declarations at the top of your source:
351
+ *
352
+ * // @anchor 左手首
353
+ * // @anchor 頭
354
+ *
355
+ * gives you slot 0 and slot 1. Any bone name the model has works; valid is
356
+ * false when it does not have it, which is the normal case across rigs that
357
+ * spell things differently.
358
+ */
359
+ fn rzAnchor(subject: i32, slot: i32) -> RzAnchor {
360
+ var a: RzAnchor;
361
+ a.valid = false;
362
+ if (subject < 0 || subject >= rzSubjectCount() || slot < 0 || slot >= RZ_MAX_ANCHORS) { return a; }
363
+ let b = ${EFFECT_SUBJECTS * 3} + (slot * ${EFFECT_SUBJECTS} + subject) * 3;
364
+ a.valid = _rzCast[b].w > 0.5;
365
+ a.pos = _rzCast[b].xyz;
366
+ a.vel = _rzCast[b + 1].xyz;
367
+ a.fwd = _rzCast[b + 2].xyz;
368
+ return a;
369
+ }
370
+
371
+ const RZ_TRAIL_SAMPLES: i32 = ${EFFECT_TRAIL_SAMPLES};
372
+
373
+ /**
374
+ * How many path samples this anchor has. Zero unless it was declared with
375
+ * trail, and it climbs from zero as the trail fills after the effect loads.
376
+ *
377
+ * Loop to THIS, never to RZ_TRAIL_SAMPLES: the cap is a minimum and is free to
378
+ * grow, which stays true only while nobody hardcodes it.
379
+ */
380
+ fn rzTrailCount(subject: i32, slot: i32) -> i32 {
381
+ if (subject < 0 || subject >= rzSubjectCount() || slot < 0 || slot >= RZ_MAX_ANCHORS) { return 0; }
382
+ return i32(_rzCast[${EFFECT_SUBJECTS * 3} + (slot * ${EFFECT_SUBJECTS} + subject) * 3 + 2].w);
383
+ }
384
+
385
+ /**
386
+ * Sample i of an anchor's path: xyz where it was, w how many seconds ago.
387
+ *
388
+ * i = 0 is NOW and they run backwards in time, so a ribbon is drawn by walking i
389
+ * upward and fading on .w. Sampled at a fixed rate on the SCENE clock, not the
390
+ * display's — so the path is identical in the editor, in an export, and in a
391
+ * re-export, and its spacing does not change with framerate.
392
+ *
393
+ * This is what a hand trail wants instead of position and velocity. One position
394
+ * and one velocity is a straight segment that jitters, because a velocity is a
395
+ * difference between two frames; a path is what actually happened.
396
+ */
397
+ fn rzTrail(subject: i32, slot: i32, i: i32) -> vec4f {
398
+ let n = rzTrailCount(subject, slot);
399
+ if (i < 0 || i >= n) { return vec4f(0.0); }
400
+ let base = ${EFFECT_TRAIL_BASE} + (slot * ${EFFECT_SUBJECTS} + subject) * RZ_TRAIL_SAMPLES;
401
+ return _rzCast[base + i];
402
+ }
403
+
404
+ fn bgResolution() -> vec2f { return rzResolution(); }
405
+ fn bgCameraPos() -> vec3f { return rzCameraPos(); }
406
+ fn bgSubjectCount() -> i32 { return rzSubjectCount(); }
407
+
408
+ /**
409
+ * Where a character IS, in world space — at the hips, not on the floor.
410
+ *
411
+ * An effect that wants to RESPOND to the cast — a glow that follows someone,
412
+ * dust kicked up where they are — needs to know where they are, and the ray and
413
+ * the depth cannot tell it: they describe the pixel, not the scene.
414
+ *
415
+ * The value is model.position + センター + 全ての親. センター sits at hip
416
+ * height on every standard MMD rig, so this is a point in the middle of the
417
+ * body. It is NOT the contact point: a ripple drawn here appears at the waist.
418
+ * Ground effects want the .xz of this and their own floor height, which is what
419
+ * the effects that shipped against it already do.
420
+ *
421
+ * The comment here used to claim it was "between the feet on the floor", which
422
+ * is where that habit came from. Left as it is regardless of the name: a
423
+ * published link is immutable, so every shared scene pinning an effect that
424
+ * reads this depends on it meaning exactly what it has always meant.
222
425
  *
223
426
  * Clamped rather than bounds-checked: an effect looping past the count reads the
224
427
  * last subject instead of sampling whatever follows the array, which is a wrong
225
428
  * ripple rather than an undefined one.
226
429
  */
227
- fn bgSubjectPos(i: i32) -> vec3f { return viewU[11 + clamp(i, 0, 3)].xyz; }
430
+ fn rzSubjectHip(i: i32) -> vec3f { return viewU[11 + clamp(i, 0, 3)].xyz; }
431
+
432
+ fn bgSubjectPos(i: i32) -> vec3f { return rzSubjectHip(i); }
228
433
 
229
434
  /** Where in the WORLD the scene drew this pixel — the depth handed to
230
435
  * foreground() turned into a place. Without it an effect can only think in
@@ -235,11 +440,13 @@ fn bgSubjectPos(i: i32) -> vec3f { return viewU[11 + clamp(i, 0, 3)].xyz; }
235
440
  * the ray's projection onto camera-forward before being walked out. At the far
236
441
  * plane (nothing drawn) this lands a very long way off, which is what a sky
237
442
  * should do to anything reading it. */
238
- fn bgWorldPos(ray: vec3f, depth: f32) -> vec3f {
443
+ fn rzWorldPos(ray: vec3f, depth: f32) -> vec3f {
239
444
  let axis = max(dot(normalize(ray), viewU[5].xyz), 1e-4);
240
- return bgCameraPos() + normalize(ray) * (depth / axis);
445
+ return rzCameraPos() + normalize(ray) * (depth / axis);
241
446
  }
242
447
 
448
+ fn bgWorldPos(ray: vec3f, depth: f32) -> vec3f { return rzWorldPos(ray, depth); }
449
+
243
450
  /** Color grading, applied to the tonemapped SCENE (not the background — see the
244
451
  * call site). The core is ASC CDL, the film-industry interchange standard:
245
452
  *