@hackbuild/avatar-host 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +211 -2
- package/dist/avatar-host.iife.js +13454 -0
- package/dist/body.d.ts +78 -0
- package/dist/body.js +191 -0
- package/dist/drop.d.ts +20 -0
- package/dist/drop.js +44 -0
- package/dist/element.d.ts +9 -0
- package/dist/element.js +77 -0
- package/dist/environment.d.ts +35 -0
- package/dist/environment.js +59 -0
- package/dist/errors.d.ts +2 -0
- package/dist/errors.js +25 -0
- package/dist/follow.d.ts +42 -0
- package/dist/follow.js +97 -0
- package/dist/host.d.ts +39 -0
- package/dist/host.js +88 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +10 -0
- package/dist/load.d.ts +21 -0
- package/dist/load.js +79 -0
- package/dist/loop.d.ts +50 -0
- package/dist/loop.js +102 -0
- package/dist/placeholder.d.ts +11 -0
- package/dist/placeholder.js +90 -0
- package/dist/single.d.ts +2 -0
- package/dist/single.js +4 -0
- package/dist/source.d.ts +35 -0
- package/dist/source.js +83 -0
- package/dist/stage.d.ts +167 -0
- package/dist/stage.js +436 -0
- package/dist/three.d.ts +7 -0
- package/dist/three.js +10 -0
- package/package.json +52 -4
package/dist/follow.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// The follow camera: keeps a posed body in view as clips move it. The orbit
|
|
2
|
+
// target eases after the body's centre (plus where the person panned to),
|
|
3
|
+
// and the distance eases to fit the pose unless the person zoomed by hand.
|
|
4
|
+
// Movement inside a dead zone moves nothing, so idle sway and gestures stay
|
|
5
|
+
// put on screen. The camera pulls back as soon as a pose outgrows the view
|
|
6
|
+
// and comes closer only once it is clearly too far.
|
|
7
|
+
import { Box3, MathUtils, Vector3 } from "three";
|
|
8
|
+
export class FollowCamera {
|
|
9
|
+
camera;
|
|
10
|
+
target;
|
|
11
|
+
/** Set when the person picked a distance; fitting then leaves it alone. */
|
|
12
|
+
zoomed = false;
|
|
13
|
+
/** Where the person panned to, from the body's centre. */
|
|
14
|
+
offset = new Vector3();
|
|
15
|
+
rate;
|
|
16
|
+
deadZone;
|
|
17
|
+
slack;
|
|
18
|
+
margin;
|
|
19
|
+
v1 = new Vector3();
|
|
20
|
+
v2 = new Vector3();
|
|
21
|
+
v3 = new Vector3();
|
|
22
|
+
constructor(camera,
|
|
23
|
+
/** The orbit target the camera looks at (OrbitControls.target). */
|
|
24
|
+
target, opts = {}) {
|
|
25
|
+
this.camera = camera;
|
|
26
|
+
this.target = target;
|
|
27
|
+
this.rate = opts.rate ?? 3;
|
|
28
|
+
this.deadZone = opts.deadZone ?? 0.06;
|
|
29
|
+
this.slack = opts.slack ?? 0.12;
|
|
30
|
+
this.margin = opts.margin ?? 1.12;
|
|
31
|
+
}
|
|
32
|
+
distance() {
|
|
33
|
+
return this.camera.position.distanceTo(this.target);
|
|
34
|
+
}
|
|
35
|
+
/** Distance at which the box fills the view with the margin, from any side. */
|
|
36
|
+
fitDistance(box) {
|
|
37
|
+
const size = box.getSize(this.v2);
|
|
38
|
+
const tan = Math.tan(MathUtils.degToRad(this.camera.fov / 2));
|
|
39
|
+
const wide = Math.max(size.x, size.z);
|
|
40
|
+
const deep = Math.min(size.x, size.z);
|
|
41
|
+
return Math.max(size.y / 2 / tan, wide / 2 / (tan * this.camera.aspect)) * this.margin + deep / 2;
|
|
42
|
+
}
|
|
43
|
+
/** Frames the box from the front (+Z) and forgets the person's pan and zoom. */
|
|
44
|
+
frame(box) {
|
|
45
|
+
const center = box.getCenter(this.v1);
|
|
46
|
+
const h = Math.max(box.getSize(this.v3).y, 0.2);
|
|
47
|
+
this.offset.set(0, 0, 0);
|
|
48
|
+
this.zoomed = false;
|
|
49
|
+
this.target.copy(center);
|
|
50
|
+
this.camera.position.set(center.x, center.y + h * 0.04, center.z + this.fitDistance(box));
|
|
51
|
+
this.camera.near = Math.max(h / 100, 0.01);
|
|
52
|
+
this.camera.far = h * 100;
|
|
53
|
+
this.camera.updateProjectionMatrix();
|
|
54
|
+
this.camera.lookAt(this.target);
|
|
55
|
+
}
|
|
56
|
+
/** After the person let go: keep their pan as an offset, and their distance when it changed. */
|
|
57
|
+
released(box, startDistance) {
|
|
58
|
+
if (Math.abs(this.distance() - startDistance) > startDistance * 0.02)
|
|
59
|
+
this.zoomed = true;
|
|
60
|
+
if (box)
|
|
61
|
+
this.offset.copy(this.target).sub(box.getCenter(this.v1));
|
|
62
|
+
}
|
|
63
|
+
/** One frame of following. Returns true when the camera moved. */
|
|
64
|
+
step(box, dt) {
|
|
65
|
+
const k = 1 - Math.exp(-dt * this.rate);
|
|
66
|
+
const h = Math.max(box.getSize(this.v2).y, 0.5);
|
|
67
|
+
const gap = this.v1.copy(box.getCenter(this.v3)).add(this.offset).sub(this.target);
|
|
68
|
+
const dead = this.deadZone * h;
|
|
69
|
+
let moved = false;
|
|
70
|
+
if (gap.length() > dead) {
|
|
71
|
+
gap.setLength((gap.length() - dead) * k);
|
|
72
|
+
this.target.add(gap);
|
|
73
|
+
this.camera.position.add(gap);
|
|
74
|
+
moved = true;
|
|
75
|
+
}
|
|
76
|
+
if (this.zoomed)
|
|
77
|
+
return moved;
|
|
78
|
+
const want = this.fitDistance(box);
|
|
79
|
+
const now = this.distance();
|
|
80
|
+
// a pose that outgrows the view starts the pull back on this frame
|
|
81
|
+
if (want > now || want < now * (1 - this.slack)) {
|
|
82
|
+
this.setDistance(now + (want - now) * k);
|
|
83
|
+
moved = true;
|
|
84
|
+
}
|
|
85
|
+
return moved;
|
|
86
|
+
}
|
|
87
|
+
/** factor above 1 moves closer; the distance is then the person's. */
|
|
88
|
+
zoomBy(factor, min = 0.2, max = 50) {
|
|
89
|
+
this.setDistance(MathUtils.clamp(this.distance() / factor, min, max));
|
|
90
|
+
this.zoomed = true;
|
|
91
|
+
}
|
|
92
|
+
setDistance(d) {
|
|
93
|
+
const dir = this.v1.subVectors(this.camera.position, this.target);
|
|
94
|
+
dir.setLength(d);
|
|
95
|
+
this.camera.position.copy(this.target).add(dir);
|
|
96
|
+
}
|
|
97
|
+
}
|
package/dist/host.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export interface HostSource {
|
|
2
|
+
kind: string;
|
|
3
|
+
release?(): void;
|
|
4
|
+
}
|
|
5
|
+
export interface Adapter<S extends HostSource, T, R = unknown> {
|
|
6
|
+
/** Runtimes are shared by adapters with the same key. Default: the kind. */
|
|
7
|
+
key?: string;
|
|
8
|
+
create(): Promise<R>;
|
|
9
|
+
load(runtime: R, source: S): Promise<T>;
|
|
10
|
+
destroy?(runtime: R): void;
|
|
11
|
+
}
|
|
12
|
+
export interface AvatarHostOptions<S extends HostSource, T> {
|
|
13
|
+
/** By kind. Each adapter carries its own runtime type; the host only passes it back. */
|
|
14
|
+
adapters: Partial<Record<string, Adapter<S, T, unknown>>>;
|
|
15
|
+
/** Destroy the runtimes of other keys on a switch (one stage). Without it they stay for reuse. */
|
|
16
|
+
exclusive?: boolean;
|
|
17
|
+
/** Frees a result that lost to a later show and is never shown. */
|
|
18
|
+
discard?(result: T): void;
|
|
19
|
+
}
|
|
20
|
+
export declare class AvatarHost<S extends HostSource, T> {
|
|
21
|
+
private readonly opts;
|
|
22
|
+
private readonly slots;
|
|
23
|
+
private ticket;
|
|
24
|
+
private shown;
|
|
25
|
+
constructor(opts: AvatarHostOptions<S, T>);
|
|
26
|
+
/** The result of the latest show that finished and was not superseded. */
|
|
27
|
+
get current(): T | null;
|
|
28
|
+
/** Runtime keys alive now. */
|
|
29
|
+
get runtimes(): string[];
|
|
30
|
+
/**
|
|
31
|
+
* Resolves to the loaded result, or null when a later show superseded this
|
|
32
|
+
* one; a superseded load's result is discarded and its error dropped.
|
|
33
|
+
*/
|
|
34
|
+
show(source: S | Promise<S>): Promise<T | null>;
|
|
35
|
+
/** Supersedes any load in flight and destroys every runtime. */
|
|
36
|
+
destroy(): void;
|
|
37
|
+
private runtimeFor;
|
|
38
|
+
private drop;
|
|
39
|
+
}
|
package/dist/host.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// The ticketed loader behind a stage. Loads are numbered; only the latest
|
|
2
|
+
// one is shown, however they finish. One runtime per adapter key is made on
|
|
3
|
+
// first use and kept (the import for a format happens once); a failed
|
|
4
|
+
// create is forgotten, so the next show tries again. With `exclusive` the
|
|
5
|
+
// runtimes of other keys are destroyed when the key changes. A source's
|
|
6
|
+
// `release` runs once, whether its load wins, loses or throws.
|
|
7
|
+
export class AvatarHost {
|
|
8
|
+
opts;
|
|
9
|
+
slots = new Map();
|
|
10
|
+
ticket = 0;
|
|
11
|
+
shown = null;
|
|
12
|
+
constructor(opts) {
|
|
13
|
+
this.opts = opts;
|
|
14
|
+
}
|
|
15
|
+
/** The result of the latest show that finished and was not superseded. */
|
|
16
|
+
get current() {
|
|
17
|
+
return this.shown;
|
|
18
|
+
}
|
|
19
|
+
/** Runtime keys alive now. */
|
|
20
|
+
get runtimes() {
|
|
21
|
+
return [...this.slots.keys()];
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Resolves to the loaded result, or null when a later show superseded this
|
|
25
|
+
* one; a superseded load's result is discarded and its error dropped.
|
|
26
|
+
*/
|
|
27
|
+
async show(source) {
|
|
28
|
+
const mine = ++this.ticket;
|
|
29
|
+
let src = null;
|
|
30
|
+
try {
|
|
31
|
+
src = await source;
|
|
32
|
+
if (mine !== this.ticket)
|
|
33
|
+
return null;
|
|
34
|
+
const adapter = this.opts.adapters[src.kind];
|
|
35
|
+
if (!adapter)
|
|
36
|
+
throw new Error(`no adapter for ${src.kind} files.`);
|
|
37
|
+
const runtime = await this.runtimeFor(adapter.key ?? src.kind, adapter);
|
|
38
|
+
if (mine !== this.ticket)
|
|
39
|
+
return null;
|
|
40
|
+
const result = await adapter.load(runtime, src);
|
|
41
|
+
if (mine !== this.ticket) {
|
|
42
|
+
this.opts.discard?.(result);
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
this.shown = result;
|
|
46
|
+
return result;
|
|
47
|
+
}
|
|
48
|
+
catch (e) {
|
|
49
|
+
if (mine !== this.ticket)
|
|
50
|
+
return null;
|
|
51
|
+
throw e;
|
|
52
|
+
}
|
|
53
|
+
finally {
|
|
54
|
+
src?.release?.();
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/** Supersedes any load in flight and destroys every runtime. */
|
|
58
|
+
destroy() {
|
|
59
|
+
this.ticket++;
|
|
60
|
+
this.shown = null;
|
|
61
|
+
for (const key of [...this.slots.keys()])
|
|
62
|
+
this.drop(key);
|
|
63
|
+
}
|
|
64
|
+
runtimeFor(key, adapter) {
|
|
65
|
+
if (this.opts.exclusive)
|
|
66
|
+
for (const k of this.slots.keys())
|
|
67
|
+
if (k !== key)
|
|
68
|
+
this.drop(k);
|
|
69
|
+
let slot = this.slots.get(key);
|
|
70
|
+
if (!slot) {
|
|
71
|
+
const runtime = adapter.create();
|
|
72
|
+
slot = { key, runtime, destroy: adapter.destroy?.bind(adapter) };
|
|
73
|
+
const mine = slot;
|
|
74
|
+
runtime.catch(() => {
|
|
75
|
+
if (this.slots.get(key) === mine)
|
|
76
|
+
this.slots.delete(key);
|
|
77
|
+
});
|
|
78
|
+
this.slots.set(key, slot);
|
|
79
|
+
}
|
|
80
|
+
return slot.runtime;
|
|
81
|
+
}
|
|
82
|
+
drop(key) {
|
|
83
|
+
const slot = this.slots.get(key);
|
|
84
|
+
this.slots.delete(key);
|
|
85
|
+
if (slot)
|
|
86
|
+
void slot.runtime.then((r) => slot.destroy?.(r), () => undefined);
|
|
87
|
+
}
|
|
88
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// @hackbuild/avatar-host: host a character on a stage in any container.
|
|
2
|
+
// This entry holds the parts with no three.js: the ticketed host, sources
|
|
3
|
+
// from a URL or a drop, the folder walk, the stage loop and its pause
|
|
4
|
+
// rules, and the failure copy. `/three` holds the stage, the follow camera,
|
|
5
|
+
// the body frame order, the placeholder body and the hb-avatar-host element.
|
|
6
|
+
export * from "./host.js";
|
|
7
|
+
export * from "./source.js";
|
|
8
|
+
export * from "./drop.js";
|
|
9
|
+
export * from "./loop.js";
|
|
10
|
+
export * from "./errors.js";
|
package/dist/load.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { LoadingManager } from "three";
|
|
2
|
+
import { type Avatar, type NormalizeOptions, type RepairOptions, type VrmHook } from "@hackbuild/avatar/three";
|
|
3
|
+
import type { Adapter } from "./host.js";
|
|
4
|
+
import type { BodyKind, BodySource } from "./source.js";
|
|
5
|
+
export interface BodyLoadOptions {
|
|
6
|
+
/** The VRM hook, imported on the first .vrm: `() => import("@hackbuild/avatar/vrm").then((m) => m.vrmHook())`. Without it a VRM loads as plain glTF. */
|
|
7
|
+
vrm?: () => Promise<VrmHook>;
|
|
8
|
+
normalize?: NormalizeOptions;
|
|
9
|
+
repair?: RepairOptions | false;
|
|
10
|
+
}
|
|
11
|
+
/** The rig wasm, once per page; a failed load is tried again next time. */
|
|
12
|
+
export declare function rigReady(): Promise<unknown>;
|
|
13
|
+
/** A manager that sends references through `resolve` and can wait for every item it started. */
|
|
14
|
+
export declare function trackingManager(resolve?: (ref: string) => string | null): {
|
|
15
|
+
manager: LoadingManager;
|
|
16
|
+
settled(): Promise<void>;
|
|
17
|
+
};
|
|
18
|
+
/** Loads one body source. */
|
|
19
|
+
export declare function loadBody(src: BodySource, hook: VrmHook | null, opts?: BodyLoadOptions): Promise<Avatar>;
|
|
20
|
+
/** The adapters a stage hosts: VRM with its hook imported on first use, the other kinds over the rig mapper. */
|
|
21
|
+
export declare function bodyAdapters(opts?: BodyLoadOptions): Record<BodyKind, Adapter<BodySource, Avatar, VrmHook | null>>;
|
package/dist/load.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// The scene level loader: a body source to a loaded Avatar in the
|
|
2
|
+
// normalized humanoid space, through @hackbuild/avatar. Dropped files reach
|
|
3
|
+
// the model's texture references through the source's resolver, and the
|
|
4
|
+
// load waits for every texture the loader started, so the source's object
|
|
5
|
+
// URLs can be freed when it resolves. The rig wasm loads once, on the first
|
|
6
|
+
// body.
|
|
7
|
+
import { LoadingManager } from "three";
|
|
8
|
+
import { load as loadRig } from "@hackbuild/avatar";
|
|
9
|
+
import { loadAvatar } from "@hackbuild/avatar/three";
|
|
10
|
+
let rig = null;
|
|
11
|
+
/** The rig wasm, once per page; a failed load is tried again next time. */
|
|
12
|
+
export function rigReady() {
|
|
13
|
+
if (!rig) {
|
|
14
|
+
rig = loadRig();
|
|
15
|
+
rig.catch(() => (rig = null));
|
|
16
|
+
}
|
|
17
|
+
return rig;
|
|
18
|
+
}
|
|
19
|
+
/** A manager that sends references through `resolve` and can wait for every item it started. */
|
|
20
|
+
export function trackingManager(resolve) {
|
|
21
|
+
const manager = new LoadingManager();
|
|
22
|
+
let pending = 0;
|
|
23
|
+
let wake = null;
|
|
24
|
+
const start = manager.itemStart.bind(manager);
|
|
25
|
+
const end = manager.itemEnd.bind(manager);
|
|
26
|
+
manager.itemStart = (url) => {
|
|
27
|
+
pending++;
|
|
28
|
+
start(url);
|
|
29
|
+
};
|
|
30
|
+
manager.itemEnd = (url) => {
|
|
31
|
+
pending = Math.max(0, pending - 1);
|
|
32
|
+
end(url);
|
|
33
|
+
if (pending === 0)
|
|
34
|
+
wake?.();
|
|
35
|
+
};
|
|
36
|
+
if (resolve)
|
|
37
|
+
manager.setURLModifier((url) => (/^(blob|data):/.test(url) ? url : (resolve(url) ?? url)));
|
|
38
|
+
return {
|
|
39
|
+
manager,
|
|
40
|
+
settled: () => (pending === 0 ? Promise.resolve() : new Promise((ok) => (wake = ok))),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/** Loads one body source. */
|
|
44
|
+
export async function loadBody(src, hook, opts = {}) {
|
|
45
|
+
await rigReady();
|
|
46
|
+
const { manager, settled } = trackingManager(src.resolve?.bind(src));
|
|
47
|
+
const avatar = await loadAvatar(src.data, {
|
|
48
|
+
name: src.name,
|
|
49
|
+
manager,
|
|
50
|
+
...(hook ? { vrm: hook } : {}),
|
|
51
|
+
...(opts.normalize ? { normalize: opts.normalize } : {}),
|
|
52
|
+
...(opts.repair !== undefined ? { repair: opts.repair } : {}),
|
|
53
|
+
});
|
|
54
|
+
await settled();
|
|
55
|
+
if (src.misses?.length)
|
|
56
|
+
avatar.warnings.push(`${src.misses.length} referenced files were not in the drop: ${src.misses.slice(0, 3).join(", ")}${src.misses.length > 3 ? " and more" : ""}.`);
|
|
57
|
+
return avatar;
|
|
58
|
+
}
|
|
59
|
+
/** The adapters a stage hosts: VRM with its hook imported on first use, the other kinds over the rig mapper. */
|
|
60
|
+
export function bodyAdapters(opts = {}) {
|
|
61
|
+
const plain = {
|
|
62
|
+
key: "rig",
|
|
63
|
+
create: async () => {
|
|
64
|
+
await rigReady();
|
|
65
|
+
return null;
|
|
66
|
+
},
|
|
67
|
+
load: (_r, src) => loadBody(src, null, opts),
|
|
68
|
+
};
|
|
69
|
+
return {
|
|
70
|
+
vrm: {
|
|
71
|
+
key: "vrm",
|
|
72
|
+
create: async () => (opts.vrm ? opts.vrm() : null),
|
|
73
|
+
load: (hook, src) => loadBody(src, hook, opts),
|
|
74
|
+
},
|
|
75
|
+
glb: plain,
|
|
76
|
+
gltf: plain,
|
|
77
|
+
fbx: plain,
|
|
78
|
+
};
|
|
79
|
+
}
|
package/dist/loop.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
export type PauseReason = "hidden" | "offscreen" | "inactive" | "covered" | "lost" | "paused";
|
|
2
|
+
/** The one call of WebGLRenderer the loop needs. */
|
|
3
|
+
export interface LoopDriver {
|
|
4
|
+
setAnimationLoop(cb: ((time: number) => void) | null): void;
|
|
5
|
+
}
|
|
6
|
+
export interface StageLoopOptions {
|
|
7
|
+
driver: LoopDriver;
|
|
8
|
+
/** One frame: `dt` in seconds, `time` the loop clock in seconds (the sum of deltas). */
|
|
9
|
+
frame(dt: number, time: number): void;
|
|
10
|
+
/** True while something moves; under reduced motion the loop stops once this is false. */
|
|
11
|
+
busy?(): boolean;
|
|
12
|
+
/** Seconds of the first delta after a stop. Default 1 / 60 (proposed, one 60 Hz frame). */
|
|
13
|
+
fixedStep?: number;
|
|
14
|
+
/** Longest delta in seconds, for a stalled main thread. Default 0.1 (proposed). */
|
|
15
|
+
maxStep?: number;
|
|
16
|
+
/** Called when the loop starts or stops, with the reasons that hold. */
|
|
17
|
+
onState?(running: boolean, reasons: PauseReason[]): void;
|
|
18
|
+
}
|
|
19
|
+
export declare class StageLoop {
|
|
20
|
+
private readonly opts;
|
|
21
|
+
/** Frames drawn since the loop was made. */
|
|
22
|
+
frames: number;
|
|
23
|
+
/** The loop clock in seconds: the sum of every delta handed to `frame`. */
|
|
24
|
+
time: number;
|
|
25
|
+
private readonly reasons;
|
|
26
|
+
private reduced;
|
|
27
|
+
private running;
|
|
28
|
+
private last;
|
|
29
|
+
private pending;
|
|
30
|
+
private disposed;
|
|
31
|
+
private readonly fixed;
|
|
32
|
+
private readonly max;
|
|
33
|
+
constructor(opts: StageLoopOptions);
|
|
34
|
+
get paused(): boolean;
|
|
35
|
+
get isRunning(): boolean;
|
|
36
|
+
/** The reasons that hold now. */
|
|
37
|
+
get why(): PauseReason[];
|
|
38
|
+
get reducedMotion(): boolean;
|
|
39
|
+
set reducedMotion(on: boolean);
|
|
40
|
+
/** Sets or clears one pause reason. */
|
|
41
|
+
hold(reason: PauseReason, on: boolean): void;
|
|
42
|
+
pause(reason?: PauseReason): void;
|
|
43
|
+
resume(reason?: PauseReason): void;
|
|
44
|
+
/** Something changed: draw at least one more frame. */
|
|
45
|
+
invalidate(): void;
|
|
46
|
+
/** Recomputes whether the loop should run; call after `busy` turns true. */
|
|
47
|
+
sync(): void;
|
|
48
|
+
private readonly tick;
|
|
49
|
+
dispose(): void;
|
|
50
|
+
}
|
package/dist/loop.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// The stage loop rules, without three.js: one animation loop per stage,
|
|
2
|
+
// stopped while any pause reason holds (the page hidden, the host off
|
|
3
|
+
// screen or at zero size, the route inactive, a sheet covering it, a lost
|
|
4
|
+
// context, or a pause by hand). It restarts on the reverse event and draws
|
|
5
|
+
// one frame at once, so a stage is never blank on return. The first delta
|
|
6
|
+
// after a stop is one fixed step, so time does not jump on resume. Under
|
|
7
|
+
// reduced motion the loop runs only while something moves (a clip the
|
|
8
|
+
// person started, the camera under a finger) and otherwise draws on change.
|
|
9
|
+
export class StageLoop {
|
|
10
|
+
opts;
|
|
11
|
+
/** Frames drawn since the loop was made. */
|
|
12
|
+
frames = 0;
|
|
13
|
+
/** The loop clock in seconds: the sum of every delta handed to `frame`. */
|
|
14
|
+
time = 0;
|
|
15
|
+
reasons = new Set();
|
|
16
|
+
reduced = false;
|
|
17
|
+
running = false;
|
|
18
|
+
last = null;
|
|
19
|
+
pending = true;
|
|
20
|
+
disposed = false;
|
|
21
|
+
fixed;
|
|
22
|
+
max;
|
|
23
|
+
constructor(opts) {
|
|
24
|
+
this.opts = opts;
|
|
25
|
+
this.fixed = opts.fixedStep ?? 1 / 60;
|
|
26
|
+
this.max = opts.maxStep ?? 0.1;
|
|
27
|
+
}
|
|
28
|
+
get paused() {
|
|
29
|
+
return this.reasons.size > 0;
|
|
30
|
+
}
|
|
31
|
+
get isRunning() {
|
|
32
|
+
return this.running;
|
|
33
|
+
}
|
|
34
|
+
/** The reasons that hold now. */
|
|
35
|
+
get why() {
|
|
36
|
+
return [...this.reasons];
|
|
37
|
+
}
|
|
38
|
+
get reducedMotion() {
|
|
39
|
+
return this.reduced;
|
|
40
|
+
}
|
|
41
|
+
set reducedMotion(on) {
|
|
42
|
+
this.reduced = on;
|
|
43
|
+
this.pending = true;
|
|
44
|
+
this.sync();
|
|
45
|
+
}
|
|
46
|
+
/** Sets or clears one pause reason. */
|
|
47
|
+
hold(reason, on) {
|
|
48
|
+
if (on === this.reasons.has(reason))
|
|
49
|
+
return;
|
|
50
|
+
if (on)
|
|
51
|
+
this.reasons.add(reason);
|
|
52
|
+
else {
|
|
53
|
+
this.reasons.delete(reason);
|
|
54
|
+
// a return draws one frame at once, also under reduced motion
|
|
55
|
+
this.pending = true;
|
|
56
|
+
}
|
|
57
|
+
this.sync();
|
|
58
|
+
}
|
|
59
|
+
pause(reason = "paused") {
|
|
60
|
+
this.hold(reason, true);
|
|
61
|
+
}
|
|
62
|
+
resume(reason = "paused") {
|
|
63
|
+
this.hold(reason, false);
|
|
64
|
+
}
|
|
65
|
+
/** Something changed: draw at least one more frame. */
|
|
66
|
+
invalidate() {
|
|
67
|
+
this.pending = true;
|
|
68
|
+
this.sync();
|
|
69
|
+
}
|
|
70
|
+
/** Recomputes whether the loop should run; call after `busy` turns true. */
|
|
71
|
+
sync() {
|
|
72
|
+
if (this.disposed)
|
|
73
|
+
return;
|
|
74
|
+
const want = this.reasons.size === 0 && (!this.reduced || this.pending || (this.opts.busy?.() ?? false));
|
|
75
|
+
if (want === this.running)
|
|
76
|
+
return;
|
|
77
|
+
this.running = want;
|
|
78
|
+
if (want)
|
|
79
|
+
this.opts.driver.setAnimationLoop(this.tick);
|
|
80
|
+
else {
|
|
81
|
+
this.opts.driver.setAnimationLoop(null);
|
|
82
|
+
this.last = null;
|
|
83
|
+
}
|
|
84
|
+
this.opts.onState?.(want, this.why);
|
|
85
|
+
}
|
|
86
|
+
tick = (now) => {
|
|
87
|
+
const dt = this.last === null ? this.fixed : Math.min(this.max, Math.max(0, (now - this.last) / 1000));
|
|
88
|
+
this.last = now;
|
|
89
|
+
this.pending = false;
|
|
90
|
+
this.time += dt;
|
|
91
|
+
this.frames++;
|
|
92
|
+
this.opts.frame(dt, this.time);
|
|
93
|
+
if (this.reduced)
|
|
94
|
+
this.sync();
|
|
95
|
+
};
|
|
96
|
+
dispose() {
|
|
97
|
+
if (this.running)
|
|
98
|
+
this.opts.driver.setAnimationLoop(null);
|
|
99
|
+
this.running = false;
|
|
100
|
+
this.disposed = true;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type Avatar } from "@hackbuild/avatar/three";
|
|
2
|
+
export interface PlaceholderOptions {
|
|
3
|
+
/** Body colour. Default a warm grey. */
|
|
4
|
+
color?: number;
|
|
5
|
+
/** Joint colour. */
|
|
6
|
+
jointColor?: number;
|
|
7
|
+
/** Overall height scale; 1 puts the head top near 1.74 m (computed from the joint table). */
|
|
8
|
+
scale?: number;
|
|
9
|
+
}
|
|
10
|
+
/** A placeholder body as an Avatar: format unknown, dialect placeholder, no clips, no morphs. */
|
|
11
|
+
export declare function placeholderBody(opts?: PlaceholderOptions): Avatar;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// The placeholder body: a mannequin of capsules on a humanoid skeleton,
|
|
2
|
+
// built in code, so a stage is whole before any model file arrives. It
|
|
3
|
+
// stands in the normalized humanoid space (VRM 1.0 bone names, T pose, +Z
|
|
4
|
+
// forward, meters, feet at y = 0) with identity rest rotations, so gaze,
|
|
5
|
+
// clips and the follow camera treat it as any loaded body. No asset file.
|
|
6
|
+
import { Bone, CapsuleGeometry, Group, Mesh, MeshStandardMaterial, SphereGeometry, Vector3 } from "three";
|
|
7
|
+
import { ExpressionRig, HeadHider, Wardrobe, disposeObject } from "@hackbuild/avatar/three";
|
|
8
|
+
// joint offsets from the parent, meters
|
|
9
|
+
const JOINTS = [
|
|
10
|
+
["hips", null, 0, 0.95, 0],
|
|
11
|
+
["spine", "hips", 0, 0.1, 0],
|
|
12
|
+
["chest", "spine", 0, 0.14, 0],
|
|
13
|
+
["upperChest", "chest", 0, 0.12, 0],
|
|
14
|
+
["neck", "upperChest", 0, 0.13, 0],
|
|
15
|
+
["head", "neck", 0, 0.09, 0.01],
|
|
16
|
+
["leftEye", "head", 0.032, 0.07, 0.08],
|
|
17
|
+
["rightEye", "head", -0.032, 0.07, 0.08],
|
|
18
|
+
];
|
|
19
|
+
for (const [side, s] of [["left", 1], ["right", -1]]) {
|
|
20
|
+
JOINTS.push([`${side}Shoulder`, "upperChest", s * 0.04, 0.09, 0], [`${side}UpperArm`, `${side}Shoulder`, s * 0.13, 0, 0], [`${side}LowerArm`, `${side}UpperArm`, s * 0.27, 0, 0], [`${side}Hand`, `${side}LowerArm`, s * 0.25, 0, 0], [`${side}UpperLeg`, "hips", s * 0.09, -0.05, 0], [`${side}LowerLeg`, `${side}UpperLeg`, 0, -0.42, 0], [`${side}Foot`, `${side}LowerLeg`, 0, -0.42, 0], [`${side}Toes`, `${side}Foot`, 0, -0.06, 0.13]);
|
|
21
|
+
}
|
|
22
|
+
// segment radius per parent bone, meters
|
|
23
|
+
const RADIUS = { hips: 0.11, spine: 0.11, chest: 0.12, upperChest: 0.1, neck: 0.045 };
|
|
24
|
+
const radiusOf = (b) => RADIUS[b] ?? (/Upper(Leg|Arm)/.test(b) ? 0.055 : /Lower/.test(b) ? 0.045 : 0.035);
|
|
25
|
+
/** A placeholder body as an Avatar: format unknown, dialect placeholder, no clips, no morphs. */
|
|
26
|
+
export function placeholderBody(opts = {}) {
|
|
27
|
+
const scale = opts.scale ?? 1;
|
|
28
|
+
const skin = new MeshStandardMaterial({ name: "placeholder", color: opts.color ?? 0xc9c1b3, roughness: 0.75 });
|
|
29
|
+
const joint = new MeshStandardMaterial({ name: "placeholder joint", color: opts.jointColor ?? 0x8f877a, roughness: 0.6 });
|
|
30
|
+
const root = new Group();
|
|
31
|
+
root.name = "placeholder";
|
|
32
|
+
const bones = {};
|
|
33
|
+
for (const [name, parent, x, y, z] of JOINTS) {
|
|
34
|
+
const b = new Bone();
|
|
35
|
+
b.name = name;
|
|
36
|
+
b.position.set(x * scale, y * scale, z * scale);
|
|
37
|
+
(parent ? bones[parent] : root).add(b);
|
|
38
|
+
bones[name] = b;
|
|
39
|
+
}
|
|
40
|
+
const up = new Vector3(0, 1, 0);
|
|
41
|
+
for (const [name, parent] of JOINTS) {
|
|
42
|
+
if (!parent || name.endsWith("Eye") || (parent === "upperChest" && name !== "neck"))
|
|
43
|
+
continue;
|
|
44
|
+
const child = bones[name];
|
|
45
|
+
const len = child.position.length();
|
|
46
|
+
const r = radiusOf(parent) * scale;
|
|
47
|
+
const seg = new Mesh(new CapsuleGeometry(r, Math.max(1e-3, len - r), 4, 12), skin);
|
|
48
|
+
seg.name = `${parent} segment`;
|
|
49
|
+
seg.position.copy(child.position).multiplyScalar(0.5);
|
|
50
|
+
seg.quaternion.setFromUnitVectors(up, child.position.clone().normalize());
|
|
51
|
+
bones[parent].add(seg);
|
|
52
|
+
}
|
|
53
|
+
const head = new Mesh(new SphereGeometry(0.105 * scale, 24, 16), skin);
|
|
54
|
+
head.name = "head";
|
|
55
|
+
head.position.set(0, 0.09 * scale, 0.01 * scale);
|
|
56
|
+
head.scale.set(0.9, 1.1, 0.95);
|
|
57
|
+
bones.head.add(head);
|
|
58
|
+
for (const side of ["left", "right"]) {
|
|
59
|
+
const eye = new Mesh(new SphereGeometry(0.016 * scale, 12, 8), joint);
|
|
60
|
+
eye.name = `${side} eye`;
|
|
61
|
+
eye.position.set(0, 0, 0.022 * scale);
|
|
62
|
+
bones[`${side}Eye`].add(eye);
|
|
63
|
+
const hand = new Mesh(new SphereGeometry(0.045 * scale, 12, 8), joint);
|
|
64
|
+
hand.name = `${side} hand`;
|
|
65
|
+
hand.position.set((side === "left" ? 1 : -1) * 0.05 * scale, 0, 0);
|
|
66
|
+
bones[`${side}Hand`].add(hand);
|
|
67
|
+
}
|
|
68
|
+
root.updateMatrixWorld(true);
|
|
69
|
+
const expressions = new ExpressionRig(root);
|
|
70
|
+
return {
|
|
71
|
+
format: "unknown",
|
|
72
|
+
dialect: "placeholder",
|
|
73
|
+
root,
|
|
74
|
+
bones,
|
|
75
|
+
humanoid: null,
|
|
76
|
+
wardrobe: new Wardrobe(),
|
|
77
|
+
expressions,
|
|
78
|
+
head: new HeadHider(bones.head),
|
|
79
|
+
animations: [],
|
|
80
|
+
repair: null,
|
|
81
|
+
vrm: null,
|
|
82
|
+
warnings: [],
|
|
83
|
+
update() {
|
|
84
|
+
expressions.apply();
|
|
85
|
+
},
|
|
86
|
+
dispose() {
|
|
87
|
+
disposeObject(root);
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
}
|
package/dist/single.d.ts
ADDED
package/dist/single.js
ADDED
package/dist/source.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export type BodyKind = "vrm" | "glb" | "gltf" | "fbx";
|
|
2
|
+
export interface BodySource {
|
|
3
|
+
kind: BodyKind;
|
|
4
|
+
/** The model file: a URL, bytes or a Blob (a dropped File). */
|
|
5
|
+
data: string | URL | ArrayBuffer | ArrayBufferView | Blob;
|
|
6
|
+
/** File name; it names the body and helps the format sniff. */
|
|
7
|
+
name: string;
|
|
8
|
+
/** A URL for a file the model references, or null when the drop has none. */
|
|
9
|
+
resolve?(ref: string): string | null;
|
|
10
|
+
/** References `resolve` could not find, for the failure copy. */
|
|
11
|
+
misses?: string[];
|
|
12
|
+
/** Frees the object URLs the source made; the host calls it once the load settles. */
|
|
13
|
+
release?(): void;
|
|
14
|
+
}
|
|
15
|
+
/** A dropped file: a Blob with a name and, from a folder, a relative path. */
|
|
16
|
+
export interface DroppedFile extends Blob {
|
|
17
|
+
readonly name: string;
|
|
18
|
+
readonly webkitRelativePath?: string;
|
|
19
|
+
}
|
|
20
|
+
/** The body kind a file name says, or null. */
|
|
21
|
+
export declare function kindOf(name: string): BodyKind | null;
|
|
22
|
+
/** A model by URL; the kind comes from the name, glb when it says nothing. */
|
|
23
|
+
export declare function sourceFromUrl(url: string, name?: string): BodySource;
|
|
24
|
+
/** Lower case, forward slashes, no leading ./ and no URL escapes. */
|
|
25
|
+
export declare function normalizeRef(ref: string): string;
|
|
26
|
+
export interface UrlFactory {
|
|
27
|
+
create(b: Blob): string;
|
|
28
|
+
revoke(url: string): void;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* A source from dropped or picked files: the first .vrm, .glb, .gltf or .fbx,
|
|
32
|
+
* with every other file reachable through `resolve`. Throws with the copy
|
|
33
|
+
* for the person when the drop holds no model the stage reads.
|
|
34
|
+
*/
|
|
35
|
+
export declare function sourceFromFiles(files: readonly DroppedFile[], urls?: UrlFactory): BodySource;
|