-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathavatar-bake.test.js
More file actions
293 lines (251 loc) · 11.1 KB
/
Copy pathavatar-bake.test.js
File metadata and controls
293 lines (251 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Tests for the server-side avatar appearance baker.
//
// These exercise bakeAppearance() directly (no DB, no R2) using:
// - a base GLB built in-memory containing a Head bone-style node and a mesh
// with named morph targets we can verify get baked.
// - the real public/accessories/*.glb files generated by
// scripts/generate-accessory-glbs.mjs.
//
// Verifies:
// 1. Morph weights from outfit.morphBinding are baked into node.weights
// so a downstream loader sees the dressed pose without runtime work.
// 2. A bone-attached accessory GLB ends up parented under the matching bone.
// 3. appearanceHash() is order-invariant: { outfit: a, accessories: [b, c] }
// hashes identically to { accessories: [b, c], outfit: a }.
// 4. isBakeable() is false for null / empty appearance.
import { describe, it, expect, beforeAll } from 'vitest';
import { Document, NodeIO } from '@gltf-transform/core';
import { ALL_EXTENSIONS } from '@gltf-transform/extensions';
import { MeshoptDecoder } from 'meshoptimizer';
import { readFile } from 'node:fs/promises';
import path from 'node:path';
import {
bakeAppearance,
appearanceHash,
isBakeable,
bakedStorageKeyFor,
} from '../api/_lib/bake.js';
// ── In-memory test base GLB ────────────────────────────────────────────────
// Builds a tiny skeleton-shaped GLB with:
// - a "Hips" → "Spine" → "Head" node chain (the bone names AccessoryManager
// and bake.findBone() look for)
// - a single triangle mesh under the root with two morph targets named
// "Outfit_Casual" and "Outfit_Formal" so we can verify weight baking
async function buildTestBaseGlb() {
const doc = new Document();
doc.createBuffer();
doc.getRoot().getAsset().generator = 'avatar-bake.test fixture';
// Bone chain
const hips = doc.createNode('Hips');
const spine = doc.createNode('Spine');
const head = doc.createNode('Head');
hips.addChild(spine);
spine.addChild(head);
// One triangle, three morph targets that displace the apex vertex.
const basePositions = new Float32Array([
0, 0, 0,
1, 0, 0,
0.5, 1, 0,
]);
const baseNormals = new Float32Array([
0, 0, 1,
0, 0, 1,
0, 0, 1,
]);
const indices = new Uint16Array([0, 1, 2]);
const posAccessor = doc.createAccessor('positions').setType('VEC3').setArray(basePositions);
const norAccessor = doc.createAccessor('normals').setType('VEC3').setArray(baseNormals);
const idxAccessor = doc.createAccessor('indices').setType('SCALAR').setArray(indices);
// Morph targets are stored as Primitive targets; each target is an
// {attribute → accessor} mapping describing the delta from base.
const targetCasualPos = doc
.createAccessor('outfit_casual_pos_delta')
.setType('VEC3')
.setArray(new Float32Array([0, 0, 0, 0, 0, 0, 0, 0.5, 0]));
const targetFormalPos = doc
.createAccessor('outfit_formal_pos_delta')
.setType('VEC3')
.setArray(new Float32Array([0, 0, 0, 0, 0, 0, 0, 1.0, 0]));
const prim = doc
.createPrimitive()
.setAttribute('POSITION', posAccessor)
.setAttribute('NORMAL', norAccessor)
.setIndices(idxAccessor)
.setMaterial(doc.createMaterial('base').setBaseColorFactor([0.5, 0.5, 0.5, 1]));
const targetCasual = doc.createPrimitiveTarget('Outfit_Casual').setAttribute('POSITION', targetCasualPos);
const targetFormal = doc.createPrimitiveTarget('Outfit_Formal').setAttribute('POSITION', targetFormalPos);
prim.addTarget(targetCasual);
prim.addTarget(targetFormal);
const mesh = doc.createMesh('body').addPrimitive(prim);
// Target names live on mesh.extras.targetNames per the KHR convention.
mesh.setExtras({ targetNames: ['Outfit_Casual', 'Outfit_Formal'] });
const bodyNode = doc.createNode('body').setMesh(mesh);
const scene = doc.createScene('root').addChild(hips).addChild(bodyNode);
doc.getRoot().setDefaultScene(scene);
const io = new NodeIO();
return io.writeBinary(doc);
}
let BASE_GLB = null;
let BASEBALL_GLB = null;
beforeAll(async () => {
BASE_GLB = await buildTestBaseGlb();
BASEBALL_GLB = await readFile(
path.resolve(process.cwd(), 'public/accessories/hat-baseball.glb'),
);
});
// ── Helpers ────────────────────────────────────────────────────────────────
async function inspect(glbBytes) {
// bakeAppearance now ships meshopt-compressed buffers, so the test reader
// must register the meshopt decoder dependency to round-trip them.
await MeshoptDecoder.ready;
const io = new NodeIO()
.registerExtensions(ALL_EXTENSIONS)
.registerDependencies({ 'meshopt.decoder': MeshoptDecoder });
return io.readBinary(new Uint8Array(glbBytes));
}
function findNode(doc, name) {
return doc.getRoot().listNodes().find((n) => n.getName() === name) || null;
}
// ── Tests ──────────────────────────────────────────────────────────────────
describe('isBakeable', () => {
it('rejects null / undefined / empty', () => {
expect(isBakeable(null)).toBe(false);
expect(isBakeable(undefined)).toBe(false);
expect(isBakeable({})).toBe(false);
expect(isBakeable({ accessories: [] })).toBe(false);
expect(isBakeable({ morphs: {} })).toBe(false);
});
it('accepts any non-empty field', () => {
expect(isBakeable({ outfit: 'outfit-casual' })).toBe(true);
expect(isBakeable({ accessories: ['hat-baseball'] })).toBe(true);
expect(isBakeable({ morphs: { Smile: 0.5 } })).toBe(true);
expect(isBakeable({ colors: { outfit: '#ff0000' } })).toBe(true);
expect(isBakeable({ hidden: ['outfit'] })).toBe(true);
expect(isBakeable({ colors: {} })).toBe(false);
expect(isBakeable({ hidden: [] })).toBe(false);
});
});
describe('appearanceHash', () => {
it('is stable across key order', () => {
const a = appearanceHash({ outfit: 'outfit-casual', accessories: ['hat-baseball'] });
const b = appearanceHash({ accessories: ['hat-baseball'], outfit: 'outfit-casual' });
expect(a).toBe(b);
});
it('differs when content differs', () => {
const a = appearanceHash({ outfit: 'outfit-casual' });
const b = appearanceHash({ outfit: 'outfit-formal' });
expect(a).not.toBe(b);
});
it('is 64 hex chars (sha256)', () => {
expect(appearanceHash({ outfit: 'outfit-casual' })).toMatch(/^[a-f0-9]{64}$/);
});
});
describe('bakedStorageKeyFor', () => {
it('keeps the user/slug prefix and embeds a hash slice', () => {
const key = bakedStorageKeyFor({
baseStorageKey: 'u/abc123/dancer/xyz.glb',
hash: 'a'.repeat(64),
});
expect(key.startsWith('u/abc123/dancer/baked-')).toBe(true);
expect(key.endsWith('.glb')).toBe(true);
});
});
describe('bakeAppearance — morph weights', () => {
it('bakes outfit morphBinding into the mesh weights so downstream loaders see the pose', async () => {
const baked = await bakeAppearance(BASE_GLB, {
outfit: 'outfit-casual', // public/accessories/presets.json → { Outfit_Casual: 1.0 }
});
const doc = await inspect(baked);
const bodyNode = findNode(doc, 'body');
expect(bodyNode).toBeTruthy();
// Either node-level weights or mesh-level weights should carry the bake.
const weights = bodyNode.getWeights();
expect(weights.length).toBe(2);
// Outfit_Casual is target #0 in our fixture.
expect(weights[0]).toBeCloseTo(1.0, 3);
expect(weights[1]).toBeCloseTo(0.0, 3);
});
it('applies appearance.morphs overrides on top of preset bindings', async () => {
const baked = await bakeAppearance(BASE_GLB, {
outfit: 'outfit-casual',
morphs: { Outfit_Casual: 0.25, Outfit_Formal: 0.75 },
});
const doc = await inspect(baked);
const bodyNode = findNode(doc, 'body');
const weights = bodyNode.getWeights();
expect(weights[0]).toBeCloseTo(0.25, 3);
expect(weights[1]).toBeCloseTo(0.75, 3);
});
});
// Builds a GLB with two separate meshes carrying named Wolf3D materials, so we
// can verify colour tints land on the right material and hidden layers drop the
// right node — the garment-layer (wardrobe) bake path.
async function buildLayeredGlb() {
const doc = new Document();
doc.createBuffer();
const positions = () =>
doc.createAccessor().setType('VEC3').setArray(new Float32Array([0, 0, 0, 1, 0, 0, 0.5, 1, 0]));
const normals = () =>
doc.createAccessor().setType('VEC3').setArray(new Float32Array([0, 0, 1, 0, 0, 1, 0, 0, 1]));
const indices = () => doc.createAccessor().setType('SCALAR').setArray(new Uint16Array([0, 1, 2]));
const meshNode = (name, materialName) => {
const prim = doc
.createPrimitive()
.setAttribute('POSITION', positions())
.setAttribute('NORMAL', normals())
.setIndices(indices())
.setMaterial(doc.createMaterial(materialName).setBaseColorFactor([1, 1, 1, 1]));
const mesh = doc.createMesh(name).addPrimitive(prim);
return doc.createNode(name).setMesh(mesh);
};
const top = meshNode('OutfitTopNode', 'Wolf3D_Outfit_Top');
const skin = meshNode('SkinNode', 'Wolf3D_Skin');
const scene = doc.createScene('root').addChild(top).addChild(skin);
doc.getRoot().setDefaultScene(scene);
return new NodeIO().writeBinary(doc);
}
function findMaterial(doc, name) {
return doc.getRoot().listMaterials().find((m) => m.getName() === name) || null;
}
describe('bakeAppearance — garment layers (colors + hidden)', () => {
it('tints the slot material baseColorFactor and leaves other slots untouched', async () => {
const base = await buildLayeredGlb();
const baked = await bakeAppearance(base, { colors: { outfit: '#ff0000' } });
const doc = await inspect(baked);
const top = findMaterial(doc, 'Wolf3D_Outfit_Top');
expect(top).toBeTruthy();
const [r, g, b] = top.getBaseColorFactor();
// #ff0000 sRGB → linear ≈ [1, 0, 0].
expect(r).toBeCloseTo(1.0, 2);
expect(g).toBeCloseTo(0.0, 2);
expect(b).toBeCloseTo(0.0, 2);
// Skin was not in the colors map — its factor stays white.
const skin = findMaterial(doc, 'Wolf3D_Skin');
expect(skin.getBaseColorFactor().slice(0, 3)).toEqual([1, 1, 1]);
});
it('drops the meshes of a hidden slot but keeps the rest of the body', async () => {
const base = await buildLayeredGlb();
const baked = await bakeAppearance(base, { hidden: ['outfit'] });
const doc = await inspect(baked);
// The outfit node (and its now-orphaned material) are pruned away.
expect(findNode(doc, 'OutfitTopNode')).toBeNull();
expect(findMaterial(doc, 'Wolf3D_Outfit_Top')).toBeNull();
// The base body remains.
expect(findNode(doc, 'SkinNode')).toBeTruthy();
});
});
describe('bakeAppearance — bone-attached accessories', () => {
it('parents the hat-baseball mesh under the Head bone after baking', async () => {
const baked = await bakeAppearance(BASE_GLB, {
accessories: ['hat-baseball'],
});
const doc = await inspect(baked);
const head = findNode(doc, 'Head');
expect(head).toBeTruthy();
// After bake, Head should have at least one child that came from the
// hat-baseball.glb root (named "HatBaseball" by the generator).
const headChildren = head.listChildren();
const hatRoot = headChildren.find((n) => n.getName() === 'HatBaseball');
expect(hatRoot).toBeTruthy();
});
});