reze-engine 0.69.8 → 0.70.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.
Files changed (45) hide show
  1. package/dist/_dbg-loader.mjs +1100 -0
  2. package/dist/camera-mount.d.ts +57 -0
  3. package/dist/camera-mount.d.ts.map +1 -0
  4. package/dist/camera-mount.js +249 -0
  5. package/dist/camera.d.ts +11 -0
  6. package/dist/camera.d.ts.map +1 -1
  7. package/dist/camera.js +31 -7
  8. package/dist/engine.d.ts +130 -2
  9. package/dist/engine.d.ts.map +1 -1
  10. package/dist/engine.js +476 -12
  11. package/dist/graph/compile.js +1 -1
  12. package/dist/graph/registry.d.ts.map +1 -1
  13. package/dist/graph/registry.js +19 -2
  14. package/dist/graph/slots.js +117 -117
  15. package/dist/index.d.ts +3 -2
  16. package/dist/index.d.ts.map +1 -1
  17. package/dist/model.d.ts +35 -0
  18. package/dist/model.d.ts.map +1 -1
  19. package/dist/model.js +170 -0
  20. package/dist/shaders/materials/common.d.ts +11 -1
  21. package/dist/shaders/materials/common.d.ts.map +1 -1
  22. package/dist/shaders/materials/common.js +571 -548
  23. package/dist/shaders/passes/cull.d.ts +1 -1
  24. package/dist/shaders/passes/cull.d.ts.map +1 -1
  25. package/dist/shaders/passes/cull.js +4 -1
  26. package/dist/shaders/passes/motion-blur.d.ts +35 -0
  27. package/dist/shaders/passes/motion-blur.d.ts.map +1 -0
  28. package/dist/shaders/passes/motion-blur.js +166 -0
  29. package/dist/shaders/passes/particles.d.ts.map +1 -1
  30. package/dist/shaders/passes/particles.js +13 -2
  31. package/dist/shaders/passes/shadow.d.ts +1 -1
  32. package/dist/shaders/passes/shadow.d.ts.map +1 -1
  33. package/package.json +1 -1
  34. package/src/camera-mount.ts +281 -0
  35. package/src/camera.ts +27 -4
  36. package/src/engine.ts +21741 -21249
  37. package/src/graph/compile.ts +1 -1
  38. package/src/graph/registry.ts +20 -3
  39. package/src/graph/slots.ts +304 -304
  40. package/src/index.ts +4 -1
  41. package/src/model.ts +193 -0
  42. package/src/shaders/materials/common.ts +706 -682
  43. package/src/shaders/passes/cull.ts +4 -1
  44. package/src/shaders/passes/motion-blur.ts +171 -0
  45. package/src/shaders/passes/particles.ts +14 -2
package/dist/engine.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { Camera } from "./camera";
2
+ import { CameraMount, headMaterials } from "./camera-mount";
2
3
  import { EFFECT_SUBJECT_VEC4S } from "./shaders/cast-layout";
3
4
  import { decodeDds, isDds } from "./dds-loader";
4
5
  import { Mat4, Quat, Vec3 } from "./math";
@@ -70,6 +71,7 @@ import { STOCKINGS_GRAPH } from "./graph/presets/stockings";
70
71
  import { METAL_GRAPH } from "./graph/presets/metal";
71
72
  import { CLOTH_SMOOTH_GRAPH } from "./graph/presets/cloth_smooth";
72
73
  import { CLOTH_ROUGH_GRAPH } from "./graph/presets/cloth_rough";
74
+ import { MOTION_BLUR_UNIFORM_FLOATS, MOTION_BLUR_WGSL, MOTION_VELOCITY_FORMAT, motionVelocityWgsl, } from "./shaders/passes/motion-blur";
73
75
  /** Grow sphere `s` (x, y, z, radius; radius 0 = empty) to hold another: the
74
76
  * standard union, keeping the larger if one contains the other, else growing
75
77
  * along the line between the centres. */
