reze-engine 0.68.18 → 0.68.20

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 (52) hide show
  1. package/dist/engine.d.ts +11 -10
  2. package/dist/engine.d.ts.map +1 -1
  3. package/dist/engine.js +34 -17
  4. package/dist/graph/presets/pool_floor.d.ts +3 -0
  5. package/dist/graph/presets/pool_floor.d.ts.map +1 -0
  6. package/dist/graph/presets/pool_floor.js +42 -0
  7. package/dist/graph/presets/water.d.ts +3 -0
  8. package/dist/graph/presets/water.d.ts.map +1 -0
  9. package/dist/graph/presets/water.js +31 -0
  10. package/dist/graph/slots.d.ts.map +1 -1
  11. package/dist/graph/slots.js +8 -2
  12. package/dist/physics/autofit.d.ts +147 -0
  13. package/dist/physics/autofit.d.ts.map +1 -0
  14. package/dist/physics/autofit.js +501 -0
  15. package/dist/shaders/agx-lut.d.ts +11 -0
  16. package/dist/shaders/agx-lut.d.ts.map +1 -0
  17. package/dist/shaders/agx-lut.js +29 -0
  18. package/dist/shaders/dfg_lut.d.ts +3 -0
  19. package/dist/shaders/dfg_lut.d.ts.map +1 -0
  20. package/dist/shaders/dfg_lut.js +129 -0
  21. package/dist/shaders/ltc_mag_lut.d.ts +3 -0
  22. package/dist/shaders/ltc_mag_lut.d.ts.map +1 -0
  23. package/dist/shaders/ltc_mag_lut.js +1033 -0
  24. package/dist/shaders/materials/common.d.ts.map +1 -1
  25. package/dist/shaders/materials/common.js +36 -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 +1 -2
  29. package/dist/shaders/passes/field-blit.d.ts +26 -0
  30. package/dist/shaders/passes/field-blit.d.ts.map +1 -0
  31. package/dist/shaders/passes/field-blit.js +65 -0
  32. package/dist/shaders/passes/ghosts.d.ts +22 -0
  33. package/dist/shaders/passes/ghosts.d.ts.map +1 -0
  34. package/dist/shaders/passes/ghosts.js +75 -0
  35. package/dist/shaders/passes/ground-noise.d.ts +7 -0
  36. package/dist/shaders/passes/ground-noise.d.ts.map +1 -0
  37. package/dist/shaders/passes/ground-noise.js +88 -0
  38. package/dist/shaders/passes/sim.d.ts +34 -0
  39. package/dist/shaders/passes/sim.d.ts.map +1 -0
  40. package/dist/shaders/passes/sim.js +169 -0
  41. package/dist/shaders/score-api.d.ts +10 -0
  42. package/dist/shaders/score-api.d.ts.map +1 -0
  43. package/dist/shaders/score-api.js +114 -0
  44. package/dist/shaders/shadow-api.d.ts +29 -0
  45. package/dist/shaders/shadow-api.d.ts.map +1 -0
  46. package/dist/shaders/shadow-api.js +112 -0
  47. package/package.json +1 -1
  48. package/src/engine.ts +35 -17
  49. package/src/graph/slots.ts +8 -2
  50. package/src/shaders/materials/common.ts +36 -0
  51. package/src/shaders/materials/nodes.ts +1 -2
  52. package/dist/_dbg-loader.mjs +0 -1100
