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
@@ -1,2 +1,2 @@
1
- export declare const CULL_COMPUTE_WGSL = "\nstruct DrawMeta {\n lo: vec3f, // model-space AABB min \u2014 read only for RIGID models\n model: u32, // index into models[]\n hi: vec3f, // model-space AABB max\n flags: u32, // bit0 = casts shadow\n};\n\nstruct ModelRec {\n xform: mat4x4f, // model space -> world; meaningful only when RIGID\n sphere: vec4f, // world-space centre.xyz + radius.w; meaningful when SKINNED\n flags: u32, // bit0 = visible, bit1 = rigid\n pad0: u32,\n pad1: u32,\n pad2: u32,\n};\n\n// Camera planes at 0..5, light planes at 6..11, mirror planes at 12..17.\n// counts.x = draw count, counts.y = 0 to pass everything (setCullEnabled).\nstruct Frusta {\n planes: array<vec4f, 18>,\n counts: vec4u,\n};\n\n@group(0) @binding(0) var<storage, read> metas: array<DrawMeta>;\n@group(0) @binding(1) var<storage, read> models: array<ModelRec>;\n@group(0) @binding(2) var<uniform> frusta: Frusta;\n@group(0) @binding(3) var<storage, read_write> cameraArgs: array<u32>;\n@group(0) @binding(4) var<storage, read_write> shadowArgs: array<u32>;\n// 1 = the user hid this material, or a material morph drove its alpha to zero.\n// Here rather than in the encode loop because a render bundle bakes its draw\n// list: a face VMD rewrites the morph-hidden set every frame, so a bundle that\n// invalidated on it would re-record every frame and cost more than it saves.\n@group(0) @binding(5) var<storage, read> hidden: array<u32>;\n@group(0) @binding(6) var<storage, read_write> mirrorArgs: array<u32>;\n\nconst DRAW_CASTS_SHADOW: u32 = 1u;\nconst MODEL_VISIBLE: u32 = 1u;\nconst MODEL_RIGID: u32 = 2u;\n\n// Signed distance of the AABB's most-positive corner: the centre distance plus\n// the extent projected onto |n|. Negative means every corner is behind the\n// plane, which is the only case that can reject.\nfn aabbVisible(base: u32, c: vec3f, e: vec3f) -> bool {\n for (var i = 0u; i < 6u; i = i + 1u) {\n let p = frusta.planes[base + i];\n if (dot(p.xyz, c) + p.w + dot(e, abs(p.xyz)) < 0.0) { return false; }\n }\n return true;\n}\n\nfn sphereVisible(base: u32, s: vec4f) -> bool {\n for (var i = 0u; i < 6u; i = i + 1u) {\n let p = frusta.planes[base + i];\n if (dot(p.xyz, s.xyz) + p.w + s.w < 0.0) { return false; }\n }\n return true;\n}\n\n@compute @workgroup_size(64)\nfn cs(@builtin(global_invocation_id) gid: vec3<u32>) {\n let i = gid.x;\n if (i >= frusta.counts.x) { return; }\n\n // Named dm, not meta: \"meta\" is a WGSL reserved word (reserved for future use,\n // so it parses nowhere). No backticks anywhere in this string either \u2014 it is a\n // template literal, and one would end the shader mid-comment.\n let dm = metas[i];\n let m = models[dm.model];\n let visible = (m.flags & MODEL_VISIBLE) != 0u;\n\n var inCamera = false;\n var inLight = false;\n var inMirror = false;\n if (visible) {\n if ((m.flags & MODEL_RIGID) != 0u) {\n let c = (dm.lo + dm.hi) * 0.5;\n let e = (dm.hi - dm.lo) * 0.5;\n let wc = (m.xform * vec4f(c, 1.0)).xyz;\n // Extent of the transformed box: the abs of the linear part applied to the\n // half-extent. Exact for the axis-aligned case, conservative for a rotation.\n let a = mat3x3f(abs(m.xform[0].xyz), abs(m.xform[1].xyz), abs(m.xform[2].xyz));\n let we = a * e;\n inCamera = aabbVisible(0u, wc, we);\n inLight = aabbVisible(6u, wc, we);\n inMirror = aabbVisible(12u, wc, we);\n } else {\n inCamera = sphereVisible(0u, m.sphere);\n inLight = sphereVisible(6u, m.sphere);\n inMirror = sphereVisible(12u, m.sphere);\n }\n }\n\n // Culling off (setCullEnabled) passes everything, so a scene can be A/B'd\n // against its own unculled self without rebuilding anything. The pass still\n // runs, so the diagnostics keep reporting. It bypasses the FRUSTUM only \u2014\n // a hidden material is not culled, it is switched off, and answering \"is this\n // gone because of culling?\" requires the two to stay separable.\n let off = frusta.counts.y == 0u;\n // hidden: bit 0 switched off; bit 1 drawn by the native-material host\n // (unity/host.ts) rather than by its graph \u2014 no camera or mirror draw here,\n // and still its shadow, which the engine's pass casts for every material.\n let shown = (hidden[i] & 1u) == 0u;\n let graph = (hidden[i] & 2u) == 0u;\n let castsShadow = (dm.flags & DRAW_CASTS_SHADOW) != 0u;\n cameraArgs[i * 5u + 1u] = select(0u, 1u, (inCamera || off) && shown && graph);\n shadowArgs[i * 5u + 1u] = select(0u, 1u, ((inLight && castsShadow) || off) && shown);\n mirrorArgs[i * 5u + 1u] = select(0u, 1u, (inMirror || off) && shown && graph);\n}\n";
1
+ export declare const CULL_COMPUTE_WGSL = "\nstruct DrawMeta {\n lo: vec3f, // model-space AABB min \u2014 read only for RIGID models\n model: u32, // index into models[]\n hi: vec3f, // model-space AABB max\n flags: u32, // bit0 = casts shadow\n};\n\nstruct ModelRec {\n xform: mat4x4f, // model space -> world; meaningful only when RIGID\n sphere: vec4f, // world-space centre.xyz + radius.w; meaningful when SKINNED\n flags: u32, // bit0 = visible, bit1 = rigid\n pad0: u32,\n pad1: u32,\n pad2: u32,\n};\n\n// Camera planes at 0..5, light planes at 6..11, mirror planes at 12..17.\n// counts.x = draw count, counts.y = 0 to pass everything (setCullEnabled).\nstruct Frusta {\n planes: array<vec4f, 18>,\n counts: vec4u,\n};\n\n@group(0) @binding(0) var<storage, read> metas: array<DrawMeta>;\n@group(0) @binding(1) var<storage, read> models: array<ModelRec>;\n@group(0) @binding(2) var<uniform> frusta: Frusta;\n@group(0) @binding(3) var<storage, read_write> cameraArgs: array<u32>;\n@group(0) @binding(4) var<storage, read_write> shadowArgs: array<u32>;\n// 1 = the user hid this material, or a material morph drove its alpha to zero.\n// Here rather than in the encode loop because a render bundle bakes its draw\n// list: a face VMD rewrites the morph-hidden set every frame, so a bundle that\n// invalidated on it would re-record every frame and cost more than it saves.\n@group(0) @binding(5) var<storage, read> hidden: array<u32>;\n@group(0) @binding(6) var<storage, read_write> mirrorArgs: array<u32>;\n\nconst DRAW_CASTS_SHADOW: u32 = 1u;\nconst MODEL_VISIBLE: u32 = 1u;\nconst MODEL_RIGID: u32 = 2u;\n\n// Signed distance of the AABB's most-positive corner: the centre distance plus\n// the extent projected onto |n|. Negative means every corner is behind the\n// plane, which is the only case that can reject.\nfn aabbVisible(base: u32, c: vec3f, e: vec3f) -> bool {\n for (var i = 0u; i < 6u; i = i + 1u) {\n let p = frusta.planes[base + i];\n if (dot(p.xyz, c) + p.w + dot(e, abs(p.xyz)) < 0.0) { return false; }\n }\n return true;\n}\n\nfn sphereVisible(base: u32, s: vec4f) -> bool {\n for (var i = 0u; i < 6u; i = i + 1u) {\n let p = frusta.planes[base + i];\n if (dot(p.xyz, s.xyz) + p.w + s.w < 0.0) { return false; }\n }\n return true;\n}\n\n@compute @workgroup_size(64)\nfn cs(@builtin(global_invocation_id) gid: vec3<u32>) {\n let i = gid.x;\n if (i >= frusta.counts.x) { return; }\n\n // Named dm, not meta: \"meta\" is a WGSL reserved word (reserved for future use,\n // so it parses nowhere). No backticks anywhere in this string either \u2014 it is a\n // template literal, and one would end the shader mid-comment.\n let dm = metas[i];\n let m = models[dm.model];\n let visible = (m.flags & MODEL_VISIBLE) != 0u;\n\n var inCamera = false;\n var inLight = false;\n var inMirror = false;\n if (visible) {\n if ((m.flags & MODEL_RIGID) != 0u) {\n let c = (dm.lo + dm.hi) * 0.5;\n let e = (dm.hi - dm.lo) * 0.5;\n let wc = (m.xform * vec4f(c, 1.0)).xyz;\n // Extent of the transformed box: the abs of the linear part applied to the\n // half-extent. Exact for the axis-aligned case, conservative for a rotation.\n let a = mat3x3f(abs(m.xform[0].xyz), abs(m.xform[1].xyz), abs(m.xform[2].xyz));\n let we = a * e;\n inCamera = aabbVisible(0u, wc, we);\n inLight = aabbVisible(6u, wc, we);\n inMirror = aabbVisible(12u, wc, we);\n } else {\n inCamera = sphereVisible(0u, m.sphere);\n inLight = sphereVisible(6u, m.sphere);\n inMirror = sphereVisible(12u, m.sphere);\n }\n }\n\n // Culling off (setCullEnabled) passes everything, so a scene can be A/B'd\n // against its own unculled self without rebuilding anything. The pass still\n // runs, so the diagnostics keep reporting. It bypasses the FRUSTUM only \u2014\n // a hidden material is not culled, it is switched off, and answering \"is this\n // gone because of culling?\" requires the two to stay separable.\n let off = frusta.counts.y == 0u;\n // hidden: bit 0 switched off; bit 1 drawn by the native-material host\n // (unity/host.ts) rather than by its graph \u2014 no camera or mirror draw here,\n // and still its shadow, which the engine's pass casts for every material.\n let shown = (hidden[i] & 1u) == 0u;\n let graph = (hidden[i] & 2u) == 0u;\n // bit 2: kept out of the camera alone \u2014 her own head under a POV mount, which\n // her shadow and her mirror image still carry.\n let toCamera = (hidden[i] & 4u) == 0u;\n let castsShadow = (dm.flags & DRAW_CASTS_SHADOW) != 0u;\n cameraArgs[i * 5u + 1u] = select(0u, 1u, (inCamera || off) && shown && graph && toCamera);\n shadowArgs[i * 5u + 1u] = select(0u, 1u, ((inLight && castsShadow) || off) && shown);\n mirrorArgs[i * 5u + 1u] = select(0u, 1u, (inMirror || off) && shown && graph);\n}\n";
2
2
  //# sourceMappingURL=cull.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"cull.d.ts","sourceRoot":"","sources":["../../../src/shaders/passes/cull.ts"],"names":[],"mappings":"AAiCA,eAAO,MAAM,iBAAiB,ojJA6G7B,CAAA"}
