@margoengine/loaders 0.0.0-stage → 1.0.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 +201 -0
- package/NOTICE +4 -0
- package/README.md +1665 -2
- package/dist/3dm.d.ts +161 -0
- package/dist/3dm.js +289 -0
- package/dist/3ds.d.ts +124 -0
- package/dist/3ds.js +392 -0
- package/dist/acclaim.d.ts +90 -0
- package/dist/acclaim.js +312 -0
- package/dist/amf.d.ts +45 -0
- package/dist/amf.js +256 -0
- package/dist/babylon.d.ts +197 -0
- package/dist/babylon.js +701 -0
- package/dist/common.d.ts +7 -0
- package/dist/common.js +6 -0
- package/dist/crease.d.ts +7 -0
- package/dist/crease.js +67 -0
- package/dist/dae-animation.d.ts +66 -0
- package/dist/dae-animation.js +266 -0
- package/dist/dae-common.d.ts +95 -0
- package/dist/dae-common.js +226 -0
- package/dist/dae-controllers.d.ts +57 -0
- package/dist/dae-controllers.js +164 -0
- package/dist/dae-kinematics.d.ts +65 -0
- package/dist/dae-kinematics.js +461 -0
- package/dist/dae-pose.d.ts +131 -0
- package/dist/dae-pose.js +381 -0
- package/dist/dae-scene.d.ts +203 -0
- package/dist/dae-scene.js +279 -0
- package/dist/dae.d.ts +166 -0
- package/dist/dae.js +644 -0
- package/dist/detect.d.ts +20 -0
- package/dist/detect.js +72 -0
- package/dist/dxf-hatch.d.ts +8 -0
- package/dist/dxf-hatch.js +346 -0
- package/dist/dxf-leaders.d.ts +59 -0
- package/dist/dxf-leaders.js +258 -0
- package/dist/dxf.d.ts +122 -0
- package/dist/dxf.js +985 -0
- package/dist/engine.d.ts +41 -0
- package/dist/engine.js +120 -0
- package/dist/font-opentype.d.ts +14 -0
- package/dist/font-opentype.js +33 -0
- package/dist/font-woff.d.ts +23 -0
- package/dist/font-woff.js +293 -0
- package/dist/font.d.ts +41 -0
- package/dist/font.js +26 -0
- package/dist/gcode.d.ts +92 -0
- package/dist/gcode.js +369 -0
- package/dist/geo-model.d.ts +84 -0
- package/dist/geo-model.js +220 -0
- package/dist/geo-mvt.d.ts +15 -0
- package/dist/geo-mvt.js +302 -0
- package/dist/geo-shp.d.ts +15 -0
- package/dist/geo-shp.js +234 -0
- package/dist/geo-xml.d.ts +6 -0
- package/dist/geo-xml.js +165 -0
- package/dist/geo.d.ts +8 -0
- package/dist/geo.js +59 -0
- package/dist/geometry.d.ts +60 -0
- package/dist/geometry.js +273 -0
- package/dist/index.d.ts +54 -0
- package/dist/index.js +28 -0
- package/dist/kmz.d.ts +78 -0
- package/dist/kmz.js +173 -0
- package/dist/las.d.ts +92 -0
- package/dist/las.js +110 -0
- package/dist/ldraw.d.ts +118 -0
- package/dist/ldraw.js +474 -0
- package/dist/load.d.ts +72 -0
- package/dist/load.js +205 -0
- package/dist/lottie.d.ts +154 -0
- package/dist/lottie.js +893 -0
- package/dist/lwo.d.ts +81 -0
- package/dist/lwo.js +454 -0
- package/dist/lzf.d.ts +13 -0
- package/dist/lzf.js +56 -0
- package/dist/materialx.d.ts +120 -0
- package/dist/materialx.js +563 -0
- package/dist/md2.d.ts +52 -0
- package/dist/md2.js +112 -0
- package/dist/mmcif.d.ts +14 -0
- package/dist/mmcif.js +246 -0
- package/dist/mmd.d.ts +244 -0
- package/dist/mmd.js +958 -0
- package/dist/mocap.d.ts +71 -0
- package/dist/mocap.js +178 -0
- package/dist/molecule-common.d.ts +4 -0
- package/dist/molecule-common.js +54 -0
- package/dist/mtl.d.ts +125 -0
- package/dist/mtl.js +550 -0
- package/dist/normals.d.ts +7 -0
- package/dist/normals.js +31 -0
- package/dist/nrrd.d.ts +70 -0
- package/dist/nrrd.js +785 -0
- package/dist/nurbs.d.ts +118 -0
- package/dist/nurbs.js +265 -0
- package/dist/obj.d.ts +145 -0
- package/dist/obj.js +462 -0
- package/dist/off.d.ts +10 -0
- package/dist/off.js +155 -0
- package/dist/outline-font.d.ts +87 -0
- package/dist/outline-font.js +110 -0
- package/dist/outline.d.ts +20 -0
- package/dist/outline.js +215 -0
- package/dist/pcd.d.ts +34 -0
- package/dist/pcd.js +258 -0
- package/dist/pdb.d.ts +67 -0
- package/dist/pdb.js +126 -0
- package/dist/ply.d.ts +35 -0
- package/dist/ply.js +392 -0
- package/dist/polygon.d.ts +9 -0
- package/dist/polygon.js +49 -0
- package/dist/prwm.d.ts +24 -0
- package/dist/prwm.js +114 -0
- package/dist/render.d.ts +32 -0
- package/dist/render.js +181 -0
- package/dist/sfnt-cff.d.ts +37 -0
- package/dist/sfnt-cff.js +749 -0
- package/dist/sfnt-layout.d.ts +24 -0
- package/dist/sfnt-layout.js +266 -0
- package/dist/sfnt-reader.d.ts +28 -0
- package/dist/sfnt-reader.js +87 -0
- package/dist/sfnt-variation.d.ts +40 -0
- package/dist/sfnt-variation.js +318 -0
- package/dist/shared/asset-url.js +82 -0
- package/dist/shared/crc32.d.ts +2 -0
- package/dist/shared/crc32.js +47 -0
- package/dist/shared/errors.js +36 -0
- package/dist/shared/inflate.js +284 -0
- package/dist/shared/normals.js +26 -0
- package/dist/shared/scene-scope.js +56 -0
- package/dist/shared/trailing-data.js +45 -0
- package/dist/stl.d.ts +64 -0
- package/dist/stl.js +309 -0
- package/dist/svg.d.ts +90 -0
- package/dist/svg.js +1007 -0
- package/dist/tds.d.ts +2 -0
- package/dist/tds.js +2 -0
- package/dist/textures.d.ts +42 -0
- package/dist/textures.js +53 -0
- package/dist/threemf-beam-reader.d.ts +19 -0
- package/dist/threemf-beam-reader.js +165 -0
- package/dist/threemf-beams.d.ts +52 -0
- package/dist/threemf-beams.js +268 -0
- package/dist/threemf-boolean-read.d.ts +10 -0
- package/dist/threemf-boolean-read.js +113 -0
- package/dist/threemf-boolean.d.ts +59 -0
- package/dist/threemf-boolean.js +20 -0
- package/dist/threemf-csg.d.ts +14 -0
- package/dist/threemf-csg.js +193 -0
- package/dist/threemf-displacement-read.d.ts +8 -0
- package/dist/threemf-displacement-read.js +213 -0
- package/dist/threemf-displacement.d.ts +71 -0
- package/dist/threemf-displacement.js +18 -0
- package/dist/threemf-evaluate.d.ts +36 -0
- package/dist/threemf-evaluate.js +86 -0
- package/dist/threemf-functions.d.ts +33 -0
- package/dist/threemf-functions.js +359 -0
- package/dist/threemf-lattice-mesh.d.ts +9 -0
- package/dist/threemf-lattice-mesh.js +115 -0
- package/dist/threemf-lattice.d.ts +139 -0
- package/dist/threemf-lattice.js +379 -0
- package/dist/threemf-levelset.d.ts +23 -0
- package/dist/threemf-levelset.js +105 -0
- package/dist/threemf-materials.d.ts +162 -0
- package/dist/threemf-materials.js +497 -0
- package/dist/threemf-mesh.d.ts +75 -0
- package/dist/threemf-mesh.js +422 -0
- package/dist/threemf-secure.d.ts +50 -0
- package/dist/threemf-secure.js +153 -0
- package/dist/threemf-tessellate.d.ts +37 -0
- package/dist/threemf-tessellate.js +277 -0
- package/dist/threemf-volume-colour.d.ts +25 -0
- package/dist/threemf-volume-colour.js +233 -0
- package/dist/threemf-volume-sample.d.ts +8 -0
- package/dist/threemf-volume-sample.js +65 -0
- package/dist/threemf-volumetric-read.d.ts +15 -0
- package/dist/threemf-volumetric-read.js +539 -0
- package/dist/threemf-volumetric.d.ts +177 -0
- package/dist/threemf-volumetric.js +28 -0
- package/dist/threemf.d.ts +170 -0
- package/dist/threemf.js +609 -0
- package/dist/tilt.d.ts +62 -0
- package/dist/tilt.js +186 -0
- package/dist/tmx.d.ts +151 -0
- package/dist/tmx.js +394 -0
- package/dist/ttf.d.ts +78 -0
- package/dist/ttf.js +568 -0
- package/dist/types.d.ts +108 -0
- package/dist/types.js +35 -0
- package/dist/usd.d.ts +2 -0
- package/dist/usd.js +2 -0
- package/dist/usd.worker.d.ts +1 -0
- package/dist/usdz.d.ts +50 -0
- package/dist/usdz.js +646 -0
- package/dist/util.d.ts +75 -0
- package/dist/util.js +241 -0
- package/dist/vdb.d.ts +111 -0
- package/dist/vdb.js +663 -0
- package/dist/vrml.d.ts +53 -0
- package/dist/vrml.js +532 -0
- package/dist/vtk-cells.d.ts +29 -0
- package/dist/vtk-cells.js +211 -0
- package/dist/vtk-legacy.d.ts +57 -0
- package/dist/vtk-legacy.js +439 -0
- package/dist/vtk-reader.d.ts +19 -0
- package/dist/vtk-reader.js +134 -0
- package/dist/vtk-xml.d.ts +96 -0
- package/dist/vtk-xml.js +578 -0
- package/dist/vtk.d.ts +13 -0
- package/dist/vtk.js +234 -0
- package/dist/woff-glyf.d.ts +17 -0
- package/dist/woff-glyf.js +215 -0
- package/dist/worker.d.ts +10 -0
- package/dist/worker.js +93 -0
- package/dist/workers/usd.js +3 -0
- package/dist/xfile.d.ts +99 -0
- package/dist/xfile.js +706 -0
- package/dist/xml.d.ts +107 -0
- package/dist/xml.js +843 -0
- package/dist/xyz.d.ts +16 -0
- package/dist/xyz.js +110 -0
- package/dist/zip.d.ts +38 -0
- package/dist/zip.js +150 -0
- package/package.json +289 -4
package/dist/usdz.js
ADDED
|
@@ -0,0 +1,646 @@
|
|
|
1
|
+
import { LoaderError } from './types.js';
|
|
2
|
+
import { limits, throwIfAborted, toBytes } from './util.js';
|
|
3
|
+
import { readZipDirectory, readZipEntry } from './zip.js';
|
|
4
|
+
import { computeVertexNormals } from './normals.js';
|
|
5
|
+
import { invertMatrix, multiplyMatrices } from '@margoengine/math/mat4';
|
|
6
|
+
import { LinearToSRGB } from '@margoengine/math/color';
|
|
7
|
+
const F = 'USD';
|
|
8
|
+
function fail(code, message) {
|
|
9
|
+
throw new LoaderError(F, code, message);
|
|
10
|
+
}
|
|
11
|
+
/** Single-letter string escapes; any other escaped character stands for itself. */
|
|
12
|
+
const ESCAPES = { a: '\x07', b: '\b', f: '\f', n: '\n', r: '\r', t: '\t', v: '\v' };
|
|
13
|
+
const identity = () => new Float64Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
|
|
14
|
+
function numbers(value, size) {
|
|
15
|
+
if (!Array.isArray(value) ||
|
|
16
|
+
value.some((v) => typeof v !== 'number' || !Number.isFinite(v)) ||
|
|
17
|
+
(size !== undefined && value.length !== size))
|
|
18
|
+
fail('syntax', 'expected a numeric tuple or array');
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
function colorValues(values) {
|
|
22
|
+
if (values.some((v) => !Number.isFinite(v) || v < 0 || v > 1))
|
|
23
|
+
fail('range', 'invalid color or opacity primvar values');
|
|
24
|
+
return values;
|
|
25
|
+
}
|
|
26
|
+
/** Resolve only point-addressable interpolation; face-varying data needs vertex splitting and is rejected. */
|
|
27
|
+
function colorPrimvar(node, name, width, count, fallback) {
|
|
28
|
+
const key = `primvars:${name}`, data = node.attrs.get(key);
|
|
29
|
+
if (data === undefined)
|
|
30
|
+
return { values: colorValues(fallback), varying: false };
|
|
31
|
+
const metadata = node.properties.get(key), interpolation = metadata?.get('interpolation') ?? 'constant';
|
|
32
|
+
if (interpolation !== 'constant' && interpolation !== 'vertex')
|
|
33
|
+
fail('unsupported', `unsupported color primvar interpolation ${interpolation}`);
|
|
34
|
+
if ((metadata?.get('elementSize') ?? 1) !== 1)
|
|
35
|
+
fail('unsupported', 'color primvar elementSize must be 1');
|
|
36
|
+
if (!Array.isArray(data))
|
|
37
|
+
fail('syntax', 'color primvar must be an array');
|
|
38
|
+
const values = width === 1 ? numbers(data) : data.flatMap((value) => numbers(value, width));
|
|
39
|
+
colorValues(values);
|
|
40
|
+
const indices = node.attrs.get(`${key}:indices`), indexed = indices === undefined ? undefined : numbers(indices), expected = interpolation === 'vertex' ? count : 1;
|
|
41
|
+
if ((indexed?.length ?? values.length / width) !== expected)
|
|
42
|
+
fail('syntax', 'color primvar length does not match interpolation');
|
|
43
|
+
if (indexed) {
|
|
44
|
+
const resolved = [];
|
|
45
|
+
for (const index of indexed) {
|
|
46
|
+
if (!Number.isSafeInteger(index) || index < 0 || index >= values.length / width)
|
|
47
|
+
fail('range', 'color primvar index outside values');
|
|
48
|
+
for (let c = 0; c < width; c++)
|
|
49
|
+
resolved.push(values[index * width + c]);
|
|
50
|
+
}
|
|
51
|
+
return { values: resolved, varying: interpolation === 'vertex' };
|
|
52
|
+
}
|
|
53
|
+
return { values, varying: interpolation === 'vertex' };
|
|
54
|
+
}
|
|
55
|
+
function shaderColor(node, shader, input, width, count, prims, fallback) {
|
|
56
|
+
const connection = shader.attrs.get(`inputs:${input}.connect`);
|
|
57
|
+
if (connection === undefined) {
|
|
58
|
+
const value = shader.attrs.get(`inputs:${input}`);
|
|
59
|
+
return {
|
|
60
|
+
values: colorValues(value === undefined ? fallback : width === 1 && typeof value === 'number' ? [value] : numbers(value, width)),
|
|
61
|
+
varying: false,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
if (!connection ||
|
|
65
|
+
typeof connection !== 'object' ||
|
|
66
|
+
Array.isArray(connection) ||
|
|
67
|
+
!('path' in connection) ||
|
|
68
|
+
!connection.path.endsWith('.outputs:result'))
|
|
69
|
+
fail('unsupported', 'color connections require a UsdPrimvarReader result');
|
|
70
|
+
const reader = prims.get(connection.path.slice(0, -'.outputs:result'.length)), name = reader?.attrs.get('inputs:varname');
|
|
71
|
+
if (!reader ||
|
|
72
|
+
reader.attrs.get('info:id') !== `UsdPrimvarReader_float${width === 1 ? '' : width}` ||
|
|
73
|
+
typeof name !== 'string' ||
|
|
74
|
+
reader.attrs.has('inputs:varname.connect') ||
|
|
75
|
+
reader.attrs.has('inputs:fallback.connect'))
|
|
76
|
+
fail('unsupported', 'color connections require an unconnected matching UsdPrimvarReader');
|
|
77
|
+
const value = reader.attrs.get('inputs:fallback'), defaultValue = value === undefined
|
|
78
|
+
? Array(width).fill(0)
|
|
79
|
+
: width === 1 && typeof value === 'number'
|
|
80
|
+
? [value]
|
|
81
|
+
: numbers(value, width);
|
|
82
|
+
return colorPrimvar(node, name, width, count, defaultValue);
|
|
83
|
+
}
|
|
84
|
+
function transform(prim, parent, base) {
|
|
85
|
+
const order = prim.attrs.get('xformOpOrder');
|
|
86
|
+
if (order === undefined) {
|
|
87
|
+
if ([...prim.attrs.keys()].some((key) => key.startsWith('xformOp:')))
|
|
88
|
+
fail('unsupported', 'transforms require xformOpOrder');
|
|
89
|
+
return parent;
|
|
90
|
+
}
|
|
91
|
+
if (!Array.isArray(order) || order.some((v) => typeof v !== 'string'))
|
|
92
|
+
fail('syntax', 'invalid xformOpOrder');
|
|
93
|
+
// Everything up to the last reset is ignored, by the UsdGeomXformable schema.
|
|
94
|
+
const reset = order.lastIndexOf('!resetXformStack!'), inherited = reset < 0 ? parent : base;
|
|
95
|
+
let local = identity();
|
|
96
|
+
for (const entry of order.slice(reset + 1)) {
|
|
97
|
+
// UsdGeomXformOp: an op attribute is xformOp:<type>[:<suffix>]; a "!invert!" entry applies that op's inverse.
|
|
98
|
+
const invert = entry.startsWith('!invert!'), name = invert ? entry.slice(8) : entry, type = name.split(':')[1] ?? '', value = prim.attrs.get(name);
|
|
99
|
+
if (!name.startsWith('xformOp:'))
|
|
100
|
+
fail('syntax', `invalid xformOpOrder entry ${entry}`);
|
|
101
|
+
if (value === undefined)
|
|
102
|
+
fail('syntax', `xformOpOrder names ${name}, which is not authored`);
|
|
103
|
+
let matrix = identity();
|
|
104
|
+
if (type === 'transform') {
|
|
105
|
+
if (!Array.isArray(value) || value.length !== 4)
|
|
106
|
+
fail('syntax', 'expected a 4x4 transform');
|
|
107
|
+
matrix = new Float64Array(value.flatMap((row) => numbers(row, 4)));
|
|
108
|
+
if (matrix[3] !== 0 || matrix[7] !== 0 || matrix[11] !== 0 || matrix[15] !== 1)
|
|
109
|
+
fail('unsupported', 'only affine transforms are supported');
|
|
110
|
+
}
|
|
111
|
+
else if (type === 'translate')
|
|
112
|
+
matrix.set(numbers(value, 3), 12);
|
|
113
|
+
else if (type === 'scale') {
|
|
114
|
+
const v = numbers(value, 3);
|
|
115
|
+
matrix[0] = v[0];
|
|
116
|
+
matrix[5] = v[1];
|
|
117
|
+
matrix[10] = v[2];
|
|
118
|
+
}
|
|
119
|
+
else if (type === 'orient') {
|
|
120
|
+
// A quaternion written (real, i, j, k); normalized as UsdGeomXformOp does.
|
|
121
|
+
const [w, x, y, z] = numbers(value, 4), length = Math.hypot(w, x, y, z);
|
|
122
|
+
if (!length)
|
|
123
|
+
fail('range', 'orient quaternion has zero length');
|
|
124
|
+
const [qw, qx, qy, qz] = [w / length, x / length, y / length, z / length];
|
|
125
|
+
matrix.set([
|
|
126
|
+
1 - 2 * (qy * qy + qz * qz),
|
|
127
|
+
2 * (qx * qy + qw * qz),
|
|
128
|
+
2 * (qx * qz - qw * qy),
|
|
129
|
+
0,
|
|
130
|
+
2 * (qx * qy - qw * qz),
|
|
131
|
+
1 - 2 * (qx * qx + qz * qz),
|
|
132
|
+
2 * (qy * qz + qw * qx),
|
|
133
|
+
0,
|
|
134
|
+
2 * (qx * qz + qw * qy),
|
|
135
|
+
2 * (qy * qz - qw * qx),
|
|
136
|
+
1 - 2 * (qx * qx + qy * qy),
|
|
137
|
+
0,
|
|
138
|
+
]);
|
|
139
|
+
}
|
|
140
|
+
else if (/^rotate([XYZ]|XYZ|XZY|YXZ|YZX|ZXY|ZYX)$/.test(type)) {
|
|
141
|
+
// rotateABC turns about A first, then B, then C (degrees); rotateX, rotateY and rotateZ take one angle.
|
|
142
|
+
const axes = type.slice(6), angles = axes.length === 1 ? [typeof value === 'number' ? value : numbers(value, 1)[0]] : numbers(value, 3);
|
|
143
|
+
if (!angles.every(Number.isFinite))
|
|
144
|
+
fail('syntax', `invalid ${name} angle`);
|
|
145
|
+
for (let k = 0; k < axes.length; k++) {
|
|
146
|
+
const axis = 'XYZ'.indexOf(axes[k]), angle = (angles[k] * Math.PI) / 180, rotation = identity(), a = (axis + 1) % 3, b = (axis + 2) % 3, c = Math.cos(angle), s = Math.sin(angle);
|
|
147
|
+
rotation[a * 4 + a] = c;
|
|
148
|
+
rotation[b * 4 + b] = c;
|
|
149
|
+
rotation[a * 4 + b] = s;
|
|
150
|
+
rotation[b * 4 + a] = -s;
|
|
151
|
+
matrix = multiplyMatrices(rotation, matrix, new Float64Array(16));
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
else
|
|
155
|
+
fail('unsupported', `unsupported transform ${name}`);
|
|
156
|
+
if (invert)
|
|
157
|
+
matrix = invertMatrix(matrix) ?? fail('range', `${name} cannot be inverted`);
|
|
158
|
+
local = multiplyMatrices(local, matrix, new Float64Array(16));
|
|
159
|
+
}
|
|
160
|
+
return multiplyMatrices(inherited, local, new Float64Array(16));
|
|
161
|
+
}
|
|
162
|
+
/** Tokenizes a static USDA layer into its `def` prim tree. Composition, variants and time samples are rejected explicitly. */
|
|
163
|
+
export function readUSDA(text, options = {}) {
|
|
164
|
+
const maxDepth = options.maxDepth ?? 32;
|
|
165
|
+
if (!Number.isSafeInteger(maxDepth) || maxDepth < 1 || maxDepth > 64)
|
|
166
|
+
fail('range', 'maxDepth must be 1..64');
|
|
167
|
+
if (!text.startsWith('#usda 1.0'))
|
|
168
|
+
fail('syntax', 'expected #usda 1.0');
|
|
169
|
+
const token = /\s+|#[^\n]*|"""[\s\S]*?"""|'''[\s\S]*?'''|"(?:\\[\s\S]|[^"\\])*"|'(?:\\[\s\S]|[^'\\])*'|<[^>]*>|@[\s\S]*?@|[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?|[A-Za-z_!][\w:!.]*|[{}()[\]=,;]/y;
|
|
170
|
+
let offset = 0, cached, numbers = 0;
|
|
171
|
+
function peek() {
|
|
172
|
+
if (cached)
|
|
173
|
+
return cached;
|
|
174
|
+
let newLine = false;
|
|
175
|
+
while (offset < text.length) {
|
|
176
|
+
token.lastIndex = offset;
|
|
177
|
+
const match = token.exec(text);
|
|
178
|
+
if (!match)
|
|
179
|
+
fail('syntax', `invalid token at offset ${offset}`);
|
|
180
|
+
offset = token.lastIndex;
|
|
181
|
+
const word = match[0];
|
|
182
|
+
if (/^\s|^#/.test(word)) {
|
|
183
|
+
newLine ||= word.includes('\n');
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
return (cached = { text: word, newLine });
|
|
187
|
+
}
|
|
188
|
+
return (cached = { text: '', newLine });
|
|
189
|
+
}
|
|
190
|
+
const take = () => {
|
|
191
|
+
const value = peek().text;
|
|
192
|
+
cached = undefined;
|
|
193
|
+
return value;
|
|
194
|
+
};
|
|
195
|
+
// A quoted token (single, double or triple quotes) with C escapes as OpenUSD reads them (TfEscapeString): \xHH (one
|
|
196
|
+
// or two hex digits) and \ooo (one to three octal digits) are bytes, a run of them decoded together as UTF-8; \a \b
|
|
197
|
+
// \f \n \r \t \v name their control characters and any other escaped character stands for itself.
|
|
198
|
+
const string = (word) => {
|
|
199
|
+
const mark = word.startsWith('"""') || word.startsWith("'''") ? 3 : 1, body = word.slice(mark, -mark);
|
|
200
|
+
let out = '', from = 0, i;
|
|
201
|
+
while ((i = body.indexOf('\\', from)) >= 0) {
|
|
202
|
+
out += body.slice(from, i);
|
|
203
|
+
const bytes = [];
|
|
204
|
+
for (;;) {
|
|
205
|
+
const hex = body[i + 1] === 'x' ? /^[0-9a-fA-F]{1,2}/.exec(body.slice(i + 2, i + 4))?.[0] : undefined, octal = /^[0-7]{1,3}/.exec(body.slice(i + 1, i + 4))?.[0];
|
|
206
|
+
if (hex !== undefined) {
|
|
207
|
+
bytes.push(parseInt(hex, 16));
|
|
208
|
+
i += 2 + hex.length;
|
|
209
|
+
}
|
|
210
|
+
else if (octal !== undefined) {
|
|
211
|
+
bytes.push(parseInt(octal, 8) & 255);
|
|
212
|
+
i += 1 + octal.length;
|
|
213
|
+
}
|
|
214
|
+
else
|
|
215
|
+
break;
|
|
216
|
+
if (body[i] !== '\\')
|
|
217
|
+
break;
|
|
218
|
+
}
|
|
219
|
+
if (bytes.length)
|
|
220
|
+
out += new TextDecoder().decode(new Uint8Array(bytes));
|
|
221
|
+
else {
|
|
222
|
+
const escaped = body[i + 1] ?? '\\';
|
|
223
|
+
out += ESCAPES[escaped] ?? escaped;
|
|
224
|
+
i += 2;
|
|
225
|
+
}
|
|
226
|
+
from = i;
|
|
227
|
+
}
|
|
228
|
+
return out + body.slice(from);
|
|
229
|
+
};
|
|
230
|
+
const quoted = (word) => word[0] === '"' || word[0] === "'";
|
|
231
|
+
const expect = (word) => {
|
|
232
|
+
if (take() !== word)
|
|
233
|
+
fail('syntax', `expected ${word}`);
|
|
234
|
+
};
|
|
235
|
+
const number = (word) => {
|
|
236
|
+
const n = word ? Number(word) : NaN;
|
|
237
|
+
if (!Number.isFinite(n))
|
|
238
|
+
fail('range', 'non-finite number');
|
|
239
|
+
if (++numbers > 16_000_000)
|
|
240
|
+
fail('limit', 'numeric values exceed 16 million');
|
|
241
|
+
return n;
|
|
242
|
+
};
|
|
243
|
+
function value(depth = 0) {
|
|
244
|
+
if (depth > maxDepth)
|
|
245
|
+
fail('limit', 'nested value exceeds maxDepth');
|
|
246
|
+
const word = take();
|
|
247
|
+
if (word === '[' || word === '(') {
|
|
248
|
+
const end = word === '[' ? ']' : ')', close = text.indexOf(end, offset);
|
|
249
|
+
// Fast path for flat numeric arrays and arrays of numeric tuples, the bulk of mesh data.
|
|
250
|
+
if (word === '[' &&
|
|
251
|
+
!cached &&
|
|
252
|
+
close >= 0 &&
|
|
253
|
+
/^[\s\d.eE+\-(),]*$/.test(text.slice(offset, close)) &&
|
|
254
|
+
!/\([^)]*\(/.test(text.slice(offset, close))) {
|
|
255
|
+
const body = text.slice(offset, close), tuples = body.includes('(')
|
|
256
|
+
? [...body.matchAll(/\(([^)]*)\)/g)].map((m) => m[1].split(',').map((v) => number(v.trim())))
|
|
257
|
+
: body.trim()
|
|
258
|
+
? body.split(',').map((v) => number(v.trim()))
|
|
259
|
+
: [];
|
|
260
|
+
offset = close + 1;
|
|
261
|
+
return tuples;
|
|
262
|
+
}
|
|
263
|
+
const out = [];
|
|
264
|
+
while (peek().text !== end) {
|
|
265
|
+
if (!peek().text)
|
|
266
|
+
fail('syntax', 'unterminated array');
|
|
267
|
+
out.push(value(depth + 1));
|
|
268
|
+
if (peek().text === ',')
|
|
269
|
+
take();
|
|
270
|
+
else if (peek().text !== end)
|
|
271
|
+
fail('syntax', 'expected comma');
|
|
272
|
+
}
|
|
273
|
+
take();
|
|
274
|
+
return out;
|
|
275
|
+
}
|
|
276
|
+
if (word === '{') {
|
|
277
|
+
let open = 1;
|
|
278
|
+
while (open) {
|
|
279
|
+
const t = take();
|
|
280
|
+
if (!t)
|
|
281
|
+
fail('syntax', 'unterminated dictionary');
|
|
282
|
+
open += t === '{' ? 1 : t === '}' ? -1 : 0;
|
|
283
|
+
}
|
|
284
|
+
return '';
|
|
285
|
+
}
|
|
286
|
+
if (quoted(word))
|
|
287
|
+
return string(word);
|
|
288
|
+
if (word.startsWith('<'))
|
|
289
|
+
return { path: word.slice(1, -1) };
|
|
290
|
+
if (word.startsWith('@')) {
|
|
291
|
+
if (!options.assets)
|
|
292
|
+
fail('unsupported', 'external assets and textures are unsupported');
|
|
293
|
+
return { asset: word.replace(/^@+|@+$/g, '') };
|
|
294
|
+
}
|
|
295
|
+
if (word === 'true' || word === 'false')
|
|
296
|
+
return word === 'true';
|
|
297
|
+
if (/^[+-]?(\d|\.\d)/.test(word))
|
|
298
|
+
return number(word);
|
|
299
|
+
if (word === 'None')
|
|
300
|
+
return '';
|
|
301
|
+
fail('unsupported', `unsupported value ${word}`);
|
|
302
|
+
}
|
|
303
|
+
function metadata() {
|
|
304
|
+
const out = new Map();
|
|
305
|
+
expect('(');
|
|
306
|
+
while (peek().text !== ')') {
|
|
307
|
+
let key = take();
|
|
308
|
+
if (!key)
|
|
309
|
+
fail('syntax', 'unterminated metadata');
|
|
310
|
+
if (['prepend', 'append', 'add', 'delete', 'reorder'].includes(key))
|
|
311
|
+
key = take();
|
|
312
|
+
if (quoted(key)) {
|
|
313
|
+
out.set('doc', string(key));
|
|
314
|
+
continue;
|
|
315
|
+
}
|
|
316
|
+
if (key === 'references') {
|
|
317
|
+
// Internal references only: a prim of this layer, composed below. Files and lists are composition this reader lacks.
|
|
318
|
+
expect('=');
|
|
319
|
+
if (!peek().text.startsWith('<'))
|
|
320
|
+
fail('unsupported', 'USD composition references to other files is unsupported');
|
|
321
|
+
out.set(key, value());
|
|
322
|
+
if (peek().text === ',' || peek().text === ';')
|
|
323
|
+
take();
|
|
324
|
+
continue;
|
|
325
|
+
}
|
|
326
|
+
if ([
|
|
327
|
+
'payload',
|
|
328
|
+
'subLayers',
|
|
329
|
+
'variants',
|
|
330
|
+
'variantSets',
|
|
331
|
+
'inherits',
|
|
332
|
+
'specializes',
|
|
333
|
+
'instanceable',
|
|
334
|
+
'relocates',
|
|
335
|
+
].includes(key))
|
|
336
|
+
fail('unsupported', `USD composition ${key} is unsupported`);
|
|
337
|
+
expect('=');
|
|
338
|
+
out.set(key, value());
|
|
339
|
+
if (peek().text === ',' || peek().text === ';')
|
|
340
|
+
take();
|
|
341
|
+
}
|
|
342
|
+
take();
|
|
343
|
+
return out;
|
|
344
|
+
}
|
|
345
|
+
const prims = new Map(), classes = new Map();
|
|
346
|
+
function prim(parent, depth) {
|
|
347
|
+
if (depth > maxDepth)
|
|
348
|
+
fail('limit', 'prim hierarchy exceeds maxDepth');
|
|
349
|
+
const specifier = take();
|
|
350
|
+
if (specifier !== 'def' && specifier !== 'class' && !(options.skipOver && specifier === 'over'))
|
|
351
|
+
fail('unsupported', 'only defined static prims are supported');
|
|
352
|
+
let type = take();
|
|
353
|
+
const name = quoted(type) ? string(type) : value();
|
|
354
|
+
if (quoted(type))
|
|
355
|
+
type = 'Xform';
|
|
356
|
+
if (typeof name !== 'string')
|
|
357
|
+
fail('syntax', 'prim name must be a string');
|
|
358
|
+
const path = `${parent}/${name}`, attrs = new Map(), properties = new Map(), meta = peek().text === '(' ? metadata() : new Map(), children = [];
|
|
359
|
+
if (prims.has(path) || classes.has(path))
|
|
360
|
+
fail('syntax', 'duplicate prim path');
|
|
361
|
+
const result = { type, name: name, path, attrs, metadata: meta, properties, children };
|
|
362
|
+
if (specifier === 'def')
|
|
363
|
+
prims.set(path, result);
|
|
364
|
+
else if (specifier === 'class')
|
|
365
|
+
classes.set(path, result);
|
|
366
|
+
expect('{');
|
|
367
|
+
while (peek().text !== '}') {
|
|
368
|
+
throwIfAborted(options.signal, F);
|
|
369
|
+
const first = peek().text;
|
|
370
|
+
if (first === 'def' || first === 'class' || (options.skipOver && first === 'over')) {
|
|
371
|
+
const p = prim(path, depth + 1);
|
|
372
|
+
if (p)
|
|
373
|
+
children.push(p);
|
|
374
|
+
continue;
|
|
375
|
+
}
|
|
376
|
+
if (!first)
|
|
377
|
+
fail('syntax', 'unterminated prim');
|
|
378
|
+
if (first === 'over' || first === 'variantSet')
|
|
379
|
+
fail('unsupported', `USD composition ${first} is unsupported`);
|
|
380
|
+
const header = [];
|
|
381
|
+
while (peek().text !== '=') {
|
|
382
|
+
if (header.length >= 2 && (peek().newLine || peek().text === '}' || peek().text === '('))
|
|
383
|
+
break;
|
|
384
|
+
if (!peek().text || header.length > 8)
|
|
385
|
+
fail('syntax', 'invalid property declaration');
|
|
386
|
+
header.push(take());
|
|
387
|
+
}
|
|
388
|
+
const key = header[header.length - 1];
|
|
389
|
+
if (!key)
|
|
390
|
+
fail('syntax', 'missing property name');
|
|
391
|
+
if (key.includes('timeSamples'))
|
|
392
|
+
fail('unsupported', 'animated USD attributes are unsupported');
|
|
393
|
+
if (peek().text === '=') {
|
|
394
|
+
take();
|
|
395
|
+
if (attrs.has(key))
|
|
396
|
+
fail('syntax', 'duplicate property');
|
|
397
|
+
attrs.set(key, value());
|
|
398
|
+
}
|
|
399
|
+
if (peek().text === '(')
|
|
400
|
+
properties.set(key, metadata());
|
|
401
|
+
if (peek().text === ';')
|
|
402
|
+
take();
|
|
403
|
+
}
|
|
404
|
+
take();
|
|
405
|
+
return specifier === 'def' ? result : undefined;
|
|
406
|
+
}
|
|
407
|
+
const metadataMap = peek().text === '(' ? metadata() : new Map(), roots = [];
|
|
408
|
+
while (peek().text) {
|
|
409
|
+
const p = prim('', 1);
|
|
410
|
+
if (p)
|
|
411
|
+
roots.push(p);
|
|
412
|
+
}
|
|
413
|
+
// A reference brings its target's properties under the prim; what the prim authors itself is stronger.
|
|
414
|
+
for (const node of prims.values()) {
|
|
415
|
+
const reference = node.metadata.get('references');
|
|
416
|
+
if (reference === undefined)
|
|
417
|
+
continue;
|
|
418
|
+
const path = reference.path, target = path === undefined ? undefined : (classes.get(path) ?? prims.get(path));
|
|
419
|
+
if (!target)
|
|
420
|
+
fail('syntax', `reference target ${path} not found`);
|
|
421
|
+
if (target.children.length || target.metadata.has('references'))
|
|
422
|
+
fail('unsupported', 'USD composition references to a prim with children or references is unsupported');
|
|
423
|
+
for (const [key, value] of target.attrs)
|
|
424
|
+
if (!node.attrs.has(key))
|
|
425
|
+
node.attrs.set(key, value);
|
|
426
|
+
for (const [key, value] of target.properties)
|
|
427
|
+
if (!node.properties.has(key))
|
|
428
|
+
node.properties.set(key, value);
|
|
429
|
+
node.metadata.delete('references');
|
|
430
|
+
}
|
|
431
|
+
return { metadata: metadataMap, roots, prims };
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* Static USDA polygon meshes. Internal references (`references = </Path>`, to a `class` or `def` prim without children) are
|
|
435
|
+
* composed; other composition, animation and binary Crate layers are rejected explicitly.
|
|
436
|
+
*/
|
|
437
|
+
export function parseUSDA(input, options = {}) {
|
|
438
|
+
throwIfAborted(options.signal, F);
|
|
439
|
+
const limit = limits(options, F), maxBytes = options.maxEntryBytes ?? 64 * 1024 * 1024;
|
|
440
|
+
if (!Number.isSafeInteger(maxBytes) || maxBytes < 0)
|
|
441
|
+
fail('range', 'invalid maxEntryBytes');
|
|
442
|
+
const bytes = typeof input === 'string' ? new TextEncoder().encode(input) : toBytes(input, F);
|
|
443
|
+
if (bytes.length > maxBytes)
|
|
444
|
+
fail('limit', 'USDA exceeds maxEntryBytes');
|
|
445
|
+
let text;
|
|
446
|
+
try {
|
|
447
|
+
text = new TextDecoder('utf-8', { fatal: true }).decode(bytes);
|
|
448
|
+
}
|
|
449
|
+
catch {
|
|
450
|
+
fail('syntax', 'invalid UTF-8');
|
|
451
|
+
}
|
|
452
|
+
if (options.units !== undefined && options.units !== 'meters' && options.units !== 'file')
|
|
453
|
+
fail('range', 'units must be meters or file');
|
|
454
|
+
const { metadata: meta, roots, prims } = readUSDA(text, { maxDepth: options.maxDepth, signal: options.signal });
|
|
455
|
+
let polygonWork = 0;
|
|
456
|
+
const unit = meta.get('metersPerUnit') ?? 0.01, axis = meta.get('upAxis') ?? 'Y';
|
|
457
|
+
if (typeof unit !== 'number' || unit <= 0 || !Number.isFinite(unit))
|
|
458
|
+
fail('range', 'invalid metersPerUnit');
|
|
459
|
+
if (axis !== 'Y' && axis !== 'Z')
|
|
460
|
+
fail('unsupported', 'only Y and Z up stages are supported');
|
|
461
|
+
const scale = options.units === 'file' ? 1 : unit, base = identity();
|
|
462
|
+
base[0] = scale;
|
|
463
|
+
base[5] = scale;
|
|
464
|
+
base[10] = scale;
|
|
465
|
+
if (axis === 'Z') {
|
|
466
|
+
base[5] = 0;
|
|
467
|
+
base[10] = 0;
|
|
468
|
+
base[6] = -scale;
|
|
469
|
+
base[9] = scale;
|
|
470
|
+
}
|
|
471
|
+
const positions = [], indices = [], groups = [], appearance = [];
|
|
472
|
+
function visit(node, parent, hidden = false, bound) {
|
|
473
|
+
// UsdGeomImageable: visibility "invisible" and purpose "guide" hide the subtree; both inherit.
|
|
474
|
+
hidden ||=
|
|
475
|
+
node.attrs.get('visibility') === 'invisible' ||
|
|
476
|
+
node.attrs.get('purpose') === 'guide' ||
|
|
477
|
+
node.metadata.get('active') === false;
|
|
478
|
+
if (hidden)
|
|
479
|
+
return;
|
|
480
|
+
// UsdShadeMaterialBindingAPI: a binding applies to the prim's descendants; the nearest one wins.
|
|
481
|
+
bound = node.attrs.get('material:binding') ?? bound;
|
|
482
|
+
const world = transform(node, parent, base);
|
|
483
|
+
if (node.type === 'Mesh') {
|
|
484
|
+
if ((node.attrs.get('subdivisionScheme') ?? 'catmullClark') !== 'none')
|
|
485
|
+
fail('unsupported', 'subdivision surfaces are unsupported');
|
|
486
|
+
const holes = node.attrs.get('holeIndices');
|
|
487
|
+
if (holes !== undefined && (!Array.isArray(holes) || holes.length))
|
|
488
|
+
fail('unsupported', 'mesh hole faces are unsupported');
|
|
489
|
+
if (!Array.isArray(node.attrs.get('points')))
|
|
490
|
+
fail('syntax', 'mesh has no points');
|
|
491
|
+
const points = node.attrs.get('points').map((p) => numbers(p, 3)), counts = numbers(node.attrs.get('faceVertexCounts')), faces = numbers(node.attrs.get('faceVertexIndices'));
|
|
492
|
+
if (positions.length / 3 + points.length > limit.maxVertices)
|
|
493
|
+
fail('limit', 'vertex limit exceeded');
|
|
494
|
+
const det = world[0] * (world[5] * world[10] - world[6] * world[9]) -
|
|
495
|
+
world[4] * (world[1] * world[10] - world[2] * world[9]) +
|
|
496
|
+
world[8] * (world[1] * world[6] - world[2] * world[5]);
|
|
497
|
+
if (!det || !Number.isFinite(det))
|
|
498
|
+
fail('range', 'singular transform');
|
|
499
|
+
const start = indices.length, vertexBase = positions.length / 3;
|
|
500
|
+
for (const [x, y, z] of points)
|
|
501
|
+
for (let r = 0; r < 3; r++) {
|
|
502
|
+
const v = Math.fround(world[r] * x + world[4 + r] * y + world[8 + r] * z + world[12 + r]);
|
|
503
|
+
if (!Number.isFinite(v))
|
|
504
|
+
fail('range', 'vertex outside Float32 range');
|
|
505
|
+
positions.push(v);
|
|
506
|
+
}
|
|
507
|
+
const orientation = node.attrs.get('orientation') ?? 'rightHanded';
|
|
508
|
+
if (orientation !== 'rightHanded' && orientation !== 'leftHanded')
|
|
509
|
+
fail('range', 'invalid orientation');
|
|
510
|
+
const reverse = (orientation === 'leftHanded') !== det < 0;
|
|
511
|
+
let cursor = 0;
|
|
512
|
+
for (const count of counts) {
|
|
513
|
+
if (!Number.isSafeInteger(count) || count < 3 || cursor + count > faces.length)
|
|
514
|
+
fail('syntax', 'invalid faceVertexCounts');
|
|
515
|
+
if (indices.length / 3 + count - 2 > limit.maxTriangles)
|
|
516
|
+
fail('limit', 'triangle limit exceeded');
|
|
517
|
+
const face = faces.slice(cursor, cursor + count);
|
|
518
|
+
cursor += count;
|
|
519
|
+
if (face.some((v) => !Number.isSafeInteger(v) || v < 0 || v >= points.length))
|
|
520
|
+
fail('range', 'face index outside points');
|
|
521
|
+
if (count > 3) {
|
|
522
|
+
// shortcut: convex planar polygons only, use a polygon tessellator before importing concave faces.
|
|
523
|
+
polygonWork += count * count;
|
|
524
|
+
if (polygonWork > 10_000_000)
|
|
525
|
+
fail('limit', 'polygon validation work limit exceeded');
|
|
526
|
+
const a = points[face[0]], b = points[face[1]], c = points[face[2]], u = b.map((v, i) => v - a[i]), v = c.map((v, i) => v - a[i]), n = [u[1] * v[2] - u[2] * v[1], u[2] * v[0] - u[0] * v[2], u[0] * v[1] - u[1] * v[0]], length = Math.hypot(...n);
|
|
527
|
+
if (!length)
|
|
528
|
+
fail('unsupported', 'degenerate polygon');
|
|
529
|
+
for (let k = 0; k < count; k++) {
|
|
530
|
+
const p = points[face[k]], q = points[face[(k + 1) % count]], r = points[face[(k + 2) % count]], edge = q.map((v, i) => v - p[i]), next = r.map((v, i) => v - q[i]);
|
|
531
|
+
if (Math.abs(n.reduce((s, v, i) => s + v * (p[i] - a[i]), 0)) > length * 1e-5 ||
|
|
532
|
+
n[0] * (edge[1] * next[2] - edge[2] * next[1]) +
|
|
533
|
+
n[1] * (edge[2] * next[0] - edge[0] * next[2]) +
|
|
534
|
+
n[2] * (edge[0] * next[1] - edge[1] * next[0]) <
|
|
535
|
+
0)
|
|
536
|
+
fail('unsupported', 'concave or non-planar polygon');
|
|
537
|
+
for (const index of face) {
|
|
538
|
+
const w = points[index].map((v, i) => v - p[i]);
|
|
539
|
+
if (n[0] * (edge[1] * w[2] - edge[2] * w[1]) +
|
|
540
|
+
n[1] * (edge[2] * w[0] - edge[0] * w[2]) +
|
|
541
|
+
n[2] * (edge[0] * w[1] - edge[1] * w[0]) <
|
|
542
|
+
-length * 1e-10)
|
|
543
|
+
fail('unsupported', 'self-intersecting or concave polygon');
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
for (let k = 1; k < count - 1; k++)
|
|
548
|
+
indices.push(vertexBase + face[0], vertexBase + face[reverse ? k + 1 : k], vertexBase + face[reverse ? k : k + 1]);
|
|
549
|
+
}
|
|
550
|
+
if (cursor !== faces.length)
|
|
551
|
+
fail('syntax', 'faceVertexCounts does not match indices');
|
|
552
|
+
let color, rgb, opacity;
|
|
553
|
+
const binding = bound;
|
|
554
|
+
if (binding && typeof binding === 'object' && !Array.isArray(binding) && 'path' in binding) {
|
|
555
|
+
const material = prims.get(binding.path), surface = material?.attrs.get('outputs:surface.connect');
|
|
556
|
+
if (!surface || typeof surface !== 'object' || Array.isArray(surface) || !('path' in surface))
|
|
557
|
+
fail('unsupported', 'unsupported material binding');
|
|
558
|
+
const shader = prims.get(surface.path.split('.outputs:')[0]);
|
|
559
|
+
if (!shader || shader.attrs.get('info:id') !== 'UsdPreviewSurface')
|
|
560
|
+
fail('unsupported', 'only UsdPreviewSurface colors are supported');
|
|
561
|
+
rgb = shaderColor(node, shader, 'diffuseColor', 3, points.length, prims, [0.18, 0.18, 0.18]);
|
|
562
|
+
opacity = shaderColor(node, shader, 'opacity', 1, points.length, prims, [1]);
|
|
563
|
+
}
|
|
564
|
+
else {
|
|
565
|
+
if (node.attrs.has('primvars:displayColor'))
|
|
566
|
+
rgb = colorPrimvar(node, 'displayColor', 3, points.length, [1, 1, 1]);
|
|
567
|
+
if (node.attrs.has('primvars:displayOpacity'))
|
|
568
|
+
opacity = colorPrimvar(node, 'displayOpacity', 1, points.length, [1]);
|
|
569
|
+
}
|
|
570
|
+
if ((rgb || opacity) && !rgb?.varying && !opacity?.varying)
|
|
571
|
+
color = [...(rgb?.values ?? [1, 1, 1]).map(LinearToSRGB), opacity?.values[0] ?? 1];
|
|
572
|
+
appearance.push({
|
|
573
|
+
count: points.length,
|
|
574
|
+
rgb: rgb ?? { values: [1, 1, 1], varying: false },
|
|
575
|
+
opacity: opacity ?? { values: [1], varying: false },
|
|
576
|
+
});
|
|
577
|
+
if (color?.some((v) => !Number.isFinite(v) || v < 0 || v > 1))
|
|
578
|
+
fail('range', 'invalid display color');
|
|
579
|
+
groups.push({
|
|
580
|
+
start,
|
|
581
|
+
count: indices.length - start,
|
|
582
|
+
name: typeof node.attrs.get('displayName') === 'string' ? node.attrs.get('displayName') : node.name,
|
|
583
|
+
objectId: node.path,
|
|
584
|
+
...(color ? { color } : {}),
|
|
585
|
+
});
|
|
586
|
+
}
|
|
587
|
+
else if (!['Xform', 'Scope', 'Material', 'Shader'].includes(node.type))
|
|
588
|
+
fail('unsupported', `unsupported USD prim type ${node.type}`);
|
|
589
|
+
for (const child of node.children)
|
|
590
|
+
visit(child, world, hidden, bound);
|
|
591
|
+
}
|
|
592
|
+
for (const root of roots)
|
|
593
|
+
visit(root, base);
|
|
594
|
+
if (!indices.length)
|
|
595
|
+
fail('unsupported', 'stage contains no polygon meshes');
|
|
596
|
+
const vertices = new Float32Array(positions), triangles = new Uint32Array(indices);
|
|
597
|
+
let colors, opacity;
|
|
598
|
+
if (appearance.some((part) => part.rgb.varying || part.opacity.varying)) {
|
|
599
|
+
colors = new Float32Array(positions.length);
|
|
600
|
+
opacity = new Float32Array(positions.length / 3);
|
|
601
|
+
let vertex = 0;
|
|
602
|
+
for (const part of appearance)
|
|
603
|
+
for (let v = 0; v < part.count; v++, vertex++) {
|
|
604
|
+
for (let c = 0; c < 3; c++)
|
|
605
|
+
colors[vertex * 3 + c] = LinearToSRGB(part.rgb.values[(part.rgb.varying ? v * 3 : 0) + c]);
|
|
606
|
+
opacity[vertex] = part.opacity.values[part.opacity.varying ? v : 0];
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
return {
|
|
610
|
+
kind: 'mesh',
|
|
611
|
+
positions: vertices,
|
|
612
|
+
indices: triangles,
|
|
613
|
+
normals: computeVertexNormals(vertices, triangles),
|
|
614
|
+
groups,
|
|
615
|
+
...(colors ? { colors, attributes: { opacity: opacity } } : {}),
|
|
616
|
+
};
|
|
617
|
+
}
|
|
618
|
+
/** Reads a USDZ root USDA layer; binary USDC and composed stages are unsupported. */
|
|
619
|
+
export async function parseUSDZ(input, options = {}) {
|
|
620
|
+
throwIfAborted(options.signal, F);
|
|
621
|
+
for (const value of [options.maxEntries ?? 10_000, options.maxEntryBytes ?? 64 * 1024 * 1024])
|
|
622
|
+
if (!Number.isSafeInteger(value) || value < 0)
|
|
623
|
+
fail('range', 'invalid ZIP limit');
|
|
624
|
+
const bytes = toBytes(input, F), entries = readZipDirectory(bytes, options);
|
|
625
|
+
const root = entries.reduce((first, entry) => (!first || entry.headerOffset < first.headerOffset ? entry : first), undefined);
|
|
626
|
+
if (!root || !/\.(usd|usda|usdc)$/i.test(root.name))
|
|
627
|
+
fail('unsupported', 'USDZ first entry must be its root USD layer');
|
|
628
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
629
|
+
for (const entry of entries) {
|
|
630
|
+
if (entry.method !== 0)
|
|
631
|
+
fail('unsupported', 'USDZ entries must be stored without compression');
|
|
632
|
+
if (entry.headerOffset + 30 > bytes.length)
|
|
633
|
+
fail('syntax', 'truncated ZIP local header');
|
|
634
|
+
const offset = entry.headerOffset +
|
|
635
|
+
30 +
|
|
636
|
+
view.getUint16(entry.headerOffset + 26, true) +
|
|
637
|
+
view.getUint16(entry.headerOffset + 28, true);
|
|
638
|
+
if (offset % 64)
|
|
639
|
+
fail('syntax', 'USDZ entries must be aligned to 64 bytes');
|
|
640
|
+
}
|
|
641
|
+
const data = await readZipEntry(bytes, root, options);
|
|
642
|
+
throwIfAborted(options.signal, F);
|
|
643
|
+
if (new TextDecoder().decode(data.subarray(0, 8)) === 'PXR-USDC')
|
|
644
|
+
fail('unsupported', 'binary USDC is unsupported; convert the root layer to USDA with usdcat');
|
|
645
|
+
return parseUSDA(data, options);
|
|
646
|
+
}
|