@hackbuild/bake 0.1.0 → 0.1.2
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 +154 -111
- package/dist/bake.iife.js +9 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +9 -0
- package/dist/load.d.ts +1 -0
- package/how.ts +28 -0
- package/llms.md +346 -0
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -1,152 +1,195 @@
|
|
|
1
1
|
# @hackbuild/bake
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
cache and vertices by first use, writes packed buffers with fresh
|
|
15
|
-
POSITION bounds, and drops accessors and buffer views nothing references.
|
|
16
|
-
Points, lines, strips, quantized positions and primitives with
|
|
17
|
-
extensions pass through and are listed in `report.skipped`. Draco and
|
|
18
|
-
meshopt compressed files and external buffers are refused with the
|
|
19
|
-
reason.
|
|
20
|
-
- Texture hooks: the crate decodes no images. `resize` gets each embedded
|
|
21
|
-
image with its role (baseColor, emissive, normal, orm, other, from the
|
|
22
|
-
materials that sample it), its size from the PNG or JPEG header and the
|
|
23
|
-
role's pixel limit, and returns new bytes and a mime type or null. The
|
|
24
|
-
hook may be async (`createImageBitmap` and an `OffscreenCanvas` in a
|
|
25
|
-
page, sharp in Node). Without a hook textures pass through untouched.
|
|
26
|
-
- `bakeClips(input)`: local rotations per role and a root track in, an
|
|
27
|
-
HBCP pack out. Rotations are int16 quaternion components (value / 32767),
|
|
28
|
-
sign aligned frame to frame; the root is int16 with a per clip scale. The
|
|
29
|
-
report gives the largest joint angle error after quantisation with the
|
|
30
|
-
role and frame, the root position error, a loop seam check (last to first
|
|
31
|
-
frame against the larger of a limit, default 2 degrees, and twice the
|
|
32
|
-
largest ordinary frame step), and foot contact frames found by forward
|
|
33
|
-
kinematics from height and horizontal speed thresholds, with short runs
|
|
34
|
-
merged. `decodePack` and `sampleClip` are plain TypeScript, so a runtime
|
|
35
|
-
reader skips the wasm.
|
|
36
|
-
- `writeLock`, `checkLock`, `lockEntry`, `sha256Hex`: assets.lock.json with
|
|
37
|
-
path, source, licence, SHA-256, size and an optional byte budget. The
|
|
38
|
-
check fails on a hash or size drift, a missing file, an empty licence or
|
|
39
|
-
source, a licence naming a forbidden term as whole tokens (`NC` fails
|
|
40
|
-
`CC-BY-NC-4.0` and passes `NCSA`), or a file over its budget. The CLI
|
|
41
|
-
stores each path relative to the lock file's folder.
|
|
3
|
+
Optimize GLB models, bake animation clips into compact packs, and keep an asset lock with licences and hashes, in the browser, in Node or on the command line.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@hackbuild/bake) [](../../LICENSE)  
|
|
6
|
+
|
|
7
|
+
## What it does
|
|
8
|
+
|
|
9
|
+
- Welds, simplifies and reorders the triangle meshes of a GLB, drops unused data, and hands each embedded image to your resize hook.
|
|
10
|
+
- Bakes clips of local rotations and a root track into an HBCP pack of int16 values, with a report of the quantization error, the loop seam and the foot contacts of each clip.
|
|
11
|
+
- Reads packs and samples clips in plain TypeScript, so a runtime reader needs no wasm.
|
|
12
|
+
- Writes and checks `assets.lock.json`: path, source, licence, SHA-256, size and an optional byte budget per asset.
|
|
13
|
+
- Gives the same bytes in a page, in Node and from the `hackbuild-bake` command line tool, which build from one Rust core.
|
|
42
14
|
|
|
43
15
|
## Install
|
|
44
16
|
|
|
45
|
-
|
|
17
|
+
```sh
|
|
18
|
+
npm install @hackbuild/bake
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
No dependencies and no peers. Image decoding and resizing stay on your side, through the hook.
|
|
46
22
|
|
|
47
|
-
|
|
48
|
-
|
|
23
|
+
| subpath | what |
|
|
24
|
+
|---|---|
|
|
25
|
+
| `@hackbuild/bake` | optimize, clip bake, pack reader, lock |
|
|
26
|
+
| `@hackbuild/bake/wasm` | the wasm file, to pass to `load()` as a URL |
|
|
27
|
+
| `@hackbuild/bake/inline` | the wasm as base64 |
|
|
28
|
+
| `@hackbuild/bake/iife` | the single file build, global `HackbuildBake`, wasm inlined |
|
|
49
29
|
|
|
50
|
-
##
|
|
30
|
+
## Quick start
|
|
51
31
|
|
|
52
32
|
```ts
|
|
53
|
-
import { load, optimizeModel,
|
|
33
|
+
import { bakeClips, decodePack, load, optimizeModel, sampleClip, syntheticClips, syntheticGlb } from "@hackbuild/bake";
|
|
54
34
|
|
|
55
35
|
await load();
|
|
56
|
-
|
|
57
|
-
|
|
36
|
+
// stand ins made in code: a bumpy grid GLB and three clips on a leg rig
|
|
37
|
+
const { glb, report } = await optimizeModel(syntheticGlb(1, 48), { ratio: 0.5 });
|
|
38
|
+
console.log(report.trianglesIn, "to", report.trianglesOut, "triangles,", report.bytesIn, "to", glb.byteLength, "bytes");
|
|
39
|
+
|
|
40
|
+
const baked = bakeClips(syntheticClips(1));
|
|
41
|
+
for (const r of baked.report) console.log(r.name, r.maxAngleErrorDeg.toFixed(3), "degrees worst error, seam ok:", r.seamOk);
|
|
42
|
+
|
|
43
|
+
// a reader needs no wasm: decode the pack and sample a clip at 0.25 s
|
|
44
|
+
const pack = decodePack(baked.bytes);
|
|
45
|
+
const first = Object.values(pack.clips)[0]!;
|
|
46
|
+
const pose = new Float32Array(pack.roles.length * 4);
|
|
47
|
+
const root = sampleClip(pack, first, 0.25, pose);
|
|
48
|
+
console.log(first.name, root, pose.slice(0, 4));
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Live example
|
|
52
|
+
|
|
53
|
+
The package page explains how the baker works, with its code: [hackbuild-motion/bake](https://hackbuild.github.io/hackbuild-motion/bake/). There is no interactive bake page yet.
|
|
54
|
+
|
|
55
|
+
## Concepts
|
|
56
|
+
|
|
57
|
+
### optimize
|
|
58
|
+
|
|
59
|
+
`optimizeModel(glb, opts)` works per triangle primitive. It welds vertices that match across every attribute and morph target, so a soup with one vertex per face corner shrinks to shared corners; simplifies when `ratio` is under 1 with the meshoptimizer quadric simplifier (through meshopt-pure, a pure Rust port, MIT) within `error` of the mesh extent; reorders triangles for the vertex cache and vertices by first use; writes packed buffers with fresh POSITION bounds; and drops accessors and buffer views nothing references. Points, lines, strips, quantized positions and primitives with extensions pass through and are listed in `report.skipped`.
|
|
60
|
+
|
|
61
|
+
### texture hooks
|
|
62
|
+
|
|
63
|
+
The core decodes no images. Your `resize` hook gets each embedded image with its role (baseColor, emissive, normal, orm or other, from the materials that sample it), its size from the PNG or JPEG header and the role's limit from `textures`, and returns new bytes and a mime type, or null to keep it. The hook may be async: `createImageBitmap` and an `OffscreenCanvas` in a page, an image library in Node.
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
const { glb } = await optimizeModel(bytes, {
|
|
58
67
|
textures: { baseColor: 1024, normal: 512 },
|
|
59
68
|
resize: async (t) => (t.width && t.width > t.maxSize ? { bytes: await shrink(t.data, t.maxSize), mime: "image/webp" } : null),
|
|
60
69
|
});
|
|
61
|
-
const { bytes: pack, report: clips } = bakeClips({ fps: 30, skeleton, clips, contacts: { feet: ["leftFoot", "rightFoot"], height: 0.12, speed: 0.45, minFrames: 2 } });
|
|
62
|
-
const lock = writeLock([lockEntry("dog.glb", glb, "Meshy export", "owner"), lockEntry("ual.hbcp", pack, "Quaternius UAL", "CC0-1.0", 400_000)]);
|
|
63
70
|
```
|
|
64
71
|
|
|
65
|
-
|
|
72
|
+
### clip packs
|
|
73
|
+
|
|
74
|
+
`bakeClips(input)` takes a skeleton of roles, parents and rest offsets and clips of local rotations per role and an optional root track. Rotations become int16 quaternion components (value / 32767), sign aligned frame to frame; the root becomes int16 with a per clip scale. The report names the largest joint angle error with its role and frame, the root position error, a loop seam check (last to first frame against the larger of `seamLimitDeg`, default 2 degrees, and twice the largest ordinary step), and contact frames found from foot height and horizontal speed with short runs merged. `sampleClip` blends linearly between frames, wraps loops and clamps one shots.
|
|
75
|
+
|
|
76
|
+
The pack format, HBCP version 1, little endian:
|
|
77
|
+
|
|
78
|
+
```text
|
|
79
|
+
"HBCP" u16 version f32 fps u16 roles u8 feet u16 clips
|
|
80
|
+
roles: str each, parents: i16 each, feet: str each
|
|
81
|
+
per clip: str name, f32 dur, u32 n, u8 loop, f32 posScale,
|
|
82
|
+
i16 q[n * roles * 4], i16 p[n * 3], u8 contacts[n]
|
|
83
|
+
str: u16 byte length, then utf-8
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### the asset lock
|
|
87
|
+
|
|
88
|
+
`lockEntry(path, bytes, source, licence, budget)` measures one file; `writeLock(entries)` writes `assets.lock.json` sorted by path; `checkLock(text, files, forbid)` fails on a hash or size drift, a missing file, an empty licence or source, a licence naming a forbidden term as whole tokens (`NC` fails `CC-BY-NC-4.0` and passes `NCSA`), or a file over its budget.
|
|
89
|
+
|
|
90
|
+
### the command line tool
|
|
91
|
+
|
|
92
|
+
The same Rust core builds a native `hackbuild-bake` binary with `cargo build --release -p hackbuild-bake` from the repository; it is not part of the npm package. It stores each lock path relative to the lock file's folder, and refuses unknown flags, the `--name=value` form, a ratio outside 0 to 1, a negative error and an output that is its own input.
|
|
93
|
+
|
|
94
|
+
```sh
|
|
95
|
+
hackbuild-bake optimize in.glb out.glb --ratio 0.5 --texture-max baseColor=1024 --texture-hook ./resize.sh
|
|
96
|
+
hackbuild-bake textures in.glb
|
|
97
|
+
hackbuild-bake clips clips.json walk.hbcp --report report.json
|
|
98
|
+
hackbuild-bake lock add walk.hbcp --source "my clips" --licence CC0-1.0 --budget 400000
|
|
99
|
+
hackbuild-bake lock check --forbid NC
|
|
100
|
+
hackbuild-bake hash file
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
The texture hook is a program you name. It runs as `hook <role> <max> <in> <out>`, writes the new image to `<out>` and prints its mime type; no output or a non zero exit keeps the original.
|
|
104
|
+
|
|
105
|
+
## API overview
|
|
66
106
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
107
|
+
| export | what it does |
|
|
108
|
+
|---|---|
|
|
109
|
+
| [`load`](../../docs/api/bake/README.md#load) | load the wasm core once; call before anything else |
|
|
110
|
+
| [`optimizeModel`](../../docs/api/bake/README.md#optimizemodel) | weld, simplify and repack a GLB, with the texture hook |
|
|
111
|
+
| [`listTextures`](../../docs/api/bake/README.md#listtextures) | the embedded images of a GLB with roles and sizes |
|
|
112
|
+
| [`bakeClips`](../../docs/api/bake/README.md#bakeclips) | clips into an HBCP pack, with a report per clip |
|
|
113
|
+
| [`decodePack`](../../docs/api/bake/README.md#decodepack) | read a pack; no wasm needed |
|
|
114
|
+
| [`sampleClip`](../../docs/api/bake/README.md#sampleclip) | a pose at a time from a packed clip |
|
|
115
|
+
| [`lockEntry`](../../docs/api/bake/README.md#lockentry) | a lock row measured from a file |
|
|
116
|
+
| [`writeLock`](../../docs/api/bake/README.md#writelock) | assets.lock.json text |
|
|
117
|
+
| [`checkLock`](../../docs/api/bake/README.md#checklock) | check files against the lock |
|
|
118
|
+
| [`syntheticGlb`](../../docs/api/bake/README.md#syntheticglb) | a test GLB made in code |
|
|
73
119
|
|
|
74
|
-
The
|
|
75
|
-
`hook <role> <max> <in> <out>`, writes the new image to `<out>` and prints
|
|
76
|
-
its mime type on stdout; no output or a non zero exit keeps the original.
|
|
120
|
+
The full reference is in [docs/api/bake](../../docs/api/bake/README.md).
|
|
77
121
|
|
|
78
|
-
|
|
79
|
-
inlined).
|
|
122
|
+
## Browser support
|
|
80
123
|
|
|
81
|
-
|
|
124
|
+
| needs | Chrome | Safari | Firefox | phone |
|
|
125
|
+
|---|---|---|---|---|
|
|
126
|
+
| WebAssembly with SIMD128 | yes (vendor) | 16.4 and later (vendor) | yes (vendor) | unknown |
|
|
127
|
+
| `createImageBitmap` and `OffscreenCanvas`, only in your resize hook | yes (vendor) | 16.4 and later (vendor) | yes (vendor) | unknown |
|
|
82
128
|
|
|
83
|
-
|
|
84
|
-
roles: str each, parents: i16 each, feet: str each
|
|
85
|
-
per clip: str name, f32 dur, u32 n, u8 loop, f32 posScale,
|
|
86
|
-
i16 q[n * roles * 4], i16 p[n * 3], u8 contacts[n]
|
|
87
|
-
str: u16 byte length, then utf-8
|
|
129
|
+
No WebGPU, workers or wasm threads. Nothing here has run in a real browser yet; it runs in Node in the tests. From file:// use `dist/bake.iife.js`, which carries the wasm inlined, or call `load()` with no argument.
|
|
88
130
|
|
|
89
|
-
##
|
|
131
|
+
## Errors and troubleshooting
|
|
90
132
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
133
|
+
| message | cause | what to do |
|
|
134
|
+
|---|---|---|
|
|
135
|
+
| `@hackbuild/bake: wasm not loaded. call load() first.` | a bake call before `load()` resolved | `await load()` once first |
|
|
136
|
+
| `glb: no glTF magic. only binary .glb files are read` | the file is a .gltf or not glTF | convert to a binary .glb |
|
|
137
|
+
| `glb: only glTF 2.0 is read` | an older glTF version | export as glTF 2.0 |
|
|
138
|
+
| `glb: more than one buffer. merge buffers before baking` | several binary buffers | merge buffers in your exporter |
|
|
139
|
+
| `glb: the buffer points at an external uri. embed it first` | an external buffer file | embed the buffer in the GLB |
|
|
140
|
+
| `glb: <extension> geometry is compressed. decode it before baking` | Draco or meshopt compressed geometry | decode the geometry first |
|
|
141
|
+
| `glb: ...` (missing or overrunning views and accessors) | a damaged or unusual file | check the file in a glTF validator |
|
|
142
|
+
| `clip bake: ...` | the skeleton or a clip disagrees on role counts, fps is not above 0, or more than 8 contact roles | fix the input the message names |
|
|
143
|
+
| `bake: clip input does not parse: ...` | the clip input could not be read | check every clip has `name` and `q` |
|
|
144
|
+
| `bake: no HBCP magic. this is not a clip pack` | `decodePack` got another file | pass a pack from `bakeClips` |
|
|
145
|
+
| `bake: unknown pack version` | a pack from a newer format | update the package |
|
|
146
|
+
| `bake: the pack ends early` | a truncated pack | fetch or write the pack again |
|
|
147
|
+
|
|
148
|
+
A primitive the optimizer cannot handle is not an error: it is left as it was and named in `report.skipped`.
|
|
149
|
+
|
|
150
|
+
## Works with
|
|
151
|
+
|
|
152
|
+
- [@hackbuild/avatar](../avatar/README.md) loads the optimized models.
|
|
153
|
+
- [@hackbuild/retarget](../retarget/README.md) brings clips onto one rig before they are baked.
|
|
154
|
+
- [@hackbuild/presence](../presence/README.md) and [@hackbuild/motion-match](../motion-match/README.md) play clips at run time.
|
|
155
|
+
|
|
156
|
+
## Changelog and licences
|
|
157
|
+
|
|
158
|
+
Changes are in the repository [CHANGELOG](../../CHANGELOG.md). Code is MIT ([LICENSE](../../LICENSE)); meshopt-pure, compiled into the core, is MIT. This README is CC-BY-4.0 with its code samples also MIT. No models or clips ship here; the files you bake keep their own licences, which the lock records.
|
|
94
159
|
|
|
95
160
|
## What is verified, and what is not
|
|
96
161
|
|
|
97
162
|
### Verified on this machine
|
|
98
163
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
cut three quarters of the way round; contacts in the synthetic walk
|
|
112
|
-
alternate and never leave both feet up (computed).
|
|
113
|
-
- 2026-11-22: the command line optimize, lock add, lock check (pass, then
|
|
114
|
-
fail after a file change) and clips commands ran end to end (measured in
|
|
115
|
-
`tests/cli.rs`).
|
|
116
|
-
- 2026-11-23: the CLI refuses unknown flags and the `--name=value` form, a
|
|
117
|
-
ratio outside 0 to 1, a negative error, and an output that is its own
|
|
118
|
-
input; texture hook files go in a fresh folder made with create_dir
|
|
119
|
-
(tested in `tests/cli.rs`, the hook path itself still untested).
|
|
120
|
-
- 2026-11-23: a sparse morph target (the form Blender writes shape keys
|
|
121
|
-
in) keeps its index and value buffer views through optimize; before,
|
|
122
|
-
both views were dropped and the accessor pointed at the wrong data
|
|
123
|
-
(computed, `sparse_morph_targets_keep_their_views`).
|
|
164
|
+
| check | result | label |
|
|
165
|
+
|---|---|---|
|
|
166
|
+
| `cargo test -p hackbuild-bake` | 16 of 16 pass, clippy clean natively and for wasm32 | counted |
|
|
167
|
+
| `npx vitest run packages/bake` | 5 of 5 pass | counted |
|
|
168
|
+
| golden trace (pack hash, quantization errors, seam angles, contact counts, optimized GLB hashes and counts at ratios 1 and 0.3) | bit identical between the native and the wasm build | measured |
|
|
169
|
+
| a 12 by 12 synthetic grid soup | the weld took 864 vertices to 169 and kept all 288 triangles with their winding; ratio 0.3 left 86 triangles | computed |
|
|
170
|
+
| int16 rotations on the synthetic clips | within 0.002 degrees of the source | computed |
|
|
171
|
+
| the seam check | passes a closed walk loop, fails a loop cut three quarters of the way round | computed |
|
|
172
|
+
| contacts in the synthetic walk | alternate and never leave both feet up | computed |
|
|
173
|
+
| command line optimize, lock add, lock check (pass, then fail after a file change) and clips | ran end to end in `tests/cli.rs` | measured |
|
|
174
|
+
| command line refusals of bad flags, ratios, errors and an output equal to its input | refused in `tests/cli.rs` | tested |
|
|
175
|
+
| a sparse morph target, the form Blender writes shape keys in | keeps its index and value buffer views through optimize | computed |
|
|
124
176
|
|
|
125
177
|
### Not verified by anything
|
|
126
178
|
|
|
127
|
-
- A real model: no Meshy, VRM, UAL or Mixamo file has gone through the
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
-
|
|
131
|
-
- Contact thresholds on real clips; the defaults (0.12 m, 0.45 m/s) are
|
|
132
|
-
starting values (inferred).
|
|
133
|
-
- KTX2 output, meshopt compression of the output, and size budget
|
|
134
|
-
enforcement for anything beyond the lock check.
|
|
179
|
+
- A real model: no Meshy, VRM, UAL or Mixamo file has gone through the optimizer. Sparse morph targets are tested only on a synthetic file.
|
|
180
|
+
- The `--texture-hook` path of the command line tool and any real image resize.
|
|
181
|
+
- Contact thresholds on real clips; 0.12 m and 0.45 m/s are starting values, not tuned.
|
|
182
|
+
- KTX2 output, meshopt compression of the output, and size budgets beyond the lock check.
|
|
135
183
|
- The package in a real browser.
|
|
136
184
|
|
|
137
185
|
## Size budget
|
|
138
186
|
|
|
139
187
|
| file | gzip (measured) | ceiling |
|
|
140
188
|
|---|---|---|
|
|
141
|
-
| `dist/index.js` (main entry, imports external) |
|
|
189
|
+
| `dist/index.js` (main entry, imports external) | 205,156 bytes | 215,413 bytes |
|
|
142
190
|
| `dist/wasm/bake_bg.wasm` | 148,883 bytes | 156,327 bytes |
|
|
143
|
-
| `dist/bake.iife.js` (single file build) |
|
|
191
|
+
| `dist/bake.iife.js` (single file build) | 205,266 bytes | 215,529 bytes |
|
|
144
192
|
|
|
145
|
-
Measured 2026-11-
|
|
193
|
+
Measured 2026-11-25 by `node tools/lint-packages.mjs` (gzip level 9) from the packed
|
|
146
194
|
tarball. Generated from `tools/size-budget.json`; CI fails when a file moves more than
|
|
147
195
|
5 percent from its row.
|
|
148
|
-
|
|
149
|
-
## Licence
|
|
150
|
-
|
|
151
|
-
Code MIT (`LICENSE` at the repository root). meshopt-pure is MIT. This
|
|
152
|
-
README is CC-BY-4.0, its code samples also MIT.
|
package/dist/bake.iife.js
CHANGED
|
@@ -483,13 +483,16 @@ var HackbuildBake = (function(exports) {
|
|
|
483
483
|
}
|
|
484
484
|
// src/index.ts
|
|
485
485
|
const PKG = "@hackbuild/bake";
|
|
486
|
+
/** Loads the wasm core once: from a URL, bytes or a compiled module, or from the inlined copy when you pass nothing. Call it before anything else. */
|
|
486
487
|
async function load(source) {
|
|
487
488
|
await loadWasm(source);
|
|
488
489
|
}
|
|
490
|
+
/** Loads the wasm core synchronously from bytes or a compiled module. */
|
|
489
491
|
function loadSync(source) {
|
|
490
492
|
loadWasmSync(source);
|
|
491
493
|
}
|
|
492
494
|
const bytesOf = (b) => b instanceof Uint8Array ? b : ArrayBuffer.isView(b) ? new Uint8Array(b.buffer, b.byteOffset, b.byteLength) : new Uint8Array(b);
|
|
495
|
+
/** Lists the embedded images of a GLB with their roles and sizes. */
|
|
493
496
|
function listTextures(glb) {
|
|
494
497
|
assertReady(PKG);
|
|
495
498
|
return JSON.parse(listTextures$1(bytesOf(glb)));
|
|
@@ -504,6 +507,7 @@ var HackbuildBake = (function(exports) {
|
|
|
504
507
|
function wasmGlb(glb) {
|
|
505
508
|
return bytesOf(glb).slice();
|
|
506
509
|
}
|
|
510
|
+
/** Welds, simplifies and repacks a GLB, swapping in the images your resize hook returns. */
|
|
507
511
|
async function optimizeModel(glb, opts = {}) {
|
|
508
512
|
assertReady(PKG);
|
|
509
513
|
const input = wasmGlb(glb);
|
|
@@ -568,6 +572,7 @@ var HackbuildBake = (function(exports) {
|
|
|
568
572
|
});
|
|
569
573
|
return out;
|
|
570
574
|
}
|
|
575
|
+
/** Bakes clips into the compact HBCP pack and reports error, seams and contacts per clip. */
|
|
571
576
|
function bakeClips(input) {
|
|
572
577
|
assertReady(PKG);
|
|
573
578
|
const r = bakeClips$1(JSON.stringify({
|
|
@@ -689,10 +694,12 @@ var HackbuildBake = (function(exports) {
|
|
|
689
694
|
p(2)
|
|
690
695
|
];
|
|
691
696
|
}
|
|
697
|
+
/** The SHA-256 of bytes as lowercase hex, computed in the wasm core. */
|
|
692
698
|
function sha256Hex(bytes) {
|
|
693
699
|
assertReady(PKG);
|
|
694
700
|
return sha256Hex$1(bytesOf(bytes));
|
|
695
701
|
}
|
|
702
|
+
/** A lock row for one file, with its SHA-256 and byte count measured from the bytes. */
|
|
696
703
|
function lockEntry(path, bytes, source, licence, budget) {
|
|
697
704
|
const b = bytesOf(bytes);
|
|
698
705
|
const e = {
|
|
@@ -710,6 +717,7 @@ var HackbuildBake = (function(exports) {
|
|
|
710
717
|
assertReady(PKG);
|
|
711
718
|
return writeLock$1(JSON.stringify(entries));
|
|
712
719
|
}
|
|
720
|
+
/** Checks files against assets.lock.json: hashes, sizes, budgets, missing licences and licences matching a forbidden substring such as "NC". */
|
|
713
721
|
function checkLock(lockText, files, forbid = []) {
|
|
714
722
|
assertReady(PKG);
|
|
715
723
|
const list = files instanceof Map ? [...files.entries()] : Object.entries(files);
|
|
@@ -722,6 +730,7 @@ var HackbuildBake = (function(exports) {
|
|
|
722
730
|
}
|
|
723
731
|
return JSON.parse(checkLock$1(lockText, list.map(([n]) => n).join("\n"), lengths, joined, forbid.join("\n")));
|
|
724
732
|
}
|
|
733
|
+
/** A deterministic trace of the core for one seed, compared between the native and wasm builds in tests. */
|
|
725
734
|
function goldenTrace(seed) {
|
|
726
735
|
assertReady(PKG);
|
|
727
736
|
return goldenTrace$1(seed);
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import { type WasmSource } from "./load.js";
|
|
2
2
|
export type { WasmSource } from "./load.js";
|
|
3
|
+
/** Loads the wasm core once: from a URL, bytes or a compiled module, or from the inlined copy when you pass nothing. Call it before anything else. */
|
|
3
4
|
export declare function load(source?: WasmSource): Promise<void>;
|
|
5
|
+
/** Loads the wasm core synchronously from bytes or a compiled module. */
|
|
4
6
|
export declare function loadSync(source: ArrayBuffer | ArrayBufferView | WebAssembly.Module): void;
|
|
7
|
+
/** What a texture does in its material, which picks its size limit. */
|
|
5
8
|
export type TextureRole = "baseColor" | "emissive" | "normal" | "orm" | "other";
|
|
9
|
+
/** One embedded image of a GLB: index, role, mime type, size and pixel dimensions. */
|
|
6
10
|
export interface TextureInfo {
|
|
7
11
|
image: number;
|
|
8
12
|
role: TextureRole;
|
|
@@ -12,6 +16,7 @@ export interface TextureInfo {
|
|
|
12
16
|
width: number | null;
|
|
13
17
|
height: number | null;
|
|
14
18
|
}
|
|
19
|
+
/** What the texture hook gets: the image facts, its bytes and its size limit. */
|
|
15
20
|
export interface TextureRequest extends TextureInfo {
|
|
16
21
|
data: Uint8Array;
|
|
17
22
|
/** The limit for this role in pixels, 0 when none was set. */
|
|
@@ -25,7 +30,9 @@ export type TextureHook = (t: TextureRequest) => Promise<{
|
|
|
25
30
|
bytes: Uint8Array;
|
|
26
31
|
mime: string;
|
|
27
32
|
} | null;
|
|
33
|
+
/** Lists the embedded images of a GLB with their roles and sizes. */
|
|
28
34
|
export declare function listTextures(glb: ArrayBuffer | ArrayBufferView): TextureInfo[];
|
|
35
|
+
/** Options of `optimizeModel`: simplification, texture limits and the resize hook. */
|
|
29
36
|
export interface OptimizeOptions {
|
|
30
37
|
/** Share of triangles to keep, 0 to 1. Default 1, no simplification. */
|
|
31
38
|
ratio?: number;
|
|
@@ -38,6 +45,7 @@ export interface OptimizeOptions {
|
|
|
38
45
|
/** Called for each embedded image; without it textures pass through. */
|
|
39
46
|
resize?: TextureHook;
|
|
40
47
|
}
|
|
48
|
+
/** What `optimizeModel` changed: bytes, vertices, triangles, textures and skipped primitives. */
|
|
41
49
|
export interface OptimizeReport {
|
|
42
50
|
bytesIn: number;
|
|
43
51
|
bytesOut: number;
|
|
@@ -56,10 +64,12 @@ export interface OptimizeReport {
|
|
|
56
64
|
/** Primitives left as they were, with the reason. */
|
|
57
65
|
skipped: string[];
|
|
58
66
|
}
|
|
67
|
+
/** Welds, simplifies and repacks a GLB, swapping in the images your resize hook returns. */
|
|
59
68
|
export declare function optimizeModel(glb: ArrayBuffer | ArrayBufferView, opts?: OptimizeOptions): Promise<{
|
|
60
69
|
glb: Uint8Array;
|
|
61
70
|
report: OptimizeReport;
|
|
62
71
|
}>;
|
|
72
|
+
/** The rig of a clip set: role names, parents and rest translations. */
|
|
63
73
|
export interface Skeleton {
|
|
64
74
|
roles: string[];
|
|
65
75
|
/** Parent index per role, -1 for the root; parents come first. */
|
|
@@ -67,6 +77,7 @@ export interface Skeleton {
|
|
|
67
77
|
/** Rest local translation per role in meters. */
|
|
68
78
|
rest: [number, number, number][];
|
|
69
79
|
}
|
|
80
|
+
/** One clip to bake: name, loop flag, local rotations and optional root translation. */
|
|
70
81
|
export interface SourceClip {
|
|
71
82
|
name: string;
|
|
72
83
|
loop?: boolean;
|
|
@@ -75,12 +86,14 @@ export interface SourceClip {
|
|
|
75
86
|
/** Root local translation per frame; omit for the rest offset. */
|
|
76
87
|
p?: ArrayLike<number>;
|
|
77
88
|
}
|
|
89
|
+
/** How foot contacts are found: foot roles, height and speed limits and the shortest contact. */
|
|
78
90
|
export interface ContactRule {
|
|
79
91
|
feet: string[];
|
|
80
92
|
height: number;
|
|
81
93
|
speed: number;
|
|
82
94
|
minFrames: number;
|
|
83
95
|
}
|
|
96
|
+
/** Everything `bakeClips` takes: frame rate, skeleton, clips, contact rule and seam limit. */
|
|
84
97
|
export interface BakeInput {
|
|
85
98
|
fps: number;
|
|
86
99
|
skeleton: Skeleton;
|
|
@@ -88,6 +101,7 @@ export interface BakeInput {
|
|
|
88
101
|
contacts?: ContactRule;
|
|
89
102
|
seamLimitDeg?: number;
|
|
90
103
|
}
|
|
104
|
+
/** Quantization error, seam and contacts of one baked clip. */
|
|
91
105
|
export interface ClipReport {
|
|
92
106
|
name: string;
|
|
93
107
|
frames: number;
|
|
@@ -100,6 +114,7 @@ export interface ClipReport {
|
|
|
100
114
|
seamOk: boolean | null;
|
|
101
115
|
contactFrames: number[];
|
|
102
116
|
}
|
|
117
|
+
/** One clip in a pack: int16 rotations and root positions and a contact byte per frame. */
|
|
103
118
|
export interface PackedClip {
|
|
104
119
|
name: string;
|
|
105
120
|
/** Time of the last frame, (n - 1) / fps. */
|
|
@@ -114,6 +129,7 @@ export interface PackedClip {
|
|
|
114
129
|
/** One byte per frame, bit k set while foot k is down. */
|
|
115
130
|
contacts: Uint8Array;
|
|
116
131
|
}
|
|
132
|
+
/** A decoded clip pack: frame rate, roles, parents, feet and clips by name. */
|
|
117
133
|
export interface ClipPack {
|
|
118
134
|
fps: number;
|
|
119
135
|
roles: string[];
|
|
@@ -121,6 +137,7 @@ export interface ClipPack {
|
|
|
121
137
|
feet: string[];
|
|
122
138
|
clips: Record<string, PackedClip>;
|
|
123
139
|
}
|
|
140
|
+
/** Bakes clips into the compact HBCP pack and reports error, seams and contacts per clip. */
|
|
124
141
|
export declare function bakeClips(input: BakeInput): {
|
|
125
142
|
bytes: Uint8Array;
|
|
126
143
|
pack: ClipPack;
|
|
@@ -134,6 +151,7 @@ export declare function decodePack(buf: ArrayBuffer | ArrayBufferView): ClipPack
|
|
|
134
151
|
* Loop clips wrap, others clamp. Returns the root position in meters.
|
|
135
152
|
*/
|
|
136
153
|
export declare function sampleClip(pack: ClipPack, clip: PackedClip, t: number, out: Float32Array): [number, number, number];
|
|
154
|
+
/** One row of assets.lock.json: path, source, licence, SHA-256, bytes and an optional budget. */
|
|
137
155
|
export interface AssetEntry {
|
|
138
156
|
path: string;
|
|
139
157
|
source: string;
|
|
@@ -142,14 +160,18 @@ export interface AssetEntry {
|
|
|
142
160
|
bytes: number;
|
|
143
161
|
budget?: number;
|
|
144
162
|
}
|
|
163
|
+
/** The SHA-256 of bytes as lowercase hex, computed in the wasm core. */
|
|
145
164
|
export declare function sha256Hex(bytes: ArrayBuffer | ArrayBufferView): string;
|
|
165
|
+
/** A lock row for one file, with its SHA-256 and byte count measured from the bytes. */
|
|
146
166
|
export declare function lockEntry(path: string, bytes: ArrayBuffer | ArrayBufferView, source: string, licence: string, budget?: number): AssetEntry;
|
|
147
167
|
/** assets.lock.json text, sorted by path. */
|
|
148
168
|
export declare function writeLock(entries: AssetEntry[]): string;
|
|
169
|
+
/** Checks files against assets.lock.json: hashes, sizes, budgets, missing licences and licences matching a forbidden substring such as "NC". */
|
|
149
170
|
export declare function checkLock(lockText: string, files: Map<string, Uint8Array> | Record<string, Uint8Array>, forbid?: string[]): {
|
|
150
171
|
ok: boolean;
|
|
151
172
|
failures: string[];
|
|
152
173
|
};
|
|
174
|
+
/** A deterministic trace of the core for one seed, compared between the native and wasm builds in tests. */
|
|
153
175
|
export declare function goldenTrace(seed: number): Float64Array;
|
|
154
176
|
/** A bumpy unindexed grid GLB with a line mesh and a fake 2048 px PNG. */
|
|
155
177
|
export declare function syntheticGlb(seed: number, grid: number): Uint8Array;
|
package/dist/index.js
CHANGED
|
@@ -6,13 +6,16 @@
|
|
|
6
6
|
import { assertReady, loadWasm, loadWasmSync } from "./load.js";
|
|
7
7
|
import * as wasm from "./wasm/bake.js";
|
|
8
8
|
const PKG = "@hackbuild/bake";
|
|
9
|
+
/** Loads the wasm core once: from a URL, bytes or a compiled module, or from the inlined copy when you pass nothing. Call it before anything else. */
|
|
9
10
|
export async function load(source) {
|
|
10
11
|
await loadWasm(source);
|
|
11
12
|
}
|
|
13
|
+
/** Loads the wasm core synchronously from bytes or a compiled module. */
|
|
12
14
|
export function loadSync(source) {
|
|
13
15
|
loadWasmSync(source);
|
|
14
16
|
}
|
|
15
17
|
const bytesOf = (b) => b instanceof Uint8Array ? b : ArrayBuffer.isView(b) ? new Uint8Array(b.buffer, b.byteOffset, b.byteLength) : new Uint8Array(b);
|
|
18
|
+
/** Lists the embedded images of a GLB with their roles and sizes. */
|
|
16
19
|
export function listTextures(glb) {
|
|
17
20
|
assertReady(PKG);
|
|
18
21
|
return JSON.parse(wasm.listTextures(bytesOf(glb)));
|
|
@@ -22,6 +25,7 @@ function wasmGlb(glb) {
|
|
|
22
25
|
// copy once: the hook may hold the caller's buffer while wasm reads
|
|
23
26
|
return bytesOf(glb).slice();
|
|
24
27
|
}
|
|
28
|
+
/** Welds, simplifies and repacks a GLB, swapping in the images your resize hook returns. */
|
|
25
29
|
export async function optimizeModel(glb, opts = {}) {
|
|
26
30
|
assertReady(PKG);
|
|
27
31
|
const input = wasmGlb(glb);
|
|
@@ -86,6 +90,7 @@ function imageViews(glb) {
|
|
|
86
90
|
});
|
|
87
91
|
return out;
|
|
88
92
|
}
|
|
93
|
+
/** Bakes clips into the compact HBCP pack and reports error, seams and contacts per clip. */
|
|
89
94
|
export function bakeClips(input) {
|
|
90
95
|
assertReady(PKG);
|
|
91
96
|
const json = JSON.stringify({ ...input, clips: input.clips.map((c) => ({ name: c.name, loop: c.loop ?? false, q: Array.from(c.q), p: Array.from(c.p ?? []) })) });
|
|
@@ -183,10 +188,12 @@ export function sampleClip(pack, clip, t, out) {
|
|
|
183
188
|
const p = (k) => (clip.p[i0 * 3 + k] * (1 - w) + clip.p[i1 * 3 + k] * w) * clip.posScale;
|
|
184
189
|
return [p(0), p(1), p(2)];
|
|
185
190
|
}
|
|
191
|
+
/** The SHA-256 of bytes as lowercase hex, computed in the wasm core. */
|
|
186
192
|
export function sha256Hex(bytes) {
|
|
187
193
|
assertReady(PKG);
|
|
188
194
|
return wasm.sha256Hex(bytesOf(bytes));
|
|
189
195
|
}
|
|
196
|
+
/** A lock row for one file, with its SHA-256 and byte count measured from the bytes. */
|
|
190
197
|
export function lockEntry(path, bytes, source, licence, budget) {
|
|
191
198
|
const b = bytesOf(bytes);
|
|
192
199
|
const e = { path, source, licence, sha256: sha256Hex(b), bytes: b.byteLength };
|
|
@@ -199,6 +206,7 @@ export function writeLock(entries) {
|
|
|
199
206
|
assertReady(PKG);
|
|
200
207
|
return wasm.writeLock(JSON.stringify(entries));
|
|
201
208
|
}
|
|
209
|
+
/** Checks files against assets.lock.json: hashes, sizes, budgets, missing licences and licences matching a forbidden substring such as "NC". */
|
|
202
210
|
export function checkLock(lockText, files, forbid = []) {
|
|
203
211
|
assertReady(PKG);
|
|
204
212
|
const list = files instanceof Map ? [...files.entries()] : Object.entries(files);
|
|
@@ -212,6 +220,7 @@ export function checkLock(lockText, files, forbid = []) {
|
|
|
212
220
|
return JSON.parse(wasm.checkLock(lockText, list.map(([n]) => n).join("\n"), lengths, joined, forbid.join("\n")));
|
|
213
221
|
}
|
|
214
222
|
// fixtures for tests and demos
|
|
223
|
+
/** A deterministic trace of the core for one seed, compared between the native and wasm builds in tests. */
|
|
215
224
|
export function goldenTrace(seed) {
|
|
216
225
|
assertReady(PKG);
|
|
217
226
|
return wasm.goldenTrace(seed);
|
package/dist/load.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/** Where the wasm core comes from: a URL to fetch, a Response, bytes or a compiled module. */
|
|
1
2
|
export type WasmSource = URL | string | Response | ArrayBuffer | ArrayBufferView | WebAssembly.Module;
|
|
2
3
|
export declare function wasmReady(): boolean;
|
|
3
4
|
/** Instantiates the wasm. Later calls return the first load. */
|
package/how.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// how this works: the fold of the package page and of the suite tool. Plain data, no imports;
|
|
2
|
+
// shape in tools/how.d.ts, checked by tsconfig.how.json, shipped in the tarball, never imported
|
|
3
|
+
// by the runtime entry. Text from the README.
|
|
4
|
+
const how: HowDoc = {
|
|
5
|
+
what: "glb optimize with texture hooks, clip bake into compact packs with error, seam and contact reports, and an asset lock file with licence and sha-256. rust core in wasm, plus a native command line tool.",
|
|
6
|
+
how: [
|
|
7
|
+
"one rust crate builds both the hackbuild-bake command line tool and the wasm module this package loads, so a bake in node, in a page and on the command line gives the same bytes.",
|
|
8
|
+
"optimizeModel welds duplicate vertices, simplifies when you ask for a ratio under 1, reorders for the vertex cache and drops unused data. it decodes no images; a resize hook gets each texture with its role and size and returns the new bytes. compressed files are refused with the reason.",
|
|
9
|
+
"bakeClips samples clips into a compact pack with error, seam and foot contact reports. writeLock writes assets.lock.json with each file's sha-256, source, licence and size budget.",
|
|
10
|
+
],
|
|
11
|
+
packages: ["@hackbuild/bake"],
|
|
12
|
+
install: "npm install @hackbuild/bake",
|
|
13
|
+
today: "git clone https://github.com/hackbuild/hackbuild-motion && cd hackbuild-motion && npm ci && npm run build && npm pack -w @hackbuild/bake",
|
|
14
|
+
code: `import { load, optimizeModel, bakeClips, writeLock, lockEntry } from "@hackbuild/bake";
|
|
15
|
+
|
|
16
|
+
await load();
|
|
17
|
+
const { glb, report } = await optimizeModel(bytes, {
|
|
18
|
+
ratio: 0.5,
|
|
19
|
+
textures: { baseColor: 1024, normal: 512 },
|
|
20
|
+
resize: async (t) => (t.width && t.width > t.maxSize ? { bytes: await shrink(t.data, t.maxSize), mime: "image/webp" } : null),
|
|
21
|
+
});
|
|
22
|
+
const { bytes: pack, report: clips } = bakeClips({ fps: 30, skeleton, clips, contacts: { feet: ["leftFoot", "rightFoot"], height: 0.12, speed: 0.45, minFrames: 2 } });
|
|
23
|
+
const lock = writeLock([lockEntry("dog.glb", glb, "Meshy export", "owner"), lockEntry("ual.hbcp", pack, "Quaternius UAL", "CC0-1.0", 400_000)]);`,
|
|
24
|
+
lang: "ts",
|
|
25
|
+
links: { page: "https://hackbuild.github.io/hackbuild-motion/bake/", source: "https://github.com/hackbuild/hackbuild-motion/tree/main/packages/bake" },
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export default how;
|
package/llms.md
ADDED
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
# @hackbuild/bake
|
|
2
|
+
|
|
3
|
+
> Optimize GLB models, bake animation clips into compact packs, and keep an asset lock with licences and hashes, in the browser, in Node or on the command line.
|
|
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/bake/
|
|
6
|
+
|
|
7
|
+
## Purpose
|
|
8
|
+
|
|
9
|
+
- Welds, simplifies and reorders the triangle meshes of a GLB, drops unused data, and hands each embedded image to your resize hook.
|
|
10
|
+
- Bakes clips of local rotations and a root track into an HBCP pack of int16 values, with a report of the quantization error, the loop seam and the foot contacts of each clip.
|
|
11
|
+
- Reads packs and samples clips in plain TypeScript, so a runtime reader needs no wasm.
|
|
12
|
+
- Writes and checks `assets.lock.json`: path, source, licence, SHA-256, size and an optional byte budget per asset.
|
|
13
|
+
- Gives the same bytes in a page, in Node and from the `hackbuild-bake` command line tool, which build from one Rust core.
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
npm install @hackbuild/bake
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
No dependencies and no peers. Image decoding and resizing stay on your side, through the hook.
|
|
22
|
+
|
|
23
|
+
| subpath | what |
|
|
24
|
+
|---|---|
|
|
25
|
+
| `@hackbuild/bake` | optimize, clip bake, pack reader, lock |
|
|
26
|
+
| `@hackbuild/bake/wasm` | the wasm file, to pass to `load()` as a URL |
|
|
27
|
+
| `@hackbuild/bake/inline` | the wasm as base64 |
|
|
28
|
+
| `@hackbuild/bake/iife` | the single file build, global `HackbuildBake`, wasm inlined |
|
|
29
|
+
|
|
30
|
+
## Exports
|
|
31
|
+
|
|
32
|
+
### @hackbuild/bake
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
interface AssetEntry {
|
|
36
|
+
budget?: number;
|
|
37
|
+
bytes: number;
|
|
38
|
+
licence: string;
|
|
39
|
+
path: string;
|
|
40
|
+
sha256: string;
|
|
41
|
+
source: string;
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
One row of assets.lock.json: path, source, licence, SHA-256, bytes and an optional budget.
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
function bakeClips(input: BakeInput): { bytes: Uint8Array; pack: ClipPack; report: ClipReport[] }
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Bakes clips into the compact HBCP pack and reports error, seams and contacts per clip.
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
interface BakeInput {
|
|
55
|
+
clips: SourceClip[];
|
|
56
|
+
contacts?: ContactRule;
|
|
57
|
+
fps: number;
|
|
58
|
+
seamLimitDeg?: number;
|
|
59
|
+
skeleton: Skeleton;
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Everything `bakeClips` takes: frame rate, skeleton, clips, contact rule and seam limit.
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
function checkLock(lockText: string, files: Map<string, Uint8Array<ArrayBufferLike>> | Record<string, Uint8Array<ArrayBufferLike>>, forbid?: string[]): { failures: string[]; ok: boolean }
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Checks files against assets.lock.json: hashes, sizes, budgets, missing licences and licences matching a forbidden substring such as "NC".
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
interface ClipPack {
|
|
73
|
+
clips: Record<string, PackedClip>;
|
|
74
|
+
feet: string[];
|
|
75
|
+
fps: number;
|
|
76
|
+
parents: number[];
|
|
77
|
+
roles: string[];
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
A decoded clip pack: frame rate, roles, parents, feet and clips by name.
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
interface ClipReport {
|
|
85
|
+
contactFrames: number[];
|
|
86
|
+
frames: number;
|
|
87
|
+
largestStepDeg: number | null;
|
|
88
|
+
maxAngleErrorDeg: number;
|
|
89
|
+
maxPositionErrorM: number;
|
|
90
|
+
name: string;
|
|
91
|
+
seamAngleDeg: number | null;
|
|
92
|
+
seamOk: boolean | null;
|
|
93
|
+
worstFrame: number;
|
|
94
|
+
worstRole: string;
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Quantization error, seam and contacts of one baked clip.
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
interface ContactRule {
|
|
102
|
+
feet: string[];
|
|
103
|
+
height: number;
|
|
104
|
+
minFrames: number;
|
|
105
|
+
speed: number;
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
How foot contacts are found: foot roles, height and speed limits and the shortest contact.
|
|
110
|
+
|
|
111
|
+
```ts
|
|
112
|
+
function decodePack(buf: ArrayBuffer | ArrayBufferView<ArrayBufferLike>): ClipPack
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Reads the HBCP pack format; no wasm needed.
|
|
116
|
+
|
|
117
|
+
```ts
|
|
118
|
+
function goldenTrace(seed: number): Float64Array
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
A deterministic trace of the core for one seed, compared between the native and wasm builds in tests.
|
|
122
|
+
|
|
123
|
+
```ts
|
|
124
|
+
function listTextures(glb: ArrayBuffer | ArrayBufferView<ArrayBufferLike>): TextureInfo[]
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
Lists the embedded images of a GLB with their roles and sizes.
|
|
128
|
+
|
|
129
|
+
```ts
|
|
130
|
+
function load(source?: WasmSource): Promise<void>
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
Loads the wasm core once: from a URL, bytes or a compiled module, or from the inlined copy when you pass nothing.
|
|
134
|
+
|
|
135
|
+
```ts
|
|
136
|
+
function loadSync(source: ArrayBuffer | Module | ArrayBufferView<ArrayBufferLike>): void
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
Loads the wasm core synchronously from bytes or a compiled module.
|
|
140
|
+
|
|
141
|
+
```ts
|
|
142
|
+
function lockEntry(path: string, bytes: ArrayBuffer | ArrayBufferView<ArrayBufferLike>, source: string, licence: string, budget?: number): AssetEntry
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
A lock row for one file, with its SHA-256 and byte count measured from the bytes.
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
function optimizeModel(glb: ArrayBuffer | ArrayBufferView<ArrayBufferLike>, opts?: OptimizeOptions): Promise<{ glb: Uint8Array; report: OptimizeReport }>
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
Welds, simplifies and repacks a GLB, swapping in the images your resize hook returns.
|
|
152
|
+
|
|
153
|
+
```ts
|
|
154
|
+
interface OptimizeOptions {
|
|
155
|
+
error?: number;
|
|
156
|
+
lockBorder?: boolean;
|
|
157
|
+
ratio?: number;
|
|
158
|
+
resize?: TextureHook;
|
|
159
|
+
textures?: Partial<Record<TextureRole, number>>;
|
|
160
|
+
}
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
Options of `optimizeModel`: simplification, texture limits and the resize hook.
|
|
164
|
+
|
|
165
|
+
```ts
|
|
166
|
+
interface OptimizeReport {
|
|
167
|
+
bytesIn: number;
|
|
168
|
+
bytesOut: number;
|
|
169
|
+
primitives: number;
|
|
170
|
+
skipped: string[];
|
|
171
|
+
textures: { bytesIn: number; bytesOut: number; image: number; mime: string; role: TextureRole }[];
|
|
172
|
+
trianglesIn: number;
|
|
173
|
+
trianglesOut: number;
|
|
174
|
+
verticesIn: number;
|
|
175
|
+
verticesOut: number;
|
|
176
|
+
}
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
What `optimizeModel` changed: bytes, vertices, triangles, textures and skipped primitives.
|
|
180
|
+
|
|
181
|
+
```ts
|
|
182
|
+
interface PackedClip {
|
|
183
|
+
contacts: Uint8Array;
|
|
184
|
+
dur: number;
|
|
185
|
+
loop: boolean;
|
|
186
|
+
n: number;
|
|
187
|
+
name: string;
|
|
188
|
+
p: Int16Array;
|
|
189
|
+
posScale: number;
|
|
190
|
+
q: Int16Array;
|
|
191
|
+
}
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
One clip in a pack: int16 rotations and root positions and a contact byte per frame.
|
|
195
|
+
|
|
196
|
+
```ts
|
|
197
|
+
function sampleClip(pack: ClipPack, clip: PackedClip, t: number, out: Float32Array): [number, number, number]
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
Samples a packed clip at time `t` seconds into `out` (roles times 4 quaternion components) with normalized linear blending between frames.
|
|
201
|
+
|
|
202
|
+
```ts
|
|
203
|
+
function sha256Hex(bytes: ArrayBuffer | ArrayBufferView<ArrayBufferLike>): string
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
The SHA-256 of bytes as lowercase hex, computed in the wasm core.
|
|
207
|
+
|
|
208
|
+
```ts
|
|
209
|
+
interface Skeleton {
|
|
210
|
+
parents: number[];
|
|
211
|
+
rest: [number, number, number][];
|
|
212
|
+
roles: string[];
|
|
213
|
+
}
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
The rig of a clip set: role names, parents and rest translations.
|
|
217
|
+
|
|
218
|
+
```ts
|
|
219
|
+
interface SourceClip {
|
|
220
|
+
loop?: boolean;
|
|
221
|
+
name: string;
|
|
222
|
+
p?: ArrayLike<number>;
|
|
223
|
+
q: ArrayLike<number>;
|
|
224
|
+
}
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
One clip to bake: name, loop flag, local rotations and optional root translation.
|
|
228
|
+
|
|
229
|
+
```ts
|
|
230
|
+
function syntheticClips(seed: number): BakeInput
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
A walk loop, a broken loop and a one shot on an eight role leg rig.
|
|
234
|
+
|
|
235
|
+
```ts
|
|
236
|
+
function syntheticGlb(seed: number, grid: number): Uint8Array
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
A bumpy unindexed grid GLB with a line mesh and a fake 2048 px PNG.
|
|
240
|
+
|
|
241
|
+
```ts
|
|
242
|
+
type TextureHook = (t: TextureRequest) => Promise<{ bytes: Uint8Array; mime: string } | null> | { bytes: Uint8Array; mime: string } | null
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
Returns new bytes and their mime type, or null to keep the image.
|
|
246
|
+
|
|
247
|
+
```ts
|
|
248
|
+
interface TextureInfo {
|
|
249
|
+
bytes: number;
|
|
250
|
+
height: number | null;
|
|
251
|
+
image: number;
|
|
252
|
+
mime: string;
|
|
253
|
+
role: TextureRole;
|
|
254
|
+
width: number | null;
|
|
255
|
+
}
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
One embedded image of a GLB: index, role, mime type, size and pixel dimensions.
|
|
259
|
+
|
|
260
|
+
```ts
|
|
261
|
+
interface TextureRequest extends TextureInfo {
|
|
262
|
+
bytes: number;
|
|
263
|
+
data: Uint8Array;
|
|
264
|
+
height: number | null;
|
|
265
|
+
image: number;
|
|
266
|
+
maxSize: number;
|
|
267
|
+
mime: string;
|
|
268
|
+
role: TextureRole;
|
|
269
|
+
width: number | null;
|
|
270
|
+
}
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
What the texture hook gets: the image facts, its bytes and its size limit.
|
|
274
|
+
|
|
275
|
+
```ts
|
|
276
|
+
type TextureRole = "baseColor" | "emissive" | "normal" | "orm" | "other"
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
What a texture does in its material, which picks its size limit.
|
|
280
|
+
|
|
281
|
+
```ts
|
|
282
|
+
type WasmSource = URL | string | Response | ArrayBuffer | ArrayBufferView | WebAssembly.Module
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
Where the wasm core comes from: a URL to fetch, a Response, bytes or a compiled module.
|
|
286
|
+
|
|
287
|
+
```ts
|
|
288
|
+
function writeLock(entries: AssetEntry[]): string
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
assets.lock.json text, sorted by path.
|
|
292
|
+
|
|
293
|
+
## Examples
|
|
294
|
+
|
|
295
|
+
### quick start
|
|
296
|
+
|
|
297
|
+
```ts
|
|
298
|
+
import { bakeClips, decodePack, load, optimizeModel, sampleClip, syntheticClips, syntheticGlb } from "@hackbuild/bake";
|
|
299
|
+
|
|
300
|
+
await load();
|
|
301
|
+
// stand ins made in code: a bumpy grid GLB and three clips on a leg rig
|
|
302
|
+
const { glb, report } = await optimizeModel(syntheticGlb(1, 48), { ratio: 0.5 });
|
|
303
|
+
console.log(report.trianglesIn, "to", report.trianglesOut, "triangles,", report.bytesIn, "to", glb.byteLength, "bytes");
|
|
304
|
+
|
|
305
|
+
const baked = bakeClips(syntheticClips(1));
|
|
306
|
+
for (const r of baked.report) console.log(r.name, r.maxAngleErrorDeg.toFixed(3), "degrees worst error, seam ok:", r.seamOk);
|
|
307
|
+
|
|
308
|
+
// a reader needs no wasm: decode the pack and sample a clip at 0.25 s
|
|
309
|
+
const pack = decodePack(baked.bytes);
|
|
310
|
+
const first = Object.values(pack.clips)[0]!;
|
|
311
|
+
const pose = new Float32Array(pack.roles.length * 4);
|
|
312
|
+
const root = sampleClip(pack, first, 0.25, pose);
|
|
313
|
+
console.log(first.name, root, pose.slice(0, 4));
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
### from the concepts
|
|
317
|
+
|
|
318
|
+
```ts
|
|
319
|
+
const { glb } = await optimizeModel(bytes, {
|
|
320
|
+
textures: { baseColor: 1024, normal: 512 },
|
|
321
|
+
resize: async (t) => (t.width && t.width > t.maxSize ? { bytes: await shrink(t.data, t.maxSize), mime: "image/webp" } : null),
|
|
322
|
+
});
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
## Mistakes to avoid
|
|
326
|
+
|
|
327
|
+
- Calling a bake function before `await load()`; only `decodePack` and `sampleClip` work without the wasm.
|
|
328
|
+
- Expecting the package to resize textures: it never decodes images, so pass a `resize` hook or the images pass through.
|
|
329
|
+
- Feeding a Draco or meshopt compressed file, or a .gltf with external buffers: decode or embed first.
|
|
330
|
+
- Leaving the licence empty in a lock entry: the check fails on it, by design.
|
|
331
|
+
- Looking for the `hackbuild-bake` binary in node_modules: build it with cargo from the repository.
|
|
332
|
+
|
|
333
|
+
## Errors this package throws
|
|
334
|
+
|
|
335
|
+
- `@hackbuild/bake: wasm not loaded. call load() first.` (a bake call before `load()` resolved): `await load()` once first
|
|
336
|
+
- `glb: no glTF magic. only binary .glb files are read` (the file is a .gltf or not glTF): convert to a binary .glb
|
|
337
|
+
- `glb: only glTF 2.0 is read` (an older glTF version): export as glTF 2.0
|
|
338
|
+
- `glb: more than one buffer. merge buffers before baking` (several binary buffers): merge buffers in your exporter
|
|
339
|
+
- `glb: the buffer points at an external uri. embed it first` (an external buffer file): embed the buffer in the GLB
|
|
340
|
+
- `glb: <extension> geometry is compressed. decode it before baking` (Draco or meshopt compressed geometry): decode the geometry first
|
|
341
|
+
- `glb: ...` (missing or overrunning views and accessors) (a damaged or unusual file): check the file in a glTF validator
|
|
342
|
+
- `clip bake: ...` (the skeleton or a clip disagrees on role counts, fps is not above 0, or more than 8 contact roles): fix the input the message names
|
|
343
|
+
- `bake: clip input does not parse: ...` (the clip input could not be read): check every clip has `name` and `q`
|
|
344
|
+
- `bake: no HBCP magic. this is not a clip pack` (`decodePack` got another file): pass a pack from `bakeClips`
|
|
345
|
+
- `bake: unknown pack version` (a pack from a newer format): update the package
|
|
346
|
+
- `bake: the pack ends early` (a truncated pack): fetch or write the pack again
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hackbuild/bake",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "GLB optimize with texture hooks, clip bake into compact packs with error, seam and contact reports, and an asset lock file with licence and SHA-256. Rust core in wasm, plus a native command line tool.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -31,7 +31,9 @@
|
|
|
31
31
|
"files": [
|
|
32
32
|
"dist",
|
|
33
33
|
"README.md",
|
|
34
|
-
"
|
|
34
|
+
"llms.md",
|
|
35
|
+
"LICENSE",
|
|
36
|
+
"how.ts"
|
|
35
37
|
],
|
|
36
38
|
"scripts": {
|
|
37
39
|
"build": "node ../../tools/build-package.mjs bake"
|