reze-engine 0.69.9 → 0.70.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 (40) hide show
  1. package/dist/camera-mount.d.ts +57 -0
  2. package/dist/camera-mount.d.ts.map +1 -0
  3. package/dist/camera-mount.js +249 -0
  4. package/dist/camera.d.ts +11 -0
  5. package/dist/camera.d.ts.map +1 -1
  6. package/dist/camera.js +31 -7
  7. package/dist/engine.d.ts +94 -2
  8. package/dist/engine.d.ts.map +1 -1
  9. package/dist/engine.js +409 -18
  10. package/dist/index.d.ts +3 -2
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/model.d.ts +35 -0
  13. package/dist/model.d.ts.map +1 -1
  14. package/dist/model.js +170 -0
  15. package/dist/shaders/cast-api.d.ts +2 -2
  16. package/dist/shaders/cast-api.d.ts.map +1 -1
  17. package/dist/shaders/cast-api.js +2 -2
  18. package/dist/shaders/cast-layout.d.ts +3 -1
  19. package/dist/shaders/cast-layout.d.ts.map +1 -1
  20. package/dist/shaders/cast-layout.js +3 -1
  21. package/dist/shaders/passes/composite.d.ts +1 -1
  22. package/dist/shaders/passes/composite.d.ts.map +1 -1
  23. package/dist/shaders/passes/composite.js +9 -2
  24. package/dist/shaders/passes/cull.d.ts +1 -1
  25. package/dist/shaders/passes/cull.d.ts.map +1 -1
  26. package/dist/shaders/passes/cull.js +4 -1
  27. package/dist/shaders/passes/motion-blur.d.ts +35 -0
  28. package/dist/shaders/passes/motion-blur.d.ts.map +1 -0
  29. package/dist/shaders/passes/motion-blur.js +166 -0
  30. package/package.json +1 -1
  31. package/src/camera-mount.ts +281 -0
  32. package/src/camera.ts +27 -4
  33. package/src/engine.ts +429 -20
  34. package/src/index.ts +3 -1
  35. package/src/model.ts +193 -0
  36. package/src/shaders/cast-api.ts +9 -2
  37. package/src/shaders/cast-layout.ts +3 -1
  38. package/src/shaders/passes/composite.ts +9 -3
  39. package/src/shaders/passes/cull.ts +4 -1
  40. package/src/shaders/passes/motion-blur.ts +171 -0
package/src/engine.ts CHANGED
@@ -1,9 +1,10 @@
1
1
  import { Camera } from "./camera"
2
+ import { CameraMount, headMaterials, type CameraMountOptions } 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"
5
6
  import { decodePsd, isPsd } from "./psd-loader"
6
- import { Model, MATERIAL_MORPH_MULTIPLY, type EyeTrackingOptions, type Material, type Skeleton } from "./model"
7
+ import { Model, MATERIAL_MORPH_MULTIPLY, type AutoBlinkOptions, type EyeTrackingOptions, type Material, type Skeleton } from "./model"
7
8
  import { MORPH_COMPUTE_WGSL } from "./shaders/passes/morph"
8
9
  import { CULL_COMPUTE_WGSL } from "./shaders/passes/cull"
9
10
  import { buildAnchorTable, anchorAliasWgsl, EMPTY_ANCHOR_TABLE, type AnchorTable } from "./shaders/anchor-table"
@@ -133,6 +134,7 @@ import {
133
134
  EFFECT_TRAIL_SAMPLES,
134
135
  } from "./shaders/passes/composite"
135
136
  import { SUBSURFACE_WGSL, SSS_SPIKE } from "./shaders/passes/subsurface"
