@margoengine/loaders 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 +201 -0
- package/NOTICE +4 -0
- package/README.md +1198 -3
- 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/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-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 +161 -0
- package/dist/dae.js +633 -0
- package/dist/detect.d.ts +20 -0
- package/dist/detect.js +72 -0
- package/dist/dxf.d.ts +81 -0
- package/dist/dxf.js +774 -0
- package/dist/engine.d.ts +36 -0
- package/dist/engine.js +117 -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 +55 -0
- package/dist/gcode.js +278 -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 +120 -0
- package/dist/mtl.js +469 -0
- package/dist/normals.d.ts +7 -0
- package/dist/normals.js +31 -0
- package/dist/nrrd.d.ts +63 -0
- package/dist/nrrd.js +743 -0
- package/dist/nurbs.d.ts +118 -0
- package/dist/nurbs.js +265 -0
- package/dist/obj.d.ts +143 -0
- package/dist/obj.js +456 -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 +27 -0
- package/dist/render.js +153 -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 +42 -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-lattice.d.ts +139 -0
- package/dist/threemf-lattice.js +514 -0
- package/dist/threemf-materials.d.ts +75 -0
- package/dist/threemf-materials.js +314 -0
- package/dist/threemf-mesh.d.ts +60 -0
- package/dist/threemf-mesh.js +201 -0
- package/dist/threemf.d.ts +101 -0
- package/dist/threemf.js +415 -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 +90 -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 +246 -6
package/dist/vrml.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { type LoadedGeometry, type ParseLimits } from './types.js';
|
|
2
|
+
import { type BinaryInput } from './util.js';
|
|
3
|
+
import { type XMLExternalOptions } from './xml.js';
|
|
4
|
+
/** The result of {@link parseVRML} and {@link parseX3D}: all shapes in world space, one group per Shape. */
|
|
5
|
+
export interface VRMLScene extends LoadedGeometry {
|
|
6
|
+
/** Node types the scene holds that were left out, each listed once: other geometry, PROTOs, Inlines, textures. */
|
|
7
|
+
warnings: string[];
|
|
8
|
+
}
|
|
9
|
+
/** Options of {@link parseVRML} and {@link parseX3D}: the shared size limits and how many nodes a scene may visit. */
|
|
10
|
+
export interface VRMLOptions extends ParseLimits, XMLExternalOptions {
|
|
11
|
+
/** Most nodes visited, counting each USE again. Default 1,000,000. */
|
|
12
|
+
maxNodes?: number;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Parses a VRML97 (`#VRML V2.0 utf8`) world: Transform, Group, Switch, LOD, Shape, Appearance, Material,
|
|
16
|
+
* IndexedFaceSet and DEF/USE. The transforms are baked in and v turned to Margo's convention (v = 0 at the image's top).
|
|
17
|
+
* Faces are fan-triangulated, or ear-clipped when the IndexedFaceSet says `convex FALSE`. A Switch draws the child
|
|
18
|
+
* its `whichChoice` names (none for -1); an LOD draws its first, most detailed level.
|
|
19
|
+
*
|
|
20
|
+
* @param input The file's bytes or text.
|
|
21
|
+
* @param options Limits.
|
|
22
|
+
* @returns A mesh with a group per Shape and what was left out.
|
|
23
|
+
* @throws LoaderError with code `header`, `syntax`, `range`, `limit` or `unsupported`.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* import { parseVRML } from '@margoengine/loaders/vrml';
|
|
28
|
+
*
|
|
29
|
+
* const scene = parseVRML(`#VRML V2.0 utf8
|
|
30
|
+
* Shape { geometry IndexedFaceSet { coord Coordinate { point [0 0 0, 1 0 0, 0 1 0] } coordIndex [0 1 2 -1] } }`);
|
|
31
|
+
* console.log(scene.positions.length, scene.indices?.length);
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export declare function parseVRML(input: BinaryInput | string, options?: VRMLOptions): VRMLScene;
|
|
35
|
+
/**
|
|
36
|
+
* Parses an X3D XML scene with the same node set as {@link parseVRML}: children are found by their usual
|
|
37
|
+
* `containerField`, and DEF and USE are attributes.
|
|
38
|
+
*
|
|
39
|
+
* @param input The file's bytes or text.
|
|
40
|
+
* @param options Limits.
|
|
41
|
+
* @returns A mesh with a group per Shape and what was left out.
|
|
42
|
+
* @throws LoaderError with code `header`, `syntax`, `range` or `limit`.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* import { parseX3D } from '@margoengine/loaders/vrml';
|
|
47
|
+
*
|
|
48
|
+
* const scene = parseX3D(`<X3D><Scene><Shape><IndexedFaceSet coordIndex="0 1 2 -1">
|
|
49
|
+
* <Coordinate point="0 0 0 1 0 0 0 1 0"/></IndexedFaceSet></Shape></Scene></X3D>`);
|
|
50
|
+
* console.log(scene.groups?.length);
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export declare function parseX3D(input: BinaryInput | string, options?: VRMLOptions): VRMLScene;
|
package/dist/vrml.js
ADDED
|
@@ -0,0 +1,532 @@
|
|
|
1
|
+
// VRML97 and X3D XML scenes: both encodings become one node tree, flattened to the package's common geometry.
|
|
2
|
+
import { LoaderError } from './types.js';
|
|
3
|
+
import { floats, limits, toText, uints } from './util.js';
|
|
4
|
+
import { triangulate } from '@margoengine/geometry/triangulate';
|
|
5
|
+
import { readXMLTree } from './xml.js';
|
|
6
|
+
const MAX_DEPTH = 256;
|
|
7
|
+
/**
|
|
8
|
+
* Parses a VRML97 (`#VRML V2.0 utf8`) world: Transform, Group, Switch, LOD, Shape, Appearance, Material,
|
|
9
|
+
* IndexedFaceSet and DEF/USE. The transforms are baked in and v turned to Margo's convention (v = 0 at the image's top).
|
|
10
|
+
* Faces are fan-triangulated, or ear-clipped when the IndexedFaceSet says `convex FALSE`. A Switch draws the child
|
|
11
|
+
* its `whichChoice` names (none for -1); an LOD draws its first, most detailed level.
|
|
12
|
+
*
|
|
13
|
+
* @param input The file's bytes or text.
|
|
14
|
+
* @param options Limits.
|
|
15
|
+
* @returns A mesh with a group per Shape and what was left out.
|
|
16
|
+
* @throws LoaderError with code `header`, `syntax`, `range`, `limit` or `unsupported`.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* import { parseVRML } from '@margoengine/loaders/vrml';
|
|
21
|
+
*
|
|
22
|
+
* const scene = parseVRML(`#VRML V2.0 utf8
|
|
23
|
+
* Shape { geometry IndexedFaceSet { coord Coordinate { point [0 0 0, 1 0 0, 0 1 0] } coordIndex [0 1 2 -1] } }`);
|
|
24
|
+
* console.log(scene.positions.length, scene.indices?.length);
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export function parseVRML(input, options = {}) {
|
|
28
|
+
const F = 'VRML', text = toText(input, F);
|
|
29
|
+
const header = /^#(VRML|X3D) V(\d)\.\d[^\n]*/.exec(text.replace(/^/, ''));
|
|
30
|
+
if (!header)
|
|
31
|
+
throw new LoaderError(F, 'header', 'missing #VRML V2.0 utf8 line');
|
|
32
|
+
if (header[1] !== 'VRML' || header[2] !== '2')
|
|
33
|
+
throw new LoaderError(F, 'unsupported', `${header[0].slice(0, 32)} is not supported; pass a VRML97 file`);
|
|
34
|
+
return flatten(readClassic(text, F), F, options);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Parses an X3D XML scene with the same node set as {@link parseVRML}: children are found by their usual
|
|
38
|
+
* `containerField`, and DEF and USE are attributes.
|
|
39
|
+
*
|
|
40
|
+
* @param input The file's bytes or text.
|
|
41
|
+
* @param options Limits.
|
|
42
|
+
* @returns A mesh with a group per Shape and what was left out.
|
|
43
|
+
* @throws LoaderError with code `header`, `syntax`, `range` or `limit`.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* import { parseX3D } from '@margoengine/loaders/vrml';
|
|
48
|
+
*
|
|
49
|
+
* const scene = parseX3D(`<X3D><Scene><Shape><IndexedFaceSet coordIndex="0 1 2 -1">
|
|
50
|
+
* <Coordinate point="0 0 0 1 0 0 0 1 0"/></IndexedFaceSet></Shape></Scene></X3D>`);
|
|
51
|
+
* console.log(scene.groups?.length);
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export function parseX3D(input, options = {}) {
|
|
55
|
+
const F = 'X3D', root = readXMLTree(toText(input, F), F, { external: options.external });
|
|
56
|
+
const x3d = root.name === 'X3D' ? root : root.children.find((child) => child.name === 'X3D'), scene = x3d?.children.find((child) => child.name === 'Scene');
|
|
57
|
+
if (!scene)
|
|
58
|
+
throw new LoaderError(F, 'header', 'missing <X3D> and <Scene> elements');
|
|
59
|
+
const defs = new Map();
|
|
60
|
+
return flatten(scene.children.map((child) => fromXML(child, defs, F)), F, options);
|
|
61
|
+
}
|
|
62
|
+
// VRML97 text: a token scanner and a grammar that needs no field types. Commas are white space.
|
|
63
|
+
function readClassic(text, F) {
|
|
64
|
+
const scan = /(?:[\s,]+|#[^\n]*)*("(?:[^"\\]|\\[\s\S])*"|[[\]{}]|[^\s,"[\]{}#]+)/y;
|
|
65
|
+
let peeked;
|
|
66
|
+
const next = () => {
|
|
67
|
+
if (peeked !== undefined) {
|
|
68
|
+
const token = peeked;
|
|
69
|
+
peeked = undefined;
|
|
70
|
+
return token;
|
|
71
|
+
}
|
|
72
|
+
const from = scan.lastIndex, match = scan.exec(text);
|
|
73
|
+
if (match)
|
|
74
|
+
return match[1];
|
|
75
|
+
if (text
|
|
76
|
+
.slice(from)
|
|
77
|
+
.replace(/#[^\n]*/g, '')
|
|
78
|
+
.trim())
|
|
79
|
+
throw new LoaderError(F, 'syntax', 'a string is never closed; close its quote');
|
|
80
|
+
return null;
|
|
81
|
+
};
|
|
82
|
+
const peek = () => {
|
|
83
|
+
if (peeked === undefined)
|
|
84
|
+
peeked = next();
|
|
85
|
+
return peeked;
|
|
86
|
+
};
|
|
87
|
+
const need = (what) => {
|
|
88
|
+
const token = next();
|
|
89
|
+
if (token === null)
|
|
90
|
+
throw new LoaderError(F, 'syntax', `file ends where ${what} should be`);
|
|
91
|
+
return token;
|
|
92
|
+
};
|
|
93
|
+
const defs = new Map();
|
|
94
|
+
const isNumber = (token) => token !== null && /^[+-]?(\d|\.\d)/.test(token);
|
|
95
|
+
const number = (token) => {
|
|
96
|
+
const value = Number(token);
|
|
97
|
+
if (!Number.isFinite(value))
|
|
98
|
+
throw new LoaderError(F, 'syntax', `invalid number "${token.slice(0, 32)}"`);
|
|
99
|
+
return value;
|
|
100
|
+
};
|
|
101
|
+
const skipBlock = (open, close) => {
|
|
102
|
+
let depth = 0;
|
|
103
|
+
do {
|
|
104
|
+
const token = need(close);
|
|
105
|
+
if (token === open)
|
|
106
|
+
depth++;
|
|
107
|
+
else if (token === close)
|
|
108
|
+
depth--;
|
|
109
|
+
} while (depth > 0);
|
|
110
|
+
};
|
|
111
|
+
const node = (depth) => {
|
|
112
|
+
if (depth > MAX_DEPTH)
|
|
113
|
+
throw new LoaderError(F, 'limit', `nodes nest deeper than ${MAX_DEPTH}`);
|
|
114
|
+
let token = need('a node');
|
|
115
|
+
if (token === 'USE') {
|
|
116
|
+
const name = need('a USE name'), used = defs.get(name);
|
|
117
|
+
if (!used)
|
|
118
|
+
throw new LoaderError(F, 'syntax', `USE ${name.slice(0, 32)} has no DEF before it`);
|
|
119
|
+
return used;
|
|
120
|
+
}
|
|
121
|
+
let def;
|
|
122
|
+
if (token === 'DEF') {
|
|
123
|
+
def = need('a DEF name');
|
|
124
|
+
token = need('a node');
|
|
125
|
+
}
|
|
126
|
+
if (need('{') !== '{')
|
|
127
|
+
throw new LoaderError(F, 'syntax', `expected { after ${token.slice(0, 32)}`);
|
|
128
|
+
const result = { type: token, def, fields: Object.create(null) };
|
|
129
|
+
for (;;) {
|
|
130
|
+
const name = need('a field or }');
|
|
131
|
+
if (name === '}')
|
|
132
|
+
break;
|
|
133
|
+
if (name === 'ROUTE') {
|
|
134
|
+
need('a route');
|
|
135
|
+
need('TO');
|
|
136
|
+
need('a route');
|
|
137
|
+
}
|
|
138
|
+
else if (name === 'PROTO' || name === 'EXTERNPROTO') {
|
|
139
|
+
need('a name');
|
|
140
|
+
skipBlock('[', ']');
|
|
141
|
+
if (name === 'PROTO')
|
|
142
|
+
skipBlock('{', '}');
|
|
143
|
+
else if (peek() === '[')
|
|
144
|
+
skipBlock('[', ']');
|
|
145
|
+
else
|
|
146
|
+
need('a URL');
|
|
147
|
+
}
|
|
148
|
+
else
|
|
149
|
+
result.fields[name] = value(depth);
|
|
150
|
+
}
|
|
151
|
+
if (def !== undefined)
|
|
152
|
+
defs.set(def, result);
|
|
153
|
+
return result;
|
|
154
|
+
};
|
|
155
|
+
const value = (depth) => {
|
|
156
|
+
const token = peek();
|
|
157
|
+
if (token === null)
|
|
158
|
+
throw new LoaderError(F, 'syntax', 'file ends where a field value should be');
|
|
159
|
+
if (token === 'TRUE' || token === 'FALSE') {
|
|
160
|
+
next();
|
|
161
|
+
return token === 'TRUE';
|
|
162
|
+
}
|
|
163
|
+
if (token === 'NULL') {
|
|
164
|
+
next();
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
if (token[0] === '"') {
|
|
168
|
+
next();
|
|
169
|
+
return [token.slice(1, -1).replace(/\\([\s\S])/g, '$1')];
|
|
170
|
+
}
|
|
171
|
+
if (isNumber(token)) {
|
|
172
|
+
const numbers = [];
|
|
173
|
+
while (isNumber(peek()))
|
|
174
|
+
numbers.push(number(next()));
|
|
175
|
+
return numbers;
|
|
176
|
+
}
|
|
177
|
+
if (token === '[') {
|
|
178
|
+
next();
|
|
179
|
+
const after = peek();
|
|
180
|
+
if (after === ']') {
|
|
181
|
+
next();
|
|
182
|
+
return [];
|
|
183
|
+
}
|
|
184
|
+
if (isNumber(after)) {
|
|
185
|
+
const numbers = [];
|
|
186
|
+
while (peek() !== ']')
|
|
187
|
+
numbers.push(number(need(']')));
|
|
188
|
+
next();
|
|
189
|
+
return numbers;
|
|
190
|
+
}
|
|
191
|
+
if (after?.[0] === '"' || after === 'TRUE' || after === 'FALSE') {
|
|
192
|
+
const strings = [];
|
|
193
|
+
while (peek() !== ']')
|
|
194
|
+
strings.push(need(']').replace(/^"|"$/g, ''));
|
|
195
|
+
next();
|
|
196
|
+
return strings;
|
|
197
|
+
}
|
|
198
|
+
const nodes = [];
|
|
199
|
+
while (peek() !== ']')
|
|
200
|
+
nodes.push(node(depth + 1));
|
|
201
|
+
next();
|
|
202
|
+
return nodes;
|
|
203
|
+
}
|
|
204
|
+
return node(depth + 1);
|
|
205
|
+
};
|
|
206
|
+
const nodes = [];
|
|
207
|
+
for (let token = peek(); token !== null; token = peek()) {
|
|
208
|
+
if (token === 'ROUTE') {
|
|
209
|
+
next();
|
|
210
|
+
need('a route');
|
|
211
|
+
need('TO');
|
|
212
|
+
need('a route');
|
|
213
|
+
}
|
|
214
|
+
else if (token === 'PROTO' || token === 'EXTERNPROTO') {
|
|
215
|
+
next();
|
|
216
|
+
need('a name');
|
|
217
|
+
skipBlock('[', ']');
|
|
218
|
+
if (token === 'PROTO')
|
|
219
|
+
skipBlock('{', '}');
|
|
220
|
+
else if (peek() === '[')
|
|
221
|
+
skipBlock('[', ']');
|
|
222
|
+
else
|
|
223
|
+
need('a URL');
|
|
224
|
+
}
|
|
225
|
+
else
|
|
226
|
+
nodes.push(node(0));
|
|
227
|
+
}
|
|
228
|
+
return nodes;
|
|
229
|
+
}
|
|
230
|
+
// X3D's default containerField per element; every other child goes to `children`.
|
|
231
|
+
const CONTAINER = {
|
|
232
|
+
Appearance: 'appearance',
|
|
233
|
+
Material: 'material',
|
|
234
|
+
IndexedFaceSet: 'geometry',
|
|
235
|
+
Coordinate: 'coord',
|
|
236
|
+
Color: 'color',
|
|
237
|
+
ColorRGBA: 'color',
|
|
238
|
+
Normal: 'normal',
|
|
239
|
+
TextureCoordinate: 'texCoord',
|
|
240
|
+
ImageTexture: 'texture',
|
|
241
|
+
};
|
|
242
|
+
function fromXML(element, defs, F) {
|
|
243
|
+
const { USE: use, DEF: def } = element.attributes;
|
|
244
|
+
if (use !== undefined) {
|
|
245
|
+
const used = defs.get(use);
|
|
246
|
+
if (!used)
|
|
247
|
+
throw new LoaderError(F, 'syntax', `USE ${use.slice(0, 32)} has no DEF before it`);
|
|
248
|
+
return used;
|
|
249
|
+
}
|
|
250
|
+
const result = { type: element.name, def, fields: Object.create(null) };
|
|
251
|
+
for (const [name, text] of Object.entries(element.attributes)) {
|
|
252
|
+
if (name === 'DEF' || name === 'containerField')
|
|
253
|
+
continue;
|
|
254
|
+
const parts = text.split(/[\s,]+/).filter(Boolean);
|
|
255
|
+
if (/^(true|false)$/i.test(text.trim()))
|
|
256
|
+
result.fields[name] = /^true$/i.test(text.trim());
|
|
257
|
+
else if (parts.length && parts.every((part) => /^[+-]?(\d|\.\d)/.test(part))) {
|
|
258
|
+
const numbers = parts.map(Number);
|
|
259
|
+
if (!numbers.every(Number.isFinite))
|
|
260
|
+
throw new LoaderError(F, 'syntax', `invalid number in ${name}="${text.slice(0, 32)}"`);
|
|
261
|
+
result.fields[name] = numbers;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
for (const child of element.children) {
|
|
265
|
+
const container = child.attributes.containerField ?? CONTAINER[child.name] ?? 'children', item = fromXML(child, defs, F);
|
|
266
|
+
if (container === 'children')
|
|
267
|
+
(result.fields.children ??= []).push(item);
|
|
268
|
+
else
|
|
269
|
+
result.fields[container] = item;
|
|
270
|
+
}
|
|
271
|
+
if (def !== undefined)
|
|
272
|
+
defs.set(def, result);
|
|
273
|
+
return result;
|
|
274
|
+
}
|
|
275
|
+
const IDENTITY = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
|
|
276
|
+
function multiply(a, b) {
|
|
277
|
+
const out = new Array(16);
|
|
278
|
+
for (let c = 0; c < 4; c++)
|
|
279
|
+
for (let r = 0; r < 4; r++)
|
|
280
|
+
out[c * 4 + r] = a[r] * b[c * 4] + a[4 + r] * b[c * 4 + 1] + a[8 + r] * b[c * 4 + 2] + a[12 + r] * b[c * 4 + 3];
|
|
281
|
+
return out;
|
|
282
|
+
}
|
|
283
|
+
const translate = (x, y, z) => [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1];
|
|
284
|
+
function rotate(r) {
|
|
285
|
+
const length = Math.hypot(r[0], r[1], r[2]);
|
|
286
|
+
if (!length || !r[3])
|
|
287
|
+
return IDENTITY;
|
|
288
|
+
const x = r[0] / length, y = r[1] / length, z = r[2] / length, c = Math.cos(r[3]), s = Math.sin(r[3]), t = 1 - c;
|
|
289
|
+
return [
|
|
290
|
+
t * x * x + c,
|
|
291
|
+
t * x * y + s * z,
|
|
292
|
+
t * x * z - s * y,
|
|
293
|
+
0,
|
|
294
|
+
t * x * y - s * z,
|
|
295
|
+
t * y * y + c,
|
|
296
|
+
t * y * z + s * x,
|
|
297
|
+
0,
|
|
298
|
+
t * x * z + s * y,
|
|
299
|
+
t * y * z - s * x,
|
|
300
|
+
t * z * z + c,
|
|
301
|
+
0,
|
|
302
|
+
0,
|
|
303
|
+
0,
|
|
304
|
+
0,
|
|
305
|
+
1,
|
|
306
|
+
];
|
|
307
|
+
}
|
|
308
|
+
const numbers = (node, field) => {
|
|
309
|
+
const value = node?.fields[field];
|
|
310
|
+
return Array.isArray(value) && (value.length === 0 || typeof value[0] === 'number') ? value : undefined;
|
|
311
|
+
};
|
|
312
|
+
const child = (node, field) => {
|
|
313
|
+
const value = node?.fields[field];
|
|
314
|
+
return value && !Array.isArray(value) && typeof value === 'object' ? value : undefined;
|
|
315
|
+
};
|
|
316
|
+
const flag = (node, field, fallback) => {
|
|
317
|
+
const value = node.fields[field];
|
|
318
|
+
return typeof value === 'boolean' ? value : fallback;
|
|
319
|
+
};
|
|
320
|
+
function flatten(roots, F, options) {
|
|
321
|
+
const { maxVertices, maxTriangles } = limits(options, F), maxNodes = options.maxNodes ?? 1_000_000;
|
|
322
|
+
if (!Number.isSafeInteger(maxNodes) || maxNodes < 0)
|
|
323
|
+
throw new LoaderError(F, 'range', 'maxNodes must be a non-negative integer');
|
|
324
|
+
const positions = floats(), normals = floats(), colors = floats(), uvs = floats(), indices = uints(), groups = [], warnings = new Set();
|
|
325
|
+
let visited = 0, allNormals = true, anyColor = false, anyUV = false;
|
|
326
|
+
const shape = (node, matrix) => {
|
|
327
|
+
const geometry = child(node, 'geometry');
|
|
328
|
+
if (!geometry)
|
|
329
|
+
return;
|
|
330
|
+
if (geometry.type !== 'IndexedFaceSet') {
|
|
331
|
+
warnings.add(geometry.type);
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
const material = child(child(node, 'appearance'), 'material'), texture = child(child(node, 'appearance'), 'texture');
|
|
335
|
+
if (texture)
|
|
336
|
+
warnings.add(texture.type);
|
|
337
|
+
const point = numbers(child(geometry, 'coord'), 'point') ?? [], coordIndex = numbers(geometry, 'coordIndex') ?? [], normal = numbers(child(geometry, 'normal'), 'vector'), colorNode = child(geometry, 'color'), color = numbers(colorNode, 'color'), colorWidth = colorNode?.type === 'ColorRGBA' ? 4 : 3, texCoord = numbers(child(geometry, 'texCoord'), 'point');
|
|
338
|
+
const pick = (field, perVertex) => {
|
|
339
|
+
const own = numbers(geometry, field);
|
|
340
|
+
return own?.length ? own : perVertex ? coordIndex : undefined;
|
|
341
|
+
};
|
|
342
|
+
const colorPerVertex = flag(geometry, 'colorPerVertex', true), normalPerVertex = flag(geometry, 'normalPerVertex', true), colorIndex = pick('colorIndex', colorPerVertex), normalIndex = pick('normalIndex', normalPerVertex), texIndex = pick('texCoordIndex', true);
|
|
343
|
+
// Inverse transpose for normals: the upper 3 x 3's cofactors; only the direction matters.
|
|
344
|
+
const m = matrix, inverse = [
|
|
345
|
+
m[5] * m[10] - m[6] * m[9],
|
|
346
|
+
m[6] * m[8] - m[4] * m[10],
|
|
347
|
+
m[4] * m[9] - m[5] * m[8],
|
|
348
|
+
m[2] * m[9] - m[1] * m[10],
|
|
349
|
+
m[0] * m[10] - m[2] * m[8],
|
|
350
|
+
m[1] * m[8] - m[0] * m[9],
|
|
351
|
+
m[1] * m[6] - m[2] * m[5],
|
|
352
|
+
m[2] * m[4] - m[0] * m[6],
|
|
353
|
+
m[0] * m[5] - m[1] * m[4],
|
|
354
|
+
], determinant = m[0] * inverse[0] + m[1] * inverse[1] + m[2] * inverse[2], keepWinding = flag(geometry, 'ccw', true) === determinant >= 0;
|
|
355
|
+
const start = indices.length, convex = flag(geometry, 'convex', true), flat = [];
|
|
356
|
+
let first = 0, face = 0;
|
|
357
|
+
const read = (list, index, width, what, out) => {
|
|
358
|
+
if (!Number.isInteger(index) || index < 0 || index * width + width > list.length)
|
|
359
|
+
throw new LoaderError(F, 'range', `${what} index ${index} is outside the ${list.length / width} values`);
|
|
360
|
+
out.length = 0;
|
|
361
|
+
for (let k = 0; k < width; k++)
|
|
362
|
+
out.push(list[index * width + k]);
|
|
363
|
+
return out;
|
|
364
|
+
};
|
|
365
|
+
const scratch = [];
|
|
366
|
+
for (let i = 0; i <= coordIndex.length; i++) {
|
|
367
|
+
if (i < coordIndex.length && coordIndex[i] !== -1)
|
|
368
|
+
continue;
|
|
369
|
+
const corners = i - first;
|
|
370
|
+
if (corners >= 3) {
|
|
371
|
+
const base = positions.length / 3;
|
|
372
|
+
if (base + corners > maxVertices)
|
|
373
|
+
throw new LoaderError(F, 'limit', `vertex count exceeds the limit of ${maxVertices}`);
|
|
374
|
+
if (indices.length / 3 + corners - 2 > maxTriangles)
|
|
375
|
+
throw new LoaderError(F, 'limit', `triangle count exceeds the limit of ${maxTriangles}`);
|
|
376
|
+
for (let c = 0; c < corners; c++) {
|
|
377
|
+
const at = first + c, p = read(point, coordIndex[at], 3, 'coord', scratch);
|
|
378
|
+
positions.push3(m[0] * p[0] + m[4] * p[1] + m[8] * p[2] + m[12], m[1] * p[0] + m[5] * p[1] + m[9] * p[2] + m[13], m[2] * p[0] + m[6] * p[1] + m[10] * p[2] + m[14]);
|
|
379
|
+
if (normal?.length) {
|
|
380
|
+
const n = read(normal, normalIndex?.[normalPerVertex ? at : face] ?? face, 3, 'normal', scratch), x = inverse[0] * n[0] + inverse[3] * n[1] + inverse[6] * n[2], y = inverse[1] * n[0] + inverse[4] * n[1] + inverse[7] * n[2], z = inverse[2] * n[0] + inverse[5] * n[1] + inverse[8] * n[2], length = Math.hypot(x, y, z) * (determinant < 0 ? -1 : 1) || 1;
|
|
381
|
+
normals.push3(x / length, y / length, z / length);
|
|
382
|
+
}
|
|
383
|
+
else
|
|
384
|
+
allNormals = false;
|
|
385
|
+
if (color?.length) {
|
|
386
|
+
const rgb = read(color, colorIndex?.[colorPerVertex ? at : face] ?? face, colorWidth, 'color', scratch);
|
|
387
|
+
colors.push3(rgb[0], rgb[1], rgb[2]);
|
|
388
|
+
anyColor = true;
|
|
389
|
+
}
|
|
390
|
+
else {
|
|
391
|
+
// VRML97 6.27 and 6.3: a Material's diffuseColor defaults to 0.8; no Material at all is unlit white.
|
|
392
|
+
const diffuse = material ? (numbers(material, 'diffuseColor') ?? [0.8, 0.8, 0.8]) : [1, 1, 1];
|
|
393
|
+
colors.push3(diffuse[0], diffuse[1], diffuse[2]);
|
|
394
|
+
}
|
|
395
|
+
if (texCoord?.length) {
|
|
396
|
+
const uv = read(texCoord, texIndex[at], 2, 'texCoord', scratch);
|
|
397
|
+
uvs.push(uv[0]);
|
|
398
|
+
uvs.push(1 - uv[1]);
|
|
399
|
+
anyUV = true;
|
|
400
|
+
}
|
|
401
|
+
else {
|
|
402
|
+
uvs.push(0);
|
|
403
|
+
uvs.push(0);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
if (convex || corners === 3)
|
|
407
|
+
for (let c = 1; c < corners - 1; c++)
|
|
408
|
+
if (keepWinding)
|
|
409
|
+
indices.push3(base, base + c, base + c + 1);
|
|
410
|
+
else
|
|
411
|
+
indices.push3(base, base + c + 1, base + c);
|
|
412
|
+
else {
|
|
413
|
+
// VRML97 6.23: convex FALSE faces may be concave. Project onto the plane of the face's Newell normal, keeping
|
|
414
|
+
// its turn, and ear-clip; triangulate returns counter-clockwise triples, the face's own winding.
|
|
415
|
+
let nx = 0, ny = 0, nz = 0;
|
|
416
|
+
const p = positions.array;
|
|
417
|
+
for (let c = 0, d = corners - 1; c < corners; d = c++) {
|
|
418
|
+
const a = (base + d) * 3, b = (base + c) * 3;
|
|
419
|
+
nx += (p[a + 1] - p[b + 1]) * (p[a + 2] + p[b + 2]);
|
|
420
|
+
ny += (p[a + 2] - p[b + 2]) * (p[a] + p[b]);
|
|
421
|
+
nz += (p[a] - p[b]) * (p[a + 1] + p[b + 1]);
|
|
422
|
+
}
|
|
423
|
+
const ax = Math.abs(nx), ay = Math.abs(ny), az = Math.abs(nz), [u, w] = az >= ax && az >= ay
|
|
424
|
+
? nz >= 0
|
|
425
|
+
? [0, 1]
|
|
426
|
+
: [1, 0]
|
|
427
|
+
: ax >= ay
|
|
428
|
+
? nx >= 0
|
|
429
|
+
? [1, 2]
|
|
430
|
+
: [2, 1]
|
|
431
|
+
: ny >= 0
|
|
432
|
+
? [2, 0]
|
|
433
|
+
: [0, 2];
|
|
434
|
+
flat.length = 0;
|
|
435
|
+
for (let c = 0; c < corners; c++)
|
|
436
|
+
flat.push(p[(base + c) * 3 + u], p[(base + c) * 3 + w]);
|
|
437
|
+
const triangles = triangulate(flat, [], 2);
|
|
438
|
+
for (let t = 0; t < triangles.length; t += 3)
|
|
439
|
+
if (keepWinding)
|
|
440
|
+
indices.push3(base + triangles[t], base + triangles[t + 1], base + triangles[t + 2]);
|
|
441
|
+
else
|
|
442
|
+
indices.push3(base + triangles[t], base + triangles[t + 2], base + triangles[t + 1]);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
first = i + 1;
|
|
446
|
+
face++;
|
|
447
|
+
}
|
|
448
|
+
if (indices.length === start)
|
|
449
|
+
return;
|
|
450
|
+
const group = { start, count: indices.length - start };
|
|
451
|
+
const name = node.def ?? geometry.def;
|
|
452
|
+
if (name !== undefined)
|
|
453
|
+
group.name = name;
|
|
454
|
+
if (material) {
|
|
455
|
+
const diffuse = numbers(material, 'diffuseColor') ?? [0.8, 0.8, 0.8], transparency = numbers(material, 'transparency')?.[0] ?? 0;
|
|
456
|
+
group.color = [diffuse[0], diffuse[1], diffuse[2], 1 - transparency];
|
|
457
|
+
}
|
|
458
|
+
groups.push(group);
|
|
459
|
+
};
|
|
460
|
+
const walk = (node, matrix, depth) => {
|
|
461
|
+
if (++visited > maxNodes)
|
|
462
|
+
throw new LoaderError(F, 'limit', `node count exceeds the limit of ${maxNodes}`);
|
|
463
|
+
if (depth > MAX_DEPTH)
|
|
464
|
+
throw new LoaderError(F, 'limit', `nodes nest deeper than ${MAX_DEPTH}`);
|
|
465
|
+
let world = matrix;
|
|
466
|
+
switch (node.type) {
|
|
467
|
+
case 'Shape':
|
|
468
|
+
shape(node, matrix);
|
|
469
|
+
return;
|
|
470
|
+
case 'Transform':
|
|
471
|
+
case 'CADPart': {
|
|
472
|
+
const t = numbers(node, 'translation') ?? [0, 0, 0], c = numbers(node, 'center') ?? [0, 0, 0], r = numbers(node, 'rotation') ?? [0, 0, 1, 0], s = numbers(node, 'scale') ?? [1, 1, 1], o = numbers(node, 'scaleOrientation') ?? [0, 0, 1, 0];
|
|
473
|
+
// VRML97 6.52: T * C * R * SR * S * -SR * -C.
|
|
474
|
+
const scale = [s[0], 0, 0, 0, 0, s[1], 0, 0, 0, 0, s[2], 0, 0, 0, 0, 1];
|
|
475
|
+
for (const part of [
|
|
476
|
+
translate(t[0], t[1], t[2]),
|
|
477
|
+
translate(c[0], c[1], c[2]),
|
|
478
|
+
rotate(r),
|
|
479
|
+
rotate(o),
|
|
480
|
+
scale,
|
|
481
|
+
rotate([o[0], o[1], o[2], -o[3]]),
|
|
482
|
+
translate(-c[0], -c[1], -c[2]),
|
|
483
|
+
])
|
|
484
|
+
world = multiply(world, part);
|
|
485
|
+
break;
|
|
486
|
+
}
|
|
487
|
+
case 'Switch':
|
|
488
|
+
case 'LOD': {
|
|
489
|
+
// VRML97 6.46 Switch draws choice[whichChoice] (default -1, nothing); 6.26 LOD draws one of its levels, here
|
|
490
|
+
// the first and most detailed, as there is no viewer distance. X3D names both lists `children`.
|
|
491
|
+
const list = node.fields[node.type === 'Switch' ? 'choice' : 'level'] ?? node.fields.children, items = Array.isArray(list) ? list : list && typeof list === 'object' ? [list] : [], pick = node.type === 'Switch' ? (numbers(node, 'whichChoice')?.[0] ?? -1) : 0;
|
|
492
|
+
if (Number.isInteger(pick) && pick >= 0 && pick < items.length)
|
|
493
|
+
walk(items[pick], world, depth + 1);
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
case 'Group':
|
|
497
|
+
case 'Anchor':
|
|
498
|
+
case 'Collision':
|
|
499
|
+
case 'Billboard':
|
|
500
|
+
case 'StaticGroup':
|
|
501
|
+
case 'CADLayer':
|
|
502
|
+
case 'CADAssembly':
|
|
503
|
+
case 'CADFace':
|
|
504
|
+
break;
|
|
505
|
+
default:
|
|
506
|
+
warnings.add(node.type);
|
|
507
|
+
return;
|
|
508
|
+
}
|
|
509
|
+
const children = node.fields.children;
|
|
510
|
+
if (Array.isArray(children))
|
|
511
|
+
for (const item of children)
|
|
512
|
+
walk(item, world, depth + 1);
|
|
513
|
+
else if (children && typeof children === 'object')
|
|
514
|
+
walk(children, world, depth + 1);
|
|
515
|
+
// The X3D CAD component: a CADFace holds its Shape in a field of its own.
|
|
516
|
+
if (node.type === 'CADFace' && node.fields.shape)
|
|
517
|
+
walk(node.fields.shape, world, depth + 1);
|
|
518
|
+
};
|
|
519
|
+
for (const root of roots)
|
|
520
|
+
walk(root, IDENTITY, 0);
|
|
521
|
+
const count = positions.length / 3, trim = (list) => list.array.slice(0, list.length);
|
|
522
|
+
return {
|
|
523
|
+
kind: 'mesh',
|
|
524
|
+
positions: trim(positions),
|
|
525
|
+
...(allNormals && count ? { normals: trim(normals) } : {}),
|
|
526
|
+
...(anyColor ? { colors: trim(colors) } : {}),
|
|
527
|
+
...(anyUV ? { uvs: trim(uvs) } : {}),
|
|
528
|
+
indices: indices.array.slice(0, indices.length),
|
|
529
|
+
groups,
|
|
530
|
+
warnings: [...warnings],
|
|
531
|
+
};
|
|
532
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
type Numbers = ArrayLike<number>;
|
|
2
|
+
/** Internal shared VTK surface builder. */
|
|
3
|
+
export declare class Builder {
|
|
4
|
+
readonly maxTriangles: number;
|
|
5
|
+
readonly outer: boolean;
|
|
6
|
+
readonly positions: import("./util.js").Growable<Float32Array<ArrayBufferLike>>;
|
|
7
|
+
readonly triangles: import("./util.js").Growable<Uint32Array<ArrayBufferLike>>;
|
|
8
|
+
readonly cells: import("./util.js").Growable<Uint32Array<ArrayBufferLike>>;
|
|
9
|
+
readonly lines: import("./util.js").Growable<Uint32Array<ArrayBufferLike>>;
|
|
10
|
+
readonly warnings: Set<string>;
|
|
11
|
+
/** A face of a volume cell by its sorted corners; `null` once two cells share it. */
|
|
12
|
+
readonly faces: Map<string, {
|
|
13
|
+
corners: number[];
|
|
14
|
+
cell: number;
|
|
15
|
+
} | null>;
|
|
16
|
+
/** Coordinates, three per point, that polygons are ear-clipped on; `positions` when null. */
|
|
17
|
+
coords: ArrayLike<number> | null;
|
|
18
|
+
cellCount: number;
|
|
19
|
+
constructor(maxTriangles: number, outer: boolean);
|
|
20
|
+
check(index: number, points: number): number;
|
|
21
|
+
polygon(corners: number[], cell: number): void;
|
|
22
|
+
strip(corners: number[], cell: number): void;
|
|
23
|
+
polyline(corners: number[]): void;
|
|
24
|
+
face(corners: number[], cell: number): void;
|
|
25
|
+
finish(): void;
|
|
26
|
+
}
|
|
27
|
+
/** Emits one retained VTK cell to the shared surface builder. */
|
|
28
|
+
export declare function gridCell(type: number, corners: number[], cell: number, builder: Builder, stream: Numbers | undefined, from: number, points: number, base: number): void;
|
|
29
|
+
export {};
|