@hackbuild/motion-views 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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Moheeb Zara
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,106 @@
1
- # Temporary Holding Version
1
+ # @hackbuild/motion-views
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The motion views of the hack.build libraries: `CharacterStage`,
4
+ `AvatarDrop` and `ClipStrip`. Each binds the motion packages to the
5
+ hackbuild-ui class layer and ships twice, as a Vue component and as a
6
+ custom element, so a Vue app and a plain HTML page use the same view. The
7
+ views carry no styles of their own; every class is hackbuild-ui's, and a
8
+ view fits the stage and rack layout of hackbuild-ui 0.10 as it is.
9
+
10
+ ## The views
11
+
12
+ - `CharacterStage`: a body on the `@hackbuild/avatar-host` stage, with
13
+ idle, gaze, blinks and lips from `@hackbuild/presence` and
14
+ `@hackbuild/lipsync`. It writes a `.hb-lay-stage`: the lit stage, the
15
+ corner plate (name, then what the body is doing), turn around and
16
+ framing (bust, from the knees up, or full body) at the top right, the
17
+ verb row at the foot and an inset slot at the top left. The default
18
+ verbs are say hello (the one pink action), wave, nod and look around.
19
+ Files dropped on it become the body. `say(speech)` speaks real audio:
20
+ an AudioBuffer or an audio element plays through an analyser whose
21
+ level and bands open the mouth, with the word or phoneme timings a
22
+ speech engine reports (`@hackbuild/speak`'s `Timed`, or lipsync events
23
+ such as the bellows adapter makes). Text alone moves the lips on
24
+ estimated word timings with no sound; `gesture("wave" | "nod" | "shake" | "bow")`
25
+ plays a gesture written in code over the body's own bones; `play(clip)`
26
+ plays any presence clip or three.js AnimationClip.
27
+ - `AvatarDrop`: a drop zone and a pick button. A file loads once into the
28
+ normalized humanoid, is checked as a stage body would be, drawn as a
29
+ still and freed. The hackbuild-ui avatar card (`.hb-avcard`) shows the
30
+ still, the name, the size line
31
+ ("168 kb glb, 5.5 k triangles"), the licence the file states and the
32
+ rig check in plain words ("no mouth shapes. the lips stay still while it
33
+ talks."); pressing it sends `use`. The files go out on `picked`, the
34
+ report on `checked`.
35
+ - `ClipStrip`: the hackbuild-ui clip strip (`.hb-clipstrip`): each clip's
36
+ still, name and length, the picked one pressed and the playing one
37
+ marked, and one play button that plays or stops the picked clip. Stills come from the item, or are drawn
38
+ in the background on a clay figure by the avatar-host thumb queue.
39
+
40
+ ## Entries
41
+
42
+ - `.`: the words and the clip items, no three.js and no DOM.
43
+ `rigCheck`, `sizeLine`, `licenceLine`, `secondsWord`; `clipsFromPack`
44
+ `speechEvents`, `estimateSeconds` (what a speech becomes on the mouth
45
+ clock); `clipsFromPack` (a `@hackbuild/bake` pack and report, with a warning on a loop that does
46
+ not close), `clipFromFindings` (a `@hackbuild/retarget` motion),
47
+ `clipsFromBody`, `clipMeta`, `nextClip`.
48
+ - `/vanilla`: `characterStage(host, opts)`, `avatarDrop(host, opts)`,
49
+ `clipStrip(host, opts)`, `gestureClip`, `animationClipOf`, and
50
+ `defineMotionViews(opts)`, which defines `hb-character-stage`,
51
+ `hb-avatar-drop` and `hb-clip-strip`. Importing defines nothing.
52
+ - `/vue`: `CharacterStage` (props `name`, `src`, `framing`, `verbs`,
53
+ `drop`, `options`; slot `inset`), `AvatarDrop` (`go`, `label`,
54
+ `options`) and `ClipStrip` (`items`, `current`, `playing`, `go`).
55
+ Events are the view's events by type.
56
+ - `/iife`: the vanilla entry as one classic script; three.js comes from
57
+ the page as `THREE`.
58
+
59
+ ```ts
60
+ import { defineMotionViews } from "@hackbuild/motion-views/vanilla";
61
+
62
+ defineMotionViews({ stage: { vrm: () => import("@hackbuild/avatar/vrm").then((m) => m.vrmHook()) } });
63
+ const stage = document.querySelector("hb-character-stage")!;
64
+ document.querySelector("hb-avatar-drop")!.addEventListener("picked", (e) => stage.handle?.show(e.detail.files));
65
+ ```
66
+
67
+ three.js, Vue and `@virgilvox/hackbuild-ui` 0.10 are peers; three.js and
68
+ Vue are optional (the main entry needs neither). No model or weight file
69
+ is bundled: a body comes from a URL or a file the person drops.
70
+
71
+ ## What is verified, and what is not
72
+
73
+ - Verified (measured, 2026-11-25, headless system Chrome on macOS 12,
74
+ `node tools/smoke-views.mjs`): the stand-in shows and a frame is drawn;
75
+ the full body shot stands further back than the bust (2.78 m to
76
+ 4.03 m); say hello plays audio written in code with its word timings
77
+ and the audio level reaches the lips (peak 0.215); audio with no
78
+ timings moves the lips by its level; text alone falls back to estimated
79
+ timings; it goes quiet when the audio ends; wave plays on the upper body;
80
+ turn around turns the body half a circle; the stand-in written as a
81
+ .glb in the page loads, maps 24 bones, gets a still and reads "no
82
+ licence in the file"; a text file is refused in plain words; a bvh walk
83
+ retargets onto the stand-in and reads as a walk; six clips bake into one
84
+ pack and read back; a loop cut short is flagged at its seam; a picked
85
+ clip plays; a still is drawn for every clip. Unit tests cover the words
86
+ and the clip items.
87
+ - The three demo pages pass the suite's screen gate at 1440 and 390.
88
+ - Not verified: speech from a real speech engine (the smoke's voice is
89
+ a hum written in code with word timings in the engine's shape), a real
90
+ VRM, GLB or FBX avatar with a face (the smoke uses
91
+ the stand-in built in code, which has no mouth or blink shapes), the Vue
92
+ components in a running Vue app (they typecheck; no page mounts them
93
+ yet), and frame rate on a real GPU, since the smoke runs headless.
94
+ - Known: AvatarDrop loads the file once and the stage loads it again, so a
95
+ large file is read twice.
96
+
97
+ ## Size budget
98
+
99
+ | file | gzip (measured) | ceiling |
100
+ |---|---|---|
101
+ | `dist/index.js` (main entry, imports external) | 3,266 bytes | 3,429 bytes |
102
+ | `dist/motion-views.iife.js` (single file build) | 214,660 bytes | 225,393 bytes |
103
+
104
+ Measured 2026-11-25 by `node tools/lint-packages.mjs` (gzip level 9) from the packed
105
+ tarball. Generated from `tools/size-budget.json`; CI fails when a file moves more than
106
+ 5 percent from its row.
@@ -0,0 +1,52 @@
1
+ import { type DroppedFile } from "@hackbuild/avatar-host";
2
+ import { type BodyCaps, type BodyLoadOptions } from "@hackbuild/avatar-host/three";
3
+ import { type RigCheck, type RigLine } from "./words.js";
4
+ export interface AvatarReport {
5
+ name: string;
6
+ format: string;
7
+ bytes: number;
8
+ files: number;
9
+ triangles: number;
10
+ caps: BodyCaps;
11
+ check: RigCheck;
12
+ licence: RigLine;
13
+ size: string;
14
+ /** The still, a PNG, or null when there is no webgl. */
15
+ thumb: Blob | null;
16
+ }
17
+ export interface AvatarDropOptions extends BodyLoadOptions {
18
+ /** The pick button is the one pink action of the screen. Default false. */
19
+ go?: boolean;
20
+ /** The pick button's words. */
21
+ label?: string;
22
+ /** The line in the empty zone. */
23
+ hint?: string;
24
+ }
25
+ export type AvatarDropEvent = {
26
+ type: "picked";
27
+ files: readonly DroppedFile[];
28
+ } | {
29
+ type: "checked";
30
+ report: AvatarReport;
31
+ files: readonly DroppedFile[];
32
+ } | {
33
+ type: "failed";
34
+ message: string;
35
+ }
36
+ /** The card was pressed: wear this avatar. */
37
+ | {
38
+ type: "use";
39
+ report: AvatarReport;
40
+ files: readonly DroppedFile[];
41
+ };
42
+ export interface AvatarDropHandle {
43
+ readonly report: AvatarReport | null;
44
+ /** Reads files as a drop would. Resolves to the report, or null when the files held no body it could read. */
45
+ take(files: readonly DroppedFile[]): Promise<AvatarReport | null>;
46
+ /** Opens the file picker. */
47
+ pick(): void;
48
+ on(fn: (e: AvatarDropEvent) => void): () => void;
49
+ dispose(): void;
50
+ }
51
+ /** Mounts the drop card into `host`. */
52
+ export declare function avatarDrop(host: HTMLElement, opts?: AvatarDropOptions): AvatarDropHandle;
@@ -0,0 +1,153 @@
1
+ import { seededRng } from "@hackbuild/organic";
2
+ import { sniffFormat } from "@hackbuild/avatar";
3
+ import { bodyError, sourceFromFiles } from "@hackbuild/avatar-host";
4
+ import { Body, loadBody } from "@hackbuild/avatar-host/three";
5
+ import { dropRoutes } from "@virgilvox/hackbuild-ui/vanilla";
6
+ import { button, el, statusLine, urlSlot } from "./dom.js";
7
+ import { Still } from "./thumb.js";
8
+ import { licenceLine, rigCheck, sizeLine } from "./words.js";
9
+ const ACCEPT = ".vrm,.glb,.gltf,.fbx,.png,.jpg,.jpeg,.tga,.bin";
10
+ function triangles(root) {
11
+ let n = 0;
12
+ root.traverse((o) => {
13
+ const g = o.isMesh ? o.geometry : null;
14
+ if (!g)
15
+ return;
16
+ n += Math.floor((g.index ? g.index.count : (g.getAttribute("position")?.count ?? 0)) / 3);
17
+ });
18
+ return n;
19
+ }
20
+ /** Mounts the drop card into `host`. */
21
+ export function avatarDrop(host, opts = {}) {
22
+ const doc = host.ownerDocument;
23
+ const input = el(doc, "input", "", { type: "file", multiple: true, accept: ACCEPT, hidden: true, "aria-label": "pick an avatar file" });
24
+ const pickBtn = button(doc, opts.label ?? "pick a file", opts.go === true, { "data-pick": "" });
25
+ const zone = el(doc, "div", "hb-dropzone", {}, [el(doc, "span", "", {}, [opts.hint ?? "drop a .vrm, .glb or .fbx here, with its textures"]), pickBtn, input]);
26
+ // the card is hackbuild-ui's avatar card: still, name, size line, rig line; pressing it uses the avatar
27
+ const thumbBox = el(doc, "span", "hb-avcard-thumb");
28
+ const nameEl = el(doc, "span", "hb-avcard-name");
29
+ const sizeEl = el(doc, "span", "hb-avcard-meta");
30
+ const rigEl = el(doc, "span", "hb-avcard-rig");
31
+ const head = el(doc, "button", "hb-avcard", { type: "button", hidden: true, "aria-pressed": "false" }, [thumbBox, el(doc, "span", "hb-avcard-body", {}, [nameEl, sizeEl, rigEl])]);
32
+ const lines = el(doc, "div", "", { role: "status", "aria-live": "polite" });
33
+ host.append(zone, head, lines);
34
+ const listeners = new Set();
35
+ const emit = (e) => {
36
+ for (const f of listeners)
37
+ f(e);
38
+ };
39
+ const still = new Still(doc);
40
+ const thumbUrl = urlSlot();
41
+ let report = null;
42
+ let lastFiles = null;
43
+ let ticket = 0;
44
+ let hook = null;
45
+ async function take(files) {
46
+ const mine = ++ticket;
47
+ emit({ type: "picked", files });
48
+ lines.replaceChildren(statusLine(doc, "reading the file.", "busy"));
49
+ let body = null;
50
+ try {
51
+ const src = sourceFromFiles(files);
52
+ const model = files.find((f) => f.name === src.name || (f.webkitRelativePath ?? "").endsWith(src.name)) ?? files[0];
53
+ const format = sniffFormat(new Uint8Array(await model.slice(0, 64 * 1024).arrayBuffer()), model.name);
54
+ const vrm = src.kind === "vrm" && opts.vrm ? await (hook ??= opts.vrm()) : null;
55
+ const avatar = await loadBody(src, vrm, opts);
56
+ src.release?.();
57
+ if (mine !== ticket) {
58
+ avatar.dispose();
59
+ return null;
60
+ }
61
+ body = new Body(avatar, { rng: seededRng("avatar-drop") });
62
+ const bytes = files.reduce((s, f) => s + f.size, 0);
63
+ const tris = triangles(avatar.root);
64
+ avatar.update(0);
65
+ const thumb = await still.draw(avatar.root);
66
+ const check = rigCheck({ bones: body.names, expressions: body.caps.expressions, clips: body.caps.clips, mouth: body.caps.mouth, blink: body.caps.blink, gaze: body.caps.gaze, warnings: avatar.warnings });
67
+ const r = {
68
+ name: src.name.replace(/\.[^.]+$/, ""),
69
+ format: format === "unknown" ? src.kind : format,
70
+ bytes,
71
+ files: files.length,
72
+ triangles: tris,
73
+ caps: body.caps,
74
+ check,
75
+ licence: licenceLine(format, (avatar.vrm?.meta ?? null)),
76
+ size: sizeLine({ bytes, format: format === "unknown" ? src.kind : format, files: files.length, triangles: tris }),
77
+ thumb,
78
+ };
79
+ if (mine !== ticket)
80
+ return null;
81
+ report = r;
82
+ lastFiles = files;
83
+ head.setAttribute("aria-pressed", "false");
84
+ head.classList.remove("is-on");
85
+ draw(r);
86
+ emit({ type: "checked", report: r, files });
87
+ return r;
88
+ }
89
+ catch (e) {
90
+ if (mine !== ticket)
91
+ return null;
92
+ const message = bodyError(e);
93
+ lines.replaceChildren(statusLine(doc, message, "bad"));
94
+ emit({ type: "failed", message });
95
+ return null;
96
+ }
97
+ finally {
98
+ body?.dispose();
99
+ }
100
+ }
101
+ function draw(r) {
102
+ head.hidden = false;
103
+ const src = thumbUrl(r.thumb);
104
+ thumbBox.replaceChildren(src ? el(doc, "img", "", { src, alt: "" }) : el(doc, "span", "", { "aria-hidden": "true" }, [r.name.slice(0, 1)]));
105
+ nameEl.textContent = r.name;
106
+ sizeEl.textContent = r.size;
107
+ const worst = r.check.lines.find((l) => l.tone !== "ok");
108
+ rigEl.textContent = worst ? `${r.check.head}: ${worst.text}` : `${r.check.head}, ${r.caps.bones} bones`;
109
+ rigEl.className = `hb-avcard-rig ${r.check.tone === "bad" ? "is-bad" : "is-ok"}`;
110
+ head.setAttribute("aria-label", `use ${r.name}, ${r.size}`);
111
+ const tone = (t) => (t === "bad" ? "bad" : t === "warn" ? "warn" : "ok");
112
+ lines.replaceChildren(...r.check.lines.filter((l) => l !== worst).map((l) => statusLine(doc, l.text, tone(l.tone))), statusLine(doc, r.licence.text, tone(r.licence.tone)));
113
+ pickBtn.textContent = "pick another";
114
+ }
115
+ const ac = new AbortController();
116
+ pickBtn.addEventListener("click", () => input.click(), { signal: ac.signal });
117
+ head.addEventListener("click", () => {
118
+ if (!report || !lastFiles)
119
+ return;
120
+ head.setAttribute("aria-pressed", "true");
121
+ head.classList.add("is-on");
122
+ emit({ type: "use", report, files: lastFiles });
123
+ }, { signal: ac.signal });
124
+ input.addEventListener("change", () => {
125
+ const list = [...(input.files ?? [])];
126
+ input.value = "";
127
+ if (list.length)
128
+ void take(list);
129
+ }, { signal: ac.signal });
130
+ const drop = dropRoutes(zone, [{ name: "avatar", accept: () => true, on: (files) => void take(files) }], { overlay: zone });
131
+ return {
132
+ get report() {
133
+ return report;
134
+ },
135
+ take,
136
+ pick: () => input.click(),
137
+ on(fn) {
138
+ listeners.add(fn);
139
+ return () => listeners.delete(fn);
140
+ },
141
+ dispose() {
142
+ ticket++;
143
+ ac.abort();
144
+ drop.dispose();
145
+ still.dispose();
146
+ thumbUrl(null);
147
+ listeners.clear();
148
+ zone.remove();
149
+ head.remove();
150
+ lines.remove();
151
+ },
152
+ };
153
+ }
@@ -0,0 +1,81 @@
1
+ import { type AnimationClip } from "three";
2
+ import type { Clip, PlayOptions } from "@hackbuild/presence";
3
+ import { type Speech, type SpeechSource } from "./speech.js";
4
+ import type { BodySource, DroppedFile } from "@hackbuild/avatar-host";
5
+ import { AvatarStage, type Body, type BodyCaps, type StageOptions } from "@hackbuild/avatar-host/three";
6
+ import { type Gesture } from "./gestures.js";
7
+ export type Framing = "bust" | "full";
8
+ /** One verb on the stage. `go` makes it the one pink action; give it to one verb at most. */
9
+ export interface Verb {
10
+ id: string;
11
+ label: string;
12
+ go?: boolean;
13
+ }
14
+ /** The verbs a stage shows by default: say hello is the next thing to do. */
15
+ export declare const DEFAULT_VERBS: readonly Verb[];
16
+ export interface CharacterStageOptions extends StageOptions {
17
+ /** The name on the corner plate. Default the body's name. */
18
+ name?: string;
19
+ /** A model URL to show; the placeholder body shows until it loads, and without one. */
20
+ src?: string;
21
+ verbs?: readonly Verb[];
22
+ /** Bust is the original's opening shot. Default bust. */
23
+ framing?: Framing;
24
+ /** Take files dropped on the stage. Default true. */
25
+ drop?: boolean;
26
+ /** What say hello says: text, real speech, or a function that makes the speech when pressed. */
27
+ hello?: string | Speech | (() => Speech | Promise<Speech>);
28
+ /** The context real speech plays through; default one made on the first audio. */
29
+ audioContext?: AudioContext;
30
+ }
31
+ export type CharacterStageEvent = {
32
+ type: "loaded";
33
+ name: string;
34
+ caps: BodyCaps;
35
+ warnings: string[];
36
+ placeholder: boolean;
37
+ } | {
38
+ type: "failed";
39
+ message: string;
40
+ } | {
41
+ type: "doing";
42
+ word: string;
43
+ } | {
44
+ type: "verb";
45
+ id: string;
46
+ } | {
47
+ type: "framing";
48
+ framing: Framing;
49
+ };
50
+ export interface CharacterStageHandle {
51
+ readonly stage: AvatarStage;
52
+ readonly body: Body | null;
53
+ /** The inset slot, top left: put an eye view or a caption card in it. Hidden while empty. */
54
+ readonly inset: HTMLElement;
55
+ readonly verbsEl: HTMLElement;
56
+ readonly subject: HTMLElement;
57
+ /** What the body is doing, as the corner plate says it. */
58
+ readonly doing: string;
59
+ readonly framing: Framing;
60
+ show(source: string | BodySource | readonly DroppedFile[]): Promise<Body | null>;
61
+ showPlaceholder(): void;
62
+ /**
63
+ * Speaks. Audio plays and its level moves the mouth, with the words or
64
+ * phonemes the engine timed; text alone moves the mouth on estimated word
65
+ * timings with no sound. Resolves when the speech ends or a newer one starts.
66
+ */
67
+ say(speech: string | Speech, seconds?: number): Promise<void>;
68
+ /** Where the mouth timings of the last speech came from. */
69
+ readonly speechSource: SpeechSource | null;
70
+ gesture(id: Gesture): boolean;
71
+ /** Plays a clip on the body; the corner plate shows its name. Returns the ticket, -1 when it cannot play. */
72
+ play(clip: Clip | AnimationClip | string, opts?: PlayOptions): number;
73
+ stop(): void;
74
+ turn(): void;
75
+ frame(mode: Framing): void;
76
+ setVerbs(verbs: readonly Verb[]): void;
77
+ on(fn: (e: CharacterStageEvent) => void): () => void;
78
+ dispose(): void;
79
+ }
80
+ /** Mounts a character stage into `host`, which becomes the `.hb-lay-stage`. */
81
+ export declare function characterStage(host: HTMLElement, opts?: CharacterStageOptions): CharacterStageHandle;