137
+ import { EFFECT_SUBJECTS_ALL } from "./shaders/cast-layout"
136
138
  import {
137
139
  buildParticleComputeShader,
138
140
  buildParticleRenderShader,
@@ -198,6 +200,19 @@ import { STOCKINGS_GRAPH } from "./graph/presets/stockings"
198
200
  import { METAL_GRAPH } from "./graph/presets/metal"
199
201
  import { CLOTH_SMOOTH_GRAPH } from "./graph/presets/cloth_smooth"
200
202
  import { CLOTH_ROUGH_GRAPH } from "./graph/presets/cloth_rough"
203
+ import {
204
+ MOTION_BLUR_UNIFORM_FLOATS,
205
+ MOTION_BLUR_WGSL,
206
+ MOTION_VELOCITY_FORMAT,
207
+ motionVelocityWgsl,
208
+ } from "./shaders/passes/motion-blur"
209
+
210
+ /** What setMotionBlur asks for. */
211
+ export interface MotionBlurOptions {
212
+ /** The fraction of a 1/30 s frame the shutter is open: 0.5 is film's 180°.
213
+ * 0.05–1, default 0.5. */
214
+ shutter?: number
215
+ }
201
216
 
202
217
  /** Grow sphere `s` (x, y, z, radius; radius 0 = empty) to hold another: the
203
218
  * standard union, keeping the larger if one contains the other, else growing
@@ -1190,6 +1205,9 @@ interface ModelInstance {
1190
1205
  xrayKey: string
1191
1206
  /** Materials the reflection probe leaves out (setMaterialInProbe). */
1192
1207
  probeHiddenMaterials: Set<string>
1208
+ /** Materials the main camera leaves out, shadow and mirror keeping them —
1209
+ * her own head while the camera is mounted at her eyes (setCameraMount). */
1210
+ cameraHiddenMaterials: Set<string>
1193
1211
  /** Material-morph targets, or null when the model has no type-8 morphs. */
1194
1212
  materialMorphTargets: MaterialMorphTarget[] | null
1195
1213
  /** The same targets by PMX material index, so a named offset is one lookup. */
@@ -1972,7 +1990,7 @@ interface EffectInstance {
1972
1990
  */
1973
1991
  subjects: readonly string[] | null
1974
1992
  /** The above as the shaders read it: one bit per cast slot, recomputed every
1975
- * frame. 0xf — the whole cast — while `subjects` is null. */
1993
+ * frame. EFFECT_SUBJECTS_ALL — the whole cast — while `subjects` is null. */
1976
1994
  subjectMask: number
1977
1995
  /** How many subjects this effect actually HAS this frame: the mask's bits,
1978
1996
  * counted against the live cast. What sizes the ribbons' instance count. */
@@ -3258,6 +3276,8 @@ export class Engine {
3258
3276
  private cameraFollowYaw = 0
3259
3277
  private cameraFollowPitch = 0
3260
3278
  private cameraTargetOffset: Vec3 = new Vec3(0, 0, 0)
3279
+ /** The camera on a bone (setCameraMount), and whose rig it rides. */
3280
+ private cameraMount: { name: string; mount: CameraMount } | null = null
3261
3281
 
3262
3282
  private lastFrameTime = performance.now()
3263
3283
  // Smoothness metrics are computed over a ring buffer of true frame intervals
@@ -4986,7 +5006,7 @@ export class Engine {
4986
5006
  continue
4987
5007
  }
4988
5008
  e.distVariant = this.castDistanceVariants.length
4989
- const data = new Float32Array([0xf, 0, 0, 0])
5009
+ const data = new Float32Array([EFFECT_SUBJECTS_ALL, 0, 0, 0])
4990
5010
  const uniform = device.createBuffer({
4991
5011
  label: `cast distance seeds (${key})`,
4992
5012
  size: data.byteLength,
@@ -5001,9 +5021,9 @@ export class Engine {
5001
5021
  })
5002
5022
  this.castDistanceVariants.push({
5003
5023
  subjects: e.subjects,
5004
- // 0xf is what the buffer above was written with; the first frame's
5024
+ // EFFECT_SUBJECTS_ALL is what the buffer above was written with; the first frame's
5005
5025
  // updateSubjectMasks narrows it to the live cast.
5006
- mask: 0xf,
5026
+ mask: EFFECT_SUBJECTS_ALL,
5007
5027
  uniform,
5008
5028
  data,
5009
5029
  seedBind: device.createBindGroup({
@@ -6071,7 +6091,7 @@ export class Engine {
6071
6091
  // effect did before it could be aimed. A host narrows it afterwards, the
6072
6092
  // way it sets influence and a schedule.
6073
6093
  subjects: null,
6074
- subjectMask: 0xf,
6094
+ subjectMask: EFFECT_SUBJECTS_ALL,
6075
6095
  subjectCount: 0,
6076
6096
  distVariant: 0,
6077
6097
  anchors,
@@ -10127,7 +10147,8 @@ export class Engine {
10127
10147
  label: "HDR resolve target",
10128
10148
  size: [width, height],
10129
10149
  format: this.hdrFormat,
10130
- usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING,
10150
+ // COPY_DST: motion blur writes its gather back here.
10151
+ usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST,
10131
10152
  })
10132
10153
 
10133
10154
  // The id attachment. Multisampled like the rest of the pass, and with NO
@@ -11278,6 +11299,63 @@ export class Engine {
11278
11299
  o.z = -ox * sn + o.z * c
11279
11300
  }
11280
11301
 
11302
+ /**
11303
+ * The camera on a bone: through her eyes ("pov", on 頭) or held in her hand
11304
+ * and aimed back at her face ("selfie", on a wrist). See camera-mount.ts.
11305
+ *
11306
+ * Placed every frame after her pose and physics, before the camera is
11307
+ * written, so it never trails her and an export renders what the editor
11308
+ * showed. Outranks the orbit and the follow — which stay as they were
11309
+ * underneath, and come back when the mount goes — but not a camera VMD or
11310
+ * setCameraPose. In POV her head (hair, face, eyes: the materials skinned
11311
+ * mostly to 頭 and below) leaves the camera, and only the camera: her shadow
11312
+ * and her mirror image keep it.
11313
+ *
11314
+ * Null takes the camera off. False when the model is not loaded.
11315
+ */
11316
+ setCameraMount(model: string | Model | null, options: CameraMountOptions | null): boolean {
11317
+ const prev = this.cameraMount
11318
+ if (!model || !options) {
11319
+ if (prev) this.dropCameraMount()
11320
+ return true
11321
+ }
11322
+ let modelName = typeof model === "string" ? model : ""
11323
+ if (typeof model !== "string") for (const i of this.modelInstances.values()) if (i.model === model) modelName = i.name
11324
+ const inst = this.modelInstances.get(modelName)
11325
+ if (!inst) return false
11326
+ if (prev && prev.name === modelName) prev.mount.setOptions(options)
11327
+ else {
11328
+ if (prev) this.dropCameraMount()
11329
+ this.cameraMount = { name: modelName, mount: new CameraMount(inst.model, options) }
11330
+ }
11331
+ const pov = this.cameraMount!.mount.options.mode === "pov"
11332
+ inst.cameraHiddenMaterials = new Set(pov ? headMaterials(inst.model) : [])
11333
+ this.writeCullHidden(true)
11334
+ return true
11335
+ }
11336
+
11337
+ getCameraMount(): (Required<CameraMountOptions> & { model: string }) | null {
11338
+ const m = this.cameraMount
11339
+ return m ? { ...m.mount.options, model: m.name } : null
11340
+ }
11341
+
11342
+ /** The materials a POV mount keeps out of the camera, for a host to show. */
11343
+ getCameraHiddenMaterials(modelName: string): string[] {
11344
+ return [...(this.modelInstances.get(modelName)?.cameraHiddenMaterials ?? [])]
11345
+ }
11346
+
11347
+ private dropCameraMount(): void {
11348
+ const prev = this.cameraMount
11349
+ if (!prev) return
11350
+ this.cameraMount = null
11351
+ this.camera.mountPose = null
11352
+ const inst = this.modelInstances.get(prev.name)
11353
+ if (inst && inst.cameraHiddenMaterials.size > 0) {
11354
+ inst.cameraHiddenMaterials = new Set()
11355
+ this.writeCullHidden(true)
11356
+ }
11357
+ }
11358
+
11281
11359
  /** True while the orbit target is riding a model bone (setCameraFollow). */
11282
11360
  isCameraFollowing(): boolean {
11283
11361
  return this.cameraTargetModel !== null
@@ -14867,6 +14945,12 @@ export class Engine {
14867
14945
  const eye = this.camera.getEyePosition()
14868
14946
  const m = inst.model
14869
14947
  _gazeScratch.setXYZ(eye.x - m.position.x, eye.y - m.position.y, eye.z - m.position.z)
14948
+ // Her own first-person camera sits between her eyes: looking AT it
14949
+ // crosses them. She looks where it looks, far ahead along the view.
14950
+ if (this.cameraMount?.name === inst.name && this.cameraMount.mount.options.mode === "pov") {
14951
+ const v = this.camera.getViewMatrix().values
14952
+ _gazeScratch.setXYZ(_gazeScratch.x + v[2] * 100, _gazeScratch.y + v[6] * 100, _gazeScratch.z + v[10] * 100)
14953
+ }
14870
14954
  Quat.rotateVecInvInto(m.rotation, _gazeScratch, _gazeScratch)
14871
14955
  const inv = 1 / Math.max(m.scale, 1e-6)
14872
14956
  _gazeScratch.setXYZ(_gazeScratch.x * inv, _gazeScratch.y * inv, _gazeScratch.z * inv)
@@ -15164,10 +15248,12 @@ export class Engine {
15164
15248
  let changed = force
15165
15249
  for (let i = 0; i < this.cullDraws.length; i++) {
15166
15250
  const { inst, draw } = this.cullDraws[i]
15167
- // bit 0: switched off; bit 1: drawn by its native look, not its graph
15251
+ // bit 0: switched off; bit 1: drawn by its native look, not its graph;
15252
+ // bit 2: out of the camera alone (a POV mount's head)
15168
15253
  const v =
15169
15254
  (inst.hiddenMaterials.has(draw.materialName) || inst.morphHiddenMaterials.has(draw.materialName) ? 1 : 0) |
15170
- (this.nativeLooks?.dresses(inst.name, draw.materialName) ? 2 : 0)
15255
+ (this.nativeLooks?.dresses(inst.name, draw.materialName) ? 2 : 0) |
15256
+ (inst.cameraHiddenMaterials.has(draw.materialName) ? 4 : 0)
15171
15257
  if (out[i] !== v) {
15172
15258
  out[i] = v
15173
15259
  changed = true
@@ -16264,6 +16350,7 @@ export class Engine {
16264
16350
  materialIdByName: new Map(),
16265
16351
  xrayKey: "",
16266
16352
  probeHiddenMaterials: new Set(),
16353
+ cameraHiddenMaterials: new Set(),
16267
16354
  materialMorphTargets: null,
16268
16355
  materialMorphByIndex: null,
16269
16356
  physics,
@@ -18446,6 +18533,27 @@ export class Engine {
18446
18533
  this.onRaycast(hitModel, hitMaterial, hitBone, screenX, screenY)
18447
18534
  }
18448
18535
 
18536
+ /** See setTimeScale. */
18537
+ private timeScale = 1
18538
+
18539
+ /**
18540
+ * Global playback speed: every clock the frame advances — clips and their
18541
+ * morphs, the camera VMD, physics, tweens, jelly eyes, auto blink, effect
18542
+ * time (rzTime), particles, trails, dissolves, param tracks, scheduled
18543
+ * effects and the camera follow's lag — runs on `delta × scale`, live
18544
+ * (render) and offline (renderFrame) alike. So 0.5 is true slow motion: hair
18545
+ * falls slowly, a sparkle drifts at half pace, and an export stepped at
18546
+ * 1/fps covers half as much of the clip per frame. 0.25–2; frame stats stay
18547
+ * on wall time. The host keeps the music in step (it plays the audio).
18548
+ */
18549
+ setTimeScale(scale: number): void {
18550
+ this.timeScale = Number.isFinite(scale) ? Math.min(4, Math.max(0.1, scale)) : 1
18551
+ }
18552
+
18553
+ getTimeScale(): number {
18554
+ return this.timeScale
18555
+ }
18556
+
18449
18557
  render() {
18450
18558
  if (!this.multisampleTexture || !this.camera || !this.device) return
18451
18559
 
@@ -18457,7 +18565,8 @@ export class Engine {
18457
18565
 
18458
18566
  /**
18459
18567
  * Render one frame advancing every clock — animation, physics, tweens, and the
18460
- * camera VMD — by exactly `deltaSeconds`, independent of wall time. This is the
18568
+ * camera VMD — by exactly `deltaSeconds` (times the time scale, see
18569
+ * setTimeScale), independent of wall time. This is the
18461
18570
  * offline-rendering primitive (video export): call it N times with 1/fps and the
18462
18571
  * result is deterministic whether the machine renders faster or slower than
18463
18572
  * realtime. Also resets the realtime clock so a later render() (returning to the
@@ -18469,7 +18578,9 @@ export class Engine {
18469
18578
  this.renderWithDelta(deltaSeconds)
18470
18579
  }
18471
18580
 
18472
- private renderWithDelta(deltaTime: number) {
18581
+ private renderWithDelta(wallDelta: number) {
18582
+ // Every clock in the frame runs on the scaled delta — see setTimeScale.
18583
+ const deltaTime = wallDelta * this.timeScale
18473
18584
  // The bind groups first, then the textures they used to name. A retired
18474
18585
  // texture is freed only once a frame has been rebuilt without it — freeing
18475
18586
  // it while a group still points at it is what "destroyed texture used in a
@@ -18548,6 +18659,14 @@ export class Engine {
18548
18659
  this.camera.target.y = f.y + this.cameraTargetOffset.y
18549
18660
  this.camera.target.z = f.z + this.cameraTargetOffset.z
18550
18661
  }
18662
+ // The camera on a bone, from this frame's pose (setCameraMount). A rig
18663
+ // that has gone (removed, replaced) takes the mount with it.
18664
+ const cm = this.cameraMount
18665
+ if (cm) {
18666
+ const inst = this.modelInstances.get(cm.name)
18667
+ if (!inst || inst.model !== cm.mount.model) this.dropCameraMount()
18668
+ else this.camera.mountPose = cm.mount.step(deltaTime)
18669
+ }
18551
18670
  }
18552
18671
 
18553
18672
  // Who holds the shot this frame. An external pose is a statement about
@@ -18604,7 +18723,10 @@ export class Engine {
18604
18723
  // The scattering pass is the third reader: it rejects taps across a depth
18605
18724
  // step and scales its width by distance, so a scene wearing a subsurface
18606
18725
  // look stores depth for as long as it does.
18607
- const depthRead = dofOn || this.aovCapture || this.effects.some((e) => e.hasForeground) || this.subsurfaceInUse()
18726
+ // Motion blur is a fourth: its velocity pass tests against this depth and
18727
+ // its gather rebuilds the camera's motion from it.
18728
+ const depthRead =
18729
+ dofOn || this.aovCapture || this.effects.some((e) => e.hasForeground) || this.subsurfaceInUse() || this.motionBlur !== null
18608
18730
  this.renderPassDescriptor.depthStencilAttachment!.depthStoreOp = depthRead ? "store" : "discard"
18609
18731
  if (depthRead) this.writeDepthOfFieldUniforms()
18610
18732
 
@@ -18831,6 +18953,9 @@ export class Engine {
18831
18953
  // Skin scattering, on the resolved scene and before anything reads it:
18832
18954
  // bloom should bleed the scattered skin, not the raw one.
18833
18955
  this.renderSubsurface(encoder)
18956
+ // Motion blur, on the finished scene and before bloom, so a streak blooms
18957
+ // as a camera's would.
18958
+ this.renderMotionBlur(encoder, deltaTime)
18834
18959
  // The field mounts, likewise: after the scene so foregrounds can read its
18835
18960
  // depth, before the composite that samples both layers.
18836
18961
  this.renderFieldPass(encoder)
@@ -18886,9 +19011,10 @@ export class Engine {
18886
19011
  this.resolvePickResult(pick.x / dpr, pick.y / dpr)
18887
19012
  }
18888
19013
 
18889
- // Feed the true vsync-to-vsync interval (deltaTime, computed at frame start), not the
18890
- // CPU time spent in render() — that's what actually reflects perceived smoothness.
18891
- this.updateStats(deltaTime * 1000)
19014
+ // Feed the true vsync-to-vsync interval (the wall delta, before the time
19015
+ // scale), not the CPU time spent in render() — that's what actually reflects
19016
+ // perceived smoothness.
19017
+ this.updateStats(wallDelta * 1000)
18892
19018
  }
18893
19019
 
18894
19020
  private drawInstanceShadow(sp: GPURenderPassEncoder | GPURenderBundleEncoder, inst: ModelInstance, cascade: number): void {
@@ -19288,6 +19414,285 @@ export class Engine {
19288
19414
  return true
19289
19415
  }
19290
19416
 
19417
+ /**
19418
+ * Auto blink, per model — see Model.setAutoBlink. She blinks on her own
19419
+ * every few seconds, over a morph motion's own blinks. Null hands the morph
19420
+ * back. False for a model that is not loaded or has no closing morph.
19421
+ */
19422
+ setAutoBlink(modelName: string, options: AutoBlinkOptions | null): boolean {
19423
+ const inst = this.modelInstances.get(modelName)
19424
+ if (!inst) return false
19425
+ return inst.model.setAutoBlink(options)
19426
+ }
19427
+
19428
+ getAutoBlink(modelName: string): Required<AutoBlinkOptions> | null {
19429
+ return this.modelInstances.get(modelName)?.model.getAutoBlink() ?? null
19430
+ }
19431
+
19432
+ // ─── Motion blur (shaders/passes/motion-blur.ts) ───────────────────────
19433
+ private motionBlur: Required<MotionBlurOptions> | null = null
19434
+ private mbVelocityPipeline: GPURenderPipeline | null = null
19435
+ private mbVelocityPending = false
19436
+ private mbGatherPipeline: GPURenderPipeline | null = null
19437
+ private mbFrameLayout: GPUBindGroupLayout | null = null
19438
+ private mbInstanceLayout: GPUBindGroupLayout | null = null
19439
+ private mbFrameBindGroup: GPUBindGroup | null = null
19440
+ private mbPrevCameraBuffer: GPUBuffer | null = null
19441
+ private mbUniformBuffer: GPUBuffer | null = null
19442
+ private readonly mbUniformData = new Float32Array(MOTION_BLUR_UNIFORM_FLOATS)
19443
+ private mbVelocityMs: GPUTexture | null = null
19444
+ private mbVelocity: GPUTexture | null = null
19445
+ private mbScratch: GPUTexture | null = null
19446
+ private mbGatherBindGroup: GPUBindGroup | null = null
19447
+ private mbGatherDepthView: GPUTextureView | null = null
19448
+ private mbSampler: GPUSampler | null = null
19449
+ /** Last frame's camera view-projection, or null after a cut or on the first frame. */
19450
+ private mbPrevViewProj: Float32Array | null = null
19451
+ /** Per model: last frame's skin matrices on the GPU, and the CPU copy they came from. */
19452
+ private readonly mbInstances = new WeakMap<
19453
+ ModelInstance,
19454
+ { prev: GPUBuffer; bindGroup: GPUBindGroup; last: Float32Array | null; skin: GPUBuffer; light: GPUBuffer }
19455
+ >()
19456
+
19457
+ /**
19458
+ * Motion blur on everything that moves: her limbs, hair and cloth from the
19459
+ * pose, everything else from the camera. Per pixel, live and in an export
19460
+ * (the shutter follows the frame's own delta, so a 30 fps export and a
19461
+ * 120 Hz screen streak alike). `shutter` is the fraction of a 1/30 s frame
19462
+ * the shutter is open: 0.5 is film's 180°. Null switches it off, and then it
19463
+ * costs nothing. See shaders/passes/motion-blur.ts.
19464
+ */
19465
+ setMotionBlur(options: MotionBlurOptions | null): void {
19466
+ if (!options) {
19467
+ this.motionBlur = null
19468
+ this.mbPrevViewProj = null
19469
+ return
19470
+ }
19471
+ this.motionBlur = { shutter: Math.min(1, Math.max(0.05, options.shutter ?? 0.5)) }
19472
+ }
19473
+
19474
+ getMotionBlur(): Required<MotionBlurOptions> | null {
19475
+ return this.motionBlur ? { ...this.motionBlur } : null
19476
+ }
19477
+
19478
+ private mbEnsurePipelines(): boolean {
19479
+ if (this.mbVelocityPipeline && this.mbGatherPipeline) return true
19480
+ if (this.mbVelocityPending) return false
19481
+ this.mbVelocityPending = true
19482
+ const device = this.device
19483
+ this.mbFrameLayout = device.createBindGroupLayout({
19484
+ label: "motion velocity per-frame",
19485
+ entries: [
19486
+ { binding: 0, visibility: GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT, buffer: { type: "uniform" } },
19487
+ { binding: 1, visibility: GPUShaderStage.VERTEX, buffer: { type: "uniform" } },
19488
+ { binding: 2, visibility: GPUShaderStage.FRAGMENT, sampler: {} },
19489
+ ],
19490
+ })
19491
+ this.mbInstanceLayout = device.createBindGroupLayout({
19492
+ label: "motion velocity per-instance",
19493
+ entries: [
19494
+ { binding: 0, visibility: GPUShaderStage.VERTEX, buffer: { type: "read-only-storage" } },
19495
+ { binding: 1, visibility: GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT, buffer: { type: "uniform" } },
19496
+ { binding: 2, visibility: GPUShaderStage.VERTEX, buffer: { type: "read-only-storage" } },
19497
+ ],
19498
+ })
19499
+ this.mbPrevCameraBuffer = device.createBuffer({
19500
+ label: "motion blur previous camera",
19501
+ size: 64,
19502
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
19503
+ })
19504
+ this.mbUniformBuffer = device.createBuffer({
19505
+ label: "motion blur uniforms",
19506
+ size: MOTION_BLUR_UNIFORM_FLOATS * 4,
19507
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
19508
+ })
19509
+ this.mbSampler = device.createSampler({ magFilter: "linear", minFilter: "linear", addressModeU: "clamp-to-edge", addressModeV: "clamp-to-edge" })
19510
+ this.mbFrameBindGroup = device.createBindGroup({
19511
+ label: "motion velocity per-frame",
19512
+ layout: this.mbFrameLayout,
19513
+ entries: [
19514
+ { binding: 0, resource: { buffer: this.cameraUniformBuffer } },
19515
+ { binding: 1, resource: { buffer: this.mbPrevCameraBuffer } },
19516
+ { binding: 2, resource: this.materialSampler },
19517
+ ],
19518
+ })
19519
+ const velocityModule = device.createShaderModule({ label: "motion velocity", code: motionVelocityWgsl() })
19520
+ const gatherModule = device.createShaderModule({ label: "motion blur gather", code: MOTION_BLUR_WGSL })
19521
+ const velocity = device.createRenderPipelineAsync({
19522
+ label: "motion velocity",
19523
+ layout: device.createPipelineLayout({
19524
+ label: "motion velocity",
19525
+ bindGroupLayouts: [this.mbFrameLayout, this.mbInstanceLayout, this.mainPerMaterialBindGroupLayout],
19526
+ }),
19527
+ vertex: { module: velocityModule, entryPoint: "vs", buffers: this.materialVertexBufferLayouts.slice(0, 3) as GPUVertexBufferLayout[] },
19528
+ fragment: { module: velocityModule, entryPoint: "fs", targets: [{ format: MOTION_VELOCITY_FORMAT }] },
19529
+ primitive: { cullMode: "none" },
19530
+ multisample: { count: Engine.MULTISAMPLE_COUNT },
19531
+ depthStencil: { format: this.depthFormat, depthWriteEnabled: false, depthCompare: this.depthAhead },
19532
+ })
19533
+ const gather = device.createRenderPipelineAsync({
19534
+ label: "motion blur gather",
19535
+ layout: "auto",
19536
+ vertex: { module: gatherModule, entryPoint: "vs" },
19537
+ fragment: { module: gatherModule, entryPoint: "fs", targets: [{ format: this.hdrFormat }] },
19538
+ primitive: { topology: "triangle-list" },
19539
+ })
19540
+ Promise.all([velocity, gather])
19541
+ .then(([v, g]) => {
19542
+ this.mbVelocityPipeline = v
19543
+ this.mbGatherPipeline = g
19544
+ })
19545
+ .catch((e) => console.warn("[motion blur] pipelines failed:", e))
19546
+ return false
19547
+ }
19548
+
19549
+ /** The motion pass and the gather, on the resolved HDR before bloom. */
19550
+ private renderMotionBlur(encoder: GPUCommandEncoder, deltaTime: number): void {
19551
+ const mb = this.motionBlur
19552
+ if (!mb || !this.device || !this.hdrResolveTexture || !this.depthReadView) return
19553
+ const viewProj = this.camera.getProjectionMatrix().multiply(this.camera.getViewMatrix()).values
19554
+ // The shutter: this frame's motion is deltaTime's worth; the streak is the
19555
+ // shutter's worth of it. No delta (a paused export's first frame, a seek)
19556
+ // or a hitch long enough to be a cut: no blur, and nothing carried over.
19557
+ const scale = deltaTime > 1e-4 && deltaTime < 0.25 ? (mb.shutter / 30) / deltaTime : 0
19558
+ const prevVP = this.mbPrevViewProj ?? viewProj
19559
+ if (!this.mbEnsurePipelines() || scale === 0) {
19560
+ this.mbStorePrev(viewProj)
19561
+ return
19562
+ }
19563
+ const device = this.device
19564
+ const w = this.hdrResolveTexture.width
19565
+ const h = this.hdrResolveTexture.height
19566
+ if (!this.mbVelocity || this.mbVelocity.width !== w || this.mbVelocity.height !== h) {
19567
+ this.mbVelocityMs?.destroy()
19568
+ this.mbVelocity?.destroy()
19569
+ this.mbScratch?.destroy()
19570
+ this.mbVelocityMs = device.createTexture({
19571
+ label: "motion velocity (msaa)",
19572
+ size: [w, h],
19573
+ sampleCount: Engine.MULTISAMPLE_COUNT,
19574
+ format: MOTION_VELOCITY_FORMAT,
19575
+ usage: GPUTextureUsage.RENDER_ATTACHMENT,
19576
+ })
19577
+ this.mbVelocity = device.createTexture({
19578
+ label: "motion velocity",
19579
+ size: [w, h],
19580
+ format: MOTION_VELOCITY_FORMAT,
19581
+ usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING,
19582
+ })
19583
+ this.mbScratch = device.createTexture({
19584
+ label: "motion blur scratch",
19585
+ size: [w, h],
19586
+ format: this.hdrFormat,
19587
+ usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_SRC,
19588
+ })
19589
+ this.mbGatherBindGroup = null
19590
+ }
19591
+ // The HDR resolve and depth are rebuilt on resize; the group follows them.
19592
+ if (!this.mbGatherBindGroup || this.mbGatherDepthView !== this.depthReadView) {
19593
+ this.mbGatherDepthView = this.depthReadView
19594
+ this.mbGatherBindGroup = device.createBindGroup({
19595
+ label: "motion blur gather",
19596
+ layout: this.mbGatherPipeline!.getBindGroupLayout(0),
19597
+ entries: [
19598
+ { binding: 0, resource: this.hdrResolveTexture.createView() },
19599
+ { binding: 1, resource: this.mbVelocity.createView() },
19600
+ { binding: 2, resource: this.depthReadView },
19601
+ { binding: 3, resource: { buffer: this.mbUniformBuffer! } },
19602
+ { binding: 4, resource: this.mbSampler! },
19603
+ ],
19604
+ })
19605
+ }
19606
+
19607
+ // Last frame's camera and poses, then this frame's kept for the next.
19608
+ device.queue.writeBuffer(this.mbPrevCameraBuffer!, 0, prevVP as Float32Array<ArrayBuffer>)
19609
+ const u = this.mbUniformData
19610
+ const inv = new Float32Array(16)
19611
+ if (!Mat4.inverseInto(viewProj, inv)) inv.set(viewProj)
19612
+ u.set(inv, 0)
19613
+ u.set(prevVP, 16)
19614
+ u[32] = scale
19615
+ u[33] = Math.max(8, 0.06 * w)
19616
+ u[34] = w
19617
+ u[35] = h
19618
+ u[36] = this.depthClear
19619
+ device.queue.writeBuffer(this.mbUniformBuffer!, 0, u)
19620
+
19621
+ const depthAtt = this.renderPassDescriptor.depthStencilAttachment!
19622
+ const pass = encoder.beginRenderPass({
19623
+ label: "motion velocity",
19624
+ colorAttachments: [
19625
+ {
19626
+ view: this.mbVelocityMs!.createView(),
19627
+ resolveTarget: this.mbVelocity.createView(),
19628
+ clearValue: { r: 0, g: 0, b: 0, a: 0 },
19629
+ loadOp: "clear",
19630
+ storeOp: "discard",
19631
+ },
19632
+ ],
19633
+ depthStencilAttachment: { view: depthAtt.view, depthReadOnly: true, stencilReadOnly: true },
19634
+ })
19635
+ pass.setPipeline(this.mbVelocityPipeline!)
19636
+ pass.setBindGroup(0, this.mbFrameBindGroup!)
19637
+ this.forEachInstance((inst) => {
19638
+ if (!inst.model.drawn) return
19639
+ const s = this.mbInstance(inst)
19640
+ const cur = inst.model.getSkinMatrices()
19641
+ device.queue.writeBuffer(s.prev, 0, (s.last ?? cur) as Float32Array<ArrayBuffer>)
19642
+ if (!s.last || s.last.length !== cur.length) s.last = new Float32Array(cur.length)
19643
+ s.last.set(cur)
19644
+ this.setModelDrawState(pass, inst)
19645
+ pass.setBindGroup(1, s.bindGroup)
19646
+ for (const draw of inst.drawCalls) {
19647
+ if (draw.type !== "opaque" && draw.type !== "transparent") continue
19648
+ pass.setBindGroup(2, draw.bindGroup)
19649
+ this.issueDraw(pass, draw, "camera")
19650
+ }
19651
+ })
19652
+ pass.end()
19653
+
19654
+ const gather = encoder.beginRenderPass({
19655
+ label: "motion blur gather",
19656
+ colorAttachments: [{ view: this.mbScratch!.createView(), loadOp: "clear", clearValue: [0, 0, 0, 0], storeOp: "store" }],
19657
+ })
19658
+ gather.setPipeline(this.mbGatherPipeline!)
19659
+ gather.setBindGroup(0, this.mbGatherBindGroup)
19660
+ gather.draw(3)
19661
+ gather.end()
19662
+ encoder.copyTextureToTexture({ texture: this.mbScratch! }, { texture: this.hdrResolveTexture }, [w, h])
19663
+ this.mbStorePrev(viewProj)
19664
+ }
19665
+
19666
+ private mbStorePrev(viewProj: Float32Array): void {
19667
+ if (!this.mbPrevViewProj) this.mbPrevViewProj = new Float32Array(16)
19668
+ this.mbPrevViewProj.set(viewProj)
19669
+ }
19670
+
19671
+ /** A model's last-frame skin buffer and its velocity bind group, rebuilt if
19672
+ * the model's own buffers were replaced. */
19673
+ private mbInstance(inst: ModelInstance) {
19674
+ let s = this.mbInstances.get(inst)
19675
+ if (s && s.skin === inst.skinMatrixBuffer && s.light === inst.lightBuffer) return s
19676
+ s?.prev.destroy()
19677
+ const prev = this.device.createBuffer({
19678
+ label: `${inst.name}: previous skin`,
19679
+ size: inst.skinMatrixBuffer.size,
19680
+ usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
19681
+ })
19682
+ const bindGroup = this.device.createBindGroup({
19683
+ label: `${inst.name}: motion velocity`,
19684
+ layout: this.mbInstanceLayout!,
19685
+ entries: [
19686
+ { binding: 0, resource: { buffer: inst.skinMatrixBuffer } },
19687
+ { binding: 1, resource: { buffer: inst.lightBuffer } },
19688
+ { binding: 2, resource: { buffer: prev } },
19689
+ ],
19690
+ })
19691
+ s = { prev, bindGroup, last: null, skin: inst.skinMatrixBuffer, light: inst.lightBuffer }
19692
+ this.mbInstances.set(inst, s)
19693
+ return s
19694
+ }
19695
+
19291
19696
  /**
19292
19697
  * A repeating dissolve on one model, on the scene clock.
19293
19698
  *
@@ -20608,9 +21013,13 @@ export class Engine {
20608
21013
  pz += pos.z
20609
21014
  break
20610
21015
  }
20611
- u[44 + n * 4] = px
20612
- u[45 + n * 4] = py
20613
- u[46 + n * 4] = pz
21016
+ // The view uniform keeps only the first four: its slot 15 is the tone
21017
+ // curve. Every reader goes through the cast buffer, which holds all.
21018
+ if (n < 4) {
21019
+ u[44 + n * 4] = px
21020
+ u[45 + n * 4] = py
21021
+ u[46 + n * 4] = pz
21022
+ }
20614
21023
  this.writeCastEntry(inst, n, px, py, pz)
20615
21024
  n++
20616
21025
  })
@@ -20659,7 +21068,7 @@ export class Engine {
20659
21068
  const live = this.castSubjectCount
20660
21069
  const all = live === 0 ? 0 : (1 << live) - 1
20661
21070
  for (const e of this.effects) {
20662
- let mask = 0xf
21071
+ let mask = EFFECT_SUBJECTS_ALL
20663
21072
  if (e.subjects) {
20664
21073
  mask = 0
20665
21074
  for (const name of e.subjects) {
@@ -20675,7 +21084,7 @@ export class Engine {
20675
21084
  // The floods, one per distinct target set. Their masks are written here for
20676
21085
  // the same reason the effects' are: the slots have just been decided.
20677
21086
  for (const v of this.castDistanceVariants) {
20678
- let mask = 0xf
21087
+ let mask = EFFECT_SUBJECTS_ALL
20679
21088
  if (v.subjects) {
20680
21089
  mask = 0
20681
21090
  for (const name of v.subjects) {
package/src/index.ts CHANGED
@@ -40,6 +40,7 @@ export {
40
40
  type ViewLut,
41
41
  type CullDiagnostics,
42
42
  type MidiNote,
43
+ type MotionBlurOptions,
43
44
  } from "./engine"
44
45
  export { parsePmxFolderInput, pmxFileAtRelativePath, type PmxFolderInputResult } from "./folder-upload"
45
46
  export { parseMidi } from "./midi-loader"
@@ -228,7 +229,8 @@ export {
228
229
  * Pure strings, no GPU: importing them costs a host nothing.
229
230
  */
230
231
  export { EFFECT_MATH_API, PARTICLE_STRUCT_WGSL } from "./shaders/passes/hosted-api"
231
- export type { EyeTrackingOptions } from "./model"
232
+ export type { EyeTrackingOptions, AutoBlinkOptions } from "./model"
233
+ export type { CameraMountOptions, CameraMountMode } from "./camera-mount"
232
234
  export type { JellyEyeOptions } from "./jelly-eyes"
233
235
 
234
236
  /**