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/model.ts CHANGED
@@ -52,6 +52,70 @@ const smooth01 = (a: number, b: number, x: number): number => {
52
52
  const rangeMap = (v: number, inMax: number, out: number): number =>
53
53
  (Math.min(Math.abs(v), inMax) / inMax) * out * Math.sign(v)
54
54
 
55
+ // ─── Auto blink (see Model.setAutoBlink) ───
56
+ /** Morphs that close both eyes, in order of preference. */
57
+ const BLINK_BOTH = ["まばたき", "blink", "Blink", "eye_close", "Fcl_EYE_Close"]
58
+ /** Left and right pairs, for a model without one. ウィンク2 is the plain
59
+ * closed eye; ウィンク often comes with a smile, so it is the last resort. */
60
+ const BLINK_PAIRS: [string, string][] = [
61
+ ["ウィンク2", "ウィンク2右"],
62
+ ["ウィンク2", "ウィンク2右"],
63
+ ["blink_L", "blink_R"],
64
+ ["Fcl_EYE_Close_L", "Fcl_EYE_Close_R"],
65
+ ["ウィンク", "ウィンク右"],
66
+ ["ウィンク", "ウィンク右"],
67
+ ["wink", "wink_R"],
68
+ ["Wink", "Wink_R"],
69
+ ]
70
+ /** A blink's shape at speed 1, seconds — about 0.33 s in all, the 6–8 frames
71
+ * an anime blink runs at 24 fps. Measured blinks close in 75–100 ms, rest shut
72
+ * 10–50 ms and open about twice as slowly, quick at first and then settling
73
+ * (the lid lifts against the orbit's pull), so the close is short and the
74
+ * open long with a tail. A symmetric ease reads mechanical. */
75
+ const BLINK_CLOSE = 0.09
76
+ const BLINK_HOLD = 0.04
77
+ const BLINK_OPEN = 0.2
78
+ /** How often a blink is doubled, and how soon the second follows (at speed 1). */
79
+ const BLINK_DOUBLE = 0.15
80
+ const BLINK_DOUBLE_GAP = 0.4
81
+
82
+ /** What setAutoBlink asks for. */
83
+ export interface AutoBlinkOptions {
84
+ /** The average gap between blinks, seconds; each gap is drawn from half to
85
+ * one and a half times it. 1–10, default 4. */
86
+ interval?: number
87
+ /** How fast one blink is: 2 takes half as long. 0.5–2, default 1. */
88
+ speed?: number
89
+ /** Whether a blink is now and then followed straight by a second. Default true. */
90
+ double?: boolean
91
+ }
92
+ /** A fixed value in 0..1 for an integer — the same sequence every take. */
93
+ const blinkHash = (n: number): number => {
94
+ let h = Math.imul(n ^ 0x9e3779b9, 0x85ebca6b)
95
+ h = Math.imul(h ^ (h >>> 13), 0xc2b2ae35)
96
+ h ^= h >>> 16
97
+ return (h >>> 0) / 4294967296
98
+ }
99
+ const blinkGap = (n: number, interval: number): number => interval * (0.5 + blinkHash(n))
100
+ /** How shut the eyes are t seconds into a blink at speed 1. The close is a
101
+ * quick smooth drop; the open leaves fast and settles slowly (a cubic ease-out,
102
+ * most of the lift in the first third and the rest a long approach to open). */
103
+ const blinkCurve = (t: number): number => {
104
+ if (t < 0) return 0
105
+ if (t < BLINK_CLOSE) {
106
+ const x = t / BLINK_CLOSE
107
+ return x * x * (3 - 2 * x)
108
+ }
109
+ t -= BLINK_CLOSE
110
+ if (t < BLINK_HOLD) return 1
111
+ t -= BLINK_HOLD
112
+ if (t < BLINK_OPEN) {
113
+ const x = 1 - t / BLINK_OPEN
114
+ return x * x * x
115
+ }
116
+ return 0
117
+ }
118
+
55
119
  /** What setEyeTracking asks for. */
56
120
  export interface EyeTrackingOptions {
57
121
  /** 0..1 — how much of the solved gaze replaces the motion's own. Default 1. */
@@ -1278,6 +1342,132 @@ export class Model {
1278
1342
  return this.eyeTracking !== null
1279
1343
  }
1280
1344
 
1345
+ // ─── Auto blink ──────────────────────────────────────────────────────
1346
+ /** The closing morphs auto blink drives (one for both eyes, or a left and
1347
+ * right pair), or null while it is off. */
1348
+ private autoBlink: {
1349
+ morphs: number[]
1350
+ /** Each morph's weight without the blink: what the motion or the host set,
1351
+ * handed back when the blink goes off. */
1352
+ base: number[]
1353
+ /** What the blink last wrote, to tell a fresh write from our own. */
1354
+ written: number[]
1355
+ clock: number
1356
+ /** When the current blink started, and the next. */
1357
+ start: number
1358
+ next: number
1359
+ count: number
1360
+ /** Whether the current blink is the first of a double. */
1361
+ double: boolean
1362
+ options: Required<AutoBlinkOptions>
1363
+ } | null = null
1364
+
1365
+ /**
1366
+ * Auto blink: she blinks every few seconds on her own, as eyes do — a
1367
+ * natural irregular gap around `interval`, now and then a double blink.
1368
+ *
1369
+ * A MORPH, on the model's own closing morph (まばたき, or a left and right
1370
+ * wink pair when there is none). It OWNS that morph while on, as eye
1371
+ * tracking owns the eye bones: a morph motion's own まばたき keys are
1372
+ * replaced, and handed back when it goes off. Its other expressions (a
1373
+ * smile, a wince) play as keyed. Steps on the scene clock (the delta
1374
+ * update() is handed, every frame, playing or paused) through a fixed
1375
+ * sequence of gaps.
1376
+ *
1377
+ * Null hands the morph back. Returns false for a model with no closing morph.
1378
+ */
1379
+ setAutoBlink(options: AutoBlinkOptions | null): boolean {
1380
+ if (!options) {
1381
+ const b = this.autoBlink
1382
+ if (b) {
1383
+ b.morphs.forEach((m, i) => (this.runtimeMorph.weights[m] = b.base[i]))
1384
+ this.morphsDirty = true
1385
+ }
1386
+ this.autoBlink = null
1387
+ return true
1388
+ }
1389
+ const opts: Required<AutoBlinkOptions> = {
1390
+ interval: Math.min(10, Math.max(1, options.interval ?? 4)),
1391
+ speed: Math.min(2, Math.max(0.5, options.speed ?? 1)),
1392
+ double: options.double ?? true,
1393
+ }
1394
+ if (this.autoBlink) {
1395
+ // A new interval takes from the next gap on; the current one stands.
1396
+ this.autoBlink.options = opts
1397
+ return true
1398
+ }
1399
+ const morphs = this.blinkMorphs()
1400
+ if (!morphs) return false
1401
+ const weights = this.runtimeMorph.weights
1402
+ this.autoBlink = {
1403
+ morphs,
1404
+ base: morphs.map((m) => weights[m]),
1405
+ written: morphs.map((m) => weights[m]),
1406
+ clock: 0,
1407
+ start: -1,
1408
+ next: blinkGap(0, opts.interval),
1409
+ count: 0,
1410
+ double: false,
1411
+ options: opts,
1412
+ }
1413
+ return true
1414
+ }
1415
+
1416
+ getAutoBlink(): Required<AutoBlinkOptions> | null {
1417
+ return this.autoBlink ? { ...this.autoBlink.options } : null
1418
+ }
1419
+
1420
+ /** The morphs that close both eyes, by the names MMD, VRoid and the usual
1421
+ * converters give them. A smile or a wince closes them too and is no blink. */
1422
+ private blinkMorphs(): number[] | null {
1423
+ const ni = this.runtimeMorph.nameIndex
1424
+ for (const n of BLINK_BOTH) if (ni[n] !== undefined) return [ni[n]]
1425
+ for (const [l, r] of BLINK_PAIRS) if (ni[l] !== undefined && ni[r] !== undefined) return [ni[l], ni[r]]
1426
+ return null
1427
+ }
1428
+
1429
+ /** One frame of auto blink, after the pose sources set the morphs and before
1430
+ * they are applied. True when it moved a weight. */
1431
+ private stepAutoBlink(dt: number): boolean {
1432
+ const b = this.autoBlink!
1433
+ const { interval, speed, double } = b.options
1434
+ const weights = this.runtimeMorph.weights
1435
+ // A weight that is not what we wrote was set afresh this frame.
1436
+ for (let i = 0; i < b.morphs.length; i++) {
1437
+ const w = weights[b.morphs[i]]
1438
+ if (w !== b.written[i]) b.base[i] = w
1439
+ }
1440
+ if (dt > 0) b.clock += dt
1441
+ // A seek back (or a scrub to the start) begins the sequence again.
1442
+ if (dt < 0) {
1443
+ b.clock = 0
1444
+ b.start = -1
1445
+ b.count = 0
1446
+ b.double = false
1447
+ b.next = blinkGap(0, interval)
1448
+ }
1449
+ // A loop, so a long jump forward lands on the blink it is in, not on each one passed.
1450
+ while (b.clock >= b.next) {
1451
+ b.start = b.next
1452
+ b.count++
1453
+ // Now and then a second blink follows straight after the first — never a third.
1454
+ b.double = double && !b.double && blinkHash(b.count * 7 + 3) < BLINK_DOUBLE
1455
+ b.next = b.start + (b.double ? BLINK_DOUBLE_GAP / speed : blinkGap(b.count, interval))
1456
+ }
1457
+ const w = blinkCurve((b.clock - b.start) * speed)
1458
+ let changed = false
1459
+ for (let i = 0; i < b.morphs.length; i++) {
1460
+ if (weights[b.morphs[i]] !== w) {
1461
+ weights[b.morphs[i]] = w
1462
+ changed = true
1463
+ }
1464
+ // Read back: the weights are float32, and a float64 kept here never
1465
+ // compares equal, so our own write passed for the motion's next frame.
1466
+ b.written[i] = weights[b.morphs[i]]
1467
+ }
1468
+ return changed
1469
+ }
1470
+
1281
1471
  /**
1282
1472
  * Where she looks: a unit direction in model space, or null for a model
1283
1473
  * without eye bones. See RzSubject.gaze.
@@ -2960,6 +3150,9 @@ export class Model {
2960
3150
  Quat.multiplyInto(r, offset, r)
2961
3151
  }
2962
3152
 
3153
+ // The blink over whatever the motion and the host set this frame.
3154
+ if (this.autoBlink !== null && this.stepAutoBlink(deltaTime)) this.morphsDirty = true
3155
+
2963
3156
  // Apply morphs if tweens changed morphs or animation changed morphs
2964
3157
  const verticesChanged = this.morphsDirty || tweensChangedMorphs
2965
3158
  if (this.morphsDirty || tweensChangedMorphs) {
@@ -32,7 +32,14 @@
32
32
  // — the field clock's z, the particle and trail pads, the grid's and the light
33
33
  // emitter's blocks — and each of those is written once a frame by the engine.
34
34
 
35
- import { EFFECT_ANCHORS, EFFECT_SUBJECTS, EFFECT_TRAIL_BASE, EFFECT_TRAIL_SAMPLES, EFFECT_SUBJECT_VEC4S } from "./cast-layout"
35
+ import {
36
+ EFFECT_ANCHORS,
37
+ EFFECT_SUBJECTS,
38
+ EFFECT_SUBJECTS_ALL,
39
+ EFFECT_TRAIL_BASE,
40
+ EFFECT_TRAIL_SAMPLES,
41
+ EFFECT_SUBJECT_VEC4S,
42
+ } from "./cast-layout"
36
43
 
37
44
  export const CAST_API = /* wgsl */ `
38
45
  const RZ_SUBJECTS: i32 = ${EFFECT_SUBJECTS};
@@ -212,4 +219,4 @@ fn _rzSubjectMask() -> u32 { return ${mask}; }
212
219
  * composite's own module, where nothing user-written lands and the accessors
213
220
  * exist only so the shared API compiles — and the value the engine writes for
214
221
  * an effect the scene has not aimed at anybody in particular. */
215
- export const CAST_MASK_ALL = "0xfu"
222
+ export const CAST_MASK_ALL = `${EFFECT_SUBJECTS_ALL}u`
@@ -13,7 +13,9 @@
13
13
  * and the first thing to touch the cycle throws ReferenceError at module load —
14
14
  * the whole engine failing to start on an import order nobody chose.
15
15
  */
16
- export const EFFECT_SUBJECTS = 4
16
+ export const EFFECT_SUBJECTS = 8
17
+ /** Every subject slot's bit: an effect aimed at nobody in particular. */
18
+ export const EFFECT_SUBJECTS_ALL = (1 << EFFECT_SUBJECTS) - 1
17
19
  /** vec4 slots per subject: root+dissolve, hip+objectId, bounds, gaze+looking. */
18
20
  export const EFFECT_SUBJECT_VEC4S = 4
19
21
  /**
@@ -3,7 +3,7 @@ import { sceneTapApi } from "../scene-tap"
3
3
  import { anchorAliasWgsl } from "../anchor-table"
4
4
  import { CAST_API, CAST_MASK_ALL, subjectMaskApi } from "../cast-api"
5
5
  import { clockApi, EFFECT_MATH_API, PARTICLE_STRUCT_WGSL, trailSlotsApi, viewportApi } from "./hosted-api"
6
- import { EFFECT_ANCHORS, EFFECT_SUBJECTS, EFFECT_TRAIL_BASE, EFFECT_TRAIL_SAMPLES } from "../cast-layout"
6
+ import { EFFECT_ANCHORS, EFFECT_SUBJECTS, EFFECT_SUBJECT_VEC4S, EFFECT_TRAIL_BASE, EFFECT_TRAIL_SAMPLES } from "../cast-layout"
7
7
  import { audioApi } from "../audio-api"
8
8
  import { idApi } from "../id-api"
9
9
  import { sceneLightApi } from "../scene-light-api"
@@ -154,7 +154,8 @@ override APPLY_GAMMA: bool = true;
154
154
  // viewU[9] = (grade slope.rgb, grade flags) — bit 0 the CDL grade(), bit 1 the
155
155
  // scene's own LUT (setStageGrade) — see _rzGradeScene() below.
156
156
  // viewU[10] = (camera world position, _) — refreshed with the basis above.
157
- // viewU[11..14] = the cast's positions (effects; count in viewU[10].w).
157
+ // viewU[11..14] = the first four of the cast's positions (count in viewU[10].w);
158
+ // rzSubjectHip reads the cast buffer instead, which holds all of them.
158
159
  // viewU[15] = (soft curve contrast, pixel filter weights for offsets 0, ±1, ±2)
159
160
  // — 1, 0, 0 is no filter; see _rzPixelFiltered.
160
161
  // invGamma = 1/gamma precomputed on CPU — avoids a per-pixel divide.
@@ -543,7 +544,12 @@ fn bgSubjectCount() -> i32 { return rzSubjectCount(); }
543
544
  * mapping is the identity for an effect aimed at nobody in particular, which is
544
545
  * every scene published before one could be aimed.
545
546
  */
546
- fn rzSubjectHip(i: i32) -> vec3f { return viewU[11 + clamp(_rzSubjectSlot(i), 0, 3)].xyz; }
547
+ //
548
+ // Read from the cast buffer's centre, the same point the engine writes to both.
549
+ // It was viewU[11..14], which holds only four: viewU[15] is the tone curve.
550
+ fn rzSubjectHip(i: i32) -> vec3f {
551
+ return _rzCast[clamp(_rzSubjectSlot(i), 0, RZ_SUBJECTS - 1) * ${EFFECT_SUBJECT_VEC4S} + 1].xyz;
552
+ }
547
553
 
548
554
  fn bgSubjectPos(i: i32) -> vec3f { return rzSubjectHip(i); }
549
555
 
@@ -135,8 +135,11 @@ fn cs(@builtin(global_invocation_id) gid: vec3<u32>) {
135
135
  // and still its shadow, which the engine's pass casts for every material.
136
136
  let shown = (hidden[i] & 1u) == 0u;
137
137
  let graph = (hidden[i] & 2u) == 0u;
138
+ // bit 2: kept out of the camera alone — her own head under a POV mount, which
139
+ // her shadow and her mirror image still carry.
140
+ let toCamera = (hidden[i] & 4u) == 0u;
138
141
  let castsShadow = (dm.flags & DRAW_CASTS_SHADOW) != 0u;
139
- cameraArgs[i * 5u + 1u] = select(0u, 1u, (inCamera || off) && shown && graph);
142
+ cameraArgs[i * 5u + 1u] = select(0u, 1u, (inCamera || off) && shown && graph && toCamera);
140
143
  shadowArgs[i * 5u + 1u] = select(0u, 1u, ((inLight && castsShadow) || off) && shown);
141
144
  mirrorArgs[i * 5u + 1u] = select(0u, 1u, (inMirror || off) && shown && graph);
142
145
  }
@@ -0,0 +1,171 @@
1
+ /**
2
+ * Motion blur: a per-pixel velocity buffer and a gather along it.
3
+ *
4
+ * TWO PASSES, both run only while it is on (Engine.setMotionBlur):
5
+ *
6
+ * · VELOCITY. Every model is drawn again against the scene's own depth (read
7
+ * only, compare "ahead or equal"), so exactly the visible surface writes:
8
+ * where its pixel is now minus where the same skinned vertex was last frame
9
+ * — last frame's skin matrices and last frame's camera. Its own pass and its
10
+ * own pipelines, so no scene pipeline changes and nothing costs anything
11
+ * while blur is off. The same skinned vertex path and @invariant position as
12
+ * the depth prepass, so it lands on the depths the scene wrote; the same
13
+ * alpha cutout, dissolve and x-ray tests, so a hair card's holes do not
14
+ * claim the card's motion. The target holds (velocity × coverage, coverage)
15
+ * so the MSAA resolve averages an edge into a fraction instead of garbage.
16
+ *
17
+ * · GATHER, on the resolved HDR before bloom and the view transform (bloom
18
+ * then blooms the streak, as a camera's would). Where no model drew — the
19
+ * ground, the sky, a game stage — the motion is the CAMERA's, rebuilt from
20
+ * depth: the pixel's world point this frame, projected through last frame's
21
+ * camera. The blur is the pixel's own velocity × the shutter, a straight
22
+ * average of samples along it, clamped to a few percent of the frame.
23
+ *
24
+ * What it does not do (yet): a fast arm smears within itself, not out over a
25
+ * still background, which would take a neighbourhood max velocity (tile max).
26
+ * Morphs carry no motion: a blink is not blurred.
27
+ */
28
+
29
+ import { DISSOLVE_WGSL, JELLY_VERTEX_WGSL, OBJECT_LIGHT_STRUCT_WGSL, XRAY_WGSL } from "../materials/common"
30
+
31
+ /** The velocity target: (velocity NDC xy × coverage, coverage, 1). */
32
+ export const MOTION_VELOCITY_FORMAT: GPUTextureFormat = "rgba16float"
33
+
34
+ export function motionVelocityWgsl(): string {
35
+ return /* wgsl */ `
36
+ ${DISSOLVE_WGSL}
37
+ struct CameraUniforms { view: mat4x4f, projection: mat4x4f, viewPos: vec3f, _p: f32, };
38
+ // The head of the material block, as the depth prepass declares it.
39
+ struct MaterialUniforms {
40
+ diffuseColor: vec3f,
41
+ alpha: f32,
42
+ _skip0: vec4f,
43
+ _skip1: vec4f,
44
+ _skip2: f32,
45
+ materialId: f32,
46
+ _skip3: f32,
47
+ dissolve: f32,
48
+ };
49
+ struct PrevCamera { viewProj: mat4x4f, };
50
+
51
+ @group(0) @binding(0) var<uniform> camera: CameraUniforms;
52
+ @group(0) @binding(1) var<uniform> prevCamera: PrevCamera;
53
+ @group(0) @binding(2) var diffuseSampler: sampler;
54
+ @group(1) @binding(0) var<storage, read> skinMats: array<mat4x4f>;
55
+ ${OBJECT_LIGHT_STRUCT_WGSL}
56
+ @group(1) @binding(1) var<uniform> objectLight: ObjectLight;
57
+ @group(1) @binding(2) var<storage, read> prevSkinMats: array<mat4x4f>;
58
+ ${XRAY_WGSL}
59
+ ${JELLY_VERTEX_WGSL}
60
+ @group(2) @binding(0) var diffuseTexture: texture_2d<f32>;
61
+ @group(2) @binding(1) var<uniform> material: MaterialUniforms;
62
+
63
+ struct VSOut {
64
+ @builtin(position) @invariant position: vec4f,
65
+ @location(0) uv: vec2f,
66
+ @location(1) restPos: vec3f,
67
+ @location(2) clipNow: vec4f,
68
+ @location(3) clipPrev: vec4f,
69
+ };
70
+
71
+ @vertex fn vs(
72
+ @location(0) position: vec3f,
73
+ @location(1) normal: vec3f,
74
+ @location(2) uv: vec2f,
75
+ @location(3) joints0: vec4<u32>,
76
+ @location(4) weights0: vec4<f32>,
77
+ ) -> VSOut {
78
+ let weightSum = weights0.x + weights0.y + weights0.z + weights0.w;
79
+ let invWeightSum = select(1.0, 1.0 / weightSum, weightSum > 0.0001);
80
+ let w = select(vec4f(1.0, 0.0, 0.0, 0.0), weights0 * invWeightSum, weightSum > 0.0001);
81
+ var pos4 = vec4f(position, 1.0);
82
+ if (objectLight.jelly[0].w > 0.0) { pos4 = vec4f(rzJellyVertex(position, joints0, w), 1.0); }
83
+ var skinned = vec4f(0.0);
84
+ var prev = vec4f(0.0);
85
+ for (var i = 0u; i < 4u; i++) {
86
+ skinned += (skinMats[joints0[i]] * pos4) * w[i];
87
+ prev += (prevSkinMats[joints0[i]] * pos4) * w[i];
88
+ }
89
+ var o: VSOut;
90
+ o.position = camera.projection * camera.view * vec4f(skinned.xyz, 1.0);
91
+ o.uv = uv;
92
+ o.restPos = position;
93
+ o.clipNow = o.position;
94
+ o.clipPrev = prevCamera.viewProj * vec4f(prev.xyz, 1.0);
95
+ return o;
96
+ }
97
+
98
+ @fragment fn fs(in: VSOut) -> @location(0) vec4f {
99
+ let a = material.alpha * textureSample(diffuseTexture, diffuseSampler, in.uv).a;
100
+ if (a < 0.5) { discard; }
101
+ if (material.dissolve < 0.9995 && rz_dissolve_threshold(in.restPos) > material.dissolve) { discard; }
102
+ if (rz_xray_cut(in.position, material.materialId)) { discard; }
103
+ let now = in.clipNow.xy / in.clipNow.w;
104
+ let prev = in.clipPrev.xy / max(in.clipPrev.w, 1e-5);
105
+ return vec4f(now - prev, 1.0, 1.0);
106
+ }
107
+ `
108
+ }
109
+
110
+ /** Uniforms of the gather, in floats: invViewProj (16), prevViewProj (16),
111
+ * (scale, maxPx, width, height), (depthClear, 0, 0, 0). */
112
+ export const MOTION_BLUR_UNIFORM_FLOATS = 40
113
+
114
+ export const MOTION_BLUR_WGSL = /* wgsl */ `
115
+ struct Uniforms {
116
+ invViewProj: mat4x4f,
117
+ prevViewProj: mat4x4f,
118
+ // x: shutter scale (this frame's motion → streak), y: longest streak in px,
119
+ // zw: target size in px.
120
+ params: vec4f,
121
+ // x: the depth a cleared pixel holds (the far plane, either end).
122
+ depth: vec4f,
123
+ };
124
+ @group(0) @binding(0) var hdr: texture_2d<f32>;
125
+ @group(0) @binding(1) var velocity: texture_2d<f32>;
126
+ @group(0) @binding(2) var depthTex: texture_depth_multisampled_2d;
127
+ @group(0) @binding(3) var<uniform> u: Uniforms;
128
+ @group(0) @binding(4) var linearSampler: sampler;
129
+
130
+ struct VOut { @builtin(position) pos: vec4f, @location(0) uv: vec2f, };
131
+
132
+ @vertex fn vs(@builtin(vertex_index) i: u32) -> VOut {
133
+ let p = vec2f(f32((i << 1u) & 2u), f32(i & 2u));
134
+ var o: VOut;
135
+ o.pos = vec4f(p * 2.0 - 1.0, 0.0, 1.0);
136
+ o.uv = vec2f(p.x, 1.0 - p.y);
137
+ return o;
138
+ }
139
+
140
+ const SAMPLES: i32 = 16;
141
+
142
+ @fragment fn fs(in: VOut) -> @location(0) vec4f {
143
+ let size = u.params.zw;
144
+ let px = vec2i(in.pos.xy);
145
+ let vel = textureLoad(velocity, px, 0);
146
+ // The camera's share: where this pixel's world point was last frame.
147
+ let ndc = vec2f(in.uv.x * 2.0 - 1.0, 1.0 - in.uv.y * 2.0);
148
+ var d = textureLoad(depthTex, px, 0);
149
+ // A cleared pixel is the far plane, which an infinite projection cannot
150
+ // invert: nudged a hair inside it, it reprojects as a direction.
151
+ d = select(min(d, 0.999999), max(d, 0.000001), u.depth.x < 0.5);
152
+ let world = u.invViewProj * vec4f(ndc, d, 1.0);
153
+ let prevClip = u.prevViewProj * vec4f(world.xyz / world.w, 1.0);
154
+ let camVel = ndc - prevClip.xy / max(prevClip.w, 1e-5);
155
+ // Where models drew, theirs, by how much of the pixel they cover.
156
+ let v = vel.xy + (1.0 - clamp(vel.z, 0.0, 1.0)) * camVel;
157
+ // NDC to pixels (y down), times the shutter, clamped.
158
+ var streak = vec2f(v.x, -v.y) * 0.5 * size * u.params.x;
159
+ let len = length(streak);
160
+ if (len > u.params.y) { streak *= u.params.y / len; }
161
+ let centre = textureLoad(hdr, px, 0);
162
+ if (length(streak) < 0.5) { return centre; }
163
+ var sum = vec3f(0.0);
164
+ for (var i = 0; i < SAMPLES; i++) {
165
+ let t = (f32(i) + 0.5) / f32(SAMPLES) - 0.5;
166
+ let uv = in.uv + streak * t / size;
167
+ sum += textureSampleLevel(hdr, linearSampler, uv, 0.0).rgb;
168
+ }
169
+ return vec4f(sum / f32(SAMPLES), centre.a);
170
+ }
171
+ `