reze-engine 0.69.2 → 0.69.3
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/engine.d.ts +57 -0
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +240 -14
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/param-track.d.ts +9 -0
- package/dist/param-track.d.ts.map +1 -1
- package/dist/param-track.js +11 -25
- package/dist/shaders/directives.d.ts +5 -4
- package/dist/shaders/directives.d.ts.map +1 -1
- package/dist/shaders/directives.js +40 -1
- package/package.json +1 -1
- package/src/engine.ts +252 -13
- package/src/index.ts +3 -0
- package/src/param-track.ts +21 -1
- package/src/shaders/directives.ts +45 -5
package/src/engine.ts
CHANGED
|
@@ -611,7 +611,31 @@ type SunOptions = {
|
|
|
611
611
|
/** An effect param: number → f32, vector-like → vec3f (see setEffect).
|
|
612
612
|
* Structural {x,y,z} rather than the Vec3 class so JSON-derived values (a
|
|
613
613
|
* shared scene document's params) pass straight in. */
|
|
614
|
-
export type EffectParamValue = number | { x: number; y: number; z: number }
|
|
614
|
+
export type EffectParamValue = number | { x: number; y: number; z: number } | EffectPointValue | EffectBoneValue
|
|
615
|
+
|
|
616
|
+
/** A `point` dial: a world position, or — with `model` — an offset from where
|
|
617
|
+
* that character stands, so the point travels with her. */
|
|
618
|
+
export type EffectPointValue = { x: number; y: number; z: number; model?: string | null }
|
|
619
|
+
|
|
620
|
+
/** A `bone` dial: a bone by name, on `model`, or on the effect's first
|
|
621
|
+
* character when it names none. */
|
|
622
|
+
export type EffectBoneValue = { bone: string; model?: string | null }
|
|
623
|
+
|
|
624
|
+
/** One key of a dial's track. `time` is seconds on the transport — the clock an
|
|
625
|
+
* effect's strips run on. `ease` shapes the way to the NEXT key; switches,
|
|
626
|
+
* lists and bones always hold, whatever it says. */
|
|
627
|
+
export type EffectParamKey = {
|
|
628
|
+
time: number
|
|
629
|
+
value: EffectParamValue
|
|
630
|
+
ease?: "linear" | "ease" | "hold"
|
|
631
|
+
/** The way INTO this key from the previous one, as a bezier — x1, y1, x2, y2
|
|
632
|
+
* in 0..1, the curve a VMD key carries. `ease` is that segment too.
|
|
633
|
+
* Ignored by a hold, and by dials that always hold. */
|
|
634
|
+
curve?: [number, number, number, number]
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
const isBoneValue = (v: EffectParamValue | undefined): v is EffectBoneValue =>
|
|
638
|
+
typeof v === "object" && v !== null && "bone" in v
|
|
615
639
|
|
|
616
640
|
/** A vector by shape rather than by class — Vec3 satisfies it, and so does a
|
|
617
641
|
* JSON object out of a scene document or a literal typed into a console. */
|
|
@@ -1633,7 +1657,8 @@ const TEXTURE_EXTENSION_GUESSES = [".png", ".jpg", ".jpeg", ".bmp", ".tga", ".dd
|
|
|
1633
1657
|
* darken every authored colour by the transfer curve twice.
|
|
1634
1658
|
*/
|
|
1635
1659
|
function paramValue(p: EffectParamDecl, given: EffectParamValue | undefined): EffectParamValue {
|
|
1636
|
-
if (
|
|
1660
|
+
if (p.kind === "bone") return isBoneValue(given) ? given : { bone: typeof p.value === "string" ? p.value : "", model: null }
|
|
1661
|
+
if (given !== undefined) return p.kind === "int" && typeof given === "number" ? Math.round(given) : given
|
|
1637
1662
|
if (p.kind === "color" && typeof p.value === "string") {
|
|
1638
1663
|
const n = parseInt(p.value.slice(1), 16)
|
|
1639
1664
|
return { x: ((n >> 16) & 255) / 255, y: ((n >> 8) & 255) / 255, z: (n & 255) / 255 }
|
|
@@ -1864,6 +1889,12 @@ interface EffectInstance {
|
|
|
1864
1889
|
paramLayout: Map<string, { offset: number; comps: 1 | 3 }>
|
|
1865
1890
|
paramsBuffer: GPUBuffer | null
|
|
1866
1891
|
paramsData: Float32Array<ArrayBuffer>
|
|
1892
|
+
/** The dials the engine resolves every frame rather than once: each bone
|
|
1893
|
+
* dial's bone and character, and each point dial set to follow one. */
|
|
1894
|
+
paramBindings: Map<string, EffectBoneValue | EffectPointValue>
|
|
1895
|
+
/** Dials driven by keys, by name — see setEffectParamTrack. Bone keys index
|
|
1896
|
+
* `bones`, since the sampler carries numbers. */
|
|
1897
|
+
paramTracks: Map<string, { keys: ParamKey[]; bones: EffectBoneValue[]; last: ParamValue | null }>
|
|
1867
1898
|
/** This effect IS a mirror — see the `#mirror` directive. Draws no shader;
|
|
1868
1899
|
* the engine reads its dials and folds the scene through the plane. */
|
|
1869
1900
|
hasMirror: boolean
|
|
@@ -1930,6 +1961,9 @@ interface EffectInstance {
|
|
|
1930
1961
|
* permanently half-strength effect and a scheduled one are the same dial.
|
|
1931
1962
|
*/
|
|
1932
1963
|
influence: number
|
|
1964
|
+
/** `influence` over time, keyed on the transport — a fade is two keys.
|
|
1965
|
+
* Null holds `influence`. */
|
|
1966
|
+
influenceTrack: ParamKey[] | null
|
|
1933
1967
|
/** Its strips, in scene seconds — a LANE, so one effect can fire more than
|
|
1934
1968
|
* once. Null or empty = on for the whole scene, which is what applying an
|
|
1935
1969
|
* effect does until someone places it. */
|
|
@@ -5673,7 +5707,19 @@ export class Engine {
|
|
|
5673
5707
|
// KIND comes from the declaration, never from the runtime type of the
|
|
5674
5708
|
// value: a caller handing a number to a colour must not silently turn a
|
|
5675
5709
|
// vec3f field into an f32 and shift every field after it.
|
|
5676
|
-
|
|
5710
|
+
if (p.kind === "bone") {
|
|
5711
|
+
// Three fields the engine fills each frame — where the bone is, the way
|
|
5712
|
+
// it points, and whether this rig has it — read through rzBone_NAME().
|
|
5713
|
+
for (const [field, vec] of [[p.name, true], [`${p.name}__fwd`, true], [`${p.name}__ok`, false]] as const) {
|
|
5714
|
+
const align = vec ? 16 : 4
|
|
5715
|
+
const offset = Math.ceil(cursor / align) * align
|
|
5716
|
+
layout.set(field, { offset: offset / 4, comps: vec ? 3 : 1 })
|
|
5717
|
+
fields.push(` ${field}: ${vec ? "vec3f" : "f32"},`)
|
|
5718
|
+
cursor = offset + (vec ? 12 : 4)
|
|
5719
|
+
}
|
|
5720
|
+
continue
|
|
5721
|
+
}
|
|
5722
|
+
const isVec = p.kind === "color" || p.kind === "vec3" || p.kind === "point"
|
|
5677
5723
|
const align = isVec ? 16 : 4
|
|
5678
5724
|
const offset = Math.ceil(cursor / align) * align
|
|
5679
5725
|
layout.set(p.name, { offset: offset / 4, comps: isVec ? 3 : 1 })
|
|
@@ -5681,15 +5727,35 @@ export class Engine {
|
|
|
5681
5727
|
cursor = offset + (isVec ? 12 : 4)
|
|
5682
5728
|
}
|
|
5683
5729
|
const paramsData = new Float32Array(Math.max(4, Math.ceil(cursor / 16) * 4))
|
|
5730
|
+
const paramBindings = new Map<string, EffectBoneValue | EffectPointValue>()
|
|
5684
5731
|
for (const [name, value] of entries) {
|
|
5685
5732
|
const slot = layout.get(name)!
|
|
5733
|
+
// A bone, and a point that follows someone, are resolved every frame;
|
|
5734
|
+
// their fields start empty and the first evaluation fills them.
|
|
5735
|
+
if (isBoneValue(value)) {
|
|
5736
|
+
paramBindings.set(name, value)
|
|
5737
|
+
continue
|
|
5738
|
+
}
|
|
5686
5739
|
if (typeof value === "number") paramsData[slot.offset] = value
|
|
5687
5740
|
else {
|
|
5741
|
+
if ("model" in value && value.model) paramBindings.set(name, value)
|
|
5688
5742
|
paramsData[slot.offset] = value.x
|
|
5689
5743
|
paramsData[slot.offset + 1] = value.y
|
|
5690
5744
|
paramsData[slot.offset + 2] = value.z
|
|
5691
5745
|
}
|
|
5692
5746
|
}
|
|
5747
|
+
// rzBone_NAME() for each bone dial: its own struct rather than RzAnchor, so
|
|
5748
|
+
// the accessor stands in every module a params block is spliced into.
|
|
5749
|
+
const bones = d.params.filter((p) => p.kind === "bone")
|
|
5750
|
+
const boneWgsl = bones.length
|
|
5751
|
+
? "struct RzBoneParam { pos: vec3f, fwd: vec3f, valid: bool, }\n" +
|
|
5752
|
+
bones
|
|
5753
|
+
.map(
|
|
5754
|
+
(p) =>
|
|
5755
|
+
`fn rzBone_${p.name}() -> RzBoneParam { return RzBoneParam(params.${p.name}, params.${p.name}__fwd, params.${p.name}__ok > 0.5); }\n`,
|
|
5756
|
+
)
|
|
5757
|
+
.join("")
|
|
5758
|
+
: ""
|
|
5693
5759
|
/**
|
|
5694
5760
|
* The params block, at whatever binding the pass asking has free.
|
|
5695
5761
|
*
|
|
@@ -5700,7 +5766,7 @@ export class Engine {
|
|
|
5700
5766
|
*/
|
|
5701
5767
|
const paramsWgsl = (binding: number) =>
|
|
5702
5768
|
entries.length
|
|
5703
|
-
? `struct EffectParams {\n${fields.join("\n")}\n}\n@group(0) @binding(${binding}) var<uniform> params: EffectParams;\n`
|
|
5769
|
+
? `struct EffectParams {\n${fields.join("\n")}\n}\n@group(0) @binding(${binding}) var<uniform> params: EffectParams;\n${boneWgsl}`
|
|
5704
5770
|
: ""
|
|
5705
5771
|
const paramsDecl = paramsWgsl(EFFECT_PARAMS_BINDING)
|
|
5706
5772
|
|
|
@@ -5906,6 +5972,8 @@ export class Engine {
|
|
|
5906
5972
|
paramLayout: layout,
|
|
5907
5973
|
paramsBuffer,
|
|
5908
5974
|
paramsData,
|
|
5975
|
+
paramBindings,
|
|
5976
|
+
paramTracks: new Map(),
|
|
5909
5977
|
hasMirror: d.mirror,
|
|
5910
5978
|
stepped: d.stepped,
|
|
5911
5979
|
hasBackground,
|
|
@@ -5937,6 +6005,7 @@ export class Engine {
|
|
|
5937
6005
|
// scheduling it is something a caller does afterwards, and an install
|
|
5938
6006
|
// that silently began at zero would look like a compile that failed.
|
|
5939
6007
|
influence: 1,
|
|
6008
|
+
influenceTrack: null,
|
|
5940
6009
|
window: null,
|
|
5941
6010
|
weight: 1,
|
|
5942
6011
|
sim: { start: null, time: null },
|
|
@@ -7525,15 +7594,158 @@ export class Engine {
|
|
|
7525
7594
|
setEffectParam(index: number, name: string, value: EffectParamValue): void {
|
|
7526
7595
|
const fx = this.effects[index]
|
|
7527
7596
|
if (!fx || !fx.paramsBuffer) return
|
|
7597
|
+
// A keyed dial is its track's: the frame writes it only when the sampled
|
|
7598
|
+
// value changes, so a plain value written over it would stick until then.
|
|
7599
|
+
if (fx.paramTracks.has(name)) return
|
|
7600
|
+
if (this.writeEffectParam(fx, name, value)) this.device.queue.writeBuffer(fx.paramsBuffer, 0, fx.paramsData)
|
|
7601
|
+
// A bone, or a point that now follows someone, lands on the next frame's
|
|
7602
|
+
// evaluation; resolving it now as well means a paused scene shows it too.
|
|
7603
|
+
if (fx.paramBindings.has(name)) this.evaluateEffectParams()
|
|
7604
|
+
}
|
|
7605
|
+
|
|
7606
|
+
/** One dial into the instance's block, by its declared kind. Returns whether
|
|
7607
|
+
* the block changed — a bone changes only its binding, which the frame's
|
|
7608
|
+
* evaluation writes. */
|
|
7609
|
+
private writeEffectParam(fx: EffectInstance, name: string, value: EffectParamValue): boolean {
|
|
7610
|
+
const decl = fx.paramDecls.find((p) => p.name === name)
|
|
7528
7611
|
const slot = fx.paramLayout.get(name)
|
|
7529
|
-
if (!slot) return
|
|
7530
|
-
if (
|
|
7531
|
-
|
|
7532
|
-
|
|
7533
|
-
|
|
7534
|
-
|
|
7612
|
+
if (!decl || !slot) return false
|
|
7613
|
+
if (decl.kind === "bone") {
|
|
7614
|
+
if (isBoneValue(value)) fx.paramBindings.set(name, value)
|
|
7615
|
+
return false
|
|
7616
|
+
}
|
|
7617
|
+
if (isBoneValue(value)) return false
|
|
7618
|
+
if (typeof value === "number") {
|
|
7619
|
+
fx.paramsData[slot.offset] = decl.kind === "int" ? Math.round(value) : value
|
|
7620
|
+
return true
|
|
7621
|
+
}
|
|
7622
|
+
if (decl.kind === "point" && "model" in value && value.model) fx.paramBindings.set(name, value)
|
|
7623
|
+
else fx.paramBindings.delete(name)
|
|
7624
|
+
fx.paramsData[slot.offset] = value.x
|
|
7625
|
+
fx.paramsData[slot.offset + 1] = value.y
|
|
7626
|
+
fx.paramsData[slot.offset + 2] = value.z
|
|
7627
|
+
return true
|
|
7628
|
+
}
|
|
7629
|
+
|
|
7630
|
+
/**
|
|
7631
|
+
* Drive one dial from keys on the transport — the clock the effect's strips
|
|
7632
|
+
* run on, so a key at a clip's start lands at the clip's start in the editor,
|
|
7633
|
+
* a scrub and an export alike. Null, or no keys, gives the dial back to
|
|
7634
|
+
* setEffectParam.
|
|
7635
|
+
*
|
|
7636
|
+
* Numbers, whole numbers, colours, vectors and points travel between keys,
|
|
7637
|
+
* straight or eased; switches, lists and bones hold until the next key. A
|
|
7638
|
+
* track lives on the instance, so an install builds fresh ones and the host
|
|
7639
|
+
* pushes its tracks again, as it does a schedule.
|
|
7640
|
+
*/
|
|
7641
|
+
setEffectParamTrack(index: number, name: string, keys: readonly EffectParamKey[] | null): boolean {
|
|
7642
|
+
const fx = this.effects[index]
|
|
7643
|
+
const decl = fx?.paramDecls.find((p) => p.name === name)
|
|
7644
|
+
if (!fx || !decl) return false
|
|
7645
|
+
if (!keys || keys.length === 0) {
|
|
7646
|
+
fx.paramTracks.delete(name)
|
|
7647
|
+
return true
|
|
7648
|
+
}
|
|
7649
|
+
const holds = decl.kind === "bool" || decl.kind === "enum" || decl.kind === "bone"
|
|
7650
|
+
const bones: EffectBoneValue[] = []
|
|
7651
|
+
const sorted = [...keys].sort((a, b) => a.time - b.time)
|
|
7652
|
+
const track: ParamKey[] = []
|
|
7653
|
+
for (const k of sorted) {
|
|
7654
|
+
let v: ParamValue
|
|
7655
|
+
if (decl.kind === "bone") {
|
|
7656
|
+
if (!isBoneValue(k.value)) continue
|
|
7657
|
+
v = bones.push(k.value) - 1
|
|
7658
|
+
} else if (typeof k.value === "number") v = k.value
|
|
7659
|
+
else if (isBoneValue(k.value)) continue
|
|
7660
|
+
else v = [k.value.x, k.value.y, k.value.z]
|
|
7661
|
+
track.push({
|
|
7662
|
+
t: k.time,
|
|
7663
|
+
v,
|
|
7664
|
+
e: holds ? "hold" : (k.ease ?? "linear"),
|
|
7665
|
+
...(!holds && k.curve && k.ease !== "hold" ? { b: k.curve } : {}),
|
|
7666
|
+
})
|
|
7667
|
+
}
|
|
7668
|
+
fx.paramTracks.set(name, { keys: track, bones, last: null })
|
|
7669
|
+
this.evaluateEffectParams()
|
|
7670
|
+
return true
|
|
7671
|
+
}
|
|
7672
|
+
|
|
7673
|
+
/**
|
|
7674
|
+
* Every effect's keyed dials at this transport time, then every bone and
|
|
7675
|
+
* followed point where it stands now — once a frame, after the schedules, so
|
|
7676
|
+
* each mount reads this frame's values. One upload per effect, and only for
|
|
7677
|
+
* one whose block moved.
|
|
7678
|
+
*/
|
|
7679
|
+
private evaluateEffectParams(): void {
|
|
7680
|
+
let transport: number | null = null
|
|
7681
|
+
for (const fx of this.effects) {
|
|
7682
|
+
if (!fx.paramsBuffer) continue
|
|
7683
|
+
let dirty = false
|
|
7684
|
+
for (const [name, track] of fx.paramTracks) {
|
|
7685
|
+
transport ??= this.transportTime()
|
|
7686
|
+
const v = sampleParamTrack(track.keys, transport)
|
|
7687
|
+
if (v === null || !paramChanged(v, track.last)) continue
|
|
7688
|
+
track.last = v
|
|
7689
|
+
const decl = fx.paramDecls.find((p) => p.name === name)
|
|
7690
|
+
if (decl?.kind === "bone") {
|
|
7691
|
+
const bone = typeof v === "number" ? track.bones[v] : undefined
|
|
7692
|
+
if (bone) fx.paramBindings.set(name, bone)
|
|
7693
|
+
} else if (typeof v === "number") {
|
|
7694
|
+
dirty = this.writeEffectParam(fx, name, v) || dirty
|
|
7695
|
+
} else {
|
|
7696
|
+
const held = fx.paramBindings.get(name)
|
|
7697
|
+
const model = held && !isBoneValue(held) ? held.model : undefined
|
|
7698
|
+
dirty = this.writeEffectParam(fx, name, { x: v[0], y: v[1], z: v[2], ...(model ? { model } : {}) }) || dirty
|
|
7699
|
+
}
|
|
7700
|
+
}
|
|
7701
|
+
if (fx.paramBindings.size) dirty = this.resolveParamBindings(fx) || dirty
|
|
7702
|
+
if (dirty) this.device.queue.writeBuffer(fx.paramsBuffer, 0, fx.paramsData)
|
|
7703
|
+
}
|
|
7704
|
+
}
|
|
7705
|
+
|
|
7706
|
+
/** Bones and followed points into the block, where they stand this frame. */
|
|
7707
|
+
private resolveParamBindings(fx: EffectInstance): boolean {
|
|
7708
|
+
const d = fx.paramsData
|
|
7709
|
+
let moved = false
|
|
7710
|
+
const put = (offset: number, x: number, y: number, z: number) => {
|
|
7711
|
+
if (d[offset] === x && d[offset + 1] === y && d[offset + 2] === z) return
|
|
7712
|
+
d[offset] = x
|
|
7713
|
+
d[offset + 1] = y
|
|
7714
|
+
d[offset + 2] = z
|
|
7715
|
+
moved = true
|
|
7716
|
+
}
|
|
7717
|
+
const first = () => this.castSubjectNames(fx.subjects)[0] ?? null
|
|
7718
|
+
for (const [name, value] of fx.paramBindings) {
|
|
7719
|
+
const slot = fx.paramLayout.get(name)
|
|
7720
|
+
if (!slot) continue
|
|
7721
|
+
const inst = this.modelInstances.get((value.model ?? first()) ?? "")
|
|
7722
|
+
if (isBoneValue(value)) {
|
|
7723
|
+
const fwdSlot = fx.paramLayout.get(`${name}__fwd`)
|
|
7724
|
+
const okSlot = fx.paramLayout.get(`${name}__ok`)
|
|
7725
|
+
const pos = inst?.model.getBoneWorldPosition(value.bone) ?? null
|
|
7726
|
+
const ok = pos ? 1 : 0
|
|
7727
|
+
if (okSlot && d[okSlot.offset] !== ok) {
|
|
7728
|
+
d[okSlot.offset] = ok
|
|
7729
|
+
moved = true
|
|
7730
|
+
}
|
|
7731
|
+
if (!inst || !pos) continue
|
|
7732
|
+
const m = inst.model
|
|
7733
|
+
// Model space into the world, as the cast's own anchors go.
|
|
7734
|
+
pos.setXYZ(pos.x * m.scale, pos.y * m.scale, pos.z * m.scale)
|
|
7735
|
+
Quat.rotateVecInto(m.rotation, pos, pos)
|
|
7736
|
+
put(slot.offset, pos.x + m.position.x, pos.y + m.position.y, pos.z + m.position.z)
|
|
7737
|
+
const fwd = m.getBoneWorldForward(value.bone)
|
|
7738
|
+
if (fwd && fwdSlot) {
|
|
7739
|
+
Quat.rotateVecInto(m.rotation, fwd, fwd)
|
|
7740
|
+
put(fwdSlot.offset, fwd.x, fwd.y, fwd.z)
|
|
7741
|
+
}
|
|
7742
|
+
} else if (inst) {
|
|
7743
|
+
// A followed point: the offset from where she stands.
|
|
7744
|
+
const m = inst.model
|
|
7745
|
+
put(slot.offset, m.position.x + value.x, m.position.y + value.y, m.position.z + value.z)
|
|
7746
|
+
}
|
|
7535
7747
|
}
|
|
7536
|
-
|
|
7748
|
+
return moved
|
|
7537
7749
|
}
|
|
7538
7750
|
|
|
7539
7751
|
/**
|
|
@@ -7571,6 +7783,28 @@ export class Engine {
|
|
|
7571
7783
|
fx.influence = Math.min(1, Math.max(0, influence))
|
|
7572
7784
|
}
|
|
7573
7785
|
|
|
7786
|
+
/**
|
|
7787
|
+
* Influence from keys on the transport, the way a dial is keyed: a fade in is
|
|
7788
|
+
* a key at 0 and a key at 1, eased by its curve. Null or no keys gives it
|
|
7789
|
+
* back to setEffectInfluence. It shapes the level the strips ramp toward.
|
|
7790
|
+
*/
|
|
7791
|
+
setEffectInfluenceTrack(index: number, keys: readonly EffectParamKey[] | null): void {
|
|
7792
|
+
const fx = this.effects[index]
|
|
7793
|
+
if (!fx) return
|
|
7794
|
+
const track: ParamKey[] = []
|
|
7795
|
+
for (const k of keys ?? []) {
|
|
7796
|
+
if (typeof k.value !== "number") continue
|
|
7797
|
+
track.push({
|
|
7798
|
+
t: k.time,
|
|
7799
|
+
v: k.value,
|
|
7800
|
+
...(k.ease ? { e: k.ease } : {}),
|
|
7801
|
+
...(k.curve && k.ease !== "hold" ? { b: k.curve } : {}),
|
|
7802
|
+
})
|
|
7803
|
+
}
|
|
7804
|
+
track.sort((a, b) => a.t - b.t)
|
|
7805
|
+
fx.influenceTrack = track.length ? track : null
|
|
7806
|
+
}
|
|
7807
|
+
|
|
7574
7808
|
getEffectInfluence(index: number): number {
|
|
7575
7809
|
return this.effects[index]?.influence ?? 0
|
|
7576
7810
|
}
|
|
@@ -7659,11 +7893,13 @@ export class Engine {
|
|
|
7659
7893
|
const transport = this.transportTime()
|
|
7660
7894
|
for (const fx of this.effects) {
|
|
7661
7895
|
fx.simReset = false
|
|
7896
|
+
const keyed = fx.influenceTrack ? sampleParamTrack(fx.influenceTrack, transport) : null
|
|
7897
|
+
const level = typeof keyed === "number" ? Math.min(1, Math.max(0, keyed)) : fx.influence
|
|
7662
7898
|
if (!fx.window || fx.window.length === 0) {
|
|
7663
|
-
fx.weight =
|
|
7899
|
+
fx.weight = level
|
|
7664
7900
|
continue
|
|
7665
7901
|
}
|
|
7666
|
-
const at = effectState(fx.window,
|
|
7902
|
+
const at = effectState(fx.window, level, transport)
|
|
7667
7903
|
fx.weight = at.weight
|
|
7668
7904
|
// The particles and the grid carry state from frame to frame, so they get
|
|
7669
7905
|
// more than a clock: a restart on entering a window or going back in one,
|
|
@@ -18087,6 +18323,9 @@ export class Engine {
|
|
|
18087
18323
|
// caller so that playback, the export loop and a warm-up pass cannot
|
|
18088
18324
|
// disagree about when an effect is alive — none of them has to remember it.
|
|
18089
18325
|
this.evaluateEffectSchedules()
|
|
18326
|
+
// The dials' keys and the bones and points they follow, on this frame —
|
|
18327
|
+
// after the schedules, before anything reads an effect's block.
|
|
18328
|
+
this.evaluateEffectParams()
|
|
18090
18329
|
this.stepSim(encoder, deltaTime)
|
|
18091
18330
|
this.stepParticles(encoder, deltaTime)
|
|
18092
18331
|
// Before the scene pass, which READS the slots this writes. Same buffer,
|
package/src/index.ts
CHANGED
package/src/param-track.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { bezierInterpolate } from "./animation"
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* A material parameter over time.
|
|
3
5
|
*
|
|
@@ -26,10 +28,20 @@
|
|
|
26
28
|
/** A parameter value: a scalar, or a vector for the vec3 params. */
|
|
27
29
|
export type ParamValue = number | [number, number, number]
|
|
28
30
|
|
|
31
|
+
/** How the way INTO a key runs: straight, eased in and out, or held at the
|
|
32
|
+
* previous key's value and stepped on arrival. Absent is linear. */
|
|
33
|
+
export type ParamEase = "linear" | "ease" | "hold"
|
|
34
|
+
|
|
29
35
|
export type ParamKey = {
|
|
30
36
|
/** Seconds on the SCENE clock — the same clock an export steps. */
|
|
31
37
|
t: number
|
|
32
38
|
v: ParamValue
|
|
39
|
+
/** The segment from the previous key to this one — VMD's rule, where a key
|
|
40
|
+
* carries the curve that arrives at it. */
|
|
41
|
+
e?: ParamEase
|
|
42
|
+
/** That segment as a bezier — x1, y1, x2, y2 in 0..1, VMD's own curve — in
|
|
43
|
+
* place of `e` when it is not a hold. */
|
|
44
|
+
b?: [number, number, number, number]
|
|
33
45
|
}
|
|
34
46
|
|
|
35
47
|
const lerp = (a: number, b: number, k: number): number => a + (b - a) * k
|
|
@@ -65,7 +77,15 @@ export function sampleParamTrack(keys: ParamKey[], t: number): ParamValue | null
|
|
|
65
77
|
// Two keys at the same instant are a STEP, and the later one wins. Dividing
|
|
66
78
|
// by the zero span would be a NaN written into a uniform every frame.
|
|
67
79
|
if (span <= 0) return b.v
|
|
68
|
-
|
|
80
|
+
if (b.e === "hold") return a.v
|
|
81
|
+
const u = (t - a.t) / span
|
|
82
|
+
// Smoothstep: no jump in speed at either key, which is what makes a dial
|
|
83
|
+
// that eases between two values read as a move rather than as a ramp.
|
|
84
|
+
const k = b.b
|
|
85
|
+
? bezierInterpolate(b.b[0], b.b[2], b.b[1], b.b[3], u)
|
|
86
|
+
: b.e === "ease"
|
|
87
|
+
? u * u * (3 - 2 * u)
|
|
88
|
+
: u
|
|
69
89
|
if (typeof a.v === "number" && typeof b.v === "number") return lerp(a.v, b.v, k)
|
|
70
90
|
// Mixed scalar and vector keys are an authoring mistake, not something to
|
|
71
91
|
// guess at: hold the segment's start rather than inventing a conversion.
|
|
@@ -69,11 +69,12 @@ export type EffectParamOption = { id: string; labelZh?: string }
|
|
|
69
69
|
*/
|
|
70
70
|
export type EffectParamDecl = {
|
|
71
71
|
name: string
|
|
72
|
-
kind: "float" | "color" | "vec3" | "bool" | "enum"
|
|
73
|
-
/** Numbers for float/vec3, 0 or 1 for bool, the option's index for
|
|
74
|
-
* `#rrggbb` for a colour, which the host
|
|
72
|
+
kind: "float" | "color" | "vec3" | "bool" | "enum" | "int" | "point" | "bone"
|
|
73
|
+
/** Numbers for float/vec3/int/point, 0 or 1 for bool, the option's index for
|
|
74
|
+
* enum, a bone's NAME for bone; `#rrggbb` for a colour, which the host
|
|
75
|
+
* converts. */
|
|
75
76
|
value: number | [number, number, number] | string
|
|
76
|
-
/** float only, and only when the author gave a range. */
|
|
77
|
+
/** float and int only, and only when the author gave a range. */
|
|
77
78
|
min?: number
|
|
78
79
|
max?: number
|
|
79
80
|
/** enum only: the choices, in index order. */
|
|
@@ -351,6 +352,45 @@ export function parseDirectives(wgsl: string): DirectiveResult {
|
|
|
351
352
|
d.params.push({ name, kind: "bool", value: on ? 1 : 0, ...labels })
|
|
352
353
|
return
|
|
353
354
|
}
|
|
355
|
+
if (kind === "int") {
|
|
356
|
+
// A count: strands, rows, petals. One f32 in the shader like a
|
|
357
|
+
// slider, but whole — the host steps it and a key never lands between.
|
|
358
|
+
const v = num(rest[0])
|
|
359
|
+
if (v === null || !Number.isInteger(v)) {
|
|
360
|
+
errors.push(`${at}: #param int ${name} needs a whole-number default`)
|
|
361
|
+
return
|
|
362
|
+
}
|
|
363
|
+
const lo = num(rest[1])
|
|
364
|
+
const hi = num(rest[2])
|
|
365
|
+
if ((rest[1] !== undefined) !== (rest[2] !== undefined) || (rest[1] !== undefined && (lo === null || hi === null))) {
|
|
366
|
+
errors.push(`${at}: #param int ${name} takes both a min and a max, or neither`)
|
|
367
|
+
return
|
|
368
|
+
}
|
|
369
|
+
d.params.push({ name, kind: "int", value: v, ...(lo !== null && hi !== null ? { min: lo, max: hi } : {}), ...labels })
|
|
370
|
+
return
|
|
371
|
+
}
|
|
372
|
+
if (kind === "point") {
|
|
373
|
+
// A place in the world, in world units — set by dragging it in the
|
|
374
|
+
// scene, and able to follow a character. One vec3f in the shader.
|
|
375
|
+
const v = rest.slice(0, 3).map(num)
|
|
376
|
+
if (rest.length !== 3 || v.some((x) => x === null)) {
|
|
377
|
+
errors.push(`${at}: #param point ${name} needs three numbers, a position like "0 10 0"`)
|
|
378
|
+
return
|
|
379
|
+
}
|
|
380
|
+
d.params.push({ name, kind: "point", value: v as [number, number, number], ...labels })
|
|
381
|
+
return
|
|
382
|
+
}
|
|
383
|
+
if (kind === "bone") {
|
|
384
|
+
// A bone of a character, picked by the host: read as rzBone_NAME(),
|
|
385
|
+
// a position, a forward and whether that rig has it. The default is
|
|
386
|
+
// the bone's name; whose bone it is, the host decides.
|
|
387
|
+
if (rest.length !== 1) {
|
|
388
|
+
errors.push(`${at}: #param bone ${name} needs one bone name, like 右手首`)
|
|
389
|
+
return
|
|
390
|
+
}
|
|
391
|
+
d.params.push({ name, kind: "bone", value: rest[0], ...labels })
|
|
392
|
+
return
|
|
393
|
+
}
|
|
354
394
|
if (kind === "enum") {
|
|
355
395
|
// Each choice is `id` or `id|中文`; a leading `*` marks the default,
|
|
356
396
|
// else the first is.
|
|
@@ -407,7 +447,7 @@ export function parseDirectives(wgsl: string): DirectiveResult {
|
|
|
407
447
|
d.params.push({ name, kind: "float", value: v, ...(lo !== null && hi !== null ? { min: lo, max: hi } : {}), ...labels })
|
|
408
448
|
return
|
|
409
449
|
}
|
|
410
|
-
errors.push(`${at}: #param kind must be float, color, vec3, bool or enum`)
|
|
450
|
+
errors.push(`${at}: #param kind must be float, int, color, vec3, point, bone, bool or enum`)
|
|
411
451
|
return
|
|
412
452
|
}
|
|
413
453
|
case "halfres":
|