@@ -0,0 +1,112 @@
1
+ import { SHADOW_CASCADES } from "../shadow-cascades";
2
+ // The sun's shadow, as an effect author sees it.
3
+ //
4
+ // A lawn standing in a character's shadow has to darken where the floor under
5
+ // it does, or the figure floats: the ground catcher draws her shadow, and the
6
+ // blades drawn over it hide it. The map is already there — the materials and
7
+ // the ground read it every frame — so this hands the same lookup to effects.
8
+ //
9
+ // Bound for real in the particle SHADING stage only. That is where geometry an
10
+ // effect draws inside the scene pass is shaded, and the shadow pass has already
11
+ // run by then. Every other module that compiles the author's file gets the
12
+ // stub, which answers 1: lit is what a point with no occlusion information is,
13
+ // the same answer sampleShadow gives outside every cascade.
14
+ /**
15
+ * 3×3 PCF over one cascade's map, for a point already in that cascade's NDC.
16
+ *
17
+ * Shared with the materials, so an effect's shadow and her own shadow soften
18
+ * alike. The sampler is linear-filtered, which makes each tap a 2×2 bilinear
19
+ * compare. Unrolled — Safari's Metal backend doesn't unroll nested shadow
20
+ * loops reliably. The body expects `ndc` in scope and returns.
21
+ */
22
+ export const pcf9 = (map, ts, sampler = "shadowSampler") => /* wgsl */ `
23
+ let suv = vec2f(ndc.x * 0.5 + 0.5, 0.5 - ndc.y * 0.5);
24
+ let cmpZ = ndc.z - 0.001;
25
+ let ts = ${ts};
26
+ let s00 = textureSampleCompareLevel(${map}, ${sampler}, suv + vec2f(-ts, -ts), cmpZ);
27
+ let s10 = textureSampleCompareLevel(${map}, ${sampler}, suv + vec2f(0.0, -ts), cmpZ);
28
+ let s20 = textureSampleCompareLevel(${map}, ${sampler}, suv + vec2f( ts, -ts), cmpZ);
29
+ let s01 = textureSampleCompareLevel(${map}, ${sampler}, suv + vec2f(-ts, 0.0), cmpZ);
30
+ let s11 = textureSampleCompareLevel(${map}, ${sampler}, suv, cmpZ);
31
+ let s21 = textureSampleCompareLevel(${map}, ${sampler}, suv + vec2f( ts, 0.0), cmpZ);
32
+ let s02 = textureSampleCompareLevel(${map}, ${sampler}, suv + vec2f(-ts, ts), cmpZ);
33
+ let s12 = textureSampleCompareLevel(${map}, ${sampler}, suv + vec2f(0.0, ts), cmpZ);
34
+ let s22 = textureSampleCompareLevel(${map}, ${sampler}, suv + vec2f( ts, ts), cmpZ);
35
+ return (s00 + s10 + s20 + s01 + s11 + s21 + s02 + s12 + s22) * (1.0 / 9.0);
36
+ `;
37
+ /** Bindings the real accessor takes, from `binding` upward. */
38
+ export const SHADOW_API_BINDINGS = 5;
39
+ /**
40
+ * `rzShadow(p)`: how much of the sun reaches world point `p` — 1 lit, 0 in a
41
+ * caster's shadow, soft in between.
42
+ *
43
+ * With `on`, declares five bindings from `binding`:
44
+ * +0 the light uniform (the sun's direction, and in .w the shadow switch)
45
+ * +1 the cascade view-projections, then the caster sphere
46
+ * +2 the near cascade's map +3 the far cascade's map
47
+ * +4 the comparison sampler
48
+ *
49
+ * It takes a POINT and no normal. The materials' sampleShadow also rejects a
50
+ * surface facing away from the sun, which is right for skin and wrong for a
51
+ * blade of grass or a petal: those are thin, lit through, and shaded by their
52
+ * own N·L. The point is nudged toward the sun instead, which lifts a blade's
53
+ * root off a stage floor that casts into the same map.
54
+ */
55
+ export function shadowApi(on, group, binding) {
56
+ if (!on) {
57
+ return /* wgsl */ `
58
+ fn rzShadow(p: vec3f) -> f32 { return 1.0; }
59
+ `;
60
+ }
61
+ const n = SHADOW_CASCADES.length;
62
+ return /* wgsl */ `
63
+ // The first two vec4s of the light uniform: world ambient, then the sun.
64
+ struct _RzShadowSun { ambient: vec4f, direction: vec4f, }
65
+ // The cascades' matrices, inner to outer, then every caster in one sphere
66
+ // (x, y, z, radius): radius 0 = nothing casts, negative = do not test.
67
+ struct _RzShadowBlock { viewProj: array<mat4x4f, ${n}>, casters: vec4f, }
68
+ @group(${group}) @binding(${binding}) var<uniform> _rzShadowSun: _RzShadowSun;
69
+ @group(${group}) @binding(${binding + 1}) var<uniform> _rzShadowVP: _RzShadowBlock;
70
+ @group(${group}) @binding(${binding + 2}) var _rzShadowNear: texture_depth_2d;
71
+ @group(${group}) @binding(${binding + 3}) var _rzShadowFar: texture_depth_2d;
72
+ @group(${group}) @binding(${binding + 4}) var _rzShadowCmp: sampler_comparison;
73
+
74
+ fn _rzShadowNearTaps(ndc: vec3f) -> f32 {
75
+ ${pcf9("_rzShadowNear", `1.0 / ${SHADOW_CASCADES[0].mapSize}.0`, "_rzShadowCmp")}
76
+ }
77
+
78
+ fn _rzShadowFarTaps(ndc: vec3f) -> f32 {
79
+ ${pcf9("_rzShadowFar", `1.0 / ${SHADOW_CASCADES[n - 1].mapSize}.0`, "_rzShadowCmp")}
80
+ }
81
+
82
+ fn rzShadow(p: vec3f) -> f32 {
83
+ // The scene's one shadow switch, on the sun — see sampleShadow.
84
+ let castAmt = _rzShadowSun.direction.w;
85
+ if (castAmt <= 0.0) { return 1.0; }
86
+ let toSun = normalize(-_rzShadowSun.direction.xyz);
87
+ // Can anything cast onto this point at all? The ground's test, for the same
88
+ // two reasons: most of a lawn is nowhere near her, and a scene with no
89
+ // models never renders the map, whose untouched zeroes compare as shadowed.
90
+ let cs = _rzShadowVP.casters;
91
+ if (cs.w == 0.0) { return 1.0; }
92
+ if (cs.w > 0.0) {
93
+ let toCaster = cs.xyz - p;
94
+ let along = dot(toCaster, toSun);
95
+ let perp = length(toCaster - toSun * along);
96
+ if (along <= -cs.w || perp > cs.w) { return 1.0; }
97
+ }
98
+ let q = p + toSun * 0.08;
99
+ let c0 = _rzShadowVP.viewProj[0] * vec4f(q, 1.0);
100
+ let n0 = c0.xyz / max(c0.w, 1e-6);
101
+ if (all(abs(n0.xy) < vec2f(0.98)) && n0.z > 0.0 && n0.z < 1.0) {
102
+ return mix(1.0, _rzShadowNearTaps(n0), castAmt);
103
+ }
104
+ let c1 = _rzShadowVP.viewProj[${n - 1}] * vec4f(q, 1.0);
105
+ let n1 = c1.xyz / max(c1.w, 1e-6);
106
+ if (all(abs(n1.xy) < vec2f(0.98)) && n1.z > 0.0 && n1.z < 1.0) {
107
+ return mix(1.0, _rzShadowFarTaps(n1), castAmt);
108
+ }
109
+ return 1.0;
110
+ }
111
+ `;
112
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "reze-engine",
3
- "version": "0.68.18",
3
+ "version": "0.68.20",
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",
package/src/engine.ts CHANGED
@@ -773,6 +773,9 @@ export const DEFAULT_COLOR_GRADING: ColorGradingOptions = {
773
773
  saturation: 1,
774
774
  }
775
775
 
776
+ /** Added to every stated audio time — see setAudioTime. Seconds. */
777
+ const AUDIO_TIME_NUDGE = 1e-3
778
+
776
779
  /** The cast's shadow on a stage — see Engine.setStageCastShadow. */
777
780
  export type StageCastShadow = {
778
781
  /** The way TO the light, this engine's axes. Omitted, it is the sun's, read
@@ -2923,7 +2926,8 @@ export class Engine {
2923
2926
  * with no visible difference.
2924
2927
  */
2925
2928
  private anchorTable: AnchorTable = EMPTY_ANCHOR_TABLE
2926
- private castLastMs = 0
2929
+ /** The scene clock at the last cast upload — what anchor velocities divide by. */
2930
+ private castLastScene = 0
2927
2931
  /** Recent path per trailed anchor, keyed "model\0slot". Newest first, so the
2928
2932
  * shader's index 0 is now — written by unshifting rather than by tracking a
2929
2933
  * head, because 64 is short and the alternative is an index the GPU side
@@ -10889,7 +10893,14 @@ export class Engine {
10889
10893
  */
10890
10894
  setAudioTime(seconds: number, playing = true): void {
10891
10895
  if (this.audioBuffer === this.audioFallbackBuffer) return
10892
- this.audioTimeScratch[0] = seconds
10896
+ // Nudged a millisecond off the analysis frames' edges. An export states
10897
+ // exact frame times, i/60, and in f32 i/60 ÷ 1/60 lands a hair either side
10898
+ // of i: the frame an effect read flipped at random, so a hop on the beat
10899
+ // stalled a frame, jumped two, and now and then restarted — the beat
10900
+ // effects judder in a 60 fps export and never in the editor, whose clock
10901
+ // never sits on an edge. A millisecond is far past f32's error at any song
10902
+ // length and far under anything heard.
10903
+ this.audioTimeScratch[0] = seconds + AUDIO_TIME_NUDGE
10893
10904
  this.audioTimeScratch[1] = playing ? 1 : 0
10894
10905
  this.device.queue.writeBuffer(this.audioBuffer, 12, this.audioTimeScratch)
10895
10906
  }
@@ -11709,15 +11720,15 @@ export class Engine {
11709
11720
  }
11710
11721
 
11711
11722
  /**
11712
- * The cast's shadow on a stage, as a game on SimPipeline lays its ground
11713
- * shadow: the characters drawn into a map of their own from `direction` (the
11714
- * way TO the light, this engine's axes), and every Unity-mode stage surface
11715
- * multiplied toward `color` (linear) by amount × how much of the cast stands
11716
- * in the way. Its darkness is independent of the sun's — X340's is dim
11717
- * moonlight, which left a kneeling figure no shadow at all — and without a
11718
- * `direction` it falls along the sun, so it agrees with every other shadow on
11719
- * the stage. Only stage materials read it; a scene
11720
- * without a stage is untouched. Null turns it off.
11723
+ * The cast's shadow on a stage the sun cannot reach: the characters drawn
11724
+ * into a map of their own from `direction` (the way TO the light, this
11725
+ * engine's axes), and every graph-drawn stage surface multiplied toward
11726
+ * `color` (linear) by amount × how much of the cast stands in the way —
11727
+ * where the stage roofs her from the sun (rzRoofedCastShadow). Under open
11728
+ * sky the sun's own map lays her shadow and this adds nothing; under a
11729
+ * ceiling, where the lamps cast none, it is the only shadow she has. Without
11730
+ * a `direction` it falls along the sun. A scene without a stage is
11731
+ * untouched. Null turns it off.
11721
11732
  */
11722
11733
  setStageCastShadow(opts: StageCastShadow | null): void {
11723
11734
  this.castShadow = opts
@@ -15418,11 +15429,14 @@ export class Engine {
15418
15429
 
15419
15430
  // Its ObjectLight: the world's ambient, and the layers its kind draws on —
15420
15431
  // the cast on the character layer as well as the default one. Words:
15421
- // layers [0..3], ambient flag [4..7], SH [8..43], fill [44..47], jelly
15422
- // eyes [48..71] — see materials/common.ts.
15432
+ // layers [0..3] (y: a stage, which takes the cast's shadow —
15433
+ // rzRoofedCastShadow), ambient flag [4..7], SH [8..43], fill [44..47],
15434
+ // jelly eyes [48..71] — see materials/common.ts.
15423
15435
  const objectLight = new Float32Array(96)
15424
- new Uint32Array(objectLight.buffer)[0] =
15436
+ const objectLayers = new Uint32Array(objectLight.buffer)
15437
+ objectLayers[0] =
15425
15438
  isStage || isPlane || isProp ? RENDERING_LAYER_DEFAULT : (RENDERING_LAYER_DEFAULT | RENDERING_LAYER_CHARACTER) >>> 0
15439
+ objectLayers[1] = isStage ? 1 : 0
15426
15440
  const lightBuffer = this.device.createBuffer({
15427
15441
  label: `${name}: object light`,
15428
15442
  size: objectLight.byteLength,
@@ -19814,7 +19828,7 @@ export class Engine {
19814
19828
  ? CAST_TRAIL_BASE + (lastTrail * MAX_EFFECT_SUBJECTS + MAX_EFFECT_SUBJECTS) * TRAIL_SAMPLES
19815
19829
  : CAST_SUBJECT_VEC4S + scene.length * MAX_EFFECT_SUBJECTS * 3
19816
19830
  this.device.queue.writeBuffer(this.castBuffer, 0, this.castData, 0, used * 4)
19817
- this.castLastMs = performance.now()
19831
+ this.castLastScene = this.sceneClock
19818
19832
  }
19819
19833
  }
19820
19834
  }
@@ -19960,8 +19974,12 @@ export class Engine {
19960
19974
  const anchors = this.anchorTable.entries
19961
19975
  if (anchors.length === 0) return
19962
19976
  let prev = this.anchorPrev.get(inst.name)
19963
- const dtMs = Math.max(1, performance.now() - this.castLastMs)
19964
- const invDt = 1000 / dtMs
19977
+ // On the scene clock, not the wall's: an export takes as long per frame as
19978
+ // the encoder does, and a velocity over wall time came out a fraction of
19979
+ // the real one and different every frame. A frame that moved no time (a
19980
+ // paused redraw) has none.
19981
+ const dt = this.sceneClock - this.castLastScene
19982
+ const invDt = dt > 1e-6 ? 1 / dt : 0
19965
19983
  if (!prev || prev.length !== anchors.length * 3) {
19966
19984
  prev = new Float32Array(anchors.length * 3).fill(NaN)
19967
19985
  this.anchorPrev.set(inst.name, prev)
@@ -204,6 +204,12 @@ function epilogue(renderClass: RenderClass, alphaMode: AlphaMode, hasOpacity: bo
204
204
  // shadows, and under a hard step flip whole regions at once. After the graph
205
205
  // it lifts her evenly and leaves every shadow where it is.
206
206
  const LIT = takesLight ? ` + (rzLightsDiffuseOnce(input.worldPos, n) + objectLight.fill.rgb) * albedo` : ""
207
+ // The cast's shadow on a stage the sun cannot reach under its roof
208
+ // (rzRoofedCastShadow), over everything the surface took — the lamps
209
+ // included, since they are what light a closed room. Zero off a stage.
210
+ const CAST = takesLight
211
+ ? ` out.color = vec4f(out.color.rgb * mix(vec3f(1.0), light.castShadow.rgb, rzRoofedCastShadow(input.worldPos) * light.castShadow.w), out.color.a);\n`
212
+ : ""
207
213
 
208
214
  const ALBEDO = ` let albedo = tex_color;
209
215
  `
@@ -244,13 +250,13 @@ ${ID_WRITE} return out;
244
250
 
245
251
  var out: FSOut;
246
252
  out.color = vec4f(final_color${LIT}${BURN}, outAlpha);
247
- ${FOG} out.mask = vec4f(1.0, 1.0, _rzSubsurface, out.color.a);
253
+ ${CAST}${FOG} out.mask = vec4f(1.0, 1.0, _rzSubsurface, out.color.a);
248
254
  ${ID_WRITE} return out;
249
255
  `
250
256
  }
251
257
  return `${ALBEDO} var out: FSOut;
252
258
  out.color = vec4f(final_color${LIT}${BURN}, ${alphaBase});
253
- ${FOG} out.mask = vec4f(1.0, 1.0, _rzSubsurface, out.color.a);
259
+ ${CAST}${FOG} out.mask = vec4f(1.0, 1.0, _rzSubsurface, out.color.a);
254
260
  ${ID_WRITE} return out;
255
261
  `
256
262
  }
@@ -390,6 +390,42 @@ fn sampleShadow(worldPos: vec3f, n: vec3f) -> f32 {
390
390
  return mix(1.0, _rzSunTaps(a), castAmt);
391
391
  }
392
392
 
393
+ /**
394
+ * The cast's shadow on a stage surface where the stage ROOFS the cast from the
395
+ * sun, 0–1 — setStageCastShadow on a graph-drawn stage (a GLB's). Under open
396
+ * sky the sun's own map already lays the cast's shadow, and laying this one too
397
+ * doubled it; under a ceiling the sun reaches neither the floor nor her, the
398
+ * lamps cast nothing, and she stood on the floor with no shadow at all. So
399
+ * per pixel: find the point of the cast that throws this shadow (the cast
400
+ * map's nearest depth, walked back toward the light along the map's z row),
401
+ * step off her toward the light, and ask the sun's map whether it reaches
402
+ * there. Lit: the sun shadow has it, answer 0. Shadowed: this is the only
403
+ * shadow she gets. Zero on everything that is not a stage (objectLight
404
+ * layers.y).
405
+ */
406
+ fn rzRoofedCastShadow(wp: vec3f) -> f32 {
407
+ if (light.castShadow.w <= 0.0 || objectLight.layers.y == 0u) { return 0.0; }
408
+ let cover = _rzCastShadow(wp);
409
+ if (cover <= 0.0) { return 0.0; }
410
+ let ndc = (light.castVP * vec4f(wp, 1.0)).xyz;
411
+ let size = vec2i(textureDimensions(castShadowMap));
412
+ let px = vec2i(vec2f(ndc.x * 0.5 + 0.5, 0.5 - ndc.y * 0.5) * vec2f(size));
413
+ // The nearest of her over the filter's footprint: a penumbra texel's own
414
+ // depth may be the cleared 1.
415
+ var occ = 1.0;
416
+ for (var y = -2; y <= 2; y += 2) {
417
+ for (var x = -2; x <= 2; x += 2) {
418
+ occ = min(occ, textureLoad(castShadowMap, clamp(px + vec2i(x, y), vec2i(0), size - 1), 0));
419
+ }
420
+ }
421
+ if (occ >= ndc.z) { return 0.0; }
422
+ // Orthographic: depth grows away from the light at |g| per unit.
423
+ let g = vec3f(light.castVP[0].z, light.castVP[1].z, light.castVP[2].z);
424
+ let k = length(g);
425
+ let q = wp - (g / k) * ((ndc.z - occ) / k + 0.5);
426
+ return cover * (1.0 - rzSunOcclusion(q, vec3f(0.0)));
427
+ }
428
+
393
429
  `;
394
430
 
395
431
  // ─── Skinning vertex shader ─────────────────────────────────────────
@@ -1703,8 +1703,7 @@ fn urp_lit(base: vec3f, metallic: f32, smoothness: f32, spec: f32, occlusion: f3
1703
1703
  let direct = diffuse * (sun_rgb * (NL * shadow) + lampDiff) + directSpec;
1704
1704
  let indirect = diffuse * amb_rgb * ao
1705
1705
  + rzProbeSpecular(reflect(-V, N), wp, r) * ao * urp_env_brdf(specular, r, sm, reflectivity, NV);
1706
- var lit = direct + indirect;
1707
- lit = lit * mix(vec3f(1.0), light.castShadow.rgb, _rzCastShadow(wp) * light.castShadow.w);
1706
+ let lit = direct + indirect;
1708
1707
  return vec4f(lit + max(emission, vec3f(0.0)), a * oneMinusReflectivity + reflectivity);
1709
1708
  }
1710
1709