@hackbuild/avatar-host 0.1.5 → 0.1.7
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/README.md +173 -206
- package/dist/avatar-host.iife.js +29 -1
- package/dist/body.d.ts +2 -0
- package/dist/body.js +1 -0
- package/dist/drop.d.ts +1 -0
- package/dist/element.d.ts +1 -0
- package/dist/environment.d.ts +4 -0
- package/dist/environment.js +1 -0
- package/dist/follow.d.ts +2 -0
- package/dist/follow.js +1 -0
- package/dist/host.d.ts +4 -0
- package/dist/host.js +1 -0
- package/dist/load.d.ts +1 -0
- package/dist/loop.d.ts +3 -0
- package/dist/loop.js +1 -0
- package/dist/placeholder.d.ts +1 -0
- package/dist/portrait.d.ts +3 -0
- package/dist/source.d.ts +3 -0
- package/dist/stage.d.ts +5 -0
- package/dist/stage.js +2 -0
- package/dist/thumb-queue.d.ts +2 -0
- package/dist/thumb-queue.js +1 -0
- package/dist/thumbs.d.ts +2 -0
- package/llms.md +742 -0
- package/package.json +8 -7
package/llms.md
ADDED
|
@@ -0,0 +1,742 @@
|
|
|
1
|
+
# @hackbuild/avatar-host
|
|
2
|
+
|
|
3
|
+
> Host a character on a three.js stage in any container, with a follow camera, the presence frame order and a loop that pauses when nobody can see it.
|
|
4
|
+
|
|
5
|
+
Generated by tools/llms.mjs from the TypeDoc JSON, the README and the examples of hackbuild-motion. Human docs: https://hackbuild.github.io/hackbuild-motion/docs/avatar-host/
|
|
6
|
+
|
|
7
|
+
## Purpose
|
|
8
|
+
|
|
9
|
+
- Gives you a stage in any element: a renderer sized to its container, orbit controls, plain lights or a lit scene, and a `hb-avatar-host` custom element for pages with no framework.
|
|
10
|
+
- Loads bodies through `@hackbuild/avatar` with ticketed loads, so a slow load that a newer one overtook is freed and never shown.
|
|
11
|
+
- Runs one frame order for every body: clip player, head level, pose, gaze, face with lid and mouth, then the avatar update.
|
|
12
|
+
- Frames the posed body with a camera that eases, holds still inside a dead zone and leaves a hand zoom alone.
|
|
13
|
+
- Stops the loop while the page is hidden, the stage is off screen, inactive or covered, or the 3D context is lost, and draws previews of clips and avatars in the background.
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
npm install @hackbuild/avatar-host @hackbuild/avatar three @pixiv/three-vrm
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
It depends on `@hackbuild/avatar`, `@hackbuild/presence`, `@hackbuild/lipsync`, `@hackbuild/environment`, `@hackbuild/organic` and `@hackbuild/rig`, which install with it. `three` is an optional peer needed by `/three` and `/thumbs`. `@pixiv/three-vrm` is needed only to show VRM files, and is imported on the first `.vrm` through the hook you pass.
|
|
22
|
+
|
|
23
|
+
| subpath | needs | pin |
|
|
24
|
+
|---|---|---|
|
|
25
|
+
| `@hackbuild/avatar-host` | nothing; the ticketed host, drop sources, the stage loop and failure copy | none |
|
|
26
|
+
| `@hackbuild/avatar-host/three` | `three` (optional peer), WebGL | 0.186.1 or later |
|
|
27
|
+
| `@hackbuild/avatar-host/thumbs` | `three`, WebGL | 0.186.1 or later |
|
|
28
|
+
| `@hackbuild/avatar-host/iife` | a page with a `THREE` global; leaves out three-vrm | none |
|
|
29
|
+
|
|
30
|
+
## Exports
|
|
31
|
+
|
|
32
|
+
### @hackbuild/avatar-host
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
interface Adapter<S extends HostSource, T, R = unknown> {
|
|
36
|
+
key?: string;
|
|
37
|
+
create(): Promise<R>;
|
|
38
|
+
destroy?(runtime: R): void;
|
|
39
|
+
load(runtime: R, source: S): Promise<T>;
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Loads one kind of source: makes a runtime once, loads sources into it, and may destroy it.
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
class AvatarHost<S extends HostSource, T> {
|
|
47
|
+
constructor(opts: AvatarHostOptions<S, T>);
|
|
48
|
+
readonly current: T | null;
|
|
49
|
+
readonly runtimes: string[];
|
|
50
|
+
destroy(): void;
|
|
51
|
+
show(source: S | Promise<S>): Promise<T | null>;
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Shows one source at a time through adapters: each show takes a ticket, a load that a later show overtook is discarded, and runtimes are shared or destroyed by key.
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
interface AvatarHostOptions<S extends HostSource, T> {
|
|
59
|
+
adapters: Partial<Record<string, Adapter<S, T, unknown>>>;
|
|
60
|
+
exclusive?: boolean;
|
|
61
|
+
discard?(result: T): void;
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
The adapters by kind, the exclusive switch and the discard call of an `AvatarHost`.
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
function bodyError(e: unknown): string
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Copy for the person from a load or start failure.
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
type BodyKind = "vrm" | "glb" | "gltf" | "fbx"
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
The model formats a body source can be.
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
interface BodySource {
|
|
81
|
+
data: string | Blob | URL | ArrayBuffer | ArrayBufferView<ArrayBufferLike>;
|
|
82
|
+
kind: BodyKind;
|
|
83
|
+
misses?: string[];
|
|
84
|
+
name: string;
|
|
85
|
+
release?(): void;
|
|
86
|
+
resolve?(ref: string): string | null;
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
A model to show: its kind, its file as a URL, bytes or Blob, its name, and how its referenced files resolve.
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
interface DropLike {
|
|
94
|
+
files?: ArrayLike<File>;
|
|
95
|
+
items?: ArrayLike<{ webkitGetAsEntry?(): EntryLike | null }>;
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
The parts of a DataTransfer that `filesFromDrop` reads: entries and files.
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
interface DroppedFile extends Blob {
|
|
103
|
+
readonly name: string;
|
|
104
|
+
readonly webkitRelativePath?: string;
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
A dropped file: a Blob with a name and, from a folder, a relative path.
|
|
109
|
+
|
|
110
|
+
```ts
|
|
111
|
+
function droppedFiles(dt: DropLike): Promise<File[]>
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
The files of a drop; folders are walked when the browser gives entries.
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
interface EntryLike {
|
|
118
|
+
fullPath: string;
|
|
119
|
+
isDirectory: boolean;
|
|
120
|
+
isFile: boolean;
|
|
121
|
+
createReader?(): { readEntries(ok: (batch: EntryLike[]) => void, fail?: (e: unknown) => void): void };
|
|
122
|
+
file?(ok: (f: File) => void, fail?: (e: unknown) => void): void;
|
|
123
|
+
}
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
The parts of FileSystemEntry the walk reads; tests pass plain objects.
|
|
127
|
+
|
|
128
|
+
```ts
|
|
129
|
+
function filesFromEntries(entries: readonly EntryLike[]): Promise<File[]>
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Every file under the entries, each with `webkitRelativePath` set from its place in the drop.
|
|
133
|
+
|
|
134
|
+
```ts
|
|
135
|
+
interface HostSource {
|
|
136
|
+
kind: string;
|
|
137
|
+
release?(): void;
|
|
138
|
+
}
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
The least a host source carries: its kind and an optional release call.
|
|
142
|
+
|
|
143
|
+
```ts
|
|
144
|
+
function kindOf(name: string): BodyKind | null
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
The body kind a file name says, or null.
|
|
148
|
+
|
|
149
|
+
```ts
|
|
150
|
+
interface LoopDriver {
|
|
151
|
+
setAnimationLoop(cb: (time: number) => void | null): void;
|
|
152
|
+
}
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
The one call of WebGLRenderer the loop needs.
|
|
156
|
+
|
|
157
|
+
```ts
|
|
158
|
+
function normalizeRef(ref: string): string
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
Lower case, forward slashes, no leading ./ and no URL escapes.
|
|
162
|
+
|
|
163
|
+
```ts
|
|
164
|
+
type PauseReason = "hidden" | "offscreen" | "inactive" | "covered" | "lost" | "paused"
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
Why a stage loop is stopped: hidden, off screen, inactive, covered, context lost or paused.
|
|
168
|
+
|
|
169
|
+
```ts
|
|
170
|
+
function sourceFromFiles(files: readonly DroppedFile[], urls?: UrlFactory): BodySource
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
A source from dropped or picked files: the first .vrm, .glb, .gltf or .fbx, with every other file reachable through `resolve`.
|
|
174
|
+
|
|
175
|
+
```ts
|
|
176
|
+
function sourceFromUrl(url: string, name?: string): BodySource
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
A model by URL; the kind comes from the name, glb when it says nothing.
|
|
180
|
+
|
|
181
|
+
```ts
|
|
182
|
+
class StageLoop {
|
|
183
|
+
constructor(opts: StageLoopOptions);
|
|
184
|
+
frames: number;
|
|
185
|
+
time: number;
|
|
186
|
+
readonly isRunning: boolean;
|
|
187
|
+
readonly paused: boolean;
|
|
188
|
+
reducedMotion: boolean;
|
|
189
|
+
readonly why: PauseReason[];
|
|
190
|
+
dispose(): void;
|
|
191
|
+
hold(reason: PauseReason, on: boolean): void;
|
|
192
|
+
invalidate(): void;
|
|
193
|
+
pause(reason?: PauseReason): void;
|
|
194
|
+
resume(reason?: PauseReason): void;
|
|
195
|
+
sync(): void;
|
|
196
|
+
}
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
The render loop of a stage: runs frames on the renderer's animation loop and stops while the page is hidden, off screen, covered, lost or paused.
|
|
200
|
+
|
|
201
|
+
```ts
|
|
202
|
+
interface StageLoopOptions {
|
|
203
|
+
driver: LoopDriver;
|
|
204
|
+
fixedStep?: number;
|
|
205
|
+
maxStep?: number;
|
|
206
|
+
busy?(): boolean;
|
|
207
|
+
frame(dt: number, time: number): void;
|
|
208
|
+
onState?(running: boolean, reasons: PauseReason[]): void;
|
|
209
|
+
}
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
The renderer driver, the frame call, the busy test and timing options of a `StageLoop`.
|
|
213
|
+
|
|
214
|
+
```ts
|
|
215
|
+
interface UrlFactory {
|
|
216
|
+
create(b: Blob): string;
|
|
217
|
+
revoke(url: string): void;
|
|
218
|
+
}
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
Makes and revokes object URLs; swap it in tests.
|
|
222
|
+
|
|
223
|
+
### @hackbuild/avatar-host/three
|
|
224
|
+
|
|
225
|
+
```ts
|
|
226
|
+
interface AvatarHostElement extends HTMLElement {
|
|
227
|
+
readonly stage: AvatarStage | null;
|
|
228
|
+
showFiles(files: readonly DroppedFile[]): Promise<void>;
|
|
229
|
+
}
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
The `hb-avatar-host` custom element: a stage in the element, and a call to show dropped files.
|
|
233
|
+
|
|
234
|
+
```ts
|
|
235
|
+
class AvatarStage {
|
|
236
|
+
constructor(container: HTMLElement, opts?: StageOptions);
|
|
237
|
+
body: Body | null;
|
|
238
|
+
readonly camera: PerspectiveCamera;
|
|
239
|
+
readonly canvas: HTMLCanvasElement;
|
|
240
|
+
readonly container: HTMLElement;
|
|
241
|
+
readonly controls: OrbitControls;
|
|
242
|
+
readonly environment: EnvironmentSlot;
|
|
243
|
+
readonly follow: FollowCamera;
|
|
244
|
+
readonly lights: Light[];
|
|
245
|
+
readonly loop: StageLoop;
|
|
246
|
+
readonly pointer: Vector2;
|
|
247
|
+
readonly renderer: WebGLRenderer;
|
|
248
|
+
readonly scene: Scene<Object3DEventMap>;
|
|
249
|
+
dispose(): void;
|
|
250
|
+
lookAtView(p: { x: number; y: number } | null): void;
|
|
251
|
+
on<K extends "failed" | "lost" | "loaded" | "stats" | "state" | "restored">(type: K, fn: (e: Of<K>) => void): () => void;
|
|
252
|
+
pause(): void;
|
|
253
|
+
play(): void;
|
|
254
|
+
resetView(): void;
|
|
255
|
+
resize(): void;
|
|
256
|
+
restore(): void;
|
|
257
|
+
setActive(on: boolean): void;
|
|
258
|
+
setCovered(on: boolean): void;
|
|
259
|
+
setEnvironment(env: LoadedEnvironment | null): void;
|
|
260
|
+
setFollow(on: boolean): void;
|
|
261
|
+
show(source: string | BodySource | readonly DroppedFile[] | Promise<BodySource>): Promise<Body | null>;
|
|
262
|
+
showAvatar(avatar: Avatar, name?: string): Body;
|
|
263
|
+
showEnvironment(req: string | EnvironmentRequest | null): Promise<boolean>;
|
|
264
|
+
showPlaceholder(opts?: PlaceholderOptions): Body;
|
|
265
|
+
snapshot(type?: string): Promise<Blob | null>;
|
|
266
|
+
zoomBy(factor: number): void;
|
|
267
|
+
}
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
A three.js stage in a container: renderer, camera, orbit controls, follow camera, loop, environment slot and the body it shows.
|
|
271
|
+
|
|
272
|
+
```ts
|
|
273
|
+
class Body {
|
|
274
|
+
constructor(avatar: Avatar, opts: BodyOptions);
|
|
275
|
+
readonly avatar: Avatar;
|
|
276
|
+
readonly bones: Object3D<Object3DEventMap>[];
|
|
277
|
+
readonly caps: BodyCaps;
|
|
278
|
+
readonly face: Face;
|
|
279
|
+
readonly gaze: GazeController | null;
|
|
280
|
+
gazeOn: boolean;
|
|
281
|
+
readonly lip: LipSync;
|
|
282
|
+
readonly mouth: MouthTarget;
|
|
283
|
+
readonly names: ("hips" | "spine" | "chest" | "upperChest" | "neck" | "head" | "leftEye" | "rightEye" | "jaw" | "leftUpperLeg" | "leftLowerLeg" | "leftFoot" | "leftToes" | "rightUpperLeg" | "rightLowerLeg" | "rightFoot" | "rightToes" | "leftShoulder" | "leftUpperArm" | "leftLowerArm" | "leftHand" | "rightShoulder" | "rightUpperArm" | "rightLowerArm" | "rightHand" | "leftThumbMetacarpal" | "leftThumbProximal" | "leftThumbDistal" | "leftIndexProximal" | "leftIndexIntermediate" | "leftIndexDistal" | "leftMiddleProximal" | "leftMiddleIntermediate" | "leftMiddleDistal" | "leftRingProximal" | "leftRingIntermediate" | "leftRingDistal" | "leftLittleProximal" | "leftLittleIntermediate" | "leftLittleDistal" | "rightThumbMetacarpal" | "rightThumbProximal" | "rightThumbDistal" | "rightIndexProximal" | "rightIndexIntermediate" | "rightIndexDistal" | "rightMiddleProximal" | "rightMiddleIntermediate" | "rightMiddleDistal" | "rightRingProximal" | "rightRingIntermediate" | "rightRingDistal" | "rightLittleProximal" | "rightLittleIntermediate" | "rightLittleDistal")[];
|
|
284
|
+
readonly player: ClipPlayer;
|
|
285
|
+
readonly name: string;
|
|
286
|
+
readonly wardrobe: Part[];
|
|
287
|
+
clipOf(anim: string | AnimationClip, loop?: boolean): Clip | null;
|
|
288
|
+
dispose(): void;
|
|
289
|
+
expressionNames(): string[];
|
|
290
|
+
eyePosition(out: Vector3): Vector3;
|
|
291
|
+
moving(): boolean;
|
|
292
|
+
play(clip: string | AnimationClip | Clip, opts?: PlayOptions): number;
|
|
293
|
+
setExpression(name: string | null, weight?: number): void;
|
|
294
|
+
setGazeMode(mode: GazeMode): void;
|
|
295
|
+
setPartVisible(id: string, on: boolean): void;
|
|
296
|
+
stop(layer?: Layer, fade?: number): void;
|
|
297
|
+
update(dt: number, look: Vector3 | null, still: boolean, mouthTime: number): void;
|
|
298
|
+
}
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
A loaded character driven in the presence frame order: clip player, gaze, lip sync and face, written to its bones each frame.
|
|
302
|
+
|
|
303
|
+
```ts
|
|
304
|
+
function bodyAdapters(opts?: BodyLoadOptions): Record<BodyKind, Adapter<BodySource, Avatar, VrmHook | null>>
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
The adapters a stage hosts: VRM with its hook imported on first use, the other kinds over the rig mapper.
|
|
308
|
+
|
|
309
|
+
```ts
|
|
310
|
+
interface BodyCaps {
|
|
311
|
+
blink: boolean;
|
|
312
|
+
bones: number;
|
|
313
|
+
clips: string[];
|
|
314
|
+
expressions: string[];
|
|
315
|
+
gaze: boolean;
|
|
316
|
+
mouth: boolean;
|
|
317
|
+
parts: number;
|
|
318
|
+
}
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
What a loaded body can do, for the capability note under the stage.
|
|
322
|
+
|
|
323
|
+
```ts
|
|
324
|
+
interface BodyLoadOptions {
|
|
325
|
+
normalize?: NormalizeOptions;
|
|
326
|
+
repair?: false | RepairOptions;
|
|
327
|
+
vrm?: () => Promise<VrmHook>;
|
|
328
|
+
}
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
The VRM hook, normalize and repair options used when a body loads.
|
|
332
|
+
|
|
333
|
+
```ts
|
|
334
|
+
interface BodyOptions {
|
|
335
|
+
gaze?: boolean | "auto";
|
|
336
|
+
levelHead?: boolean;
|
|
337
|
+
mouth?: MouthTarget;
|
|
338
|
+
restingFace?: false | Readonly<Record<string, number>>;
|
|
339
|
+
rng: Rng;
|
|
340
|
+
stance?: "relaxed" | "rest";
|
|
341
|
+
}
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
Random source, gaze, head levelling, mouth target and clip options of a `Body`.
|
|
345
|
+
|
|
346
|
+
```ts
|
|
347
|
+
function defineAvatarHost(tag?: string, options?: StageOptions): CustomElementConstructor
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
Defines the element once under `tag`, with stage options every instance shares.
|
|
351
|
+
|
|
352
|
+
```ts
|
|
353
|
+
type EnvironmentLoader = (req: EnvironmentRequest, target: EnvironmentTarget) => Promise<LoadedEnvironment>
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
Loads a scene preset onto a target; the default imports `@hackbuild/environment/three` on first use.
|
|
357
|
+
|
|
358
|
+
```ts
|
|
359
|
+
interface EnvironmentRequest {
|
|
360
|
+
preset: string | EnvironmentPreset;
|
|
361
|
+
resolve?: Resolve;
|
|
362
|
+
}
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
A scene preset by id or value, and how its files resolve.
|
|
366
|
+
|
|
367
|
+
```ts
|
|
368
|
+
class EnvironmentSlot {
|
|
369
|
+
constructor(target: EnvironmentTarget, load?: EnvironmentLoader);
|
|
370
|
+
readonly current: string | null;
|
|
371
|
+
cancel(): void;
|
|
372
|
+
set(req: string | EnvironmentRequest | null): Promise<LoadedEnvironment | null>;
|
|
373
|
+
}
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
Shows one lit scene at a time on a stage; a later request overtakes one still loading.
|
|
377
|
+
|
|
378
|
+
```ts
|
|
379
|
+
interface EnvironmentTarget {
|
|
380
|
+
readonly renderer: WebGLRenderer;
|
|
381
|
+
readonly scene: Scene;
|
|
382
|
+
setEnvironment(env: LoadedEnvironment | null): void;
|
|
383
|
+
}
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
What an environment slot writes to: the scene, the renderer and the call that swaps the shown scene.
|
|
387
|
+
|
|
388
|
+
```ts
|
|
389
|
+
class FollowCamera {
|
|
390
|
+
constructor(camera: PerspectiveCamera, target: Vector3, opts?: FollowOptions);
|
|
391
|
+
readonly camera: PerspectiveCamera;
|
|
392
|
+
readonly deadZone: number;
|
|
393
|
+
follow: boolean;
|
|
394
|
+
readonly margin: number;
|
|
395
|
+
readonly offset: Vector3;
|
|
396
|
+
readonly rate: number;
|
|
397
|
+
readonly slack: number;
|
|
398
|
+
readonly target: Vector3;
|
|
399
|
+
zoomed: boolean;
|
|
400
|
+
distance(): number;
|
|
401
|
+
fitDistance(box: Box3): number;
|
|
402
|
+
frame(box: Box3): void;
|
|
403
|
+
released(box: Box3 | null, startDistance: number): void;
|
|
404
|
+
step(box: Box3, dt: number): boolean;
|
|
405
|
+
zoomBy(factor: number, min?: number, max?: number): void;
|
|
406
|
+
}
|
|
407
|
+
```
|
|
408
|
+
|
|
409
|
+
Keeps a body framed: eases toward a new framing with a dead zone, and leaves a distance or pan the person picked alone.
|
|
410
|
+
|
|
411
|
+
```ts
|
|
412
|
+
interface FollowOptions {
|
|
413
|
+
deadZone?: number;
|
|
414
|
+
follow?: boolean;
|
|
415
|
+
margin?: number;
|
|
416
|
+
rate?: number;
|
|
417
|
+
slack?: number;
|
|
418
|
+
}
|
|
419
|
+
```
|
|
420
|
+
|
|
421
|
+
Follow switch, ease rate, dead zone and margin of a `FollowCamera`.
|
|
422
|
+
|
|
423
|
+
```ts
|
|
424
|
+
function loadBody(src: BodySource, hook: VrmHook | null, opts?: BodyLoadOptions): Promise<Avatar>
|
|
425
|
+
```
|
|
426
|
+
|
|
427
|
+
Loads one body source.
|
|
428
|
+
|
|
429
|
+
```ts
|
|
430
|
+
const LOST_COPY: "the 3d view was lost. press to restore."
|
|
431
|
+
```
|
|
432
|
+
|
|
433
|
+
The line a stage shows when the browser drops its 3D context.
|
|
434
|
+
|
|
435
|
+
```ts
|
|
436
|
+
function placeholderBody(opts?: PlaceholderOptions): Avatar
|
|
437
|
+
```
|
|
438
|
+
|
|
439
|
+
A placeholder body as an Avatar: format unknown, dialect placeholder, no clips, no morphs.
|
|
440
|
+
|
|
441
|
+
```ts
|
|
442
|
+
interface PlaceholderOptions {
|
|
443
|
+
color?: number;
|
|
444
|
+
jointColor?: number;
|
|
445
|
+
scale?: number;
|
|
446
|
+
}
|
|
447
|
+
```
|
|
448
|
+
|
|
449
|
+
Body colour, joint colour and height scale of the placeholder body built in code.
|
|
450
|
+
|
|
451
|
+
```ts
|
|
452
|
+
function rigReady(): Promise<unknown>
|
|
453
|
+
```
|
|
454
|
+
|
|
455
|
+
The rig wasm, once per page; a failed load is tried again next time.
|
|
456
|
+
|
|
457
|
+
```ts
|
|
458
|
+
type StageEvent = { body: Body; caps: BodyCaps; name: string; placeholder: boolean; type: "loaded"; warnings: string[] } | { cause: unknown; message: string; type: "failed" } | { stats: StageStats; type: "stats" } | { reasons: PauseReason[]; running: boolean; type: "state" } | { type: "lost" } | { type: "restored" }
|
|
459
|
+
```
|
|
460
|
+
|
|
461
|
+
What a stage reports: a body loaded or failed, stats, loop state, and the 3D context lost or restored.
|
|
462
|
+
|
|
463
|
+
```ts
|
|
464
|
+
interface StageOptions extends BodyLoadOptions {
|
|
465
|
+
background?: number | null;
|
|
466
|
+
body?: Omit<BodyOptions, "rng">;
|
|
467
|
+
canvas?: HTMLCanvasElement;
|
|
468
|
+
environment?: string | EnvironmentRequest;
|
|
469
|
+
follow?: FollowOptions;
|
|
470
|
+
lights?: boolean;
|
|
471
|
+
maxPixelRatio?: number;
|
|
472
|
+
normalize?: NormalizeOptions;
|
|
473
|
+
repair?: false | RepairOptions;
|
|
474
|
+
restoreButton?: boolean;
|
|
475
|
+
rng?: Rng;
|
|
476
|
+
statsEvery?: number;
|
|
477
|
+
vrm?: () => Promise<VrmHook>;
|
|
478
|
+
}
|
|
479
|
+
```
|
|
480
|
+
|
|
481
|
+
Canvas, random source, pixel ratio, background, environment and body load options of an `AvatarStage`.
|
|
482
|
+
|
|
483
|
+
```ts
|
|
484
|
+
function stagePixelRatio(device: number, max?: number, scene?: boolean): number
|
|
485
|
+
```
|
|
486
|
+
|
|
487
|
+
Device pixels per CSS pixel: at most `max`, and at most 1.5 with a lit scene (vendor).
|
|
488
|
+
|
|
489
|
+
```ts
|
|
490
|
+
interface StageStats {
|
|
491
|
+
calls: number;
|
|
492
|
+
fps: number;
|
|
493
|
+
frames: number;
|
|
494
|
+
height: number;
|
|
495
|
+
pixelRatio: number;
|
|
496
|
+
triangles: number;
|
|
497
|
+
width: number;
|
|
498
|
+
}
|
|
499
|
+
```
|
|
500
|
+
|
|
501
|
+
Frames per second, frame count, draw calls, triangles and buffer size of a stage.
|
|
502
|
+
|
|
503
|
+
```ts
|
|
504
|
+
function trackingManager(resolve?: (ref: string) => string | null): { manager: LoadingManager; settled(): Promise<void> }
|
|
505
|
+
```
|
|
506
|
+
|
|
507
|
+
A manager that sends references through `resolve` and can wait for every item it started.
|
|
508
|
+
|
|
509
|
+
### @hackbuild/avatar-host/thumbs
|
|
510
|
+
|
|
511
|
+
```ts
|
|
512
|
+
class AvatarPortrait {
|
|
513
|
+
readonly lost: boolean;
|
|
514
|
+
dispose(): void;
|
|
515
|
+
draw(job: AvatarThumbJob): Promise<Blob | null>;
|
|
516
|
+
static create(opts?: AvatarPortraitOptions): AvatarPortrait;
|
|
517
|
+
}
|
|
518
|
+
```
|
|
519
|
+
|
|
520
|
+
Avatar previews: each character loads, is drawn at rest with its own materials, and is freed.
|
|
521
|
+
|
|
522
|
+
```ts
|
|
523
|
+
interface AvatarPortraitOptions extends PortraitOptions, BodyLoadOptions {
|
|
524
|
+
canvas?: HTMLCanvasElement;
|
|
525
|
+
height?: number;
|
|
526
|
+
normalize?: NormalizeOptions;
|
|
527
|
+
repair?: false | RepairOptions;
|
|
528
|
+
vrm?: () => Promise<VrmHook>;
|
|
529
|
+
width?: number;
|
|
530
|
+
}
|
|
531
|
+
```
|
|
532
|
+
|
|
533
|
+
Image size, canvas and body load options of avatar previews.
|
|
534
|
+
|
|
535
|
+
```ts
|
|
536
|
+
interface AvatarThumbJob {
|
|
537
|
+
id: string;
|
|
538
|
+
source: BodySource;
|
|
539
|
+
}
|
|
540
|
+
```
|
|
541
|
+
|
|
542
|
+
One avatar to preview.
|
|
543
|
+
|
|
544
|
+
```ts
|
|
545
|
+
type AvatarThumbOptions = Omit<ThumbQueueOptions<AvatarThumbJob>, "renderer"> & { portrait?: AvatarPortraitOptions }
|
|
546
|
+
```
|
|
547
|
+
|
|
548
|
+
Queue and portrait options of `avatarThumbs`.
|
|
549
|
+
|
|
550
|
+
```ts
|
|
551
|
+
function avatarThumbs(opts: AvatarThumbOptions): ThumbQueue<AvatarThumbJob>
|
|
552
|
+
```
|
|
553
|
+
|
|
554
|
+
A queue of avatar previews, 240 by 320 px by default.
|
|
555
|
+
|
|
556
|
+
```ts
|
|
557
|
+
class ClipPortrait {
|
|
558
|
+
readonly figure: Avatar;
|
|
559
|
+
readonly lost: boolean;
|
|
560
|
+
dispose(): void;
|
|
561
|
+
draw(job: ClipThumbJob): Promise<Blob | null>;
|
|
562
|
+
static create(opts?: ClipPortraitOptions): Promise<ClipPortrait>;
|
|
563
|
+
}
|
|
564
|
+
```
|
|
565
|
+
|
|
566
|
+
Clip previews on one clay figure.
|
|
567
|
+
|
|
568
|
+
```ts
|
|
569
|
+
interface ClipPortraitOptions extends PortraitOptions {
|
|
570
|
+
canvas?: HTMLCanvasElement;
|
|
571
|
+
color?: string | number;
|
|
572
|
+
figure?: () => Avatar | Promise<Avatar>;
|
|
573
|
+
height?: number;
|
|
574
|
+
width?: number;
|
|
575
|
+
}
|
|
576
|
+
```
|
|
577
|
+
|
|
578
|
+
Image size, clay colour and figure of clip previews.
|
|
579
|
+
|
|
580
|
+
```ts
|
|
581
|
+
interface ClipThumbJob {
|
|
582
|
+
hold?: boolean;
|
|
583
|
+
id: string;
|
|
584
|
+
clip(): AnimationClip | Promise<AnimationClip>;
|
|
585
|
+
}
|
|
586
|
+
```
|
|
587
|
+
|
|
588
|
+
One clip to preview: its id, the clip in normalized bone names, and whether it is a held pose.
|
|
589
|
+
|
|
590
|
+
```ts
|
|
591
|
+
type ClipThumbOptions = Omit<ThumbQueueOptions<ClipThumbJob>, "renderer"> & { portrait?: ClipPortraitOptions }
|
|
592
|
+
```
|
|
593
|
+
|
|
594
|
+
Queue and portrait options of `clipThumbs`.
|
|
595
|
+
|
|
596
|
+
```ts
|
|
597
|
+
function clipThumbs(opts: ClipThumbOptions): ThumbQueue<ClipThumbJob>
|
|
598
|
+
```
|
|
599
|
+
|
|
600
|
+
A queue of clip previews on one clay figure, 192 px square by default.
|
|
601
|
+
|
|
602
|
+
```ts
|
|
603
|
+
function frameBox(cam: OrthographicCamera, box: Box3, aspect?: number): void
|
|
604
|
+
```
|
|
605
|
+
|
|
606
|
+
Points an orthographic camera at `box` from the portrait angle and fits it, `aspect` width over height.
|
|
607
|
+
|
|
608
|
+
```ts
|
|
609
|
+
const MEASURED: readonly HumanBone[]
|
|
610
|
+
```
|
|
611
|
+
|
|
612
|
+
Bones that judge a frame; fingers would outvote them by count.
|
|
613
|
+
|
|
614
|
+
```ts
|
|
615
|
+
interface PortraitOptions {
|
|
616
|
+
canvas?: HTMLCanvasElement;
|
|
617
|
+
height?: number;
|
|
618
|
+
width?: number;
|
|
619
|
+
}
|
|
620
|
+
```
|
|
621
|
+
|
|
622
|
+
Image width, height and canvas of a preview.
|
|
623
|
+
|
|
624
|
+
```ts
|
|
625
|
+
const SAMPLES: 24
|
|
626
|
+
```
|
|
627
|
+
|
|
628
|
+
Frames tried when looking for the most telling one.
|
|
629
|
+
|
|
630
|
+
```ts
|
|
631
|
+
function tellingTime(figure: Avatar, clip: AnimationClip, hold?: boolean, samples?: number): number
|
|
632
|
+
```
|
|
633
|
+
|
|
634
|
+
The time of the clip's most telling frame on `figure`: for a held pose or a clip under 0.05 s its end, else the sampled frame between 10 and 90 percent whose measured bones turn furthest from their rest rotation (the ends are where most clips stand near rest).
|
|
635
|
+
|
|
636
|
+
```ts
|
|
637
|
+
interface ThumbProgress {
|
|
638
|
+
active: boolean;
|
|
639
|
+
done: number;
|
|
640
|
+
total: number;
|
|
641
|
+
}
|
|
642
|
+
```
|
|
643
|
+
|
|
644
|
+
Counts for a progress line: items done of items asked for in this run.
|
|
645
|
+
|
|
646
|
+
```ts
|
|
647
|
+
class ThumbQueue<J extends { id: string }> {
|
|
648
|
+
constructor(opts: ThumbQueueOptions<J>);
|
|
649
|
+
readonly holding: boolean;
|
|
650
|
+
readonly progress: ThumbProgress;
|
|
651
|
+
readonly waiting: string[];
|
|
652
|
+
destroy(): void;
|
|
653
|
+
idle(): Promise<void>;
|
|
654
|
+
want(jobs: readonly J[], urgent?: boolean): void;
|
|
655
|
+
}
|
|
656
|
+
```
|
|
657
|
+
|
|
658
|
+
Draws previews one at a time in the background, saves each image and remembers failures, paced so the page stays responsive.
|
|
659
|
+
|
|
660
|
+
```ts
|
|
661
|
+
interface ThumbQueueOptions<J> {
|
|
662
|
+
lingerMs?: number;
|
|
663
|
+
maxRestMs?: number;
|
|
664
|
+
minRestMs?: number;
|
|
665
|
+
failed?(id: string, reason: string): void;
|
|
666
|
+
hidden?(): boolean;
|
|
667
|
+
now?(): number;
|
|
668
|
+
onProgress?(p: ThumbProgress): void;
|
|
669
|
+
renderer(): Promise<ThumbRenderer<J> | null>;
|
|
670
|
+
save(id: string, image: Blob): Promise<void>;
|
|
671
|
+
}
|
|
672
|
+
```
|
|
673
|
+
|
|
674
|
+
The renderer factory, the save call, the failure and progress calls of a `ThumbQueue`.
|
|
675
|
+
|
|
676
|
+
```ts
|
|
677
|
+
interface ThumbRenderer<J> {
|
|
678
|
+
readonly lost: boolean;
|
|
679
|
+
dispose(): void;
|
|
680
|
+
draw(job: J): Promise<Blob | null>;
|
|
681
|
+
}
|
|
682
|
+
```
|
|
683
|
+
|
|
684
|
+
Draws one item to an image.
|
|
685
|
+
|
|
686
|
+
## Examples
|
|
687
|
+
|
|
688
|
+
### quick start
|
|
689
|
+
|
|
690
|
+
```ts
|
|
691
|
+
import { AvatarStage } from "@hackbuild/avatar-host/three";
|
|
692
|
+
|
|
693
|
+
// the stage sizes itself to its container and pauses while hidden or off screen
|
|
694
|
+
const box = Object.assign(document.createElement("div"), { style: "height: 70vh" });
|
|
695
|
+
const input = Object.assign(document.createElement("input"), { type: "file", multiple: true });
|
|
696
|
+
document.body.append(input, box);
|
|
697
|
+
const stage = new AvatarStage(box, {
|
|
698
|
+
// three-vrm is imported on the first .vrm only
|
|
699
|
+
vrm: () => import("@hackbuild/avatar/vrm").then((m) => m.vrmHook()),
|
|
700
|
+
});
|
|
701
|
+
stage.on("failed", (e) => console.warn(e.message));
|
|
702
|
+
stage.on("stats", (e) => console.log(e.stats.fps.toFixed(0), "fps"));
|
|
703
|
+
|
|
704
|
+
// the placeholder body is built in code; pick a .vrm, .glb or .fbx (and its textures) to swap it
|
|
705
|
+
stage.showPlaceholder().setExpression("happy", 0.5);
|
|
706
|
+
input.onchange = async () => {
|
|
707
|
+
const body = await stage.show([...(input.files ?? [])]);
|
|
708
|
+
body?.setGazeMode("listen");
|
|
709
|
+
};
|
|
710
|
+
```
|
|
711
|
+
|
|
712
|
+
### from the concepts
|
|
713
|
+
|
|
714
|
+
```ts
|
|
715
|
+
stage.setActive(routeIsShown);
|
|
716
|
+
stage.setCovered(sheetOpen);
|
|
717
|
+
```
|
|
718
|
+
|
|
719
|
+
### from the concepts
|
|
720
|
+
|
|
721
|
+
```ts
|
|
722
|
+
const body = stage.showPlaceholder();
|
|
723
|
+
body.play(waveClip, { layer: "upper" });
|
|
724
|
+
body.lip.push(speechEvents); // timed words or visemes from a speech engine
|
|
725
|
+
```
|
|
726
|
+
|
|
727
|
+
## Mistakes to avoid
|
|
728
|
+
|
|
729
|
+
- Sizing the canvas to the window or calling `renderer.setSize` yourself: the stage follows its container.
|
|
730
|
+
- Leaving the loop running under a sheet or on a hidden route: call `setCovered` and `setActive`, or set the `covered` and `inactive` attributes.
|
|
731
|
+
- Writing bones or expression weights outside the body's frame order: use `body.play`, `body.setExpression` and `body.lip` so one owner writes each channel.
|
|
732
|
+
- Showing a VRM without the `vrm` hook in the stage options: it loads as plain glTF with no expressions or spring bones.
|
|
733
|
+
- Forgetting `stage.dispose()` when the stage leaves the page: it frees the body and the WebGL context.
|
|
734
|
+
|
|
735
|
+
## Errors this package throws
|
|
736
|
+
|
|
737
|
+
- `this file is a pmx. open it with the mmd loader of the avatar package.` (`sourceFromFiles` got a PMX or PMD): load it with `loadMmd` from `@hackbuild/avatar/mmd`, then `stage.showAvatar`
|
|
738
|
+
- `a <ext> file is an animation. drop a body: a .vrm, .glb, .gltf or .fbx.` (the drop held only a clip file): drop a model file
|
|
739
|
+
- `this drop has no model. drop a .vrm, .glb, .gltf or .fbx file.` (no model file in the drop): drop a model file
|
|
740
|
+
- `no adapter for <kind> files.` (`AvatarHost.show` got a kind with no adapter): add an adapter for that kind
|
|
741
|
+
- `this clip has <n> bones and the body <m>. build it with clipOf or over body.names.` (`body.play` got a clip built over another bone list): build the clip with `body.clipOf`
|
|
742
|
+
- `the clip moves no bone of this figure.` (a clip preview had no track for the figure's bones): check the clip's bone names; it reaches `failed(id, reason)`
|