@hackbuild/avatar-host 0.0.0-stage → 0.1.1

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,212 @@
1
- # Temporary Holding Version
1
+ # @hackbuild/avatar-host
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
+ Hosts a character on a three.js stage in any container. Bodies load
4
+ through `@hackbuild/avatar` into the normalized humanoid space (VRM 1.0
5
+ bone names, T pose, +Z forward, meters, feet at y = 0), the camera frames
6
+ and follows the posed body, and the frame loop follows the stage rules of
7
+ the suite: one `setAnimationLoop`, stopped when the page is hidden, the
8
+ host is off screen or at zero size, the route is inactive or a sheet
9
+ covers it. Framework free, with an `hb-avatar-host` custom element for
10
+ vanilla pages and for the slot of a Vue stage part.
11
+
12
+ The main entry has no three.js:
13
+
14
+ - `AvatarHost`: ticketed loads. `show(source)` resolves to the result, or
15
+ to null when a later show superseded it; a superseded result is
16
+ discarded and never shown. One runtime per adapter key, made on first
17
+ use (the import for a format happens once); a failed create is
18
+ forgotten so the next show tries again; `exclusive` destroys the other
19
+ runtimes on a switch; a source's `release` runs once whether its load
20
+ wins, loses or throws.
21
+ - `sourceFromFiles(files)`: the first `.vrm`, `.glb`, `.gltf` or `.fbx` of
22
+ a drop, with a resolver that finds referenced files by path below the
23
+ model's folder, then by file name anywhere in the drop, ignoring case.
24
+ Misses are listed; object URLs are freed by `release`. A `.pmx` drop
25
+ reads "this file is a pmx. convert it to vrm or glb."
26
+ - `droppedFiles(dataTransfer)`: walks dropped folders and keeps each
27
+ file's relative path.
28
+ - `StageLoop`: the pause reasons (`hidden`, `offscreen`, `inactive`,
29
+ `covered`, `lost`, `paused`), one fixed step (1/60 s, proposed) as the
30
+ first delta after any stop, and draw on change only under reduced
31
+ motion, while a clip, a mouth or a finger on the camera still runs it.
32
+ - `bodyError(e)`: copy for the person from a load failure ("this model has
33
+ no humanoid rig the stage can map. missing bones: hips.").
34
+
35
+ `/three`:
36
+
37
+ - `AvatarStage(container, opts)`: renderer sized to its container by a
38
+ `ResizeObserver` (never the window), pixel ratio capped at 2 (proposed),
39
+ an `IntersectionObserver` and `visibilitychange` for the pause rules,
40
+ orbit controls (one finger orbits, two pinch and pan), plain lights.
41
+ `show`, `showPlaceholder`, `showAvatar`; `play`, `pause`, `setActive`,
42
+ `setCovered`; `zoomBy`, `resetView`, `lookAtView`, `resize`, `snapshot`
43
+ (one frame stepped, rendered and read in the same task, so no
44
+ `preserveDrawingBuffer`); `dispose` frees the body, calls
45
+ `renderer.dispose()` and loses the context. A lost context shows a
46
+ button with "the 3d view was lost. press to restore." Events through
47
+ `on(type, fn)`: `loaded` (the body and its capability list), `failed`
48
+ (copy and cause), `stats` (fps from the loop clock, draw calls,
49
+ triangles, buffer size, pixel ratio), `state`, `lost`, `restored`.
50
+ - `EnvironmentSlot` as `stage.environment`, and `stage.showEnvironment(id
51
+ | { preset, resolve } | null)`: a lit scene from `@hackbuild/environment`
52
+ around the body, imported on first use. Requests are ticketed: a slow
53
+ load a newer one overtook is disposed and never attached; the same id
54
+ again does nothing. The swap disposes the old scene first, turns on the
55
+ shadow map with the body casting, hides the plain lights and caps the
56
+ pixel ratio at 1.5 (vendor) while a scene shows. A missing file emits
57
+ `failed` with the environment copy and keeps the current scene. The
58
+ studio needs no file; the element takes it as the `environment`
59
+ attribute.
60
+ - `Body`: the body surface and the frame order. Each frame the presence
61
+ `ClipPlayer` writes every bone (an idle of the rest pose, a body layer
62
+ and an upper layer), `HeadLevel` lifts a low clip head, `applyPose`,
63
+ `GazeController` through `applyGaze`, then the presence `Face` writes
64
+ every expression weight in one pass (moods eased, clip face tracks, the
65
+ gaze controller's lid, `LipSync` mouth shapes through `drive` on
66
+ `face.mouth`), then `avatar.update(dt)`. With no idle clip the base
67
+ layer is the rest stance, arms relaxed at the sides (`stance: "rest"`
68
+ keeps the loaded pose); a quiet face rests at a slight smile
69
+ (`restingFace: false` for neutral). `play(clip | animation | name)`,
70
+ `stop`, `clipOf`, `setExpression`, `setGazeMode`, `setPartVisible`,
71
+ `lip` for speech events. Gaze turns on by itself when the head rests
72
+ upright and facing +Z (VRM bodies and the placeholder); for VRM the eyes
73
+ go through three-vrm's look at.
74
+ - `FollowCamera`: the target eases after the body's centre plus the
75
+ person's pan, with a dead zone of 6 percent of the body's height; the
76
+ distance eases to fit the pose, pulls back from the first frame a pose
77
+ outgrows the view and comes closer only once 12 percent too far; a hand
78
+ zoom is left alone (rate 3, dead zone 0.06, slack 0.12, margin 1.12,
79
+ vendor, all options).
80
+ - `loadBody` and `bodyAdapters`: the scene level loader. A dropped
81
+ model's references go through the source's resolver, and the load waits
82
+ for every texture the loader started before it resolves. The rig wasm
83
+ loads once, on the first body.
84
+ - `placeholderBody()`: a mannequin of capsules on a humanoid skeleton,
85
+ built in code, head top near 1.74 m (computed). No asset file.
86
+ - `defineAvatarHost(tag?, opts?)`: defines `hb-avatar-host`. Attributes
87
+ `src` (a model URL; the placeholder shows without one), `environment`
88
+ (a scene id), `paused`,
89
+ `inactive`, `covered`. Stage events arrive as DOM events of the same
90
+ name with the stage event as `detail`. Nothing is defined on import.
91
+
92
+ Every random draw comes from an injected `Rng` (default
93
+ `seededRng("avatar-host")`); the package reads no wall clock.
94
+
95
+ ## Install
96
+
97
+ Not published yet. From a clone:
98
+
99
+ npm ci && npm run build
100
+
101
+ ## Use
102
+
103
+ ```ts
104
+ import { defineAvatarHost } from "@hackbuild/avatar-host/three";
105
+ import { droppedFiles } from "@hackbuild/avatar-host";
106
+
107
+ defineAvatarHost("hb-avatar-host", {
108
+ // three-vrm (MIT) is imported on the first .vrm only
109
+ vrm: () => import("@hackbuild/avatar/vrm").then((m) => m.vrmHook()),
110
+ });
111
+ const el = document.querySelector("hb-avatar-host")!;
112
+ el.addEventListener("loaded", (e) => console.info(e.detail.caps));
113
+ el.addEventListener("failed", (e) => showError(e.detail.message));
114
+ zone.addEventListener("drop", async (e) => el.showFiles(await droppedFiles(e.dataTransfer!)));
115
+ ```
116
+
117
+ Or without the element:
118
+
119
+ ```ts
120
+ import { AvatarStage } from "@hackbuild/avatar-host/three";
121
+
122
+ const stage = new AvatarStage(container, { background: 0xf5f0e6 });
123
+ stage.on("stats", (e) => hud(e.stats.fps));
124
+ const body = (await stage.show("https://example.org/body.glb")) ?? stage.showPlaceholder();
125
+ body.play(body.clipOf(body.avatar.animations[0]!)!, { loop: true });
126
+ body.lip.push(speechEvents);
127
+ stage.setCovered(sheetOpen);
128
+ stage.dispose();
129
+ ```
130
+
131
+ `dist/avatar-host.iife.js` is the single file build (`HackbuildAvatarHost`,
132
+ the rig wasm inlined, `THREE` read from the page). It leaves out three-vrm;
133
+ a page that wants VRM passes a hook.
134
+
135
+ ## Develop
136
+
137
+ npm run build -w @hackbuild/avatar -w @hackbuild/avatar-host
138
+ npx vitest run packages/avatar-host
139
+
140
+ The example page `examples/avatar-host` runs the browser rows with `?auto`.
141
+
142
+ ## What is verified, and what is not
143
+
144
+ ### Verified on this machine
145
+
146
+ - 2026-11-24: `npx vitest run packages/avatar-host` passed 33 of 33
147
+ tests (counted), with the face driver and rest stance: a played clip
148
+ hands back to the rest stance with the hand below the shoulder;
149
+ `stance: "rest"` keeps the loaded pose; a held mood reaches 0.63 in
150
+ 1/8 s and the lid and mouth go through the face.
151
+ - 2026-11-24: `npx vitest run packages/avatar-host` passed 31 of 31
152
+ tests (counted), with the environment slot: a slow scene load
153
+ overtaken by a newer one is disposed and never attached; the same id
154
+ loads once, a failed load keeps the scene, null clears it; the pixel
155
+ ratio caps at 1.5 with a scene.
156
+ - 2026-11-24, headless Chrome on the example page: the studio scene under
157
+ the placeholder turns on the shadow map with 24 meshes casting and the
158
+ ground taking it, 49 draw calls against 24 plain, and the plain lights
159
+ come back after it (measured). The screenshot was looked at by the
160
+ builder: a shadow falls on the ground beside the body.
161
+ - 2026-11-24: `npx vitest run packages/avatar-host` passed 28 of 28
162
+ tests (counted): two overlapping shows resolve null for the first and
163
+ never show it; a failed create is retried on the next show; `exclusive`
164
+ destroys the other runtime; `release` runs once when a load wins, loses
165
+ or throws; the two step texture lookup by path, then by name in another
166
+ case, and a miss; a nested folder walk keeps relative paths; the loop
167
+ draws 0 frames while hidden, takes one fixed step after a pause of 89 s,
168
+ and draws on change only under reduced motion; the follow camera stays
169
+ still under the dead zone, pulls back on the first frame, comes closer
170
+ only past the slack and leaves a hand zoom alone; the placeholder stands
171
+ in the normalized space; a clip plays over rest and hands back; gaze
172
+ turns the head toward a target on the left; a generated GLB loads
173
+ through the host into the normalized space and is freed on dispose.
174
+ - 2026-11-24, headless Chrome on the example page: 0 frames drawn in 1 s
175
+ with the page hidden (hidden simulated by overriding `document.hidden`),
176
+ the canvas 320 css px in a 320 px host, 24 geometries before and after
177
+ two swaps, a superseded show resolved null, a lost context showed the
178
+ restore line and came back on a press, 60 fps with a wave playing on the
179
+ placeholder (measured).
180
+
181
+ ### Not verified by anything
182
+
183
+ - No real VRM, GLB or FBX file has been shown on this stage in a browser.
184
+ The VRM eye path through three-vrm's look at (yaw and pitch signs) is
185
+ unchecked.
186
+ - Gaze on a model whose head does not rest upright is left off; such a
187
+ body gets clips, mouth and camera only.
188
+ - A real tab switch, a real GPU reset, a pixel ratio 2 screen and reduced
189
+ motion in a browser have not been run.
190
+ - Nobody has looked at the follow camera or gaze at frame rate on a
191
+ loaded model.
192
+ - The room, cafe and outdoors scenes have not been shown on this stage;
193
+ their files are not in the repository. The pixel ratio cap was measured
194
+ at a device ratio of 1 only.
195
+ - Thumbnails are a later item and not in this package yet. The face
196
+ driver has not run on a real face in a browser.
197
+
198
+ ## Size budget
199
+
200
+ | file | gzip (measured) | ceiling |
201
+ |---|---|---|
202
+ | `dist/index.js` (main entry, imports external) | 3,696 bytes | 3,880 bytes |
203
+ | `dist/avatar-host.iife.js` (single file build) | 195,826 bytes | 205,617 bytes |
204
+
205
+ Measured 2026-11-24 by `node tools/lint-packages.mjs` (gzip level 9) from the packed
206
+ tarball. Generated from `tools/size-budget.json`; CI fails when a file moves more than
207
+ 5 percent from its row.
208
+
209
+ ## Licence
210
+
211
+ Code MIT (`LICENSE` in this package and at the repository root). This
212
+ README is CC-BY-4.0, its code samples also MIT.