1
+ {"version":3,"file":"cull.d.ts","sourceRoot":"","sources":["../../../src/shaders/passes/cull.ts"],"names":[],"mappings":"AAiCA,eAAO,MAAM,iBAAiB,ovJAgH7B,CAAA"}
@@ -134,8 +134,11 @@ fn cs(@builtin(global_invocation_id) gid: vec3<u32>) {
134
134
  // and still its shadow, which the engine's pass casts for every material.
135
135
  let shown = (hidden[i] & 1u) == 0u;
136
136
  let graph = (hidden[i] & 2u) == 0u;
137
+ // bit 2: kept out of the camera alone — her own head under a POV mount, which
138
+ // her shadow and her mirror image still carry.
139
+ let toCamera = (hidden[i] & 4u) == 0u;
137
140
  let castsShadow = (dm.flags & DRAW_CASTS_SHADOW) != 0u;
138
- cameraArgs[i * 5u + 1u] = select(0u, 1u, (inCamera || off) && shown && graph);
141
+ cameraArgs[i * 5u + 1u] = select(0u, 1u, (inCamera || off) && shown && graph && toCamera);
139
142
  shadowArgs[i * 5u + 1u] = select(0u, 1u, ((inLight && castsShadow) || off) && shown);
140
143
  mirrorArgs[i * 5u + 1u] = select(0u, 1u, (inMirror || off) && shown && graph);
141
144
  }
