conveyor-engine-assets 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/dist/acquisition.d.ts +23 -0
- package/dist/acquisition.js +29 -0
- package/dist/admission.d.ts +38 -0
- package/dist/admission.js +216 -0
- package/dist/animation-profile.d.ts +31 -0
- package/dist/animation-profile.js +49 -0
- package/dist/collision.d.ts +108 -0
- package/dist/collision.js +436 -0
- package/dist/compose.d.ts +48 -0
- package/dist/compose.js +174 -0
- package/dist/fixtures.d.ts +39 -0
- package/dist/fixtures.js +95 -0
- package/dist/glb.d.ts +60 -0
- package/dist/glb.js +223 -0
- package/dist/hash.d.ts +10 -0
- package/dist/hash.js +141 -0
- package/dist/http-fetch.d.ts +30 -0
- package/dist/http-fetch.js +88 -0
- package/dist/http.d.ts +1 -0
- package/dist/http.js +1 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +18 -0
- package/dist/loader.d.ts +75 -0
- package/dist/loader.js +434 -0
- package/dist/manifest.d.ts +13 -0
- package/dist/manifest.js +57 -0
- package/dist/node-fs.d.ts +15 -0
- package/dist/node-fs.js +73 -0
- package/dist/node.d.ts +1 -0
- package/dist/node.js +1 -0
- package/dist/render-descriptor.d.ts +56 -0
- package/dist/render-descriptor.js +103 -0
- package/dist/render-profile.d.ts +56 -0
- package/dist/render-profile.js +147 -0
- package/dist/schema.d.ts +21 -0
- package/dist/schema.js +417 -0
- package/dist/skinned-fixture.d.ts +6 -0
- package/dist/skinned-fixture.js +63 -0
- package/dist/static-world.d.ts +103 -0
- package/dist/static-world.js +271 -0
- package/dist/types.d.ts +242 -0
- package/dist/types.js +1 -0
- package/package.json +35 -4
- package/src/acquisition.ts +48 -0
- package/src/admission.ts +237 -0
- package/src/animation-profile.ts +81 -0
- package/src/collision.ts +515 -0
- package/src/compose.ts +233 -0
- package/src/fixtures.ts +121 -0
- package/src/glb.ts +288 -0
- package/src/hash.ts +138 -0
- package/src/http-fetch.ts +108 -0
- package/src/http.ts +1 -0
- package/src/index.ts +18 -0
- package/src/loader.ts +535 -0
- package/src/manifest.ts +63 -0
- package/src/node-fs.ts +78 -0
- package/src/node.ts +1 -0
- package/src/render-descriptor.ts +150 -0
- package/src/render-profile.ts +205 -0
- package/src/schema.ts +459 -0
- package/src/skinned-fixture.ts +66 -0
- package/src/static-world.ts +364 -0
- package/src/types.ts +325 -0
- package/README.md +0 -3
|
@@ -0,0 +1,436 @@
|
|
|
1
|
+
export class CollisionParseError extends Error {
|
|
2
|
+
issues;
|
|
3
|
+
constructor(issues) {
|
|
4
|
+
super(issues.join("; "));
|
|
5
|
+
this.name = "CollisionParseError";
|
|
6
|
+
this.issues = issues;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
const REPS = new Set(["aabb", "triangle-mesh", "occupancy-grid", "primitives"]);
|
|
10
|
+
export function parseCollisionArtifact(input) {
|
|
11
|
+
const issues = [];
|
|
12
|
+
if (!input || typeof input !== "object" || Array.isArray(input)) {
|
|
13
|
+
throw new CollisionParseError(["collision artifact must be an object"]);
|
|
14
|
+
}
|
|
15
|
+
const raw = input;
|
|
16
|
+
rejectRenderLeak(raw, issues);
|
|
17
|
+
if (raw.formatVersion !== 1)
|
|
18
|
+
issues.push("formatVersion must be 1");
|
|
19
|
+
if (typeof raw.representation !== "string" || !REPS.has(raw.representation)) {
|
|
20
|
+
issues.push("unknown representation");
|
|
21
|
+
}
|
|
22
|
+
const coordinateSystem = raw.coordinateSystem === "z-up" ? "z-up" : raw.coordinateSystem === "y-up" ? "y-up" : undefined;
|
|
23
|
+
if (!coordinateSystem)
|
|
24
|
+
issues.push("coordinateSystem must be y-up or z-up");
|
|
25
|
+
const unitScale = num(raw.unitScale, "unitScale", issues) ?? 1;
|
|
26
|
+
if (unitScale <= 0)
|
|
27
|
+
issues.push("unitScale must be > 0");
|
|
28
|
+
const origin = vec3(raw.origin, "origin", issues) ?? { x: 0, y: 0, z: 0 };
|
|
29
|
+
const bounds = parseBounds(raw.bounds, issues);
|
|
30
|
+
const artifact = {
|
|
31
|
+
formatVersion: 1,
|
|
32
|
+
representation: raw.representation ?? "aabb",
|
|
33
|
+
coordinateSystem: coordinateSystem ?? "y-up",
|
|
34
|
+
unitScale,
|
|
35
|
+
origin,
|
|
36
|
+
bounds: bounds ?? emptyBounds(),
|
|
37
|
+
visualAssetId: typeof raw.visualAssetId === "string" ? raw.visualAssetId : undefined,
|
|
38
|
+
contentHash: typeof raw.contentHash === "string" ? raw.contentHash : undefined,
|
|
39
|
+
spawnPoints: parseSpawnPoints(raw.spawnPoints, issues),
|
|
40
|
+
};
|
|
41
|
+
if (artifact.representation === "aabb") {
|
|
42
|
+
artifact.aabbs = parseAabbs(raw.aabbs, issues);
|
|
43
|
+
}
|
|
44
|
+
else if (artifact.representation === "triangle-mesh") {
|
|
45
|
+
artifact.mesh = parseMesh(raw.mesh, issues);
|
|
46
|
+
}
|
|
47
|
+
else if (artifact.representation === "occupancy-grid") {
|
|
48
|
+
artifact.grid = parseGrid(raw.grid, issues);
|
|
49
|
+
}
|
|
50
|
+
else if (artifact.representation === "primitives") {
|
|
51
|
+
artifact.primitives = parsePrimitives(raw.primitives, issues);
|
|
52
|
+
}
|
|
53
|
+
if (issues.length)
|
|
54
|
+
throw new CollisionParseError(issues);
|
|
55
|
+
const check = validateCollisionArtifact(artifact);
|
|
56
|
+
if (!check.ok)
|
|
57
|
+
throw new CollisionParseError(check.errors);
|
|
58
|
+
return artifact;
|
|
59
|
+
}
|
|
60
|
+
export function collisionFromPayload(payload) {
|
|
61
|
+
if (payload.kind !== "json") {
|
|
62
|
+
throw new CollisionParseError(["collision payload must be json"]);
|
|
63
|
+
}
|
|
64
|
+
return parseCollisionArtifact(payload.value);
|
|
65
|
+
}
|
|
66
|
+
export function validateCollisionArtifact(artifact) {
|
|
67
|
+
const errors = [];
|
|
68
|
+
const warnings = [];
|
|
69
|
+
assertFiniteBounds(artifact.bounds, errors);
|
|
70
|
+
if (!Number.isFinite(artifact.unitScale) || artifact.unitScale <= 0)
|
|
71
|
+
errors.push("invalid unitScale");
|
|
72
|
+
for (const n of [artifact.origin.x, artifact.origin.y, artifact.origin.z]) {
|
|
73
|
+
if (!Number.isFinite(n))
|
|
74
|
+
errors.push("origin contains a non-finite value");
|
|
75
|
+
}
|
|
76
|
+
let triangleCount;
|
|
77
|
+
let aabbCount;
|
|
78
|
+
let primitiveCount;
|
|
79
|
+
let occupiedCells;
|
|
80
|
+
if (artifact.representation === "aabb") {
|
|
81
|
+
const list = artifact.aabbs ?? [];
|
|
82
|
+
aabbCount = list.length;
|
|
83
|
+
if (!list.length)
|
|
84
|
+
errors.push("aabb representation requires aabbs");
|
|
85
|
+
for (const box of list) {
|
|
86
|
+
if (box.maxX < box.minX || box.maxZ < box.minZ)
|
|
87
|
+
errors.push(`aabb ${box.id} has inverted xz`);
|
|
88
|
+
if (!allFinite([box.minX, box.maxX, box.minZ, box.maxZ]))
|
|
89
|
+
errors.push(`aabb ${box.id} is non-finite`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (artifact.representation === "triangle-mesh") {
|
|
93
|
+
const mesh = artifact.mesh;
|
|
94
|
+
if (!mesh)
|
|
95
|
+
errors.push("triangle-mesh representation requires mesh");
|
|
96
|
+
else {
|
|
97
|
+
if (mesh.positions.length % 3 !== 0)
|
|
98
|
+
errors.push("positions length must be a multiple of 3");
|
|
99
|
+
if (mesh.indices.length % 3 !== 0)
|
|
100
|
+
errors.push("indices length must be a multiple of 3");
|
|
101
|
+
const verts = mesh.positions.length / 3;
|
|
102
|
+
for (const idx of mesh.indices) {
|
|
103
|
+
if (!Number.isInteger(idx) || idx < 0 || idx >= verts) {
|
|
104
|
+
errors.push("mesh index out of range");
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (!allFinite(mesh.positions))
|
|
109
|
+
errors.push("mesh positions contain a non-finite value");
|
|
110
|
+
triangleCount = Math.floor(mesh.indices.length / 3);
|
|
111
|
+
if (triangleCount === 0)
|
|
112
|
+
errors.push("mesh has no triangles");
|
|
113
|
+
if (triangleCount > 250_000)
|
|
114
|
+
warnings.push("triangle count exceeds 250000");
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if (artifact.representation === "occupancy-grid") {
|
|
118
|
+
const grid = artifact.grid;
|
|
119
|
+
if (!grid)
|
|
120
|
+
errors.push("occupancy-grid representation requires grid");
|
|
121
|
+
else {
|
|
122
|
+
if (grid.width <= 0 || grid.depth <= 0 || grid.cellSize <= 0)
|
|
123
|
+
errors.push("grid dimensions must be positive");
|
|
124
|
+
if (grid.cells.length !== grid.width * grid.depth) {
|
|
125
|
+
errors.push("grid cells length must equal width * depth");
|
|
126
|
+
}
|
|
127
|
+
occupiedCells = grid.cells.reduce((n, v) => n + (v ? 1 : 0), 0);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
if (artifact.representation === "primitives") {
|
|
131
|
+
const list = artifact.primitives ?? [];
|
|
132
|
+
primitiveCount = list.length;
|
|
133
|
+
if (!list.length)
|
|
134
|
+
errors.push("primitives representation requires primitives");
|
|
135
|
+
for (const p of list) {
|
|
136
|
+
if (p.type === "sphere" || p.type === "capsule") {
|
|
137
|
+
if (!(p.radius > 0) || !Number.isFinite(p.radius))
|
|
138
|
+
errors.push(`primitive ${p.id} has invalid radius`);
|
|
139
|
+
}
|
|
140
|
+
if (p.type === "capsule" && (!(p.height > 0) || !Number.isFinite(p.height))) {
|
|
141
|
+
errors.push(`primitive ${p.id} has invalid height`);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return { ok: errors.length === 0, errors, warnings, triangleCount, aabbCount, primitiveCount, occupiedCells };
|
|
146
|
+
}
|
|
147
|
+
/** Flatten any representation into world StaticObstacle-shaped AABBs on XZ. */
|
|
148
|
+
export function collisionToObstacles(artifact) {
|
|
149
|
+
if (artifact.representation === "aabb") {
|
|
150
|
+
return (artifact.aabbs ?? []).map((b) => ({
|
|
151
|
+
id: b.id,
|
|
152
|
+
minX: b.minX,
|
|
153
|
+
maxX: b.maxX,
|
|
154
|
+
minZ: b.minZ,
|
|
155
|
+
maxZ: b.maxZ,
|
|
156
|
+
}));
|
|
157
|
+
}
|
|
158
|
+
if (artifact.representation === "primitives") {
|
|
159
|
+
const out = [];
|
|
160
|
+
for (const p of artifact.primitives ?? []) {
|
|
161
|
+
if (p.type === "box") {
|
|
162
|
+
out.push({ id: p.id, minX: p.minX, maxX: p.maxX, minZ: p.minZ, maxZ: p.maxZ });
|
|
163
|
+
}
|
|
164
|
+
else if (p.type === "sphere") {
|
|
165
|
+
out.push({
|
|
166
|
+
id: p.id,
|
|
167
|
+
minX: p.x - p.radius,
|
|
168
|
+
maxX: p.x + p.radius,
|
|
169
|
+
minZ: p.z - p.radius,
|
|
170
|
+
maxZ: p.z + p.radius,
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
out.push({
|
|
175
|
+
id: p.id,
|
|
176
|
+
minX: p.x - p.radius,
|
|
177
|
+
maxX: p.x + p.radius,
|
|
178
|
+
minZ: p.z - p.radius,
|
|
179
|
+
maxZ: p.z + p.radius,
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return out;
|
|
184
|
+
}
|
|
185
|
+
if (artifact.representation === "occupancy-grid" && artifact.grid) {
|
|
186
|
+
const g = artifact.grid;
|
|
187
|
+
const out = [];
|
|
188
|
+
let id = 1;
|
|
189
|
+
for (let z = 0; z < g.depth; z++) {
|
|
190
|
+
for (let x = 0; x < g.width; x++) {
|
|
191
|
+
if (!g.cells[z * g.width + x])
|
|
192
|
+
continue;
|
|
193
|
+
const minX = g.originX + x * g.cellSize;
|
|
194
|
+
const minZ = g.originZ + z * g.cellSize;
|
|
195
|
+
out.push({ id: id++, minX, maxX: minX + g.cellSize, minZ, maxZ: minZ + g.cellSize });
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return out;
|
|
199
|
+
}
|
|
200
|
+
if (artifact.representation === "triangle-mesh" && artifact.mesh) {
|
|
201
|
+
return trianglesToAabbs(artifact.mesh);
|
|
202
|
+
}
|
|
203
|
+
return [];
|
|
204
|
+
}
|
|
205
|
+
function trianglesToAabbs(mesh) {
|
|
206
|
+
const out = [];
|
|
207
|
+
for (let t = 0; t < mesh.indices.length; t += 3) {
|
|
208
|
+
const ia = mesh.indices[t] * 3;
|
|
209
|
+
const ib = mesh.indices[t + 1] * 3;
|
|
210
|
+
const ic = mesh.indices[t + 2] * 3;
|
|
211
|
+
const xs = [mesh.positions[ia], mesh.positions[ib], mesh.positions[ic]];
|
|
212
|
+
const zs = [mesh.positions[ia + 2], mesh.positions[ib + 2], mesh.positions[ic + 2]];
|
|
213
|
+
out.push({
|
|
214
|
+
id: t / 3 + 1,
|
|
215
|
+
minX: Math.min(...xs),
|
|
216
|
+
maxX: Math.max(...xs),
|
|
217
|
+
minZ: Math.min(...zs),
|
|
218
|
+
maxZ: Math.max(...zs),
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
return out;
|
|
222
|
+
}
|
|
223
|
+
const RENDER_KEYS = new Set([
|
|
224
|
+
"THREE",
|
|
225
|
+
"scene",
|
|
226
|
+
"material",
|
|
227
|
+
"texture",
|
|
228
|
+
"renderer",
|
|
229
|
+
"Object3D",
|
|
230
|
+
"SkinnedMesh",
|
|
231
|
+
"AnimationMixer",
|
|
232
|
+
"gltf",
|
|
233
|
+
"glb",
|
|
234
|
+
]);
|
|
235
|
+
function rejectRenderLeak(raw, issues) {
|
|
236
|
+
for (const key of Object.keys(raw)) {
|
|
237
|
+
if (RENDER_KEYS.has(key))
|
|
238
|
+
issues.push(`render/three field ${key} is forbidden on collision artifacts`);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
function parseSpawnPoints(value, issues) {
|
|
242
|
+
if (value === undefined)
|
|
243
|
+
return undefined;
|
|
244
|
+
if (!Array.isArray(value)) {
|
|
245
|
+
issues.push("spawnPoints must be an array");
|
|
246
|
+
return undefined;
|
|
247
|
+
}
|
|
248
|
+
const out = [];
|
|
249
|
+
value.forEach((item, i) => {
|
|
250
|
+
if (!item || typeof item !== "object") {
|
|
251
|
+
issues.push(`spawnPoints[${i}] invalid`);
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
const raw = item;
|
|
255
|
+
const id = typeof raw.id === "string" ? raw.id : typeof raw.id === "number" ? String(raw.id) : "";
|
|
256
|
+
const x = num(raw.x, `spawnPoints[${i}].x`, issues);
|
|
257
|
+
const z = num(raw.z, `spawnPoints[${i}].z`, issues);
|
|
258
|
+
if (!id || x === undefined || z === undefined) {
|
|
259
|
+
issues.push(`spawnPoints[${i}] requires id, x, z`);
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
out.push({
|
|
263
|
+
id,
|
|
264
|
+
x,
|
|
265
|
+
y: typeof raw.y === "number" ? raw.y : undefined,
|
|
266
|
+
z,
|
|
267
|
+
yaw: typeof raw.yaw === "number" ? raw.yaw : undefined,
|
|
268
|
+
team: typeof raw.team === "string" ? raw.team : undefined,
|
|
269
|
+
priority: typeof raw.priority === "number" ? raw.priority : undefined,
|
|
270
|
+
clearance: typeof raw.clearance === "number" ? raw.clearance : undefined,
|
|
271
|
+
});
|
|
272
|
+
});
|
|
273
|
+
return out;
|
|
274
|
+
}
|
|
275
|
+
function parseBounds(value, issues) {
|
|
276
|
+
if (!value || typeof value !== "object") {
|
|
277
|
+
issues.push("bounds required");
|
|
278
|
+
return undefined;
|
|
279
|
+
}
|
|
280
|
+
const raw = value;
|
|
281
|
+
const bounds = {
|
|
282
|
+
minX: num(raw.minX, "bounds.minX", issues) ?? 0,
|
|
283
|
+
maxX: num(raw.maxX, "bounds.maxX", issues) ?? 0,
|
|
284
|
+
minY: num(raw.minY, "bounds.minY", issues) ?? 0,
|
|
285
|
+
maxY: num(raw.maxY, "bounds.maxY", issues) ?? 0,
|
|
286
|
+
minZ: num(raw.minZ, "bounds.minZ", issues) ?? 0,
|
|
287
|
+
maxZ: num(raw.maxZ, "bounds.maxZ", issues) ?? 0,
|
|
288
|
+
};
|
|
289
|
+
return bounds;
|
|
290
|
+
}
|
|
291
|
+
function parseAabbs(value, issues) {
|
|
292
|
+
if (!Array.isArray(value)) {
|
|
293
|
+
issues.push("aabbs must be an array");
|
|
294
|
+
return [];
|
|
295
|
+
}
|
|
296
|
+
const out = [];
|
|
297
|
+
value.forEach((item, i) => {
|
|
298
|
+
if (!item || typeof item !== "object") {
|
|
299
|
+
issues.push(`aabbs[${i}] invalid`);
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
const raw = item;
|
|
303
|
+
const id = num(raw.id, `aabbs[${i}].id`, issues);
|
|
304
|
+
const minX = num(raw.minX, `aabbs[${i}].minX`, issues);
|
|
305
|
+
const maxX = num(raw.maxX, `aabbs[${i}].maxX`, issues);
|
|
306
|
+
const minZ = num(raw.minZ, `aabbs[${i}].minZ`, issues);
|
|
307
|
+
const maxZ = num(raw.maxZ, `aabbs[${i}].maxZ`, issues);
|
|
308
|
+
if (id === undefined || minX === undefined || maxX === undefined || minZ === undefined || maxZ === undefined)
|
|
309
|
+
return;
|
|
310
|
+
out.push({
|
|
311
|
+
id,
|
|
312
|
+
minX,
|
|
313
|
+
maxX,
|
|
314
|
+
minZ,
|
|
315
|
+
maxZ,
|
|
316
|
+
minY: typeof raw.minY === "number" ? raw.minY : undefined,
|
|
317
|
+
maxY: typeof raw.maxY === "number" ? raw.maxY : undefined,
|
|
318
|
+
});
|
|
319
|
+
});
|
|
320
|
+
return out;
|
|
321
|
+
}
|
|
322
|
+
function parseMesh(value, issues) {
|
|
323
|
+
if (!value || typeof value !== "object") {
|
|
324
|
+
issues.push("mesh required");
|
|
325
|
+
return undefined;
|
|
326
|
+
}
|
|
327
|
+
const raw = value;
|
|
328
|
+
if (!Array.isArray(raw.positions) || !Array.isArray(raw.indices)) {
|
|
329
|
+
issues.push("mesh.positions and mesh.indices required");
|
|
330
|
+
return undefined;
|
|
331
|
+
}
|
|
332
|
+
return {
|
|
333
|
+
positions: raw.positions.map((n, i) => num(n, `mesh.positions[${i}]`, issues) ?? NaN),
|
|
334
|
+
indices: raw.indices.map((n, i) => num(n, `mesh.indices[${i}]`, issues) ?? NaN),
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
function parseGrid(value, issues) {
|
|
338
|
+
if (!value || typeof value !== "object") {
|
|
339
|
+
issues.push("grid required");
|
|
340
|
+
return undefined;
|
|
341
|
+
}
|
|
342
|
+
const raw = value;
|
|
343
|
+
if (!Array.isArray(raw.cells)) {
|
|
344
|
+
issues.push("grid.cells required");
|
|
345
|
+
return undefined;
|
|
346
|
+
}
|
|
347
|
+
return {
|
|
348
|
+
originX: num(raw.originX, "grid.originX", issues) ?? 0,
|
|
349
|
+
originZ: num(raw.originZ, "grid.originZ", issues) ?? 0,
|
|
350
|
+
cellSize: num(raw.cellSize, "grid.cellSize", issues) ?? 1,
|
|
351
|
+
width: num(raw.width, "grid.width", issues) ?? 0,
|
|
352
|
+
depth: num(raw.depth, "grid.depth", issues) ?? 0,
|
|
353
|
+
cells: raw.cells.map((n, i) => num(n, `grid.cells[${i}]`, issues) ?? 0),
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
function parsePrimitives(value, issues) {
|
|
357
|
+
if (!Array.isArray(value)) {
|
|
358
|
+
issues.push("primitives must be an array");
|
|
359
|
+
return [];
|
|
360
|
+
}
|
|
361
|
+
const out = [];
|
|
362
|
+
value.forEach((item, i) => {
|
|
363
|
+
if (!item || typeof item !== "object") {
|
|
364
|
+
issues.push(`primitives[${i}] invalid`);
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
const raw = item;
|
|
368
|
+
const type = raw.type;
|
|
369
|
+
const id = num(raw.id, `primitives[${i}].id`, issues);
|
|
370
|
+
if (id === undefined)
|
|
371
|
+
return;
|
|
372
|
+
if (type === "box") {
|
|
373
|
+
const minX = num(raw.minX, `primitives[${i}].minX`, issues);
|
|
374
|
+
const maxX = num(raw.maxX, `primitives[${i}].maxX`, issues);
|
|
375
|
+
const minZ = num(raw.minZ, `primitives[${i}].minZ`, issues);
|
|
376
|
+
const maxZ = num(raw.maxZ, `primitives[${i}].maxZ`, issues);
|
|
377
|
+
if (minX === undefined || maxX === undefined || minZ === undefined || maxZ === undefined)
|
|
378
|
+
return;
|
|
379
|
+
out.push({ type: "box", id, minX, maxX, minZ, maxZ });
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
if (type === "sphere" || type === "capsule") {
|
|
383
|
+
const x = num(raw.x, `primitives[${i}].x`, issues);
|
|
384
|
+
const z = num(raw.z, `primitives[${i}].z`, issues);
|
|
385
|
+
const radius = num(raw.radius, `primitives[${i}].radius`, issues);
|
|
386
|
+
if (x === undefined || z === undefined || radius === undefined)
|
|
387
|
+
return;
|
|
388
|
+
if (type === "capsule") {
|
|
389
|
+
const height = num(raw.height, `primitives[${i}].height`, issues);
|
|
390
|
+
if (height === undefined)
|
|
391
|
+
return;
|
|
392
|
+
out.push({ type: "capsule", id, x, y: typeof raw.y === "number" ? raw.y : undefined, z, radius, height });
|
|
393
|
+
}
|
|
394
|
+
else {
|
|
395
|
+
out.push({ type: "sphere", id, x, y: typeof raw.y === "number" ? raw.y : undefined, z, radius });
|
|
396
|
+
}
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
issues.push(`primitives[${i}] unknown type`);
|
|
400
|
+
});
|
|
401
|
+
return out;
|
|
402
|
+
}
|
|
403
|
+
function vec3(value, path, issues) {
|
|
404
|
+
if (!value || typeof value !== "object") {
|
|
405
|
+
issues.push(`${path} required`);
|
|
406
|
+
return undefined;
|
|
407
|
+
}
|
|
408
|
+
const raw = value;
|
|
409
|
+
const x = num(raw.x, `${path}.x`, issues);
|
|
410
|
+
const y = num(raw.y, `${path}.y`, issues);
|
|
411
|
+
const z = num(raw.z, `${path}.z`, issues);
|
|
412
|
+
if (x === undefined || y === undefined || z === undefined)
|
|
413
|
+
return undefined;
|
|
414
|
+
return { x, y, z };
|
|
415
|
+
}
|
|
416
|
+
function num(value, path, issues) {
|
|
417
|
+
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
418
|
+
issues.push(`${path} must be a finite number`);
|
|
419
|
+
return undefined;
|
|
420
|
+
}
|
|
421
|
+
return value;
|
|
422
|
+
}
|
|
423
|
+
function allFinite(values) {
|
|
424
|
+
return values.every((n) => Number.isFinite(n));
|
|
425
|
+
}
|
|
426
|
+
function assertFiniteBounds(bounds, errors) {
|
|
427
|
+
for (const [k, v] of Object.entries(bounds)) {
|
|
428
|
+
if (!Number.isFinite(v))
|
|
429
|
+
errors.push(`bounds.${k} is non-finite`);
|
|
430
|
+
}
|
|
431
|
+
if (bounds.maxX < bounds.minX || bounds.maxZ < bounds.minZ)
|
|
432
|
+
errors.push("bounds are inverted on xz");
|
|
433
|
+
}
|
|
434
|
+
function emptyBounds() {
|
|
435
|
+
return { minX: 0, maxX: 0, minY: 0, maxY: 0, minZ: 0, maxZ: 0 };
|
|
436
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { AssetRuntime, AssetHandle, RequestOptions } from "./loader.js";
|
|
2
|
+
import type { AssetAdmissionQueue } from "./admission.js";
|
|
3
|
+
import { collisionFromPayload } from "./collision.js";
|
|
4
|
+
import { type GlbValidationReport } from "./glb.js";
|
|
5
|
+
import type { AssetEntry, AssetId, AssetMetrics, AssetSnapshot, LoadedPayload, RequestId } from "./types.js";
|
|
6
|
+
export type AssetWork = {
|
|
7
|
+
assetId: AssetId;
|
|
8
|
+
options?: RequestOptions;
|
|
9
|
+
entry?: AssetEntry;
|
|
10
|
+
handle?: AssetHandle;
|
|
11
|
+
payload?: LoadedPayload;
|
|
12
|
+
snapshot?: AssetSnapshot;
|
|
13
|
+
glb?: GlbValidationReport;
|
|
14
|
+
collision?: ReturnType<typeof collisionFromPayload>;
|
|
15
|
+
error?: {
|
|
16
|
+
code: string;
|
|
17
|
+
message: string;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
export type AssetStage = {
|
|
21
|
+
id: string;
|
|
22
|
+
handler: (work: AssetWork, signal?: AbortSignal) => AssetWork | Promise<AssetWork>;
|
|
23
|
+
};
|
|
24
|
+
export type AssetPipelineSinks = {
|
|
25
|
+
onPresentationReady?: (work: AssetWork) => void;
|
|
26
|
+
onAuthoritativeReady?: (work: AssetWork) => void;
|
|
27
|
+
onAuthoritativeStaged?: (work: AssetWork) => void;
|
|
28
|
+
onProvenance?: (work: AssetWork) => void;
|
|
29
|
+
onMetrics?: (metrics: AssetMetrics) => void;
|
|
30
|
+
onFailure?: (work: AssetWork) => void;
|
|
31
|
+
};
|
|
32
|
+
export type AssetPipelineOptions = {
|
|
33
|
+
runtime: AssetRuntime;
|
|
34
|
+
admission?: AssetAdmissionQueue;
|
|
35
|
+
sinks?: AssetPipelineSinks;
|
|
36
|
+
sniffGlb?: boolean;
|
|
37
|
+
};
|
|
38
|
+
/** Named stages compatible with conveyor-graph-compose SimpleStage / ConveyorStage.handler. */
|
|
39
|
+
export declare function createAssetStages(opts: AssetPipelineOptions): AssetStage[];
|
|
40
|
+
export declare function runAssetPipeline(stages: AssetStage[], work: AssetWork, signal?: AbortSignal): Promise<AssetWork>;
|
|
41
|
+
export declare function loadThroughPipeline(opts: AssetPipelineOptions, assetId: AssetId, options?: RequestOptions): Promise<AssetWork>;
|
|
42
|
+
/** Stage list for `buildConveyorPipeline({ stages })` without importing compose. */
|
|
43
|
+
export declare function assetComposeStageSpecs(opts: AssetPipelineOptions): Array<{
|
|
44
|
+
id: string;
|
|
45
|
+
handler: AssetStage["handler"];
|
|
46
|
+
}>;
|
|
47
|
+
export declare function staticCompatibilityOf(runtime: AssetRuntime): import("./types.js").AuthoritativeCompatibility;
|
|
48
|
+
export type { RequestId };
|
package/dist/compose.js
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { collectAuthoritativeCompatibility } from "./schema.js";
|
|
2
|
+
import { collisionFromPayload } from "./collision.js";
|
|
3
|
+
import { sniffGlb } from "./glb.js";
|
|
4
|
+
/** Named stages compatible with conveyor-graph-compose SimpleStage / ConveyorStage.handler. */
|
|
5
|
+
export function createAssetStages(opts) {
|
|
6
|
+
const { runtime, admission, sinks } = opts;
|
|
7
|
+
const shouldSniff = opts.sniffGlb !== false;
|
|
8
|
+
const resolveManifest = {
|
|
9
|
+
id: "resolve-manifest",
|
|
10
|
+
handler: (work) => {
|
|
11
|
+
const entry = runtime.getEntry(work.assetId);
|
|
12
|
+
if (!entry) {
|
|
13
|
+
return {
|
|
14
|
+
...work,
|
|
15
|
+
error: { code: "not-found", message: `unknown asset ${work.assetId}` },
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
return { ...work, entry };
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
const resolveDeps = {
|
|
22
|
+
id: "resolve-deps",
|
|
23
|
+
handler: async (work, signal) => {
|
|
24
|
+
if (work.error || !work.entry)
|
|
25
|
+
return work;
|
|
26
|
+
for (const dep of work.entry.dependencies ?? []) {
|
|
27
|
+
const child = runtime.getEntry(dep.id);
|
|
28
|
+
if (!child) {
|
|
29
|
+
return { ...work, error: { code: "dependency", message: `missing dependency ${dep.id}` } };
|
|
30
|
+
}
|
|
31
|
+
if (dep.version && child.version !== dep.version) {
|
|
32
|
+
return { ...work, error: { code: "dependency", message: `dependency version mismatch ${dep.id}` } };
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
void signal;
|
|
36
|
+
return work;
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
const acquireVerifyDecodeOwn = {
|
|
40
|
+
id: "acquire-verify-decode-own",
|
|
41
|
+
handler: async (work, signal) => {
|
|
42
|
+
if (work.error || !work.entry)
|
|
43
|
+
return work;
|
|
44
|
+
try {
|
|
45
|
+
const handle = await runtime.request(work.assetId, {
|
|
46
|
+
...work.options,
|
|
47
|
+
signal: signal ?? work.options?.signal,
|
|
48
|
+
});
|
|
49
|
+
const next = {
|
|
50
|
+
...work,
|
|
51
|
+
handle,
|
|
52
|
+
payload: handle.payload,
|
|
53
|
+
snapshot: handle.snapshot(),
|
|
54
|
+
};
|
|
55
|
+
if (shouldSniff && handle.payload?.kind === "bytes" && isVisual(work.entry.authority, work.entry.kind)) {
|
|
56
|
+
try {
|
|
57
|
+
next.glb = sniffGlb(handle.payload.bytes);
|
|
58
|
+
}
|
|
59
|
+
catch (err) {
|
|
60
|
+
next.glb = {
|
|
61
|
+
ok: false,
|
|
62
|
+
support: "unsupported",
|
|
63
|
+
jsonByteLength: 0,
|
|
64
|
+
binByteLength: 0,
|
|
65
|
+
sceneCount: 0,
|
|
66
|
+
nodeCount: 0,
|
|
67
|
+
meshCount: 0,
|
|
68
|
+
primitiveCount: 0,
|
|
69
|
+
materialCount: 0,
|
|
70
|
+
textureCount: 0,
|
|
71
|
+
imageCount: 0,
|
|
72
|
+
bufferCount: 0,
|
|
73
|
+
bufferViewCount: 0,
|
|
74
|
+
accessorCount: 0,
|
|
75
|
+
skinCount: 0,
|
|
76
|
+
animationCount: 0,
|
|
77
|
+
animationNames: [],
|
|
78
|
+
jointCount: 0,
|
|
79
|
+
extensionsUsed: [],
|
|
80
|
+
extensionsRequired: [],
|
|
81
|
+
unsupportedRequired: [],
|
|
82
|
+
scenes: [],
|
|
83
|
+
nodes: [],
|
|
84
|
+
finiteTransforms: true,
|
|
85
|
+
errors: [err.message],
|
|
86
|
+
warnings: [],
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
if (handle.payload && work.entry.kind === "static-collision-scene") {
|
|
91
|
+
try {
|
|
92
|
+
next.collision = collisionFromPayload(handle.payload);
|
|
93
|
+
}
|
|
94
|
+
catch (err) {
|
|
95
|
+
return { ...next, error: { code: "decode", message: err.message } };
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return next;
|
|
99
|
+
}
|
|
100
|
+
catch (err) {
|
|
101
|
+
return {
|
|
102
|
+
...work,
|
|
103
|
+
error: { code: codeOf(err), message: err instanceof Error ? err.message : String(err) },
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
const sink = {
|
|
109
|
+
id: "sink",
|
|
110
|
+
handler: async (work) => {
|
|
111
|
+
sinks?.onMetrics?.(runtime.metrics);
|
|
112
|
+
if (work.error) {
|
|
113
|
+
sinks?.onFailure?.(work);
|
|
114
|
+
return work;
|
|
115
|
+
}
|
|
116
|
+
if (work.entry?.provenance)
|
|
117
|
+
sinks?.onProvenance?.(work);
|
|
118
|
+
if (work.entry && isPresentation(work.entry.authority)) {
|
|
119
|
+
sinks?.onPresentationReady?.(work);
|
|
120
|
+
}
|
|
121
|
+
if (work.entry && isStaticAuth(work.entry.authority)) {
|
|
122
|
+
sinks?.onAuthoritativeReady?.(work);
|
|
123
|
+
}
|
|
124
|
+
if (admission && work.entry && work.entry.authority === "authoritative-admitted") {
|
|
125
|
+
await admission.stage(work.assetId, work.options);
|
|
126
|
+
sinks?.onAuthoritativeStaged?.(work);
|
|
127
|
+
}
|
|
128
|
+
return work;
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
return [resolveManifest, resolveDeps, acquireVerifyDecodeOwn, sink];
|
|
132
|
+
}
|
|
133
|
+
export async function runAssetPipeline(stages, work, signal) {
|
|
134
|
+
let current = work;
|
|
135
|
+
for (const stage of stages) {
|
|
136
|
+
if (signal?.aborted) {
|
|
137
|
+
return { ...current, error: { code: "cancelled", message: "pipeline aborted" } };
|
|
138
|
+
}
|
|
139
|
+
try {
|
|
140
|
+
current = await stage.handler(current, signal);
|
|
141
|
+
}
|
|
142
|
+
catch (err) {
|
|
143
|
+
const code = err && typeof err === "object" && "code" in err ? String(err.code) : "provider";
|
|
144
|
+
return { ...current, error: { code, message: err instanceof Error ? err.message : String(err) } };
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return current;
|
|
148
|
+
}
|
|
149
|
+
export function loadThroughPipeline(opts, assetId, options) {
|
|
150
|
+
return runAssetPipeline(createAssetStages(opts), { assetId, options }, options?.signal);
|
|
151
|
+
}
|
|
152
|
+
/** Stage list for `buildConveyorPipeline({ stages })` without importing compose. */
|
|
153
|
+
export function assetComposeStageSpecs(opts) {
|
|
154
|
+
return createAssetStages(opts).map((s) => ({ id: s.id, handler: s.handler }));
|
|
155
|
+
}
|
|
156
|
+
export function staticCompatibilityOf(runtime) {
|
|
157
|
+
return collectAuthoritativeCompatibility(runtime.manifest);
|
|
158
|
+
}
|
|
159
|
+
function isPresentation(authority) {
|
|
160
|
+
return authority === "presentation" || authority === "presentation-required";
|
|
161
|
+
}
|
|
162
|
+
function isStaticAuth(authority) {
|
|
163
|
+
return authority === "authoritative" || authority === "authoritative-static";
|
|
164
|
+
}
|
|
165
|
+
function isVisual(authority, kind) {
|
|
166
|
+
return (isPresentation(authority) &&
|
|
167
|
+
(kind === "static-visual-scene" || kind === "renderable-model" || kind === "skinned-character-model"));
|
|
168
|
+
}
|
|
169
|
+
function codeOf(err) {
|
|
170
|
+
if (err && typeof err === "object" && "code" in err && typeof err.code === "string") {
|
|
171
|
+
return err.code;
|
|
172
|
+
}
|
|
173
|
+
return "provider";
|
|
174
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { type AcquisitionContext, type AcquisitionProvider, type ByteSource } from "./acquisition.js";
|
|
2
|
+
export type FixtureOutcome = {
|
|
3
|
+
type: "success";
|
|
4
|
+
bytes: Uint8Array;
|
|
5
|
+
contentType?: string;
|
|
6
|
+
} | {
|
|
7
|
+
type: "failure";
|
|
8
|
+
code: string;
|
|
9
|
+
message: string;
|
|
10
|
+
} | {
|
|
11
|
+
type: "cancel";
|
|
12
|
+
};
|
|
13
|
+
export type FixtureScript = {
|
|
14
|
+
delayMs?: number;
|
|
15
|
+
bytes?: Uint8Array | string;
|
|
16
|
+
contentType?: string;
|
|
17
|
+
fail?: {
|
|
18
|
+
code: string;
|
|
19
|
+
message: string;
|
|
20
|
+
};
|
|
21
|
+
cancel?: boolean;
|
|
22
|
+
/** Wait out delay without aborting, then reject with `stale` if the caller already aborted. */
|
|
23
|
+
completeAfterAbort?: boolean;
|
|
24
|
+
};
|
|
25
|
+
export declare class MemoryAcquisitionProvider implements AcquisitionProvider {
|
|
26
|
+
readonly id = "memory";
|
|
27
|
+
private readonly scripts;
|
|
28
|
+
register(uri: string, script: FixtureScript | Uint8Array | string): this;
|
|
29
|
+
has(uri: string): boolean;
|
|
30
|
+
accepts(uri: string): boolean;
|
|
31
|
+
acquire(ctx: AcquisitionContext): Promise<ByteSource>;
|
|
32
|
+
}
|
|
33
|
+
export declare class FixtureRegistry {
|
|
34
|
+
readonly provider: MemoryAcquisitionProvider;
|
|
35
|
+
put(uri: string, script: FixtureScript | Uint8Array | string): this;
|
|
36
|
+
success(uri: string, bytes: Uint8Array | string, extra?: Omit<FixtureScript, "bytes" | "fail" | "cancel">): this;
|
|
37
|
+
fail(uri: string, code: string, message: string, extra?: Pick<FixtureScript, "delayMs">): this;
|
|
38
|
+
delay(uri: string, bytes: Uint8Array | string, delayMs: number): this;
|
|
39
|
+
}
|