@hackbuild/bake 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 +21 -0
- package/README.md +151 -2
- package/dist/bake.iife.js +754 -0
- package/dist/index.d.ts +157 -0
- package/dist/index.js +228 -0
- package/dist/load.d.ts +7 -0
- package/dist/load.js +42 -0
- package/dist/single.d.ts +1 -0
- package/dist/single.js +2 -0
- package/dist/wasm/bake.d.ts +86 -0
- package/dist/wasm/bake.js +495 -0
- package/dist/wasm/bake_bg.wasm +0 -0
- package/dist/wasm/bake_bg.wasm.d.ts +21 -0
- package/dist/wasm/inline.d.ts +2 -0
- package/dist/wasm/inline.js +8 -0
- package/how.ts +28 -0
- package/package.json +38 -4
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,152 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @hackbuild/bake
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Asset baking for the family: GLB optimize, clip packs and the asset lock.
|
|
4
|
+
The core is the Rust crate `crates/bake`, which builds both the
|
|
5
|
+
`hackbuild-bake` command line tool and the wasm32 (SIMD128) module this
|
|
6
|
+
package loads, so a bake in Node, in a page and on the command line gives
|
|
7
|
+
the same bytes.
|
|
8
|
+
|
|
9
|
+
- `optimizeModel(glb, opts)`: per triangle primitive, welds vertices that
|
|
10
|
+
are identical across every attribute and morph target (Meshy style soups
|
|
11
|
+
with one vertex per face corner shrink to shared corners), simplifies
|
|
12
|
+
when `ratio` is under 1 (the meshoptimizer quadric simplifier through
|
|
13
|
+
meshopt-pure, a pure Rust port, MIT), reorders triangles for the vertex
|
|
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.
|
|
42
|
+
|
|
43
|
+
## Install
|
|
44
|
+
|
|
45
|
+
Not published yet. From a clone:
|
|
46
|
+
|
|
47
|
+
npm ci && npm run build -w @hackbuild/bake
|
|
48
|
+
cargo build --release -p hackbuild-bake
|
|
49
|
+
|
|
50
|
+
## Use
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
import { load, optimizeModel, bakeClips, writeLock, lockEntry } from "@hackbuild/bake";
|
|
54
|
+
|
|
55
|
+
await load();
|
|
56
|
+
const { glb, report } = await optimizeModel(bytes, {
|
|
57
|
+
ratio: 0.5,
|
|
58
|
+
textures: { baseColor: 1024, normal: 512 },
|
|
59
|
+
resize: async (t) => (t.width && t.width > t.maxSize ? { bytes: await shrink(t.data, t.maxSize), mime: "image/webp" } : null),
|
|
60
|
+
});
|
|
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
|
+
```
|
|
64
|
+
|
|
65
|
+
Command line:
|
|
66
|
+
|
|
67
|
+
hackbuild-bake optimize in.glb out.glb --ratio 0.5 --texture-max baseColor=1024 --texture-hook ./resize.sh
|
|
68
|
+
hackbuild-bake textures in.glb
|
|
69
|
+
hackbuild-bake clips clips.json ual.hbcp --report report.json
|
|
70
|
+
hackbuild-bake lock add ual.hbcp --source "Quaternius UAL" --licence CC0-1.0 --budget 400000
|
|
71
|
+
hackbuild-bake lock check --forbid NC
|
|
72
|
+
hackbuild-bake hash file
|
|
73
|
+
|
|
74
|
+
The texture hook is a program you name. It runs as
|
|
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.
|
|
77
|
+
|
|
78
|
+
`dist/bake.iife.js` is the single file build (`HackbuildBake`, wasm
|
|
79
|
+
inlined).
|
|
80
|
+
|
|
81
|
+
## Pack format (HBCP version 1, little endian)
|
|
82
|
+
|
|
83
|
+
"HBCP" u16 version f32 fps u16 roles u8 feet u16 clips
|
|
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
|
|
88
|
+
|
|
89
|
+
## Develop
|
|
90
|
+
|
|
91
|
+
npm run build -w @hackbuild/bake
|
|
92
|
+
npx vitest run packages/bake
|
|
93
|
+
cargo test -p hackbuild-bake
|
|
94
|
+
|
|
95
|
+
## What is verified, and what is not
|
|
96
|
+
|
|
97
|
+
### Verified on this machine
|
|
98
|
+
|
|
99
|
+
- 2026-11-22: `cargo test -p hackbuild-bake` passed 16 of 16 tests
|
|
100
|
+
(counted), clippy clean natively and for wasm32.
|
|
101
|
+
- 2026-11-22: `npx vitest run packages/bake` passed 5 of 5 tests (counted).
|
|
102
|
+
- 2026-11-22: the golden trace (pack bytes hash, quantisation errors, seam
|
|
103
|
+
angles, contact counts, optimized GLB hashes and counts at ratios 1 and
|
|
104
|
+
0.3) is bit identical between the native and the wasm build (measured).
|
|
105
|
+
- 2026-11-22: on a 12 by 12 synthetic grid soup, the weld took 864
|
|
106
|
+
vertices to 169 and kept all 288 triangles with their winding; at ratio
|
|
107
|
+
0.3 the simplifier left 86 triangles (computed).
|
|
108
|
+
- 2026-11-22: int16 rotations stayed within 0.002 degrees of the source on
|
|
109
|
+
the synthetic clips (computed, atan2 of the relative rotation in f64).
|
|
110
|
+
- 2026-11-22: the seam check passes a closed walk loop and fails a loop
|
|
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`).
|
|
124
|
+
|
|
125
|
+
### Not verified by anything
|
|
126
|
+
|
|
127
|
+
- A real model: no Meshy, VRM, UAL or Mixamo file has gone through the
|
|
128
|
+
optimizer yet. Primitives with sparse morph targets pass through
|
|
129
|
+
unwelded; the sparse case is tested only on a synthetic file.
|
|
130
|
+
- The `--texture-hook` command path of the CLI and any real image resize.
|
|
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.
|
|
135
|
+
- The package in a real browser.
|
|
136
|
+
|
|
137
|
+
## Size budget
|
|
138
|
+
|
|
139
|
+
| file | gzip (measured) | ceiling |
|
|
140
|
+
|---|---|---|
|
|
141
|
+
| `dist/index.js` (main entry, imports external) | 204,749 bytes | 214,986 bytes |
|
|
142
|
+
| `dist/wasm/bake_bg.wasm` | 148,883 bytes | 156,327 bytes |
|
|
143
|
+
| `dist/bake.iife.js` (single file build) | 204,855 bytes | 215,097 bytes |
|
|
144
|
+
|
|
145
|
+
Measured 2026-11-24 by `node tools/lint-packages.mjs` (gzip level 9) from the packed
|
|
146
|
+
tarball. Generated from `tools/size-budget.json`; CI fails when a file moves more than
|
|
147
|
+
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.
|