@@ -0,0 +1,35 @@
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
+ /** The velocity target: (velocity NDC xy × coverage, coverage, 1). */
29
+ export declare const MOTION_VELOCITY_FORMAT: GPUTextureFormat;
30
+ export declare function motionVelocityWgsl(): string;
31
+ /** Uniforms of the gather, in floats: invViewProj (16), prevViewProj (16),
32
+ * (scale, maxPx, width, height), (depthClear, 0, 0, 0). */
33
+ export declare const MOTION_BLUR_UNIFORM_FLOATS = 40;
34
+ export declare const MOTION_BLUR_WGSL = "\nstruct Uniforms {\n invViewProj: mat4x4f,\n prevViewProj: mat4x4f,\n // x: shutter scale (this frame's motion \u2192 streak), y: longest streak in px,\n // zw: target size in px.\n params: vec4f,\n // x: the depth a cleared pixel holds (the far plane, either end).\n depth: vec4f,\n};\n@group(0) @binding(0) var hdr: texture_2d<f32>;\n@group(0) @binding(1) var velocity: texture_2d<f32>;\n@group(0) @binding(2) var depthTex: texture_depth_multisampled_2d;\n@group(0) @binding(3) var<uniform> u: Uniforms;\n@group(0) @binding(4) var linearSampler: sampler;\n\nstruct VOut { @builtin(position) pos: vec4f, @location(0) uv: vec2f, };\n\n@vertex fn vs(@builtin(vertex_index) i: u32) -> VOut {\n let p = vec2f(f32((i << 1u) & 2u), f32(i & 2u));\n var o: VOut;\n o.pos = vec4f(p * 2.0 - 1.0, 0.0, 1.0);\n o.uv = vec2f(p.x, 1.0 - p.y);\n return o;\n}\n\nconst SAMPLES: i32 = 16;\n\n@fragment fn fs(in: VOut) -> @location(0) vec4f {\n let size = u.params.zw;\n let px = vec2i(in.pos.xy);\n let vel = textureLoad(velocity, px, 0);\n // The camera's share: where this pixel's world point was last frame.\n let ndc = vec2f(in.uv.x * 2.0 - 1.0, 1.0 - in.uv.y * 2.0);\n var d = textureLoad(depthTex, px, 0);\n // A cleared pixel is the far plane, which an infinite projection cannot\n // invert: nudged a hair inside it, it reprojects as a direction.\n d = select(min(d, 0.999999), max(d, 0.000001), u.depth.x < 0.5);\n let world = u.invViewProj * vec4f(ndc, d, 1.0);\n let prevClip = u.prevViewProj * vec4f(world.xyz / world.w, 1.0);\n let camVel = ndc - prevClip.xy / max(prevClip.w, 1e-5);\n // Where models drew, theirs, by how much of the pixel they cover.\n let v = vel.xy + (1.0 - clamp(vel.z, 0.0, 1.0)) * camVel;\n // NDC to pixels (y down), times the shutter, clamped.\n var streak = vec2f(v.x, -v.y) * 0.5 * size * u.params.x;\n let len = length(streak);\n if (len > u.params.y) { streak *= u.params.y / len; }\n let centre = textureLoad(hdr, px, 0);\n if (length(streak) < 0.5) { return centre; }\n var sum = vec3f(0.0);\n for (var i = 0; i < SAMPLES; i++) {\n let t = (f32(i) + 0.5) / f32(SAMPLES) - 0.5;\n let uv = in.uv + streak * t / size;\n sum += textureSampleLevel(hdr, linearSampler, uv, 0.0).rgb;\n }\n return vec4f(sum / f32(SAMPLES), centre.a);\n}\n";
35
+ //# sourceMappingURL=motion-blur.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"motion-blur.d.ts","sourceRoot":"","sources":["../../../src/shaders/passes/motion-blur.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAIH,sEAAsE;AACtE,eAAO,MAAM,sBAAsB,EAAE,gBAAgC,CAAA;AAErE,wBAAgB,kBAAkB,IAAI,MAAM,CA0E3C;AAED;4DAC4D;AAC5D,eAAO,MAAM,0BAA0B,KAAK,CAAA;AAE5C,eAAO,MAAM,gBAAgB,uwEAyD5B,CAAA"}
@@ -0,0 +1,166 @@
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
+ import { DISSOLVE_WGSL, JELLY_VERTEX_WGSL, OBJECT_LIGHT_STRUCT_WGSL, XRAY_WGSL } from "../materials/common";
29
+ /** The velocity target: (velocity NDC xy × coverage, coverage, 1). */
30
+ export const MOTION_VELOCITY_FORMAT = "rgba16float";
31
+ export function motionVelocityWgsl() {
32
+ return /* wgsl */ `
33
+ ${DISSOLVE_WGSL}
34
+ struct CameraUniforms { view: mat4x4f, projection: mat4x4f, viewPos: vec3f, _p: f32, };
35
+ // The head of the material block, as the depth prepass declares it.
36
+ struct MaterialUniforms {
37
+ diffuseColor: vec3f,
38
+ alpha: f32,
39
+ _skip0: vec4f,
40
+ _skip1: vec4f,
41
+ _skip2: f32,
42
+ materialId: f32,
43
+ _skip3: f32,
44
+ dissolve: f32,
45
+ };
46
+ struct PrevCamera { viewProj: mat4x4f, };
47
+
48
+ @group(0) @binding(0) var<uniform> camera: CameraUniforms;
49
+ @group(0) @binding(1) var<uniform> prevCamera: PrevCamera;
50
+ @group(0) @binding(2) var diffuseSampler: sampler;
51
+ @group(1) @binding(0) var<storage, read> skinMats: array<mat4x4f>;
52
+ ${OBJECT_LIGHT_STRUCT_WGSL}
53
+ @group(1) @binding(1) var<uniform> objectLight: ObjectLight;
54
+ @group(1) @binding(2) var<storage, read> prevSkinMats: array<mat4x4f>;
55
+ ${XRAY_WGSL}
56
+ ${JELLY_VERTEX_WGSL}
57
+ @group(2) @binding(0) var diffuseTexture: texture_2d<f32>;
58
+ @group(2) @binding(1) var<uniform> material: MaterialUniforms;
59
+
60
+ struct VSOut {
61
+ @builtin(position) @invariant position: vec4f,
62
+ @location(0) uv: vec2f,
63
+ @location(1) restPos: vec3f,
64
+ @location(2) clipNow: vec4f,
65
+ @location(3) clipPrev: vec4f,
66
+ };
67
+
68
+ @vertex fn vs(
69
+ @location(0) position: vec3f,
70
+ @location(1) normal: vec3f,
71
+ @location(2) uv: vec2f,
72
+ @location(3) joints0: vec4<u32>,
73
+ @location(4) weights0: vec4<f32>,
74
+ ) -> VSOut {
75
+ let weightSum = weights0.x + weights0.y + weights0.z + weights0.w;
76
+ let invWeightSum = select(1.0, 1.0 / weightSum, weightSum > 0.0001);
77
+ let w = select(vec4f(1.0, 0.0, 0.0, 0.0), weights0 * invWeightSum, weightSum > 0.0001);
78
+ var pos4 = vec4f(position, 1.0);
79
+ if (objectLight.jelly[0].w > 0.0) { pos4 = vec4f(rzJellyVertex(position, joints0, w), 1.0); }
80
+ var skinned = vec4f(0.0);
81
+ var prev = vec4f(0.0);
82
+ for (var i = 0u; i < 4u; i++) {
83
+ skinned += (skinMats[joints0[i]] * pos4) * w[i];
84
+ prev += (prevSkinMats[joints0[i]] * pos4) * w[i];
85
+ }
86
+ var o: VSOut;
87
+ o.position = camera.projection * camera.view * vec4f(skinned.xyz, 1.0);
88
+ o.uv = uv;
89
+ o.restPos = position;
90
+ o.clipNow = o.position;
91
+ o.clipPrev = prevCamera.viewProj * vec4f(prev.xyz, 1.0);
92
+ return o;
93
+ }
94
+
95
+ @fragment fn fs(in: VSOut) -> @location(0) vec4f {
96
+ let a = material.alpha * textureSample(diffuseTexture, diffuseSampler, in.uv).a;
97
+ if (a < 0.5) { discard; }
98
+ if (material.dissolve < 0.9995 && rz_dissolve_threshold(in.restPos) > material.dissolve) { discard; }
99
+ if (rz_xray_cut(in.position, material.materialId)) { discard; }
100
+ let now = in.clipNow.xy / in.clipNow.w;
101
+ let prev = in.clipPrev.xy / max(in.clipPrev.w, 1e-5);
102
+ return vec4f(now - prev, 1.0, 1.0);
103
+ }
104
+ `;
105
+ }
106
+ /** Uniforms of the gather, in floats: invViewProj (16), prevViewProj (16),
107
+ * (scale, maxPx, width, height), (depthClear, 0, 0, 0). */
108
+ export const MOTION_BLUR_UNIFORM_FLOATS = 40;
109
+ export const MOTION_BLUR_WGSL = /* wgsl */ `
110
+ struct Uniforms {
111
+ invViewProj: mat4x4f,
112
+ prevViewProj: mat4x4f,
113
+ // x: shutter scale (this frame's motion → streak), y: longest streak in px,
114
+ // zw: target size in px.
115
+ params: vec4f,
116
+ // x: the depth a cleared pixel holds (the far plane, either end).
117
+ depth: vec4f,
118
+ };
119
+ @group(0) @binding(0) var hdr: texture_2d<f32>;
120
+ @group(0) @binding(1) var velocity: texture_2d<f32>;
121
+ @group(0) @binding(2) var depthTex: texture_depth_multisampled_2d;
122
+ @group(0) @binding(3) var<uniform> u: Uniforms;
123
+ @group(0) @binding(4) var linearSampler: sampler;
124
+
125
+ struct VOut { @builtin(position) pos: vec4f, @location(0) uv: vec2f, };
126
+
127
+ @vertex fn vs(@builtin(vertex_index) i: u32) -> VOut {
128
+ let p = vec2f(f32((i << 1u) & 2u), f32(i & 2u));
129
+ var o: VOut;
130
+ o.pos = vec4f(p * 2.0 - 1.0, 0.0, 1.0);
131
+ o.uv = vec2f(p.x, 1.0 - p.y);
132
+ return o;
133
+ }
134
+
135
+ const SAMPLES: i32 = 16;
136
+
137
+ @fragment fn fs(in: VOut) -> @location(0) vec4f {
138
+ let size = u.params.zw;
139
+ let px = vec2i(in.pos.xy);
140
+ let vel = textureLoad(velocity, px, 0);
141
+ // The camera's share: where this pixel's world point was last frame.
142
+ let ndc = vec2f(in.uv.x * 2.0 - 1.0, 1.0 - in.uv.y * 2.0);
143
+ var d = textureLoad(depthTex, px, 0);
144
+ // A cleared pixel is the far plane, which an infinite projection cannot
145
+ // invert: nudged a hair inside it, it reprojects as a direction.
146
+ d = select(min(d, 0.999999), max(d, 0.000001), u.depth.x < 0.5);
147
+ let world = u.invViewProj * vec4f(ndc, d, 1.0);
148
+ let prevClip = u.prevViewProj * vec4f(world.xyz / world.w, 1.0);
149
+ let camVel = ndc - prevClip.xy / max(prevClip.w, 1e-5);
150
+ // Where models drew, theirs, by how much of the pixel they cover.
151
+ let v = vel.xy + (1.0 - clamp(vel.z, 0.0, 1.0)) * camVel;
152
+ // NDC to pixels (y down), times the shutter, clamped.
153
+ var streak = vec2f(v.x, -v.y) * 0.5 * size * u.params.x;
154
+ let len = length(streak);
155
+ if (len > u.params.y) { streak *= u.params.y / len; }
156
+ let centre = textureLoad(hdr, px, 0);
157
+ if (length(streak) < 0.5) { return centre; }
158
+ var sum = vec3f(0.0);
159
+ for (var i = 0; i < SAMPLES; i++) {
160
+ let t = (f32(i) + 0.5) / f32(SAMPLES) - 0.5;
161
+ let uv = in.uv + streak * t / size;
162
+ sum += textureSampleLevel(hdr, linearSampler, uv, 0.0).rgb;
163
+ }
164
+ return vec4f(sum / f32(SAMPLES), centre.a);
165
+ }
166
+ `;
@@ -1 +1 @@
1
- {"version":3,"file":"particles.d.ts","sourceRoot":"","sources":["../../../src/shaders/passes/particles.ts"],"names":[],"mappings":"AAgCA,sEAAsE;AACtE,MAAM,MAAM,UAAU,GAAG;IACvB,QAAQ,EAAE,MAAM,CAAA;IAChB,OAAO,EAAE,MAAM,CAAA;IACf,IAAI,EAAE,MAAM,CAAA;IACZ,SAAS,EAAE,MAAM,CAAA;IACjB,iFAAiF;IACjF,KAAK,EAAE,MAAM,CAAA;IACb;;mFAE+E;IAC/E,UAAU,EAAE,MAAM,CAAA;IAClB,+EAA+E;IAC/E,KAAK,EAAE,MAAM,EAAE,CAAA;CAChB,CAAA;AAkCD,qDAAqD;AACrD;0EAC0E;AAC1E,KAAK,aAAa,GAAG,OAAO,GAAG,UAAU,GAAG,QAAQ,CAAA;AAEpD,KAAK,cAAc,GAAG;IACpB,kCAAkC;IAClC,IAAI,EAAE,MAAM,CAAA;IACZ,qFAAqF;IACrF,KAAK,EAAE,MAAM,CAAA;IACb,KAAK,EAAE,aAAa,CAAA;IACpB,qEAAqE;IACrE,KAAK,EAAE,OAAO,CAAA;IACd;;+BAE2B;IAC3B,UAAU,EAAE,MAAM,CAAA;IAClB;;;;;iEAK6D;IAC7D,KAAK,EAAE,OAAO,CAAA;IACd;;;;;;;yEAOqE;IACrE,MAAM,CAAC,EAAE,OAAO,CAAA;IAChB;;;;;mFAK+E;IAC/E,MAAM,CAAC,EAAE,OAAO,CAAA;IAChB;;;;;;;kFAO8E;IAC9E,IAAI,EAAE,OAAO,CAAA;IACb;;;wEAGoE;IACpE,QAAQ,EAAE,MAAM,CAAA;IAChB;mDAC+C;IAC/C,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB,CAAA;AAED,0FAA0F;AAC1F,eAAO,MAAM,sBAAsB,KAAK,CAAA;AACxC,iFAAiF;AACjF,eAAO,MAAM,uBAAuB,KAAK,CAAA;AACzC;;sDAEsD;AACtD,eAAO,MAAM,yBAAyB,KAAK,CAAA;AAC3C,eAAO,MAAM,uBAAuB,KAAK,CAAA;AACzC;uCACuC;AACvC,eAAO,MAAM,wBAAwB,KAAK,CAAA;AAC1C,eAAO,MAAM,gCAAgC,KAAK,CAAA;AAClD,eAAO,MAAM,6BAA6B,KAAK,CAAA;AAE/C,oEAAoE;AACpE,eAAO,MAAM,eAAe,KAAK,CAAA;AAwDjC;;2EAE2E;AAC3E,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,GAAG;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,IAAI,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,OAAO,CAAC;IAAC,MAAM,EAAE,OAAO,CAAA;CAAE,CASnJ;AAED;;;;;;;;GAQG;AACH,wBAAgB,0BAA0B,CAAC,GAAG,EAAE,cAAc,EAAE,IAAI,EAAE,UAAU,GAAG,MAAM,CA6ExF;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,yBAAyB,CAAC,GAAG,EAAE,cAAc,EAAE,IAAI,EAAE,UAAU,GAAG,MAAM,CAyMvF"}
1
+ {"version":3,"file":"particles.d.ts","sourceRoot":"","sources":["../../../src/shaders/passes/particles.ts"],"names":[],"mappings":"AAgCA,sEAAsE;AACtE,MAAM,MAAM,UAAU,GAAG;IACvB,QAAQ,EAAE,MAAM,CAAA;IAChB,OAAO,EAAE,MAAM,CAAA;IACf,IAAI,EAAE,MAAM,CAAA;IACZ,SAAS,EAAE,MAAM,CAAA;IACjB,iFAAiF;IACjF,KAAK,EAAE,MAAM,CAAA;IACb;;mFAE+E;IAC/E,UAAU,EAAE,MAAM,CAAA;IAClB,+EAA+E;IAC/E,KAAK,EAAE,MAAM,EAAE,CAAA;CAChB,CAAA;AAkCD,qDAAqD;AACrD;0EAC0E;AAC1E,KAAK,aAAa,GAAG,OAAO,GAAG,UAAU,GAAG,QAAQ,CAAA;AAEpD,KAAK,cAAc,GAAG;IACpB,kCAAkC;IAClC,IAAI,EAAE,MAAM,CAAA;IACZ,qFAAqF;IACrF,KAAK,EAAE,MAAM,CAAA;IACb,KAAK,EAAE,aAAa,CAAA;IACpB,qEAAqE;IACrE,KAAK,EAAE,OAAO,CAAA;IACd;;+BAE2B;IAC3B,UAAU,EAAE,MAAM,CAAA;IAClB;;;;;iEAK6D;IAC7D,KAAK,EAAE,OAAO,CAAA;IACd;;;;;;;yEAOqE;IACrE,MAAM,CAAC,EAAE,OAAO,CAAA;IAChB;;;;;mFAK+E;IAC/E,MAAM,CAAC,EAAE,OAAO,CAAA;IAChB;;;;;;;kFAO8E;IAC9E,IAAI,EAAE,OAAO,CAAA;IACb;;;wEAGoE;IACpE,QAAQ,EAAE,MAAM,CAAA;IAChB;mDAC+C;IAC/C,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB,CAAA;AAED,0FAA0F;AAC1F,eAAO,MAAM,sBAAsB,KAAK,CAAA;AACxC,iFAAiF;AACjF,eAAO,MAAM,uBAAuB,KAAK,CAAA;AACzC;;sDAEsD;AACtD,eAAO,MAAM,yBAAyB,KAAK,CAAA;AAC3C,eAAO,MAAM,uBAAuB,KAAK,CAAA;AACzC;uCACuC;AACvC,eAAO,MAAM,wBAAwB,KAAK,CAAA;AAC1C,eAAO,MAAM,gCAAgC,KAAK,CAAA;AAClD,eAAO,MAAM,6BAA6B,KAAK,CAAA;AAE/C,oEAAoE;AACpE,eAAO,MAAM,eAAe,KAAK,CAAA;AA+DjC;;2EAE2E;AAC3E,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,GAAG;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,IAAI,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,OAAO,CAAC;IAAC,MAAM,EAAE,OAAO,CAAA;CAAE,CASnJ;AAED;;;;;;;;GAQG;AACH,wBAAgB,0BAA0B,CAAC,GAAG,EAAE,cAAc,EAAE,IAAI,EAAE,UAAU,GAAG,MAAM,CA6ExF;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,yBAAyB,CAAC,GAAG,EAAE,cAAc,EAAE,IAAI,EAAE,UAAU,GAAG,MAAM,CA8MvF"}
@@ -109,6 +109,12 @@ fn rzProject(p: vec3f) -> vec3f {
109
109
  }
