@hackbuild/bake 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 +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/package.json +37 -4
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { type WasmSource } from "./load.js";
|
|
2
|
+
export type { WasmSource } from "./load.js";
|
|
3
|
+
export declare function load(source?: WasmSource): Promise<void>;
|
|
4
|
+
export declare function loadSync(source: ArrayBuffer | ArrayBufferView | WebAssembly.Module): void;
|
|
5
|
+
export type TextureRole = "baseColor" | "emissive" | "normal" | "orm" | "other";
|
|
6
|
+
export interface TextureInfo {
|
|
7
|
+
image: number;
|
|
8
|
+
role: TextureRole;
|
|
9
|
+
mime: string;
|
|
10
|
+
bytes: number;
|
|
11
|
+
/** From the PNG or JPEG header; null for other formats. */
|
|
12
|
+
width: number | null;
|
|
13
|
+
height: number | null;
|
|
14
|
+
}
|
|
15
|
+
export interface TextureRequest extends TextureInfo {
|
|
16
|
+
data: Uint8Array;
|
|
17
|
+
/** The limit for this role in pixels, 0 when none was set. */
|
|
18
|
+
maxSize: number;
|
|
19
|
+
}
|
|
20
|
+
/** Returns new bytes and their mime type, or null to keep the image. */
|
|
21
|
+
export type TextureHook = (t: TextureRequest) => Promise<{
|
|
22
|
+
bytes: Uint8Array;
|
|
23
|
+
mime: string;
|
|
24
|
+
} | null> | {
|
|
25
|
+
bytes: Uint8Array;
|
|
26
|
+
mime: string;
|
|
27
|
+
} | null;
|
|
28
|
+
export declare function listTextures(glb: ArrayBuffer | ArrayBufferView): TextureInfo[];
|
|
29
|
+
export interface OptimizeOptions {
|
|
30
|
+
/** Share of triangles to keep, 0 to 1. Default 1, no simplification. */
|
|
31
|
+
ratio?: number;
|
|
32
|
+
/** Largest simplification error relative to the mesh extent. Default 0.01. */
|
|
33
|
+
error?: number;
|
|
34
|
+
/** Keep open borders fixed while simplifying. Default true. */
|
|
35
|
+
lockBorder?: boolean;
|
|
36
|
+
/** Longest side per role in pixels, passed to the hook as `maxSize`. */
|
|
37
|
+
textures?: Partial<Record<TextureRole, number>>;
|
|
38
|
+
/** Called for each embedded image; without it textures pass through. */
|
|
39
|
+
resize?: TextureHook;
|
|
40
|
+
}
|
|
41
|
+
export interface OptimizeReport {
|
|
42
|
+
bytesIn: number;
|
|
43
|
+
bytesOut: number;
|
|
44
|
+
verticesIn: number;
|
|
45
|
+
verticesOut: number;
|
|
46
|
+
trianglesIn: number;
|
|
47
|
+
trianglesOut: number;
|
|
48
|
+
primitives: number;
|
|
49
|
+
textures: {
|
|
50
|
+
image: number;
|
|
51
|
+
role: TextureRole;
|
|
52
|
+
bytesIn: number;
|
|
53
|
+
bytesOut: number;
|
|
54
|
+
mime: string;
|
|
55
|
+
}[];
|
|
56
|
+
/** Primitives left as they were, with the reason. */
|
|
57
|
+
skipped: string[];
|
|
58
|
+
}
|
|
59
|
+
export declare function optimizeModel(glb: ArrayBuffer | ArrayBufferView, opts?: OptimizeOptions): Promise<{
|
|
60
|
+
glb: Uint8Array;
|
|
61
|
+
report: OptimizeReport;
|
|
62
|
+
}>;
|
|
63
|
+
export interface Skeleton {
|
|
64
|
+
roles: string[];
|
|
65
|
+
/** Parent index per role, -1 for the root; parents come first. */
|
|
66
|
+
parents: number[];
|
|
67
|
+
/** Rest local translation per role in meters. */
|
|
68
|
+
rest: [number, number, number][];
|
|
69
|
+
}
|
|
70
|
+
export interface SourceClip {
|
|
71
|
+
name: string;
|
|
72
|
+
loop?: boolean;
|
|
73
|
+
/** Local rotations, frame major then role, `x y z w`. */
|
|
74
|
+
q: ArrayLike<number>;
|
|
75
|
+
/** Root local translation per frame; omit for the rest offset. */
|
|
76
|
+
p?: ArrayLike<number>;
|
|
77
|
+
}
|
|
78
|
+
export interface ContactRule {
|
|
79
|
+
feet: string[];
|
|
80
|
+
height: number;
|
|
81
|
+
speed: number;
|
|
82
|
+
minFrames: number;
|
|
83
|
+
}
|
|
84
|
+
export interface BakeInput {
|
|
85
|
+
fps: number;
|
|
86
|
+
skeleton: Skeleton;
|
|
87
|
+
clips: SourceClip[];
|
|
88
|
+
contacts?: ContactRule;
|
|
89
|
+
seamLimitDeg?: number;
|
|
90
|
+
}
|
|
91
|
+
export interface ClipReport {
|
|
92
|
+
name: string;
|
|
93
|
+
frames: number;
|
|
94
|
+
maxAngleErrorDeg: number;
|
|
95
|
+
worstRole: string;
|
|
96
|
+
worstFrame: number;
|
|
97
|
+
maxPositionErrorM: number;
|
|
98
|
+
seamAngleDeg: number | null;
|
|
99
|
+
largestStepDeg: number | null;
|
|
100
|
+
seamOk: boolean | null;
|
|
101
|
+
contactFrames: number[];
|
|
102
|
+
}
|
|
103
|
+
export interface PackedClip {
|
|
104
|
+
name: string;
|
|
105
|
+
/** Time of the last frame, (n - 1) / fps. */
|
|
106
|
+
dur: number;
|
|
107
|
+
n: number;
|
|
108
|
+
loop: boolean;
|
|
109
|
+
posScale: number;
|
|
110
|
+
/** int16 quaternion components, value / 32767, frame major then role. */
|
|
111
|
+
q: Int16Array;
|
|
112
|
+
/** int16 root position, value times posScale meters. */
|
|
113
|
+
p: Int16Array;
|
|
114
|
+
/** One byte per frame, bit k set while foot k is down. */
|
|
115
|
+
contacts: Uint8Array;
|
|
116
|
+
}
|
|
117
|
+
export interface ClipPack {
|
|
118
|
+
fps: number;
|
|
119
|
+
roles: string[];
|
|
120
|
+
parents: number[];
|
|
121
|
+
feet: string[];
|
|
122
|
+
clips: Record<string, PackedClip>;
|
|
123
|
+
}
|
|
124
|
+
export declare function bakeClips(input: BakeInput): {
|
|
125
|
+
bytes: Uint8Array;
|
|
126
|
+
pack: ClipPack;
|
|
127
|
+
report: ClipReport[];
|
|
128
|
+
};
|
|
129
|
+
/** Reads the HBCP pack format; no wasm needed. */
|
|
130
|
+
export declare function decodePack(buf: ArrayBuffer | ArrayBufferView): ClipPack;
|
|
131
|
+
/**
|
|
132
|
+
* Samples a packed clip at time `t` seconds into `out` (roles times 4
|
|
133
|
+
* quaternion components) with normalized linear blending between frames.
|
|
134
|
+
* Loop clips wrap, others clamp. Returns the root position in meters.
|
|
135
|
+
*/
|
|
136
|
+
export declare function sampleClip(pack: ClipPack, clip: PackedClip, t: number, out: Float32Array): [number, number, number];
|
|
137
|
+
export interface AssetEntry {
|
|
138
|
+
path: string;
|
|
139
|
+
source: string;
|
|
140
|
+
licence: string;
|
|
141
|
+
sha256: string;
|
|
142
|
+
bytes: number;
|
|
143
|
+
budget?: number;
|
|
144
|
+
}
|
|
145
|
+
export declare function sha256Hex(bytes: ArrayBuffer | ArrayBufferView): string;
|
|
146
|
+
export declare function lockEntry(path: string, bytes: ArrayBuffer | ArrayBufferView, source: string, licence: string, budget?: number): AssetEntry;
|
|
147
|
+
/** assets.lock.json text, sorted by path. */
|
|
148
|
+
export declare function writeLock(entries: AssetEntry[]): string;
|
|
149
|
+
export declare function checkLock(lockText: string, files: Map<string, Uint8Array> | Record<string, Uint8Array>, forbid?: string[]): {
|
|
150
|
+
ok: boolean;
|
|
151
|
+
failures: string[];
|
|
152
|
+
};
|
|
153
|
+
export declare function goldenTrace(seed: number): Float64Array;
|
|
154
|
+
/** A bumpy unindexed grid GLB with a line mesh and a fake 2048 px PNG. */
|
|
155
|
+
export declare function syntheticGlb(seed: number, grid: number): Uint8Array;
|
|
156
|
+
/** A walk loop, a broken loop and a one shot on an eight role leg rig. */
|
|
157
|
+
export declare function syntheticClips(seed: number): BakeInput;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
// @hackbuild/bake: GLB optimize, clip packs and the asset lock, on the Rust
|
|
2
|
+
// core compiled to wasm. Texture resizing stays on the caller's side: the
|
|
3
|
+
// caller's async hook gets each embedded image and returns new bytes, which
|
|
4
|
+
// the wasm swaps into the file. Pack decoding and sampling are plain
|
|
5
|
+
// TypeScript so a runtime reader does not need the wasm.
|
|
6
|
+
import { assertReady, loadWasm, loadWasmSync } from "./load.js";
|
|
7
|
+
import * as wasm from "./wasm/bake.js";
|
|
8
|
+
const PKG = "@hackbuild/bake";
|
|
9
|
+
export async function load(source) {
|
|
10
|
+
await loadWasm(source);
|
|
11
|
+
}
|
|
12
|
+
export function loadSync(source) {
|
|
13
|
+
loadWasmSync(source);
|
|
14
|
+
}
|
|
15
|
+
const bytesOf = (b) => b instanceof Uint8Array ? b : ArrayBuffer.isView(b) ? new Uint8Array(b.buffer, b.byteOffset, b.byteLength) : new Uint8Array(b);
|
|
16
|
+
export function listTextures(glb) {
|
|
17
|
+
assertReady(PKG);
|
|
18
|
+
return JSON.parse(wasm.listTextures(bytesOf(glb)));
|
|
19
|
+
}
|
|
20
|
+
const ROLES = ["baseColor", "emissive", "normal", "orm", "other"];
|
|
21
|
+
function wasmGlb(glb) {
|
|
22
|
+
// copy once: the hook may hold the caller's buffer while wasm reads
|
|
23
|
+
return bytesOf(glb).slice();
|
|
24
|
+
}
|
|
25
|
+
export async function optimizeModel(glb, opts = {}) {
|
|
26
|
+
assertReady(PKG);
|
|
27
|
+
const input = wasmGlb(glb);
|
|
28
|
+
const limits = Uint32Array.from(ROLES.map((r) => opts.textures?.[r] ?? 0));
|
|
29
|
+
const images = [];
|
|
30
|
+
const lengths = [];
|
|
31
|
+
const parts = [];
|
|
32
|
+
const mimes = [];
|
|
33
|
+
if (opts.resize) {
|
|
34
|
+
const list = listTextures(input);
|
|
35
|
+
const views = imageViews(input);
|
|
36
|
+
for (const t of list) {
|
|
37
|
+
const data = views.get(t.image);
|
|
38
|
+
if (!data)
|
|
39
|
+
continue;
|
|
40
|
+
const out = await opts.resize({ ...t, data, maxSize: opts.textures?.[t.role] ?? 0 });
|
|
41
|
+
if (!out)
|
|
42
|
+
continue;
|
|
43
|
+
images.push(t.image);
|
|
44
|
+
lengths.push(out.bytes.byteLength);
|
|
45
|
+
parts.push(out.bytes);
|
|
46
|
+
mimes.push(out.mime);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
const joined = new Uint8Array(lengths.reduce((a, b) => a + b, 0));
|
|
50
|
+
let at = 0;
|
|
51
|
+
for (const p of parts) {
|
|
52
|
+
joined.set(p, at);
|
|
53
|
+
at += p.byteLength;
|
|
54
|
+
}
|
|
55
|
+
const r = wasm.optimizeGlb(input, opts.ratio ?? 1, opts.error ?? 0.01, opts.lockBorder ?? true, limits, Uint32Array.from(images), Uint32Array.from(lengths), joined, mimes.join("\n"));
|
|
56
|
+
try {
|
|
57
|
+
return { glb: r.bytes(), report: JSON.parse(r.report()) };
|
|
58
|
+
}
|
|
59
|
+
finally {
|
|
60
|
+
r.free();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/** Embedded image bytes by image index, read from the GLB in TypeScript. */
|
|
64
|
+
function imageViews(glb) {
|
|
65
|
+
const dv = new DataView(glb.buffer, glb.byteOffset, glb.byteLength);
|
|
66
|
+
const out = new Map();
|
|
67
|
+
let at = 12;
|
|
68
|
+
let json = null;
|
|
69
|
+
let bin = null;
|
|
70
|
+
while (at + 8 <= glb.byteLength) {
|
|
71
|
+
const len = dv.getUint32(at, true);
|
|
72
|
+
const kind = dv.getUint32(at + 4, true);
|
|
73
|
+
const body = glb.subarray(at + 8, at + 8 + len);
|
|
74
|
+
if (kind === 0x4e4f534a)
|
|
75
|
+
json = JSON.parse(new TextDecoder().decode(body));
|
|
76
|
+
else if (kind === 0x004e4942 && !bin)
|
|
77
|
+
bin = body;
|
|
78
|
+
at += 8 + len;
|
|
79
|
+
}
|
|
80
|
+
if (!json || !bin)
|
|
81
|
+
return out;
|
|
82
|
+
(json.images ?? []).forEach((im, i) => {
|
|
83
|
+
const v = im.bufferView === undefined ? undefined : json.bufferViews?.[im.bufferView];
|
|
84
|
+
if (v)
|
|
85
|
+
out.set(i, bin.subarray(v.byteOffset ?? 0, (v.byteOffset ?? 0) + v.byteLength));
|
|
86
|
+
});
|
|
87
|
+
return out;
|
|
88
|
+
}
|
|
89
|
+
export function bakeClips(input) {
|
|
90
|
+
assertReady(PKG);
|
|
91
|
+
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 ?? []) })) });
|
|
92
|
+
const r = wasm.bakeClips(json);
|
|
93
|
+
try {
|
|
94
|
+
const bytes = r.bytes();
|
|
95
|
+
return { bytes, pack: decodePack(bytes), report: JSON.parse(r.report()) };
|
|
96
|
+
}
|
|
97
|
+
finally {
|
|
98
|
+
r.free();
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
/** Reads the HBCP pack format; no wasm needed. */
|
|
102
|
+
export function decodePack(buf) {
|
|
103
|
+
const b = bytesOf(buf);
|
|
104
|
+
const dv = new DataView(b.buffer, b.byteOffset, b.byteLength);
|
|
105
|
+
let at = 0;
|
|
106
|
+
const need = (n) => {
|
|
107
|
+
if (at + n > b.byteLength)
|
|
108
|
+
throw new Error("bake: the pack ends early");
|
|
109
|
+
};
|
|
110
|
+
const u8 = () => (need(1), dv.getUint8(at++));
|
|
111
|
+
const u16 = () => (need(2), (at += 2), dv.getUint16(at - 2, true));
|
|
112
|
+
const u32 = () => (need(4), (at += 4), dv.getUint32(at - 4, true));
|
|
113
|
+
const f32 = () => (need(4), (at += 4), dv.getFloat32(at - 4, true));
|
|
114
|
+
const str = () => {
|
|
115
|
+
const n = u16();
|
|
116
|
+
need(n);
|
|
117
|
+
at += n;
|
|
118
|
+
return new TextDecoder().decode(b.subarray(at - n, at));
|
|
119
|
+
};
|
|
120
|
+
const i16s = (n) => {
|
|
121
|
+
need(n * 2);
|
|
122
|
+
const o = new Int16Array(n);
|
|
123
|
+
for (let i = 0; i < n; i++)
|
|
124
|
+
o[i] = dv.getInt16(at + i * 2, true);
|
|
125
|
+
at += n * 2;
|
|
126
|
+
return o;
|
|
127
|
+
};
|
|
128
|
+
need(4);
|
|
129
|
+
if (String.fromCharCode(b[0], b[1], b[2], b[3]) !== "HBCP")
|
|
130
|
+
throw new Error("bake: no HBCP magic. this is not a clip pack");
|
|
131
|
+
at = 4;
|
|
132
|
+
if (u16() !== 1)
|
|
133
|
+
throw new Error("bake: unknown pack version");
|
|
134
|
+
const fps = f32();
|
|
135
|
+
const nr = u16();
|
|
136
|
+
const nf = u8();
|
|
137
|
+
const nc = u16();
|
|
138
|
+
const roles = Array.from({ length: nr }, str);
|
|
139
|
+
const parents = Array.from(i16s(nr));
|
|
140
|
+
const feet = Array.from({ length: nf }, str);
|
|
141
|
+
const clips = {};
|
|
142
|
+
for (let c = 0; c < nc; c++) {
|
|
143
|
+
const name = str();
|
|
144
|
+
const dur = f32();
|
|
145
|
+
const n = u32();
|
|
146
|
+
const loop = u8() !== 0;
|
|
147
|
+
const posScale = f32();
|
|
148
|
+
const q = i16s(n * nr * 4);
|
|
149
|
+
const p = i16s(n * 3);
|
|
150
|
+
need(n);
|
|
151
|
+
const contacts = b.slice(at, at + n);
|
|
152
|
+
at += n;
|
|
153
|
+
clips[name] = { name, dur, n, loop, posScale, q, p, contacts };
|
|
154
|
+
}
|
|
155
|
+
return { fps, roles, parents, feet, clips };
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Samples a packed clip at time `t` seconds into `out` (roles times 4
|
|
159
|
+
* quaternion components) with normalized linear blending between frames.
|
|
160
|
+
* Loop clips wrap, others clamp. Returns the root position in meters.
|
|
161
|
+
*/
|
|
162
|
+
export function sampleClip(pack, clip, t, out) {
|
|
163
|
+
const nr = pack.roles.length;
|
|
164
|
+
const f = t * pack.fps;
|
|
165
|
+
const last = clip.n - 1;
|
|
166
|
+
const x = clip.loop && last > 0 ? ((f % last) + last) % last : Math.min(Math.max(f, 0), last);
|
|
167
|
+
const i0 = Math.floor(x);
|
|
168
|
+
const i1 = Math.min(i0 + 1, last);
|
|
169
|
+
const w = x - i0;
|
|
170
|
+
for (let r = 0; r < nr; r++) {
|
|
171
|
+
const a = (i0 * nr + r) * 4;
|
|
172
|
+
const b = (i1 * nr + r) * 4;
|
|
173
|
+
let l = 0;
|
|
174
|
+
for (let k = 0; k < 4; k++) {
|
|
175
|
+
const v = (clip.q[a + k] * (1 - w) + clip.q[b + k] * w) / 32767;
|
|
176
|
+
out[r * 4 + k] = v;
|
|
177
|
+
l += v * v;
|
|
178
|
+
}
|
|
179
|
+
l = Math.sqrt(l) || 1;
|
|
180
|
+
for (let k = 0; k < 4; k++)
|
|
181
|
+
out[r * 4 + k] /= l;
|
|
182
|
+
}
|
|
183
|
+
const p = (k) => (clip.p[i0 * 3 + k] * (1 - w) + clip.p[i1 * 3 + k] * w) * clip.posScale;
|
|
184
|
+
return [p(0), p(1), p(2)];
|
|
185
|
+
}
|
|
186
|
+
export function sha256Hex(bytes) {
|
|
187
|
+
assertReady(PKG);
|
|
188
|
+
return wasm.sha256Hex(bytesOf(bytes));
|
|
189
|
+
}
|
|
190
|
+
export function lockEntry(path, bytes, source, licence, budget) {
|
|
191
|
+
const b = bytesOf(bytes);
|
|
192
|
+
const e = { path, source, licence, sha256: sha256Hex(b), bytes: b.byteLength };
|
|
193
|
+
if (budget !== undefined)
|
|
194
|
+
e.budget = budget;
|
|
195
|
+
return e;
|
|
196
|
+
}
|
|
197
|
+
/** assets.lock.json text, sorted by path. */
|
|
198
|
+
export function writeLock(entries) {
|
|
199
|
+
assertReady(PKG);
|
|
200
|
+
return wasm.writeLock(JSON.stringify(entries));
|
|
201
|
+
}
|
|
202
|
+
export function checkLock(lockText, files, forbid = []) {
|
|
203
|
+
assertReady(PKG);
|
|
204
|
+
const list = files instanceof Map ? [...files.entries()] : Object.entries(files);
|
|
205
|
+
const lengths = Uint32Array.from(list.map(([, b]) => b.byteLength));
|
|
206
|
+
const joined = new Uint8Array(lengths.reduce((a, b) => a + b, 0));
|
|
207
|
+
let at = 0;
|
|
208
|
+
for (const [, b] of list) {
|
|
209
|
+
joined.set(b, at);
|
|
210
|
+
at += b.byteLength;
|
|
211
|
+
}
|
|
212
|
+
return JSON.parse(wasm.checkLock(lockText, list.map(([n]) => n).join("\n"), lengths, joined, forbid.join("\n")));
|
|
213
|
+
}
|
|
214
|
+
// fixtures for tests and demos
|
|
215
|
+
export function goldenTrace(seed) {
|
|
216
|
+
assertReady(PKG);
|
|
217
|
+
return wasm.goldenTrace(seed);
|
|
218
|
+
}
|
|
219
|
+
/** A bumpy unindexed grid GLB with a line mesh and a fake 2048 px PNG. */
|
|
220
|
+
export function syntheticGlb(seed, grid) {
|
|
221
|
+
assertReady(PKG);
|
|
222
|
+
return wasm.syntheticGlb(seed, grid);
|
|
223
|
+
}
|
|
224
|
+
/** A walk loop, a broken loop and a one shot on an eight role leg rig. */
|
|
225
|
+
export function syntheticClips(seed) {
|
|
226
|
+
assertReady(PKG);
|
|
227
|
+
return JSON.parse(wasm.syntheticClips(seed));
|
|
228
|
+
}
|
package/dist/load.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export type WasmSource = URL | string | Response | ArrayBuffer | ArrayBufferView | WebAssembly.Module;
|
|
2
|
+
export declare function wasmReady(): boolean;
|
|
3
|
+
/** Instantiates the wasm. Later calls return the first load. */
|
|
4
|
+
export declare function loadWasm(source?: WasmSource): Promise<void>;
|
|
5
|
+
/** Synchronous load from bytes or a compiled module. */
|
|
6
|
+
export declare function loadWasmSync(source: ArrayBuffer | ArrayBufferView | WebAssembly.Module): void;
|
|
7
|
+
export declare function assertReady(pkg: string): void;
|
package/dist/load.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// Loads the bake wasm once. The source can be a URL (fetched), bytes
|
|
2
|
+
// (ArrayBuffer or a typed array), a compiled WebAssembly.Module, or nothing,
|
|
3
|
+
// in which case the base64 copy in ./wasm/inline.js is decoded, which works
|
|
4
|
+
// from file:// where fetch of a local file is refused.
|
|
5
|
+
import initAsync, { initSync as initFromBytes } from "./wasm/bake.js";
|
|
6
|
+
let pending = null;
|
|
7
|
+
let ready = false;
|
|
8
|
+
const isBytes = (s) => s instanceof ArrayBuffer || ArrayBuffer.isView(s) || (typeof WebAssembly !== "undefined" && s instanceof WebAssembly.Module);
|
|
9
|
+
export function wasmReady() {
|
|
10
|
+
return ready;
|
|
11
|
+
}
|
|
12
|
+
/** Instantiates the wasm. Later calls return the first load. */
|
|
13
|
+
export function loadWasm(source) {
|
|
14
|
+
if (pending)
|
|
15
|
+
return pending;
|
|
16
|
+
pending = (async () => {
|
|
17
|
+
let input = source;
|
|
18
|
+
if (input === undefined)
|
|
19
|
+
input = (await import("./wasm/inline.js")).wasmBytes();
|
|
20
|
+
if (isBytes(input))
|
|
21
|
+
initFromBytes({ module: input });
|
|
22
|
+
else
|
|
23
|
+
await initAsync({ module_or_path: input });
|
|
24
|
+
ready = true;
|
|
25
|
+
})();
|
|
26
|
+
pending.catch(() => {
|
|
27
|
+
pending = null;
|
|
28
|
+
});
|
|
29
|
+
return pending;
|
|
30
|
+
}
|
|
31
|
+
/** Synchronous load from bytes or a compiled module. */
|
|
32
|
+
export function loadWasmSync(source) {
|
|
33
|
+
if (ready)
|
|
34
|
+
return;
|
|
35
|
+
initFromBytes({ module: source });
|
|
36
|
+
ready = true;
|
|
37
|
+
pending = Promise.resolve();
|
|
38
|
+
}
|
|
39
|
+
export function assertReady(pkg) {
|
|
40
|
+
if (!ready)
|
|
41
|
+
throw new Error(`${pkg}: wasm not loaded. call load() first.`);
|
|
42
|
+
}
|
package/dist/single.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./index.js";
|
package/dist/single.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
|
|
4
|
+
export class Baked {
|
|
5
|
+
private constructor();
|
|
6
|
+
free(): void;
|
|
7
|
+
[Symbol.dispose](): void;
|
|
8
|
+
bytes(): Uint8Array;
|
|
9
|
+
report(): string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function bakeClips(input_json: string): Baked;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Files as parallel lists: names joined by newlines, byte lengths, and the
|
|
16
|
+
* concatenated bytes. Returns `{ ok, failures }` as JSON.
|
|
17
|
+
*/
|
|
18
|
+
export function checkLock(lock_text: string, names: string, lengths: Uint32Array, bytes: Uint8Array, forbid: string): string;
|
|
19
|
+
|
|
20
|
+
export function goldenTrace(seed: number): Float64Array;
|
|
21
|
+
|
|
22
|
+
export function listTextures(glb: Uint8Array): string;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* `limits`: baseColor, emissive, normal, orm, other. Replacement images
|
|
26
|
+
* arrive as parallel lists: image index, byte length, concatenated bytes,
|
|
27
|
+
* and mime types joined by newlines.
|
|
28
|
+
*/
|
|
29
|
+
export function optimizeGlb(glb: Uint8Array, ratio: number, error: number, lock_border: boolean, limits: Uint32Array, images: Uint32Array, lengths: Uint32Array, bytes: Uint8Array, mimes: string): Baked;
|
|
30
|
+
|
|
31
|
+
export function sha256Hex(bytes: Uint8Array): string;
|
|
32
|
+
|
|
33
|
+
export function syntheticClips(seed: number): string;
|
|
34
|
+
|
|
35
|
+
export function syntheticGlb(seed: number, grid: number): Uint8Array;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Entries as JSON in, assets.lock.json text out.
|
|
39
|
+
*/
|
|
40
|
+
export function writeLock(entries_json: string): string;
|
|
41
|
+
|
|
42
|
+
export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
|
|
43
|
+
|
|
44
|
+
export interface InitOutput {
|
|
45
|
+
readonly memory: WebAssembly.Memory;
|
|
46
|
+
readonly __wbg_baked_free: (a: number, b: number) => void;
|
|
47
|
+
readonly bakeClips: (a: number, b: number) => [number, number, number];
|
|
48
|
+
readonly baked_bytes: (a: number) => [number, number];
|
|
49
|
+
readonly baked_report: (a: number) => [number, number];
|
|
50
|
+
readonly checkLock: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number) => [number, number, number, number];
|
|
51
|
+
readonly goldenTrace: (a: number) => [number, number];
|
|
52
|
+
readonly listTextures: (a: number, b: number) => [number, number, number, number];
|
|
53
|
+
readonly optimizeGlb: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number, l: number, m: number, n: number, o: number) => [number, number, number];
|
|
54
|
+
readonly sha256Hex: (a: number, b: number) => [number, number];
|
|
55
|
+
readonly syntheticClips: (a: number) => [number, number];
|
|
56
|
+
readonly syntheticGlb: (a: number, b: number) => [number, number];
|
|
57
|
+
readonly writeLock: (a: number, b: number) => [number, number, number, number];
|
|
58
|
+
readonly __wbindgen_externrefs: WebAssembly.Table;
|
|
59
|
+
readonly __wbindgen_malloc: (a: number, b: number) => number;
|
|
60
|
+
readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
|
|
61
|
+
readonly __externref_table_dealloc: (a: number) => void;
|
|
62
|
+
readonly __wbindgen_free: (a: number, b: number, c: number) => void;
|
|
63
|
+
readonly __wbindgen_start: () => void;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export type SyncInitInput = BufferSource | WebAssembly.Module;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Instantiates the given `module`, which can either be bytes or
|
|
70
|
+
* a precompiled `WebAssembly.Module`.
|
|
71
|
+
*
|
|
72
|
+
* @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
|
|
73
|
+
*
|
|
74
|
+
* @returns {InitOutput}
|
|
75
|
+
*/
|
|
76
|
+
export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* If `module_or_path` is {RequestInfo} or {URL}, makes a request and
|
|
80
|
+
* for everything else, calls `WebAssembly.instantiate` directly.
|
|
81
|
+
*
|
|
82
|
+
* @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
|
|
83
|
+
*
|
|
84
|
+
* @returns {Promise<InitOutput>}
|
|
85
|
+
*/
|
|
86
|
+
export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;
|