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