110
110
  fn rzCamPos() -> vec3f { return cam.camPos; }
111
111
  `;
112
+ /** `fn particleStep(p, dt, id: u32)`: the step is told the particle's slot, as
113
+ * particleInit is, so it can read the slot's emitter. Two parameters is the
114
+ * usual step and is called as before. */
115
+ function stepTakesId(wgsl) {
116
+ return /\bfn\s+particleStep\s*\([^)]*,[^)]*,[^)]*\)/.test(wgsl);
117
+ }
112
118
  /** Does the source define the particle contract? All three are required
113
119
  * together; `cover` is optional and only means anything to a cutout, and
114
120
  * `count` is optional and bounds the frame's work — see particleCount. */
@@ -200,7 +206,7 @@ fn main(@builtin(global_invocation_id) gid: vec3u) {
200
206
  // its life staggers the pool, which is the difference between snow and a
201
207
  // pulse of snow arriving all at once every few seconds.
202
208
  } else {
203
- p = particleStep(p, pu.dt);
209
+ p = particleStep(p, pu.dt${stepTakesId(src.wgsl) ? ", i" : ""});
204
210
  p.age = p.age + pu.dt;
205
211
  }
206
212
  particles[i] = p;
@@ -271,7 +277,12 @@ struct VSOut {
271
277
  fn vs(@builtin(vertex_index) vi: u32, @builtin(instance_index) ii: u32) -> VSOut {
272
278
  var out: VSOut;
273
279
  out.id = ii;
274
- let p = particles[ii];
280
+ ${/\bfn\s+particleWorld\s*\(/.test(src.wgsl) ? ` // the effect keeps the pool in its emitters' own frames (Unity's Local
281
+ // simulation space: a beam riding a moving head); particleWorld puts this
282
+ // one in the world as its emitter stands now, before the card is built
283
+ let p = particleWorld(particles[ii], ii);
284
+ ` : ` let p = particles[ii];
285
+ `}
275
286
  // Two triangles, corners in the order 0,1,2, 2,1,3.
276
287
  let quad = array<vec2f, 6>(
277
288
  vec2f(-1.0, -1.0), vec2f(1.0, -1.0), vec2f(-1.0, 1.0),
@@ -1,2 +1,2 @@
1
- export declare const SHADOW_DEPTH_SHADER_WGSL = "\n\n/** Clump size, in object-space units \u2014 MMD's are roughly centimetres.\n *\n * Raised from 0.42: at that size the pieces are near enough to speckle that a\n * departure reads as a fade rather than as a body coming apart, which is the\n * thing the clumping exists to avoid. Hand-sized pieces are visible ones. */\nconst RZ_DISSOLVE_GRAIN: f32 = 1.2;\n/** How much the clumps pull a fragment off its own grit value \u2014 the whole of\n * the LOOK, and none of the rate. See rz_dissolve_threshold. */\nconst RZ_DISSOLVE_CLUMP: f32 = 0.6;\n/** Grit cell, as a fraction of a clump. See rz_dissolve_threshold: this is what\n * keeps a material SMALLER than a clump from having one threshold for all of\n * it, and it has to be smaller than the SMALLEST material a model carries \u2014\n * an eye highlight, a button, a buckle \u2014 not merely small. Two and a half\n * millimetres on a normal model, which is under all of them. */\nconst RZ_DISSOLVE_GRIT: f32 = 0.021;\n/** How wide the glowing front is, in threshold units. Thin: the front is a\n * rim, and a wide one lights whole limbs at once. */\nconst RZ_DISSOLVE_EDGE: f32 = 0.11;\n/** What the front burns with. Emission, added after lighting and after the\n * graph \u2014 a colour that ramps or takes a shadow reads as paint, not as heat.\n * Above 1 on purpose: it is meant to reach the bloom pyramid. Blue-violet,\n * the same light the motes leaving her are drawn and lit with. */\nconst RZ_BURN_COLOR: vec3f = vec3f(0.45, 0.62, 2.40);\n\nfn rz_dissolve_hash(p: vec3f) -> f32 {\n // INTEGER, for exactly the reason _hash33 in nodes.ts is, and its comment\n // says it: above 24 bits an f32 loses the low ones. The float hash this\n // replaces multiplied three terms into the hundreds of millions and took\n // fract() of the product \u2014 which past 2^24 is not noise, it is a quantised\n // staircase, and wherever that staircase landed on zero the threshold was\n // zero. A surface whose threshold is zero never crosses it: it stays behind\n // while the rest of her leaves, and it sits inside the burning front the\n // whole time she is gone. Those were the white pieces that would not go.\n //\n // Floored first, unlike _hash33: vec3i() truncates toward zero, so -0.5 and\n // +0.5 are the same cell, and half this model is at negative x.\n var h = vec3u(vec3i(floor(p)) + vec3i(32768));\n h = h * vec3u(1664525u, 1013904223u, 2654435761u);\n h = (h.yzx ^ h) * vec3u(2246822519u, 3266489917u, 668265263u);\n h = h ^ (h >> vec3u(16u));\n return f32((h.x ^ h.y ^ h.z) & 16777215u) * (1.0 / 16777216.0);\n}\n\n/** Value noise over that hash \u2014 smooth within a flake, uncorrelated between. */\nfn rz_dissolve_field(p: vec3f) -> f32 {\n let i = floor(p);\n let f = fract(p);\n let u = f * f * (3.0 - 2.0 * f);\n let c000 = rz_dissolve_hash(i);\n let c100 = rz_dissolve_hash(i + vec3f(1.0, 0.0, 0.0));\n let c010 = rz_dissolve_hash(i + vec3f(0.0, 1.0, 0.0));\n let c110 = rz_dissolve_hash(i + vec3f(1.0, 1.0, 0.0));\n let c001 = rz_dissolve_hash(i + vec3f(0.0, 0.0, 1.0));\n let c101 = rz_dissolve_hash(i + vec3f(1.0, 0.0, 1.0));\n let c011 = rz_dissolve_hash(i + vec3f(0.0, 1.0, 1.0));\n let c111 = rz_dissolve_hash(i + vec3f(1.0, 1.0, 1.0));\n let x00 = mix(c000, c100, u.x);\n let x10 = mix(c010, c110, u.x);\n let x01 = mix(c001, c101, u.x);\n let x11 = mix(c011, c111, u.x);\n return mix(mix(x00, x10, u.y), mix(x01, x11, u.y), u.z);\n}\n\n/**\n * The threshold this fragment is measured against.\n *\n * NOISE ALONE, so the whole body comes apart at once \u2014 a hand, a shoulder and\n * an ankle all thinning together rather than a line travelling up her. It was\n * tilted by height at first, on the argument that a sweep reads as something\n * happening TO her; what it actually reads as is a wipe, and a wipe has a\n * direction, an edge and a speed the rest of the effect knows nothing about.\n * Everywhere at once is also the honest match for what leaves: the motes are\n * shed from every limb in the same breath.\n *\n * TWO SCALES. Clumps alone meant every fragment of a material SMALLER than one\n * cell shared a single threshold: an eye highlight, a brow, a button, a hair\n * clip. All of it sat inside the glowing front at once, so the piece flashed\n * white as a whole and then popped \u2014 the two things a disintegration must never\n * do. The grit is a per-cell hash at a fraction of the clump, so a part a few\n * millimetres across still comes apart in pieces.\n *\n * THE GRIT IS THE BASE, though, and the clumps only push a fragment off it.\n * That ordering is what makes the dissolve smooth, and it is not obvious: the\n * two used to be WEIGHTED AND ADDED, and a sum is not uniformly distributed\n * however uniform each half is. It piles up in the middle, so most of the\n * surface had a threshold near a half \u2014 all of it went as the dissolve swept\n * through the middle, and what remained was the thin tail near zero. That tail\n * was the second stage everyone could see: a body that mostly vanished, then a\n * scatter of glowing scraps, then nothing.\n *\n * WRAPPED, not added. Offsetting a uniform value and taking fract() leaves it\n * EXACTLY uniform, where scaling a sum back into range only flattens the middle\n * \u2014 measured over four hundred thousand samples, the sum removed 12% of her per\n * step at the halfway mark and 0.1% at each end (a 110x swing), the scaled sum\n * still swung 89x, and the wrap is flat: 5.0% per step from the first flake to\n * the last, a ratio of 1.0.\n *\n * That flatness IS the smoothness. An uneven rate is what produced the two\n * stages you could see \u2014 a body that mostly vanished at once, then a scatter of\n * scraps that took as long again to follow.\n *\n * The clumping survives the wrap: neighbouring fragments share a clump value,\n * so the offset moves them together. What wraps past 1 comes back at 0 and goes\n * at the other end instead, which costs a little speckle in a region and buys\n * an even rate everywhere.\n */\nfn rz_dissolve_threshold(restPos: vec3f) -> f32 {\n let grit = rz_dissolve_hash(floor(restPos / (RZ_DISSOLVE_GRAIN * RZ_DISSOLVE_GRIT)));\n let clump = rz_dissolve_field(restPos / RZ_DISSOLVE_GRAIN) - 0.5;\n return fract(grit + clump * RZ_DISSOLVE_CLUMP);\n}\n\nstruct LightVP { viewProj: mat4x4f, };\n@group(0) @binding(0) var<uniform> lp: LightVP;\n@group(0) @binding(1) var<storage, read> skinMats: array<mat4x4f>;\n@group(0) @binding(2) var texSampler: sampler;\n@group(1) @binding(0) var diffuseTexture: texture_2d<f32>;\n// The head of the material block and the one field at its tail \u2014 the same\n// reach the depth prepass takes, and for the same reason: a shadow cast by a\n// body that is no longer drawn is the tell that the vanishing is a trick.\nstruct MaterialDiffuse {\n diffuse: vec4f,\n _skip0: vec4f,\n _skip1: vec4f,\n _skip2: f32,\n materialId: f32,\n _skip3: f32,\n dissolve: f32,\n};\n@group(1) @binding(1) var<uniform> material: MaterialDiffuse;\n// The model's ObjectLight, for its jelly eyes: the map must hold the iris where\n// the colour pass draws it.\nstruct ObjectLight { layers: vec4u, ambient: vec4f, sh: array<vec4f, 9>, fill: vec4f, jelly: array<vec4f, 12>, bloom: vec4f, xray: array<vec4f, 4>, xrayMask: array<vec4u, 16> }\n@group(0) @binding(3) var<uniform> objectLight: ObjectLight;\n\nfn _rzJellyTurn(q: vec2f, a: f32) -> vec2f {\n let c = cos(a);\n let s = sin(a);\n return vec2f(c * q.x - s * q.y, s * q.x + c * q.y);\n}\n\n/**\n * Where the jelly puts an iris vertex. The iris is the part of the eye the eye\n * bones carry, so it is moved as a whole, in bind space before skinning:\n * scaled about its centre (the CPU composes the blink's spring, the lid's\n * press and her motion's knock into one scale), turned and drifted a hair on\n * the spring, brought forward a hair, and rolled about the eye bone with a\n * closing expression \u2014 inside the white, which stays where it is. A highlight\n * layer (this draw's material is one of jelly[1].w's) takes the iris's move,\n * then its own scale, turn and tremble, before the roll. Vertices take it by their weight on the eye bones\n * (jelly[2].w, jelly[3].w), so nothing else on the face moves.\n */\nfn rzJellyVertex(p: vec3f, joints: vec4<u32>, nw: vec4f) -> vec3f {\n let j = objectLight.jelly;\n let ids = u32(j[1].w);\n let mid = u32(material.materialId);\n let hl = mid > 0u && (mid == (ids & 4095u) || mid == (ids >> 12u));\n var out = p;\n for (var e = 0u; e < 2u; e++) {\n let bone = j[2u + e].w;\n if (bone < 0.0) { continue; }\n let b = u32(bone);\n let w = dot(nw, select(vec4f(0.0), vec4f(1.0), joints == vec4<u32>(b)));\n if (w <= 0.0) { continue; }\n let c = j[e].xyz;\n // A rig that carries both eyes on one bone: each vertex follows its own side.\n if (j[2].w == j[3].w && e == 0u && distance(p.xy, j[1].xy) < distance(p.xy, j[0].xy)) { continue; }\n if (j[2].w == j[3].w && e == 1u && distance(p.xy, j[0].xy) <= distance(p.xy, j[1].xy)) { continue; }\n let t = j[4u + e];\n let m = j[6u + e];\n let h = j[10u + e];\n var q = _rzJellyTurn((p.xy - c.xy) * t.xy, t.z) + m.xy;\n // Forward is \u2212Z (MMD faces \u2212Z).\n var push = h.z;\n if (hl) {\n // Sliding over the iris's dome on its own, a highlight sheet a hair in\n // front of it would sink into it where the dome bulges more and the two\n // fight for depth; so it comes forward by as far as it slid \u2014 a slope of\n // one, steeper than the dome anywhere it draws.\n let before = q;\n q = _rzJellyTurn(q * h.xy, t.w) + m.zw;\n push += length(q - before);\n }\n var moved = vec3f(c.xy + q, p.z - push);\n let roll = j[8u + e];\n if (roll.w != 0.0) {\n let d = moved - roll.xyz;\n let rd = _rzJellyTurn(d.yz, roll.w);\n moved = roll.xyz + vec3f(d.x, rd.x, rd.y);\n }\n out = mix(p, moved, w);\n }\n return out;\n}\n\n\nstruct VSOut {\n @builtin(position) position: vec4f,\n @location(0) uv: vec2f,\n /** Bind-pose position, for the dissolve test \u2014 object space, as everywhere.\n * A flake that is gone must stop casting. */\n @location(1) restPos: vec3f,\n};\n\n@vertex fn vs(@location(0) position: vec3f, @location(1) normal: vec3f, @location(2) uv: vec2f,\n @location(3) joints0: vec4<u32>, @location(4) weights0: vec4<f32>) -> VSOut {\n let ws = weights0.x + weights0.y + weights0.z + weights0.w;\n let inv = select(1.0, 1.0 / ws, ws > 0.0001);\n let nw = select(vec4f(1.0,0.0,0.0,0.0), weights0 * inv, ws > 0.0001);\n var pos4 = vec4f(position, 1.0);\n if (objectLight.jelly[0].w > 0.0) { pos4 = vec4f(rzJellyVertex(position, joints0, nw), 1.0); }\n var sp = vec4f(0.0);\n for (var i = 0u; i < 4u; i++) { sp += (skinMats[joints0[i]] * pos4) * nw[i]; }\n var out: VSOut;\n out.position = lp.viewProj * vec4f(sp.xyz, 1.0);\n out.uv = uv;\n out.restPos = position;\n return out;\n}\n\n@fragment fn fs(in: VSOut) {\n let alpha = textureSample(diffuseTexture, texSampler, in.uv).a * material.diffuse.a;\n if (alpha < 0.5) { discard; }\n // The dissolve, run as the colour pass runs it. A flake that is gone stops\n // casting: without this she leaves a whole shadow standing on the floor while\n // her body is in the air.\n if (material.dissolve < 0.9995 && rz_dissolve_threshold(in.restPos) > material.dissolve) { discard; }\n}\n";
1
+ export declare const SHADOW_DEPTH_SHADER_WGSL = "\n\n/** Clump size, in object-space units \u2014 MMD's are roughly centimetres.\n *\n * Raised from 0.42: at that size the pieces are near enough to speckle that a\n * departure reads as a fade rather than as a body coming apart, which is the\n * thing the clumping exists to avoid. Hand-sized pieces are visible ones. */\nconst RZ_DISSOLVE_GRAIN: f32 = 1.2;\n/** How much the clumps pull a fragment off its own grit value \u2014 the whole of\n * the LOOK, and none of the rate. See rz_dissolve_threshold. */\nconst RZ_DISSOLVE_CLUMP: f32 = 0.6;\n/** Grit cell, as a fraction of a clump. See rz_dissolve_threshold: this is what\n * keeps a material SMALLER than a clump from having one threshold for all of\n * it, and it has to be smaller than the SMALLEST material a model carries \u2014\n * an eye highlight, a button, a buckle \u2014 not merely small. Two and a half\n * millimetres on a normal model, which is under all of them. */\nconst RZ_DISSOLVE_GRIT: f32 = 0.021;\n/** How wide the glowing front is, in threshold units. Thin: the front is a\n * rim, and a wide one lights whole limbs at once. */\nconst RZ_DISSOLVE_EDGE: f32 = 0.11;\n/** What the front burns with. Emission, added after lighting and after the\n * graph \u2014 a colour that ramps or takes a shadow reads as paint, not as heat.\n * Above 1 on purpose: it is meant to reach the bloom pyramid. Blue-violet,\n * the same light the motes leaving her are drawn and lit with. */\nconst RZ_BURN_COLOR: vec3f = vec3f(0.45, 0.62, 2.40);\n\nfn rz_dissolve_hash(p: vec3f) -> f32 {\n // INTEGER, for exactly the reason _hash33 in nodes.ts is, and its comment\n // says it: above 24 bits an f32 loses the low ones. The float hash this\n // replaces multiplied three terms into the hundreds of millions and took\n // fract() of the product \u2014 which past 2^24 is not noise, it is a quantised\n // staircase, and wherever that staircase landed on zero the threshold was\n // zero. A surface whose threshold is zero never crosses it: it stays behind\n // while the rest of her leaves, and it sits inside the burning front the\n // whole time she is gone. Those were the white pieces that would not go.\n //\n // Floored first, unlike _hash33: vec3i() truncates toward zero, so -0.5 and\n // +0.5 are the same cell, and half this model is at negative x.\n var h = vec3u(vec3i(floor(p)) + vec3i(32768));\n h = h * vec3u(1664525u, 1013904223u, 2654435761u);\n h = (h.yzx ^ h) * vec3u(2246822519u, 3266489917u, 668265263u);\n h = h ^ (h >> vec3u(16u));\n return f32((h.x ^ h.y ^ h.z) & 16777215u) * (1.0 / 16777216.0);\n}\n\n/** Value noise over that hash \u2014 smooth within a flake, uncorrelated between. */\nfn rz_dissolve_field(p: vec3f) -> f32 {\n let i = floor(p);\n let f = fract(p);\n let u = f * f * (3.0 - 2.0 * f);\n let c000 = rz_dissolve_hash(i);\n let c100 = rz_dissolve_hash(i + vec3f(1.0, 0.0, 0.0));\n let c010 = rz_dissolve_hash(i + vec3f(0.0, 1.0, 0.0));\n let c110 = rz_dissolve_hash(i + vec3f(1.0, 1.0, 0.0));\n let c001 = rz_dissolve_hash(i + vec3f(0.0, 0.0, 1.0));\n let c101 = rz_dissolve_hash(i + vec3f(1.0, 0.0, 1.0));\n let c011 = rz_dissolve_hash(i + vec3f(0.0, 1.0, 1.0));\n let c111 = rz_dissolve_hash(i + vec3f(1.0, 1.0, 1.0));\n let x00 = mix(c000, c100, u.x);\n let x10 = mix(c010, c110, u.x);\n let x01 = mix(c001, c101, u.x);\n let x11 = mix(c011, c111, u.x);\n return mix(mix(x00, x10, u.y), mix(x01, x11, u.y), u.z);\n}\n\n/**\n * The threshold this fragment is measured against.\n *\n * NOISE ALONE, so the whole body comes apart at once \u2014 a hand, a shoulder and\n * an ankle all thinning together rather than a line travelling up her. It was\n * tilted by height at first, on the argument that a sweep reads as something\n * happening TO her; what it actually reads as is a wipe, and a wipe has a\n * direction, an edge and a speed the rest of the effect knows nothing about.\n * Everywhere at once is also the honest match for what leaves: the motes are\n * shed from every limb in the same breath.\n *\n * TWO SCALES. Clumps alone meant every fragment of a material SMALLER than one\n * cell shared a single threshold: an eye highlight, a brow, a button, a hair\n * clip. All of it sat inside the glowing front at once, so the piece flashed\n * white as a whole and then popped \u2014 the two things a disintegration must never\n * do. The grit is a per-cell hash at a fraction of the clump, so a part a few\n * millimetres across still comes apart in pieces.\n *\n * THE GRIT IS THE BASE, though, and the clumps only push a fragment off it.\n * That ordering is what makes the dissolve smooth, and it is not obvious: the\n * two used to be WEIGHTED AND ADDED, and a sum is not uniformly distributed\n * however uniform each half is. It piles up in the middle, so most of the\n * surface had a threshold near a half \u2014 all of it went as the dissolve swept\n * through the middle, and what remained was the thin tail near zero. That tail\n * was the second stage everyone could see: a body that mostly vanished, then a\n * scatter of glowing scraps, then nothing.\n *\n * WRAPPED, not added. Offsetting a uniform value and taking fract() leaves it\n * EXACTLY uniform, where scaling a sum back into range only flattens the middle\n * \u2014 measured over four hundred thousand samples, the sum removed 12% of her per\n * step at the halfway mark and 0.1% at each end (a 110x swing), the scaled sum\n * still swung 89x, and the wrap is flat: 5.0% per step from the first flake to\n * the last, a ratio of 1.0.\n *\n * That flatness IS the smoothness. An uneven rate is what produced the two\n * stages you could see \u2014 a body that mostly vanished at once, then a scatter of\n * scraps that took as long again to follow.\n *\n * The clumping survives the wrap: neighbouring fragments share a clump value,\n * so the offset moves them together. What wraps past 1 comes back at 0 and goes\n * at the other end instead, which costs a little speckle in a region and buys\n * an even rate everywhere.\n */\nfn rz_dissolve_threshold(restPos: vec3f) -> f32 {\n let grit = rz_dissolve_hash(floor(restPos / (RZ_DISSOLVE_GRAIN * RZ_DISSOLVE_GRIT)));\n let clump = rz_dissolve_field(restPos / RZ_DISSOLVE_GRAIN) - 0.5;\n return fract(grit + clump * RZ_DISSOLVE_CLUMP);\n}\n\nstruct LightVP { viewProj: mat4x4f, };\n@group(0) @binding(0) var<uniform> lp: LightVP;\n@group(0) @binding(1) var<storage, read> skinMats: array<mat4x4f>;\n@group(0) @binding(2) var texSampler: sampler;\n@group(1) @binding(0) var diffuseTexture: texture_2d<f32>;\n// The head of the material block and the one field at its tail \u2014 the same\n// reach the depth prepass takes, and for the same reason: a shadow cast by a\n// body that is no longer drawn is the tell that the vanishing is a trick.\nstruct MaterialDiffuse {\n diffuse: vec4f,\n _skip0: vec4f,\n _skip1: vec4f,\n _skip2: f32,\n materialId: f32,\n _skip3: f32,\n dissolve: f32,\n};\n@group(1) @binding(1) var<uniform> material: MaterialDiffuse;\n// The model's ObjectLight, for its jelly eyes: the map must hold the iris where\n// the colour pass draws it.\nstruct ObjectLight { layers: vec4u, ambient: vec4f, sh: array<vec4f, 9>, fill: vec4f, jelly: array<vec4f, 12>, bloom: vec4f, xray: array<vec4f, 4>, xrayMask: array<vec4u, 16>, matLayers: array<vec4u, 128> }\n@group(0) @binding(3) var<uniform> objectLight: ObjectLight;\n\nfn _rzJellyTurn(q: vec2f, a: f32) -> vec2f {\n let c = cos(a);\n let s = sin(a);\n return vec2f(c * q.x - s * q.y, s * q.x + c * q.y);\n}\n\n/**\n * Where the jelly puts an iris vertex. The iris is the part of the eye the eye\n * bones carry, so it is moved as a whole, in bind space before skinning:\n * scaled about its centre (the CPU composes the blink's spring, the lid's\n * press and her motion's knock into one scale), turned and drifted a hair on\n * the spring, brought forward a hair, and rolled about the eye bone with a\n * closing expression \u2014 inside the white, which stays where it is. A highlight\n * layer (this draw's material is one of jelly[1].w's) takes the iris's move,\n * then its own scale, turn and tremble, before the roll. Vertices take it by their weight on the eye bones\n * (jelly[2].w, jelly[3].w), so nothing else on the face moves.\n */\nfn rzJellyVertex(p: vec3f, joints: vec4<u32>, nw: vec4f) -> vec3f {\n let j = objectLight.jelly;\n let ids = u32(j[1].w);\n let mid = u32(material.materialId);\n let hl = mid > 0u && (mid == (ids & 4095u) || mid == (ids >> 12u));\n var out = p;\n for (var e = 0u; e < 2u; e++) {\n let bone = j[2u + e].w;\n if (bone < 0.0) { continue; }\n let b = u32(bone);\n let w = dot(nw, select(vec4f(0.0), vec4f(1.0), joints == vec4<u32>(b)));\n if (w <= 0.0) { continue; }\n let c = j[e].xyz;\n // A rig that carries both eyes on one bone: each vertex follows its own side.\n if (j[2].w == j[3].w && e == 0u && distance(p.xy, j[1].xy) < distance(p.xy, j[0].xy)) { continue; }\n if (j[2].w == j[3].w && e == 1u && distance(p.xy, j[0].xy) <= distance(p.xy, j[1].xy)) { continue; }\n let t = j[4u + e];\n let m = j[6u + e];\n let h = j[10u + e];\n var q = _rzJellyTurn((p.xy - c.xy) * t.xy, t.z) + m.xy;\n // Forward is \u2212Z (MMD faces \u2212Z).\n var push = h.z;\n if (hl) {\n // Sliding over the iris's dome on its own, a highlight sheet a hair in\n // front of it would sink into it where the dome bulges more and the two\n // fight for depth; so it comes forward by as far as it slid \u2014 a slope of\n // one, steeper than the dome anywhere it draws.\n let before = q;\n q = _rzJellyTurn(q * h.xy, t.w) + m.zw;\n push += length(q - before);\n }\n var moved = vec3f(c.xy + q, p.z - push);\n let roll = j[8u + e];\n if (roll.w != 0.0) {\n let d = moved - roll.xyz;\n let rd = _rzJellyTurn(d.yz, roll.w);\n moved = roll.xyz + vec3f(d.x, rd.x, rd.y);\n }\n out = mix(p, moved, w);\n }\n return out;\n}\n\n\nstruct VSOut {\n @builtin(position) position: vec4f,\n @location(0) uv: vec2f,\n /** Bind-pose position, for the dissolve test \u2014 object space, as everywhere.\n * A flake that is gone must stop casting. */\n @location(1) restPos: vec3f,\n};\n\n@vertex fn vs(@location(0) position: vec3f, @location(1) normal: vec3f, @location(2) uv: vec2f,\n @location(3) joints0: vec4<u32>, @location(4) weights0: vec4<f32>) -> VSOut {\n let ws = weights0.x + weights0.y + weights0.z + weights0.w;\n let inv = select(1.0, 1.0 / ws, ws > 0.0001);\n let nw = select(vec4f(1.0,0.0,0.0,0.0), weights0 * inv, ws > 0.0001);\n var pos4 = vec4f(position, 1.0);\n if (objectLight.jelly[0].w > 0.0) { pos4 = vec4f(rzJellyVertex(position, joints0, nw), 1.0); }\n var sp = vec4f(0.0);\n for (var i = 0u; i < 4u; i++) { sp += (skinMats[joints0[i]] * pos4) * nw[i]; }\n var out: VSOut;\n out.position = lp.viewProj * vec4f(sp.xyz, 1.0);\n out.uv = uv;\n out.restPos = position;\n return out;\n}\n\n@fragment fn fs(in: VSOut) {\n let alpha = textureSample(diffuseTexture, texSampler, in.uv).a * material.diffuse.a;\n if (alpha < 0.5) { discard; }\n // The dissolve, run as the colour pass runs it. A flake that is gone stops\n // casting: without this she leaves a whole shadow standing on the floor while\n // her body is in the air.\n if (material.dissolve < 0.9995 && rz_dissolve_threshold(in.restPos) > material.dissolve) { discard; }\n}\n";
2
2
  //# sourceMappingURL=shadow.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"shadow.d.ts","sourceRoot":"","sources":["../../../src/shaders/passes/shadow.ts"],"names":[],"mappings":"AASA,eAAO,MAAM,wBAAwB,srWA0DpC,CAAA"}
1
+ {"version":3,"file":"shadow.d.ts","sourceRoot":"","sources":["../../../src/shaders/passes/shadow.ts"],"names":[],"mappings":"AASA,eAAO,MAAM,wBAAwB,otWA0DpC,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "reze-engine",
3
- "version": "0.69.8",
3
+ "version": "0.70.0",
4
4
  "description": "A lightweight WebGPU engine for real-time 3D MMD/PMX model rendering",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -0,0 +1,281 @@
1
+ // The camera on a bone: her own eyes (POV), or a phone in her hand (selfie).
2
+ //
3
+ // A third way to hold the camera, beside the orbit and the follow. The follow
4
+ // moves the point the orbit turns about and leaves the orbit's own angle and
5
+ // distance to the person at the mouse; a mount places the EYE and the LOOK from
6
+ // the rig itself, every frame, so the shot is hers.
7
+ //
8
+ // Stepped by the engine after the pose and physics, before the camera uniforms
9
+ // (as the follow is), so it never trails her by a frame and an export renders
10
+ // what the editor showed. A camera VMD or setCameraPose still wins: they are
11
+ // scene data and an explicit host statement about where the camera IS.
12
+
13
+ import { Vec3 } from "./math"
14
+ import { Quat } from "./math"
15
+ import type { Model } from "./model"
16
+
17
+ export type CameraMountMode = "pov" | "selfie"
18
+
19
+ /** What setCameraMount asks for. */
20
+ export interface CameraMountOptions {
21
+ /** "pov": through her eyes, turning with her head. "selfie": held in her
22
+ * hand, aimed back at her face. */
23
+ mode: CameraMountMode
24
+ /** The bone the camera rides. POV 頭 (the head), selfie 右手首 (a wrist). */
25
+ bone?: string
26
+ /** Seconds the camera's turn trails the bone, eased (POV 0.12, selfie 0.25):
27
+ * a dance's head snaps read as a jolt without it. 0 is rigid. */
28
+ smoothing?: number
29
+ /** POV: keep the horizon level, dropping the head's tilt. Default true. */
30
+ level?: boolean
31
+ /** Selfie: how far from her face the phone is held at full stretch, model
32
+ * units (an arm's length is about 7). Closer when her hand is. Default 7. */
33
+ distance?: number
34
+ }
35
+
36
+ /** Where the mounted camera is this frame, world space. */
37
+ export interface MountPose {
38
+ eye: Vec3
39
+ forward: Vec3
40
+ up: Vec3
41
+ }
42
+
43
+ /** The bones a face is read from, and the head. */
44
+ const HEAD = "頭"
45
+ const EYES = ["左目", "右目"] as const
46
+ /** POV: how far in front of the eyeballs the lens sits, model units — past the
47
+ * eye's own surface, so nothing of her that is still drawn sits on the lens. */
48
+ const POV_FORWARD = 0.25
49
+ /** Where a rig without eye bones keeps its eyes: up the head bone, model units. */
50
+ const EYES_UP_FALLBACK = 1.1
51
+ /** Selfie: the nearest the phone comes to her face, as a share of `distance`. */
52
+ const SELFIE_MIN = 0.55
53
+ const UP = new Vec3(0, 1, 0)
54
+
55
+ export function resolveMountOptions(o: CameraMountOptions): Required<CameraMountOptions> {
56
+ const pov = o.mode !== "selfie"
57
+ return {
58
+ mode: pov ? "pov" : "selfie",
59
+ bone: o.bone || (pov ? HEAD : "右手首"),
60
+ smoothing: Math.min(2, Math.max(0, o.smoothing ?? (pov ? 0.12 : 0.25))),
61
+ level: o.level ?? true,
62
+ distance: Math.min(40, Math.max(2, o.distance ?? 7)),
63
+ }
64
+ }
65
+
66
+ /** A model-space point placed as the scene has the model (setModelTransform). */
67
+ function toWorld(m: Model, p: Vec3): Vec3 {
68
+ const s = m.scale
69
+ const out = new Vec3(p.x * s, p.y * s, p.z * s)
70
+ Quat.rotateVecInto(m.rotation, out, out)
71
+ out.setXYZ(out.x + m.position.x, out.y + m.position.y, out.z + m.position.z)
72
+ return out
73
+ }
74
+
75
+ /** A model-space direction turned as the model is; unit length. */
76
+ function dirToWorld(m: Model, x: number, y: number, z: number): Vec3 {
77
+ const out = new Vec3(x, y, z)
78
+ Quat.rotateVecInto(m.rotation, out, out)
79
+ const l = Math.hypot(out.x, out.y, out.z) || 1
80
+ out.setXYZ(out.x / l, out.y / l, out.z / l)
81
+ return out
82
+ }
83
+
84
+ /**
85
+ * Turn a unit vector toward another by k of the angle between them (a slerp).
86
+ * Not a straight blend: two opposite vectors blend to nothing, so a half turn
87
+ * — her spinning round — would never start to swing.
88
+ */
89
+ function ease(v: Vec3, to: Vec3, k: number): void {
90
+ if (k >= 1) {
91
+ v.set(to)
92
+ return
93
+ }
94
+ const d = Math.max(-1, Math.min(1, v.x * to.x + v.y * to.y + v.z * to.z))
95
+ const angle = Math.acos(d)
96
+ if (angle < 1e-5) return
97
+ // The axis to turn about: v × to, or for a half turn any axis square to v.
98
+ let ax = v.y * to.z - v.z * to.y
99
+ let ay = v.z * to.x - v.x * to.z
100
+ let az = v.x * to.y - v.y * to.x
101
+ let al = Math.hypot(ax, ay, az)
102
+ if (al < 1e-6) {
103
+ // Up, unless v is itself vertical.
104
+ if (Math.abs(v.y) < 0.9) {
105
+ ax = -v.z
106
+ ay = 0
107
+ az = v.x
108
+ } else {
109
+ ax = 0
110
+ ay = v.z
111
+ az = -v.y
112
+ }
113
+ al = Math.hypot(ax, ay, az)
114
+ }
115
+ ax /= al
116
+ ay /= al
117
+ az /= al
118
+ // Rodrigues: v cos θ + (a × v) sin θ + a (a·v)(1 − cos θ).
119
+ const t = angle * k
120
+ const c = Math.cos(t)
121
+ const s = Math.sin(t)
122
+ const dot = ax * v.x + ay * v.y + az * v.z
123
+ const cx = ay * v.z - az * v.y
124
+ const cy = az * v.x - ax * v.z
125
+ const cz = ax * v.y - ay * v.x
126
+ const x = v.x * c + cx * s + ax * dot * (1 - c)
127
+ const y = v.y * c + cy * s + ay * dot * (1 - c)
128
+ const z = v.z * c + cz * s + az * dot * (1 - c)
129
+ const l = Math.hypot(x, y, z) || 1
130
+ v.setXYZ(x / l, y / l, z / l)
131
+ }
132
+
133
+ /**
134
+ * Between her eyes, world space: the eye bones' midpoint, or up the head bone
135
+ * on a rig without them. Null without a head.
136
+ */
137
+ export function facePoint(m: Model): Vec3 | null {
138
+ const l = m.getBoneWorldPosition(EYES[0])
139
+ const r = m.getBoneWorldPosition(EYES[1])
140
+ if (l && r) return toWorld(m, new Vec3((l.x + r.x) / 2, (l.y + r.y) / 2, (l.z + r.z) / 2))
141
+ const h = m.getBoneWorldMatrix(HEAD)
142
+ if (!h) return null
143
+ const up = new Vec3(h[4], h[5], h[6])
144
+ const ul = Math.hypot(up.x, up.y, up.z) || 1
145
+ return toWorld(m, new Vec3(h[12] + (up.x / ul) * EYES_UP_FALLBACK, h[13] + (up.y / ul) * EYES_UP_FALLBACK, h[14] + (up.z / ul) * EYES_UP_FALLBACK))
146
+ }
147
+
148
+ export class CameraMount {
149
+ options: Required<CameraMountOptions>
150
+ private seeded = false
151
+ private readonly forward = new Vec3(0, 0, -1)
152
+ private readonly up = new Vec3(0, 1, 0)
153
+ /** Selfie: from her face toward the phone, eased. */
154
+ private readonly arm = new Vec3(0, 0, -1)
155
+ private reach = 0
156
+
157
+ constructor(
158
+ readonly model: Model,
159
+ options: CameraMountOptions,
160
+ ) {
161
+ this.options = resolveMountOptions(options)
162
+ }
163
+
164
+ setOptions(options: CameraMountOptions): void {
165
+ const next = resolveMountOptions(options)
166
+ // A new mode or bone starts from where the rig is, not from the old shot.
167
+ if (next.mode !== this.options.mode || next.bone !== this.options.bone) this.seeded = false
168
+ this.options = next
169
+ }
170
+
171
+ /** Snap to the rig on the next step: a seek or a cut is no camera move. */
172
+ reseed(): void {
173
+ this.seeded = false
174
+ }
175
+
176
+ /** This frame's pose, after her pose. Null when the rig lacks the bones. */
177
+ step(dt: number): MountPose | null {
178
+ const o = this.options
179
+ const k = !this.seeded || o.smoothing <= 0 || dt <= 0 ? 1 : 1 - Math.exp(-dt / o.smoothing)
180
+ // A big jump (a seek, a tab come back) is no move to glide through.
181
+ const snap = !this.seeded || dt > 0.25
182
+ return o.mode === "pov" ? this.pov(snap ? 1 : k) : this.selfie(snap ? 1 : k)
183
+ }
184
+
185
+ private pov(k: number): MountPose | null {
186
+ const m = this.model
187
+ const h = m.getBoneWorldMatrix(this.options.bone) ?? m.getBoneWorldMatrix(HEAD)
188
+ if (!h) return null
189
+ const face = this.options.bone === HEAD ? facePoint(m) : toWorld(m, new Vec3(h[12], h[13], h[14]))
190
+ if (!face) return null
191
+ // MMD rests facing −Z: forward is the head's negated third column, up its second.
192
+ const fwd = dirToWorld(m, -h[8], -h[9], -h[10])
193
+ const up = this.options.level && Math.abs(fwd.y) < 0.97 ? UP : dirToWorld(m, h[4], h[5], h[6])
194
+ ease(this.forward, fwd, k)
195
+ ease(this.up, up, k)
196
+ this.seeded = true
197
+ const f = this.forward
198
+ const s = POV_FORWARD * m.scale
199
+ return {
200
+ eye: new Vec3(face.x + f.x * s, face.y + f.y * s, face.z + f.z * s),
201
+ forward: new Vec3(f.x, f.y, f.z),
202
+ up: new Vec3(this.up.x, this.up.y, this.up.z),
203
+ }
204
+ }
205
+
206
+ private selfie(k: number): MountPose | null {
207
+ const m = this.model
208
+ const face = facePoint(m)
209
+ const hand = m.getBoneWorldPosition(this.options.bone)
210
+ if (!face || !hand) return null
211
+ const hw = toWorld(m, hand)
212
+ let ax = hw.x - face.x
213
+ let ay = hw.y - face.y
214
+ let az = hw.z - face.z
215
+ const d = Math.hypot(ax, ay, az)
216
+ if (d < 1e-4) {
217
+ // Hand on the face: hold the direction it last had.
218
+ ax = this.arm.x
219
+ ay = this.arm.y
220
+ az = this.arm.z
221
+ } else {
222
+ ax /= d
223
+ ay /= d
224
+ az /= d
225
+ }
226
+ const full = this.options.distance * m.scale
227
+ const reach = Math.min(full, Math.max(full * SELFIE_MIN, d))
228
+ ease(this.arm, new Vec3(ax, ay, az), k)
229
+ this.reach = this.seeded ? this.reach + (reach - this.reach) * k : reach
230
+ this.seeded = true
231
+ const a = this.arm
232
+ const eye = new Vec3(face.x + a.x * this.reach, face.y + a.y * this.reach, face.z + a.z * this.reach)
233
+ // Aimed back at her face, level: a phone is held upright, not at the wrist's twist.
234
+ const forward = new Vec3(-a.x, -a.y, -a.z)
235
+ const up = Math.abs(forward.y) < 0.97 ? new Vec3(0, 1, 0) : dirToWorld(m, 0, 0, -1)
236
+ return { eye, forward, up }
237
+ }
238
+ }
239
+
240
+ /**
241
+ * The materials that ARE her head — hair, face, eyes, brows — for a POV shot
242
+ * to leave out of the camera (and only the camera: her shadow and her mirror
243
+ * image keep them). A material is hers when most of its skinning weight rides
244
+ * 頭 or a bone under it. Computed once per mount.
245
+ */
246
+ export function headMaterials(m: Model): string[] {
247
+ const bones = m.getSkeleton().bones
248
+ const head = bones.findIndex((b) => b.name === HEAD)
249
+ if (head < 0) return []
250
+ const under = new Uint8Array(bones.length)
251
+ for (let i = 0; i < bones.length; i++) {
252
+ // Walk up to the root; a rig's chain is short.
253
+ for (let b = i, n = 0; b >= 0 && n < 256; b = bones[b].parentIndex, n++) {
254
+ if (b === head) {
255
+ under[i] = 1
256
+ break
257
+ }
258
+ }
259
+ }
260
+ const { joints, weights } = m.getSkinning()
261
+ const indices = m.getIndices()
262
+ const out: string[] = []
263
+ let start = 0
264
+ for (const mat of m.getMaterials()) {
265
+ const count = mat.vertexCount
266
+ let onHead = 0
267
+ let total = 0
268
+ for (let i = start; i < start + count; i++) {
269
+ const v = indices[i]
270
+ for (let j = 0; j < 4; j++) {
271
+ const w = weights[v * 4 + j]
272
+ if (w === 0) continue
273
+ total += w
274
+ if (under[joints[v * 4 + j]]) onHead += w
275
+ }
276
+ }
277
+ start += count
278
+ if (total > 0 && onHead / total > 0.5) out.push(mat.name)
279
+ }
280
+ return out
281
+ }