@@ -1406,6 +1408,8 @@ export class Engine {
1406
1408
  this.cameraFollowYaw = 0;
1407
1409
  this.cameraFollowPitch = 0;
1408
1410
  this.cameraTargetOffset = new Vec3(0, 0, 0);
1411
+ /** The camera on a bone (setCameraMount), and whose rig it rides. */
1412
+ this.cameraMount = null;
1409
1413
  this.lastFrameTime = performance.now();
1410
1414
  this.frameIntervals = new Float32Array(Engine.STATS_WINDOW);
1411
1415
  this.frameIntervalWrite = 0;
@@ -1527,6 +1531,8 @@ export class Engine {
1527
1531
  * resources are half-built, which is exactly the moment a host swaps a sky.
1528
1532
  * Retried at the top of a frame until one of them takes. */
1529
1533
  this.worldBindingsDirty = false;
1534
+ this.castKey = null;
1535
+ this.castKeyAim = [0, 0, 0];
1530
1536
  this.tweenFrom = { position: new Vec3(0, 0, 0), rotation: Quat.identity() };
1531
1537
  this.tweenTo = { position: new Vec3(0, 0, 0), rotation: Quat.identity() };
1532
1538
  this.tweenPosition = new Vec3(0, 0, 0);
@@ -1818,6 +1824,29 @@ export class Engine {
1818
1824
  this.gizmoDrag = null;
1819
1825
  this.camera?.setInputLocked(false);
1820
1826
  };
1827
+ /** See setTimeScale. */
1828
+ this.timeScale = 1;
1829
+ // ─── Motion blur (shaders/passes/motion-blur.ts) ───────────────────────
1830
+ this.motionBlur = null;
1831
+ this.mbVelocityPipeline = null;
1832
+ this.mbVelocityPending = false;
1833
+ this.mbGatherPipeline = null;
1834
+ this.mbFrameLayout = null;
1835
+ this.mbInstanceLayout = null;
1836
+ this.mbFrameBindGroup = null;
1837
+ this.mbPrevCameraBuffer = null;
1838
+ this.mbUniformBuffer = null;
1839
+ this.mbUniformData = new Float32Array(MOTION_BLUR_UNIFORM_FLOATS);
1840
+ this.mbVelocityMs = null;
1841
+ this.mbVelocity = null;
1842
+ this.mbScratch = null;
1843
+ this.mbGatherBindGroup = null;
1844
+ this.mbGatherDepthView = null;
1845
+ this.mbSampler = null;
1846
+ /** Last frame's camera view-projection, or null after a cut or on the first frame. */
1847
+ this.mbPrevViewProj = null;
1848
+ /** Per model: last frame's skin matrices on the GPU, and the CPU copy they came from. */
1849
+ this.mbInstances = new WeakMap();
1821
1850
  /** Models this wrote a dissolve onto last frame, so one nothing asks for any
1822
1851
  * more comes back whole instead of holding wherever it was left. */
1823
1852
  this.dissolveTouched = new Set();
@@ -6337,6 +6366,30 @@ export class Engine {
6337
6366
  this.device.queue.writeBuffer(inst.lightBuffer, 116 * 4, inst.objectLight, 116, 64);
6338
6367
  return true;
6339
6368
  }
6369
+ /**
6370
+ * Each named material's own rendering layers, the bits a light's are tested
6371
+ * against (RENDERING_LAYER_*), in place of the model's. A game lights each
6372
+ * renderer by its own few lamps — URP's per-object lights, four on Aether
6373
+ * Gazer's stages — and a stage converted to one model carries that as a
6374
+ * layer per group of renderers that share lamps, and each lamp on the
6375
+ * groups it lights. Materials left out, or the whole map null, take the
6376
+ * model's layers again.
6377
+ */
6378
+ setMaterialLayers(modelName, layers) {
6379
+ const inst = this.modelInstances.get(modelName);
6380
+ if (!inst)
6381
+ return false;
6382
+ const table = new Uint32Array(inst.objectLight.buffer, 180 * 4, 512);
6383
+ table.fill(0);
6384
+ for (const [n, bits] of Object.entries(layers ?? {})) {
6385
+ const id = inst.materialIdByName.get(n);
6386
+ if (id === undefined || id >= 512)
6387
+ continue;
6388
+ table[id] = bits >>> 0;
6389
+ }
6390
+ this.device.queue.writeBuffer(inst.lightBuffer, 180 * 4, inst.objectLight, 180, 512);
6391
+ return true;
6392
+ }
6340
6393
  /** Bones and followed points into the block, where they stand this frame. */
6341
6394
  resolveParamBindings(fx) {
6342
6395
  const d = fx.paramsData;
@@ -8265,7 +8318,8 @@ export class Engine {
8265
8318
  label: "HDR resolve target",
8266
8319
  size: [width, height],
8267
8320
  format: this.hdrFormat,
8268
- usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING,
8321
+ // COPY_DST: motion blur writes its gather back here.
8322
+ usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST,
8269
8323
  });
8270
8324
  // The id attachment. Multisampled like the rest of the pass, and with NO
8271
8325
  // resolve texture beside it: resolving averages, and the average of two
@@ -9325,6 +9379,67 @@ export class Engine {
9325
9379
  o.x = ox * c + o.z * sn;
9326
9380
  o.z = -ox * sn + o.z * c;
9327
9381
  }
9382
+ /**
9383
+ * The camera on a bone: through her eyes ("pov", on 頭) or held in her hand
9384
+ * and aimed back at her face ("selfie", on a wrist). See camera-mount.ts.
9385
+ *
9386
+ * Placed every frame after her pose and physics, before the camera is
9387
+ * written, so it never trails her and an export renders what the editor
9388
+ * showed. Outranks the orbit and the follow — which stay as they were
9389
+ * underneath, and come back when the mount goes — but not a camera VMD or
9390
+ * setCameraPose. In POV her head (hair, face, eyes: the materials skinned
9391
+ * mostly to 頭 and below) leaves the camera, and only the camera: her shadow
9392
+ * and her mirror image keep it.
9393
+ *
9394
+ * Null takes the camera off. False when the model is not loaded.
9395
+ */
9396
+ setCameraMount(model, options) {
9397
+ const prev = this.cameraMount;
9398
+ if (!model || !options) {
9399
+ if (prev)
9400
+ this.dropCameraMount();
9401
+ return true;
9402
+ }
9403
+ let modelName = typeof model === "string" ? model : "";
9404
+ if (typeof model !== "string")
9405
+ for (const i of this.modelInstances.values())
9406
+ if (i.model === model)
9407
+ modelName = i.name;
9408
+ const inst = this.modelInstances.get(modelName);
9409
+ if (!inst)
9410
+ return false;
9411
+ if (prev && prev.name === modelName)
9412
+ prev.mount.setOptions(options);
9413
+ else {
9414
+ if (prev)
9415
+ this.dropCameraMount();
9416
+ this.cameraMount = { name: modelName, mount: new CameraMount(inst.model, options) };
9417
+ }
9418
+ const pov = this.cameraMount.mount.options.mode === "pov";
9419
+ inst.cameraHiddenMaterials = new Set(pov ? headMaterials(inst.model) : []);
9420
+ this.writeCullHidden(true);
9421
+ return true;
9422
+ }
9423
+ getCameraMount() {
9424
+ const m = this.cameraMount;
9425
+ return m ? { ...m.mount.options, model: m.name } : null;
9426
+ }
9427
+ /** The materials a POV mount keeps out of the camera, for a host to show. */
9428
+ getCameraHiddenMaterials(modelName) {
9429
+ return [...(this.modelInstances.get(modelName)?.cameraHiddenMaterials ?? [])];
9430
+ }
9431
+ dropCameraMount() {
9432
+ const prev = this.cameraMount;
9433
+ if (!prev)
9434
+ return;
9435
+ this.cameraMount = null;
9436
+ this.camera.mountPose = null;
9437
+ const inst = this.modelInstances.get(prev.name);
9438
+ if (inst && inst.cameraHiddenMaterials.size > 0) {
9439
+ inst.cameraHiddenMaterials = new Set();
9440
+ this.writeCullHidden(true);
9441
+ }
9442
+ }
9328
9443
  /** True while the orbit target is riding a model bone (setCameraFollow). */
9329
9444
  isCameraFollowing() {
9330
9445
  return this.cameraTargetModel !== null;
@@ -10524,6 +10639,54 @@ export class Engine {
10524
10639
  cullIndex: -1,
10525
10640
  };
10526
10641
  }
10642
+ /**
10643
+ * The cast's own key light, turning with the camera (CastKey). It takes the
10644
+ * last directional slot; null gives the slot back to the scene's lights.
10645
+ */
10646
+ setCastKey(key) {
10647
+ this.castKey = key;
10648
+ this.castKeyAim = [0, 0, 0];
10649
+ if (!key) {
10650
+ this.setLights(this.sceneLights);
10651
+ return;
10652
+ }
10653
+ this.updateCastKey();
10654
+ }
10655
+ /** The cast key's direction for this frame's camera: the toward frame is the
10656
+ * view's back (towards the eye) flattened to the ground, up, and their cross. */
10657
+ updateCastKey() {
10658
+ const key = this.castKey;
10659
+ if (!key || !this.camera)
10660
+ return;
10661
+ const v = this.camera.getViewMatrix().values;
10662
+ let bx = v[2], bz = v[10];
10663
+ const bl = Math.hypot(bx, bz);
10664
+ if (bl < 1e-4) {
10665
+ bx = 0;
10666
+ bz = 1;
10667
+ }
10668
+ else {
10669
+ bx /= bl;
10670
+ bz /= bl;
10671
+ }
10672
+ const inc = (key.inclination * Math.PI) / 180;
10673
+ const az = (key.azimuth * Math.PI) / 180;
10674
+ // towards the light: back·cos(az) + right·sin(az) round the ground, up by inc
10675
+ const rx = bz, rz = -bx;
10676
+ const tx = (bx * Math.cos(az) + rx * Math.sin(az)) * Math.cos(inc);
10677
+ const ty = Math.sin(inc);
10678
+ const tz = (bz * Math.cos(az) + rz * Math.sin(az)) * Math.cos(inc);
10679
+ const aim = [-tx, -ty, -tz];
10680
+ const was = this.castKeyAim;
10681
+ if (Math.abs(aim[0] - was[0]) < 1e-5 && Math.abs(aim[1] - was[1]) < 1e-5 && Math.abs(aim[2] - was[2]) < 1e-5)
10682
+ return;
10683
+ this.castKeyAim = aim;
10684
+ const base = 4 + 3 * 8;
10685
+ const s = Math.max(key.strength, 0);
10686
+ this.lightData.set([aim[0], aim[1], aim[2], 0, key.color.x * s, key.color.y * s, key.color.z * s, 1], base);
10687
+ this.lightDataWords[DIR_LAYERS_AT + 3] = (key.layers ?? RENDERING_LAYER_CHARACTER) >>> 0;
10688
+ this.updateLightBuffer();
10689
+ }
10527
10690
  /**
10528
10691
  * The scene's positional lights — an ADDITIVE layer over the sun, which stays
10529
10692
  * the key light and keeps the toon ramp to itself.
@@ -10551,7 +10714,8 @@ export class Engine {
10551
10714
  // The directional ones go to the light uniform's slots beside the sun's —
10552
10715
  // three of them, in order; the rest are records the grid indexes.
10553
10716
  const directional = (lights ?? []).filter((l) => l.kind === "directional");
10554
- for (let k = 1; k < 4; k++) {
10717
+ // the last slot is the cast's key while one is set (setCastKey)
10718
+ for (let k = 1; k < (this.castKey ? 3 : 4); k++) {
10555
10719
  const l = directional[k - 1];
10556
10720
  const base = 4 + k * 8;
10557
10721
  const a = l?.aim;
@@ -12599,6 +12763,12 @@ export class Engine {
12599
12763
  const eye = this.camera.getEyePosition();
12600
12764
  const m = inst.model;
12601
12765
  _gazeScratch.setXYZ(eye.x - m.position.x, eye.y - m.position.y, eye.z - m.position.z);
12766
+ // Her own first-person camera sits between her eyes: looking AT it
12767
+ // crosses them. She looks where it looks, far ahead along the view.
12768
+ if (this.cameraMount?.name === inst.name && this.cameraMount.mount.options.mode === "pov") {
12769
+ const v = this.camera.getViewMatrix().values;
12770
+ _gazeScratch.setXYZ(_gazeScratch.x + v[2] * 100, _gazeScratch.y + v[6] * 100, _gazeScratch.z + v[10] * 100);
12771
+ }
12602
12772
  Quat.rotateVecInvInto(m.rotation, _gazeScratch, _gazeScratch);
12603
12773
  const inv = 1 / Math.max(m.scale, 1e-6);
12604
12774
  _gazeScratch.setXYZ(_gazeScratch.x * inv, _gazeScratch.y * inv, _gazeScratch.z * inv);
@@ -12876,9 +13046,11 @@ export class Engine {
12876
13046
  let changed = force;
12877
13047
  for (let i = 0; i < this.cullDraws.length; i++) {
12878
13048
  const { inst, draw } = this.cullDraws[i];
12879
- // bit 0: switched off; bit 1: drawn by its native look, not its graph
13049
+ // bit 0: switched off; bit 1: drawn by its native look, not its graph;
13050
+ // bit 2: out of the camera alone (a POV mount's head)
12880
13051
  const v = (inst.hiddenMaterials.has(draw.materialName) || inst.morphHiddenMaterials.has(draw.materialName) ? 1 : 0) |
12881
- (this.nativeLooks?.dresses(inst.name, draw.materialName) ? 2 : 0);
13052
+ (this.nativeLooks?.dresses(inst.name, draw.materialName) ? 2 : 0) |
13053
+ (inst.cameraHiddenMaterials.has(draw.materialName) ? 4 : 0);
12882
13054
  if (out[i] !== v) {
12883
13055
  out[i] = v;
12884
13056
  changed = true;
@@ -13832,8 +14004,10 @@ export class Engine {
13832
14004
  // rzRoofedCastShadow), ambient flag [4..7], SH [8..43], fill [44..47],
13833
14005
  // jelly eyes [48..95], bloom [96..99] — see materials/common.ts.
13834
14006
  // 100 for the lights, the jelly eyes and the bloom flag; 16 for the
13835
- // see-through window and 64 for the materials it cuts (XRAY_WGSL).
13836
- const objectLight = new Float32Array(180);
14007
+ // see-through window and 64 for the materials it cuts (XRAY_WGSL); 512
14008
+ // for each material's own rendering layers (DRAW_LAYERS_WGSL), 0 = the
14009
+ // model's.
14010
+ const objectLight = new Float32Array(692);
13837
14011
  objectLight[96] = 1;
13838
14012
  const objectLayers = new Uint32Array(objectLight.buffer);
13839
14013
  objectLayers[0] =
@@ -13922,6 +14096,7 @@ export class Engine {
13922
14096
  materialIdByName: new Map(),
13923
14097
  xrayKey: "",
13924
14098
  probeHiddenMaterials: new Set(),
14099
+ cameraHiddenMaterials: new Set(),
13925
14100
  materialMorphTargets: null,
13926
14101
  materialMorphByIndex: null,
13927
14102
  physics,
@@ -15707,6 +15882,22 @@ export class Engine {
15707
15882
  }
15708
15883
  this.onRaycast(hitModel, hitMaterial, hitBone, screenX, screenY);
15709
15884
  }
15885
+ /**
15886
+ * Global playback speed: every clock the frame advances — clips and their
15887
+ * morphs, the camera VMD, physics, tweens, jelly eyes, auto blink, effect
15888
+ * time (rzTime), particles, trails, dissolves, param tracks, scheduled
15889
+ * effects and the camera follow's lag — runs on `delta × scale`, live
15890
+ * (render) and offline (renderFrame) alike. So 0.5 is true slow motion: hair
15891
+ * falls slowly, a sparkle drifts at half pace, and an export stepped at
15892
+ * 1/fps covers half as much of the clip per frame. 0.25–2; frame stats stay
15893
+ * on wall time. The host keeps the music in step (it plays the audio).
15894
+ */
15895
+ setTimeScale(scale) {
15896
+ this.timeScale = Number.isFinite(scale) ? Math.min(4, Math.max(0.1, scale)) : 1;
15897
+ }
15898
+ getTimeScale() {
15899
+ return this.timeScale;
15900
+ }
15710
15901
  render() {
15711
15902
  if (!this.multisampleTexture || !this.camera || !this.device)
15712
15903
  return;
@@ -15717,7 +15908,8 @@ export class Engine {
15717
15908
  }
15718
15909
  /**
15719
15910
  * Render one frame advancing every clock — animation, physics, tweens, and the
15720
- * camera VMD — by exactly `deltaSeconds`, independent of wall time. This is the
15911
+ * camera VMD — by exactly `deltaSeconds` (times the time scale, see
15912
+ * setTimeScale), independent of wall time. This is the
15721
15913
  * offline-rendering primitive (video export): call it N times with 1/fps and the
15722
15914
  * result is deterministic whether the machine renders faster or slower than
15723
15915
  * realtime. Also resets the realtime clock so a later render() (returning to the
@@ -15729,7 +15921,9 @@ export class Engine {
15729
15921
  this.lastFrameTime = performance.now();
15730
15922
  this.renderWithDelta(deltaSeconds);
15731
15923
  }
15732
- renderWithDelta(deltaTime) {
15924
+ renderWithDelta(wallDelta) {
15925
+ // Every clock in the frame runs on the scaled delta — see setTimeScale.
15926
+ const deltaTime = wallDelta * this.timeScale;
15733
15927
  // The bind groups first, then the textures they used to name. A retired
15734
15928
  // texture is freed only once a frame has been rebuilt without it — freeing
15735
15929
  // it while a group still points at it is what "destroyed texture used in a
@@ -15756,6 +15950,7 @@ export class Engine {
15756
15950
  this.handleResize();
15757
15951
  }
15758
15952
  const hasModels = this.modelInstances.size > 0;
15953
+ this.updateCastKey();
15759
15954
  this.syncSteppedFromEffects();
15760
15955
  if (hasModels) {
15761
15956
  this.updateInstances(deltaTime);
@@ -15808,6 +16003,16 @@ export class Engine {
15808
16003
  this.camera.target.y = f.y + this.cameraTargetOffset.y;
15809
16004
  this.camera.target.z = f.z + this.cameraTargetOffset.z;
15810
16005
  }
16006
+ // The camera on a bone, from this frame's pose (setCameraMount). A rig
16007
+ // that has gone (removed, replaced) takes the mount with it.
16008
+ const cm = this.cameraMount;
16009
+ if (cm) {
16010
+ const inst = this.modelInstances.get(cm.name);
16011
+ if (!inst || inst.model !== cm.mount.model)
16012
+ this.dropCameraMount();
16013
+ else
16014
+ this.camera.mountPose = cm.mount.step(deltaTime);
16015
+ }
15811
16016
  }
15812
16017
  // Who holds the shot this frame. An external pose is a statement about
15813
16018
  // where the camera IS, so it is reapplied rather than sampled — and it
@@ -15863,7 +16068,9 @@ export class Engine {
15863
16068
  // The scattering pass is the third reader: it rejects taps across a depth
15864
16069
  // step and scales its width by distance, so a scene wearing a subsurface
15865
16070
  // look stores depth for as long as it does.
15866
- const depthRead = dofOn || this.aovCapture || this.effects.some((e) => e.hasForeground) || this.subsurfaceInUse();
16071
+ // Motion blur is a fourth: its velocity pass tests against this depth and
16072
+ // its gather rebuilds the camera's motion from it.
16073
+ const depthRead = dofOn || this.aovCapture || this.effects.some((e) => e.hasForeground) || this.subsurfaceInUse() || this.motionBlur !== null;
15867
16074
  this.renderPassDescriptor.depthStencilAttachment.depthStoreOp = depthRead ? "store" : "discard";
15868
16075
  if (depthRead)
15869
16076
  this.writeDepthOfFieldUniforms();
@@ -16092,6 +16299,9 @@ export class Engine {
16092
16299
  // Skin scattering, on the resolved scene and before anything reads it:
16093
16300
  // bloom should bleed the scattered skin, not the raw one.
16094
16301
  this.renderSubsurface(encoder);
16302
+ // Motion blur, on the finished scene and before bloom, so a streak blooms
16303
+ // as a camera's would.
16304
+ this.renderMotionBlur(encoder, deltaTime);
16095
16305
  // The field mounts, likewise: after the scene so foregrounds can read its
16096
16306
  // depth, before the composite that samples both layers.
16097
16307
  this.renderFieldPass(encoder);
@@ -16140,9 +16350,10 @@ export class Engine {
16140
16350
  const dpr = window.devicePixelRatio || 1;
16141
16351
  this.resolvePickResult(pick.x / dpr, pick.y / dpr);
16142
16352
  }
16143
- // Feed the true vsync-to-vsync interval (deltaTime, computed at frame start), not the
16144
- // CPU time spent in render() — that's what actually reflects perceived smoothness.
16145
- this.updateStats(deltaTime * 1000);
16353
+ // Feed the true vsync-to-vsync interval (the wall delta, before the time
16354
+ // scale), not the CPU time spent in render() — that's what actually reflects
16355
+ // perceived smoothness.
16356
+ this.updateStats(wallDelta * 1000);
16146
16357
  }
16147
16358
  drawInstanceShadow(sp, inst, cascade) {
16148
16359
  sp.setBindGroup(0, inst.shadowBindGroups[cascade]);
@@ -16536,6 +16747,259 @@ export class Engine {
16536
16747
  inst.model.setEyeTracking(options);
16537
16748
  return true;
16538
16749
  }
16750
+ /**
16751
+ * Auto blink, per model — see Model.setAutoBlink. She blinks on her own
16752
+ * every few seconds, over a morph motion's own blinks. Null hands the morph
16753
+ * back. False for a model that is not loaded or has no closing morph.
16754
+ */
16755
+ setAutoBlink(modelName, options) {
16756
+ const inst = this.modelInstances.get(modelName);
16757
+ if (!inst)
16758
+ return false;
16759
+ return inst.model.setAutoBlink(options);
16760
+ }
16761
+ getAutoBlink(modelName) {
16762
+ return this.modelInstances.get(modelName)?.model.getAutoBlink() ?? null;
16763
+ }
16764
+ /**
16765
+ * Motion blur on everything that moves: her limbs, hair and cloth from the
16766
+ * pose, everything else from the camera. Per pixel, live and in an export
16767
+ * (the shutter follows the frame's own delta, so a 30 fps export and a
16768
+ * 120 Hz screen streak alike). `shutter` is the fraction of a 1/30 s frame
16769
+ * the shutter is open: 0.5 is film's 180°. Null switches it off, and then it
16770
+ * costs nothing. See shaders/passes/motion-blur.ts.
16771
+ */
16772
+ setMotionBlur(options) {
16773
+ if (!options) {
16774
+ this.motionBlur = null;
16775
+ this.mbPrevViewProj = null;
16776
+ return;
16777
+ }
16778
+ this.motionBlur = { shutter: Math.min(1, Math.max(0.05, options.shutter ?? 0.5)) };
16779
+ }
16780
+ getMotionBlur() {
16781
+ return this.motionBlur ? { ...this.motionBlur } : null;
16782
+ }
16783
+ mbEnsurePipelines() {
16784
+ if (this.mbVelocityPipeline && this.mbGatherPipeline)
16785
+ return true;
16786
+ if (this.mbVelocityPending)
16787
+ return false;
16788
+ this.mbVelocityPending = true;
16789
+ const device = this.device;
16790
+ this.mbFrameLayout = device.createBindGroupLayout({
16791
+ label: "motion velocity per-frame",
16792
+ entries: [
16793
+ { binding: 0, visibility: GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT, buffer: { type: "uniform" } },
16794
+ { binding: 1, visibility: GPUShaderStage.VERTEX, buffer: { type: "uniform" } },
16795
+ { binding: 2, visibility: GPUShaderStage.FRAGMENT, sampler: {} },
16796
+ ],
16797
+ });
16798
+ this.mbInstanceLayout = device.createBindGroupLayout({
16799
+ label: "motion velocity per-instance",
16800
+ entries: [
16801
+ { binding: 0, visibility: GPUShaderStage.VERTEX, buffer: { type: "read-only-storage" } },
16802
+ { binding: 1, visibility: GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT, buffer: { type: "uniform" } },
16803
+ { binding: 2, visibility: GPUShaderStage.VERTEX, buffer: { type: "read-only-storage" } },
16804
+ ],
16805
+ });
16806
+ this.mbPrevCameraBuffer = device.createBuffer({
16807
+ label: "motion blur previous camera",
16808
+ size: 64,
16809
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
16810
+ });
16811
+ this.mbUniformBuffer = device.createBuffer({
16812
+ label: "motion blur uniforms",
16813
+ size: MOTION_BLUR_UNIFORM_FLOATS * 4,
16814
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
16815
+ });
16816
+ this.mbSampler = device.createSampler({ magFilter: "linear", minFilter: "linear", addressModeU: "clamp-to-edge", addressModeV: "clamp-to-edge" });
16817
+ this.mbFrameBindGroup = device.createBindGroup({
16818
+ label: "motion velocity per-frame",
16819
+ layout: this.mbFrameLayout,
16820
+ entries: [
16821
+ { binding: 0, resource: { buffer: this.cameraUniformBuffer } },
16822
+ { binding: 1, resource: { buffer: this.mbPrevCameraBuffer } },
16823
+ { binding: 2, resource: this.materialSampler },
16824
+ ],
16825
+ });
16826
+ const velocityModule = device.createShaderModule({ label: "motion velocity", code: motionVelocityWgsl() });
16827
+ const gatherModule = device.createShaderModule({ label: "motion blur gather", code: MOTION_BLUR_WGSL });
16828
+ const velocity = device.createRenderPipelineAsync({
16829
+ label: "motion velocity",
16830
+ layout: device.createPipelineLayout({
16831
+ label: "motion velocity",
16832
+ bindGroupLayouts: [this.mbFrameLayout, this.mbInstanceLayout, this.mainPerMaterialBindGroupLayout],
16833
+ }),
16834
+ vertex: { module: velocityModule, entryPoint: "vs", buffers: this.materialVertexBufferLayouts.slice(0, 3) },
16835
+ fragment: { module: velocityModule, entryPoint: "fs", targets: [{ format: MOTION_VELOCITY_FORMAT }] },
16836
+ primitive: { cullMode: "none" },
16837
+ multisample: { count: Engine.MULTISAMPLE_COUNT },
16838
+ depthStencil: { format: this.depthFormat, depthWriteEnabled: false, depthCompare: this.depthAhead },
16839
+ });
16840
+ const gather = device.createRenderPipelineAsync({
16841
+ label: "motion blur gather",
16842
+ layout: "auto",
16843
+ vertex: { module: gatherModule, entryPoint: "vs" },
16844
+ fragment: { module: gatherModule, entryPoint: "fs", targets: [{ format: this.hdrFormat }] },
16845
+ primitive: { topology: "triangle-list" },
16846
+ });
16847
+ Promise.all([velocity, gather])
16848
+ .then(([v, g]) => {
16849
+ this.mbVelocityPipeline = v;
16850
+ this.mbGatherPipeline = g;
16851
+ })
16852
+ .catch((e) => console.warn("[motion blur] pipelines failed:", e));
16853
+ return false;
16854
+ }
16855
+ /** The motion pass and the gather, on the resolved HDR before bloom. */
16856
+ renderMotionBlur(encoder, deltaTime) {
16857
+ const mb = this.motionBlur;
16858
+ if (!mb || !this.device || !this.hdrResolveTexture || !this.depthReadView)
16859
+ return;
16860
+ const viewProj = this.camera.getProjectionMatrix().multiply(this.camera.getViewMatrix()).values;
16861
+ // The shutter: this frame's motion is deltaTime's worth; the streak is the
16862
+ // shutter's worth of it. No delta (a paused export's first frame, a seek)
16863
+ // or a hitch long enough to be a cut: no blur, and nothing carried over.
16864
+ const scale = deltaTime > 1e-4 && deltaTime < 0.25 ? (mb.shutter / 30) / deltaTime : 0;
16865
+ const prevVP = this.mbPrevViewProj ?? viewProj;
16866
+ if (!this.mbEnsurePipelines() || scale === 0) {
16867
+ this.mbStorePrev(viewProj);
16868
+ return;
16869
+ }
16870
+ const device = this.device;
16871
+ const w = this.hdrResolveTexture.width;
16872
+ const h = this.hdrResolveTexture.height;
16873
+ if (!this.mbVelocity || this.mbVelocity.width !== w || this.mbVelocity.height !== h) {
16874
+ this.mbVelocityMs?.destroy();
16875
+ this.mbVelocity?.destroy();
16876
+ this.mbScratch?.destroy();
16877
+ this.mbVelocityMs = device.createTexture({
16878
+ label: "motion velocity (msaa)",
16879
+ size: [w, h],
16880
+ sampleCount: Engine.MULTISAMPLE_COUNT,
16881
+ format: MOTION_VELOCITY_FORMAT,
16882
+ usage: GPUTextureUsage.RENDER_ATTACHMENT,
16883
+ });
16884
+ this.mbVelocity = device.createTexture({
16885
+ label: "motion velocity",
16886
+ size: [w, h],
16887
+ format: MOTION_VELOCITY_FORMAT,
16888
+ usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING,
16889
+ });
16890
+ this.mbScratch = device.createTexture({
16891
+ label: "motion blur scratch",
16892
+ size: [w, h],
16893
+ format: this.hdrFormat,
16894
+ usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_SRC,
16895
+ });
16896
+ this.mbGatherBindGroup = null;
16897
+ }
16898
+ // The HDR resolve and depth are rebuilt on resize; the group follows them.
16899
+ if (!this.mbGatherBindGroup || this.mbGatherDepthView !== this.depthReadView) {
16900
+ this.mbGatherDepthView = this.depthReadView;
16901
+ this.mbGatherBindGroup = device.createBindGroup({
16902
+ label: "motion blur gather",
16903
+ layout: this.mbGatherPipeline.getBindGroupLayout(0),
16904
+ entries: [
16905
+ { binding: 0, resource: this.hdrResolveTexture.createView() },
16906
+ { binding: 1, resource: this.mbVelocity.createView() },
16907
+ { binding: 2, resource: this.depthReadView },
16908
+ { binding: 3, resource: { buffer: this.mbUniformBuffer } },
16909
+ { binding: 4, resource: this.mbSampler },
16910
+ ],
16911
+ });
16912
+ }
16913
+ // Last frame's camera and poses, then this frame's kept for the next.
16914
+ device.queue.writeBuffer(this.mbPrevCameraBuffer, 0, prevVP);
16915
+ const u = this.mbUniformData;
16916
+ const inv = new Float32Array(16);
16917
+ if (!Mat4.inverseInto(viewProj, inv))
16918
+ inv.set(viewProj);
16919
+ u.set(inv, 0);
16920
+ u.set(prevVP, 16);
16921
+ u[32] = scale;
16922
+ u[33] = Math.max(8, 0.06 * w);
16923
+ u[34] = w;
16924
+ u[35] = h;
16925
+ u[36] = this.depthClear;
16926
+ device.queue.writeBuffer(this.mbUniformBuffer, 0, u);
16927
+ const depthAtt = this.renderPassDescriptor.depthStencilAttachment;
16928
+ const pass = encoder.beginRenderPass({
16929
+ label: "motion velocity",
16930
+ colorAttachments: [
16931
+ {
16932
+ view: this.mbVelocityMs.createView(),
16933
+ resolveTarget: this.mbVelocity.createView(),
16934
+ clearValue: { r: 0, g: 0, b: 0, a: 0 },
16935
+ loadOp: "clear",
16936
+ storeOp: "discard",
16937
+ },
16938
+ ],
16939
+ depthStencilAttachment: { view: depthAtt.view, depthReadOnly: true, stencilReadOnly: true },
16940
+ });
16941
+ pass.setPipeline(this.mbVelocityPipeline);
16942
+ pass.setBindGroup(0, this.mbFrameBindGroup);
16943
+ this.forEachInstance((inst) => {
16944
+ if (!inst.model.drawn)
16945
+ return;
16946
+ const s = this.mbInstance(inst);
16947
+ const cur = inst.model.getSkinMatrices();
16948
+ device.queue.writeBuffer(s.prev, 0, (s.last ?? cur));
16949
+ if (!s.last || s.last.length !== cur.length)
16950
+ s.last = new Float32Array(cur.length);
16951
+ s.last.set(cur);
16952
+ this.setModelDrawState(pass, inst);
16953
+ pass.setBindGroup(1, s.bindGroup);
16954
+ for (const draw of inst.drawCalls) {
16955
+ if (draw.type !== "opaque" && draw.type !== "transparent")
16956
+ continue;
16957
+ pass.setBindGroup(2, draw.bindGroup);
16958
+ this.issueDraw(pass, draw, "camera");
16959
+ }
16960
+ });
16961
+ pass.end();
16962
+ const gather = encoder.beginRenderPass({
16963
+ label: "motion blur gather",
16964
+ colorAttachments: [{ view: this.mbScratch.createView(), loadOp: "clear", clearValue: [0, 0, 0, 0], storeOp: "store" }],
16965
+ });
16966
+ gather.setPipeline(this.mbGatherPipeline);
16967
+ gather.setBindGroup(0, this.mbGatherBindGroup);
16968
+ gather.draw(3);
16969
+ gather.end();
16970
+ encoder.copyTextureToTexture({ texture: this.mbScratch }, { texture: this.hdrResolveTexture }, [w, h]);
16971
+ this.mbStorePrev(viewProj);
16972
+ }
16973
+ mbStorePrev(viewProj) {
16974
+ if (!this.mbPrevViewProj)
16975
+ this.mbPrevViewProj = new Float32Array(16);
16976
+ this.mbPrevViewProj.set(viewProj);
16977
+ }
16978
+ /** A model's last-frame skin buffer and its velocity bind group, rebuilt if
16979
+ * the model's own buffers were replaced. */
16980
+ mbInstance(inst) {
16981
+ let s = this.mbInstances.get(inst);
16982
+ if (s && s.skin === inst.skinMatrixBuffer && s.light === inst.lightBuffer)
16983
+ return s;
16984
+ s?.prev.destroy();
16985
+ const prev = this.device.createBuffer({
16986
+ label: `${inst.name}: previous skin`,
16987
+ size: inst.skinMatrixBuffer.size,
16988
+ usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
16989
+ });
16990
+ const bindGroup = this.device.createBindGroup({
16991
+ label: `${inst.name}: motion velocity`,
16992
+ layout: this.mbInstanceLayout,
16993
+ entries: [
16994
+ { binding: 0, resource: { buffer: inst.skinMatrixBuffer } },
16995
+ { binding: 1, resource: { buffer: inst.lightBuffer } },
16996
+ { binding: 2, resource: { buffer: prev } },
16997
+ ],
16998
+ });
16999
+ s = { prev, bindGroup, last: null, skin: inst.skinMatrixBuffer, light: inst.lightBuffer };
17000
+ this.mbInstances.set(inst, s);
17001
+ return s;
17002
+ }
16539
17003
  /**
16540
17004
  * A repeating dissolve on one model, on the scene clock.
16541
17005
  *
@@ -308,7 +308,7 @@ export function compileGraph(graph, opts = {}) {
308
308
  // lamps' diffuse by its own albedo, as URP does. Mixed in as a layer (a
309
309
  // toon look's gloss), the lamps stay the epilogue's, on the whole material.
310
310
  if (node.type === "lit" && node.id !== out.node)
311
- return spec(node).emit(raw).replace(/, true\)$/, ", false)");
311
+ return spec(node).emit(raw).replace(/input\.worldPos, true\)/, "input.worldPos, false)");
312
312
  return spec(node).emit(raw);
313
313
  };
314
314
  const lines = [];
@@ -1 +1 @@
1
- {"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../../src/graph/registry.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,UAAU,CAAA;AAE3C,MAAM,MAAM,KAAK,GAAG,OAAO,GAAG,OAAO,GAAG,QAAQ,GAAG,MAAM,CAAA;AAEzD,KAAK,SAAS,GAAG;IACf,IAAI,EAAE,KAAK,CAAA;IACX,OAAO,CAAC,EAAE,WAAW,CAAA;IACrB,4FAA4F;IAC5F,YAAY,CAAC,EAAE,OAAO,CAAA;IACtB,yFAAyF;IACzF,cAAc,CAAC,EAAE,MAAM,CAAA;CACxB,CAAA;AAED,MAAM,MAAM,QAAQ,GAAG;IACrB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAA;IACjC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,CAAA;IAC9B,2FAA2F;IAC3F,IAAI,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,KAAK,MAAM,CAAA;IAC/C,qEAAqE;IACrE,cAAc,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IACvC,gFAAgF;IAChF,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IACrC;sEACkE;IAClE,UAAU,CAAC,EAAE,OAAO,CAAA;CACrB,CAAA;AAYD,wBAAgB,QAAQ,CAAC,KAAK,EAAE,WAAW,EAAE,IAAI,EAAE,KAAK,GAAG,MAAM,CAoBhE;AAED,wFAAwF;AACxF,wBAAgB,WAAW,CAAC,KAAK,EAAE,WAAW,EAAE,IAAI,EAAE,KAAK,GAAG,OAAO,CAIpE;AAMD,wBAAgB,UAAU,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,EAAE,KAAK,GAAG,OAAO,CAM1D;AAED,wBAAgB,OAAO,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAKpE;AAsLD,eAAO,MAAM,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,QAAQ,CAuzBlD,CAAA"}
1
+ {"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../../src/graph/registry.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,UAAU,CAAA;AAE3C,MAAM,MAAM,KAAK,GAAG,OAAO,GAAG,OAAO,GAAG,QAAQ,GAAG,MAAM,CAAA;AAEzD,KAAK,SAAS,GAAG;IACf,IAAI,EAAE,KAAK,CAAA;IACX,OAAO,CAAC,EAAE,WAAW,CAAA;IACrB,4FAA4F;IAC5F,YAAY,CAAC,EAAE,OAAO,CAAA;IACtB,yFAAyF;IACzF,cAAc,CAAC,EAAE,MAAM,CAAA;CACxB,CAAA;AAED,MAAM,MAAM,QAAQ,GAAG;IACrB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAA;IACjC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,CAAA;IAC9B,2FAA2F;IAC3F,IAAI,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,KAAK,MAAM,CAAA;IAC/C,qEAAqE;IACrE,cAAc,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IACvC,gFAAgF;IAChF,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IACrC;sEACkE;IAClE,UAAU,CAAC,EAAE,OAAO,CAAA;CACrB,CAAA;AAYD,wBAAgB,QAAQ,CAAC,KAAK,EAAE,WAAW,EAAE,IAAI,EAAE,KAAK,GAAG,MAAM,CAoBhE;AAED,wFAAwF;AACxF,wBAAgB,WAAW,CAAC,KAAK,EAAE,WAAW,EAAE,IAAI,EAAE,KAAK,GAAG,OAAO,CAIpE;AAMD,wBAAgB,UAAU,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,EAAE,KAAK,GAAG,OAAO,CAM1D;AAED,wBAAgB,OAAO,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAKpE;AAsLD,eAAO,MAAM,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,QAAQ,CAw0BlD,CAAA"}