@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/common.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { LoaderError } from './types.js';
|
|
2
|
+
export type { LoadedGeometry, LoadedGroup, ParseLimits, LoaderErrorCode, AssetResolver } from './types.js';
|
|
3
|
+
export type { BinaryInput } from './util.js';
|
|
4
|
+
export { fetchBytes, DEFAULT_MAX_BYTES, type FetchOptions } from './load.js';
|
|
5
|
+
export { readZipDirectory, readZipEntry, crc32 } from './zip.js';
|
|
6
|
+
export type { ZipEntry, ZipOptions } from './zip.js';
|
|
7
|
+
export { computeVertexNormals, faceNormal } from './normals.js';
|
package/dist/common.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// Building blocks shared by format loaders: `@margoengine/loaders/common`. Packages that read their own formats (usdz,
|
|
2
|
+
// ifc) import these instead of the root, which loads every parser.
|
|
3
|
+
export { LoaderError } from './types.js';
|
|
4
|
+
export { fetchBytes, DEFAULT_MAX_BYTES } from './load.js';
|
|
5
|
+
export { readZipDirectory, readZipEntry, crc32 } from './zip.js';
|
|
6
|
+
export { computeVertexNormals, faceNormal } from './normals.js';
|
package/dist/crease.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type LoadedGeometry } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* `loaded` with a normal for every corner: a vertex whose corners get different normals is copied, at the end of each
|
|
4
|
+
* per-vertex array, so indices into the first vertices (lines included) keep their meaning. Groups are unchanged.
|
|
5
|
+
* Returned as it is when it has normals of its own, has no triangles, or `angle` is 180.
|
|
6
|
+
*/
|
|
7
|
+
export declare function creased(loaded: LoadedGeometry, angle?: number): LoadedGeometry;
|
package/dist/crease.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// Sharp edges for a mesh whose file has no normals: createMeshes and createPBRMeshes smooth only across faces within
|
|
2
|
+
// a crease angle of each other, so a box keeps its edges and a curved surface stays smooth.
|
|
3
|
+
import { creasedNormals } from '@margoengine/geometry/utils';
|
|
4
|
+
import { LoaderError } from './types.js';
|
|
5
|
+
/** The crease angle in degrees when none is given, as for create3MFMesh. */
|
|
6
|
+
const CREASE_ANGLE = 40;
|
|
7
|
+
/**
|
|
8
|
+
* `loaded` with a normal for every corner: a vertex whose corners get different normals is copied, at the end of each
|
|
9
|
+
* per-vertex array, so indices into the first vertices (lines included) keep their meaning. Groups are unchanged.
|
|
10
|
+
* Returned as it is when it has normals of its own, has no triangles, or `angle` is 180.
|
|
11
|
+
*/
|
|
12
|
+
export function creased(loaded, angle = CREASE_ANGLE) {
|
|
13
|
+
if (!(angle >= 0 && angle <= 180))
|
|
14
|
+
throw new LoaderError('Engine', 'range', 'creaseAngle must be 0 to 180 degrees');
|
|
15
|
+
const { positions, indices, normals } = loaded;
|
|
16
|
+
if (loaded.kind !== 'mesh' || normals || !indices?.length || angle === 180)
|
|
17
|
+
return loaded;
|
|
18
|
+
const count = positions.length / 3;
|
|
19
|
+
// A malformed mesh is left for toGeometries to refuse.
|
|
20
|
+
if (indices.length % 3 || indices.some((v) => v >= count))
|
|
21
|
+
return loaded;
|
|
22
|
+
const corners = new Float32Array(indices.length * 3);
|
|
23
|
+
for (let c = 0; c < indices.length; c++)
|
|
24
|
+
for (let k = 0; k < 3; k++)
|
|
25
|
+
corners[c * 3 + k] = positions[indices[c] * 3 + k];
|
|
26
|
+
const own = creasedNormals(corners, { angle, tolerance: 0 }), out = indices.slice(),
|
|
27
|
+
/** The input vertex of each output vertex, and the first corner that gave it its normal. */
|
|
28
|
+
source = Array.from({ length: count }, (_, v) => v), corner = new Array(count).fill(-1), next = new Array(count).fill(-1);
|
|
29
|
+
for (let c = 0; c < indices.length; c++) {
|
|
30
|
+
let at = indices[c];
|
|
31
|
+
while (corner[at] >= 0 && ![0, 1, 2].every((k) => own[corner[at] * 3 + k] === own[c * 3 + k])) {
|
|
32
|
+
if (next[at] < 0) {
|
|
33
|
+
next[at] = source.length;
|
|
34
|
+
source.push(indices[c]);
|
|
35
|
+
corner.push(-1);
|
|
36
|
+
next.push(-1);
|
|
37
|
+
}
|
|
38
|
+
at = next[at];
|
|
39
|
+
}
|
|
40
|
+
if (corner[at] < 0)
|
|
41
|
+
corner[at] = c;
|
|
42
|
+
out[c] = at;
|
|
43
|
+
}
|
|
44
|
+
const copied = (values) => {
|
|
45
|
+
const size = values.length / count;
|
|
46
|
+
if (!Number.isInteger(size))
|
|
47
|
+
return values;
|
|
48
|
+
const result = new Float32Array(source.length * size);
|
|
49
|
+
result.set(values);
|
|
50
|
+
for (let v = count; v < source.length; v++)
|
|
51
|
+
result.set(values.subarray(source[v] * size, (source[v] + 1) * size), v * size);
|
|
52
|
+
return result;
|
|
53
|
+
};
|
|
54
|
+
const made = new Float32Array(source.length * 3);
|
|
55
|
+
corner.forEach((c, v) => {
|
|
56
|
+
if (c >= 0)
|
|
57
|
+
made.set(own.subarray(c * 3, c * 3 + 3), v * 3);
|
|
58
|
+
});
|
|
59
|
+
const result = { ...loaded, positions: copied(positions), normals: made, indices: out };
|
|
60
|
+
if (loaded.colors)
|
|
61
|
+
result.colors = copied(loaded.colors);
|
|
62
|
+
if (loaded.uvs)
|
|
63
|
+
result.uvs = copied(loaded.uvs);
|
|
64
|
+
if (loaded.attributes)
|
|
65
|
+
result.attributes = Object.fromEntries(Object.entries(loaded.attributes).map(([name, values]) => [name, copied(values)]));
|
|
66
|
+
return result;
|
|
67
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { type DAETransform } from './dae-common.js';
|
|
2
|
+
import type { DAEMorph } from './dae-controllers.js';
|
|
3
|
+
import type { XMLElement } from './xml.js';
|
|
4
|
+
/** How a sampler moves from one key to the next. */
|
|
5
|
+
export type DAEInterpolation = 'LINEAR' | 'STEP' | 'BEZIER' | 'HERMITE';
|
|
6
|
+
/** A `<sampler>`: key times, values and per-key interpolation, as written (the file's axes and units). */
|
|
7
|
+
export interface DAESampler {
|
|
8
|
+
/** Key times in seconds, increasing. */
|
|
9
|
+
input: Float64Array;
|
|
10
|
+
/** `stride` values per key. */
|
|
11
|
+
output: Float64Array;
|
|
12
|
+
stride: number;
|
|
13
|
+
/** The interpolation from each key to the next. */
|
|
14
|
+
interpolations: DAEInterpolation[];
|
|
15
|
+
/** BEZIER and HERMITE tangents, `tangentStride` values per key: `(time, value)` pairs per component, or values. */
|
|
16
|
+
inTangents?: Float64Array;
|
|
17
|
+
outTangents?: Float64Array;
|
|
18
|
+
tangentStride?: number;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* A `<channel>`. A transform channel names a node (`node`) and one element of its transform stack (`transform`, the
|
|
22
|
+
* element's sid); a weights channel names a morph (`morph`). `index` is the first value written: a component of the
|
|
23
|
+
* transform's values (`.X` 0, `.Y` 1, `.Z` 2, `.ANGLE` 3, `(i)`, and `(row)(column)` as row * 4 + column in a
|
|
24
|
+
* matrix's row-major values) or a weight `(i)`; without it the channel writes all the values.
|
|
25
|
+
*/
|
|
26
|
+
export interface DAEAnimationChannel {
|
|
27
|
+
/** The `target` attribute as written. */
|
|
28
|
+
target: string;
|
|
29
|
+
node?: string;
|
|
30
|
+
transform?: string;
|
|
31
|
+
morph?: number;
|
|
32
|
+
index?: number;
|
|
33
|
+
sampler: DAESampler;
|
|
34
|
+
}
|
|
35
|
+
/** An `<animation>`; nested animations are listed too, each naming its `parent` (-1 at the top). */
|
|
36
|
+
export interface DAEAnimation {
|
|
37
|
+
id?: string;
|
|
38
|
+
name?: string;
|
|
39
|
+
parent: number;
|
|
40
|
+
channels: DAEAnimationChannel[];
|
|
41
|
+
}
|
|
42
|
+
/** An `<animation_clip>`: a time range over some animations (and the animations nested in them). */
|
|
43
|
+
export interface DAEClip {
|
|
44
|
+
id?: string;
|
|
45
|
+
name?: string;
|
|
46
|
+
/** Start in seconds (default 0). */
|
|
47
|
+
start: number;
|
|
48
|
+
/** End in seconds, when given. */
|
|
49
|
+
end?: number;
|
|
50
|
+
/** Indices into `LoadedDAE.animations`. */
|
|
51
|
+
animations: number[];
|
|
52
|
+
}
|
|
53
|
+
/** Reads every `<animation>` (nested ones too) and `<animation_clip>`. */
|
|
54
|
+
export declare function readAnimations(root: XMLElement, ids: Map<string, XMLElement>, morphs: readonly DAEMorph[], warn: (message: string) => void): {
|
|
55
|
+
animations: DAEAnimation[];
|
|
56
|
+
clips: DAEClip[];
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* A sampler's values at `time` seconds: the first key's before it, the last key's after it, else the segment's
|
|
60
|
+
* interpolation. BEZIER segments are 2D curves through (time, value) control points: OUT_TANGENT of the key and
|
|
61
|
+
* IN_TANGENT of the next one, solved for the time (value-only tangents sit at a third of the segment). HERMITE uses the
|
|
62
|
+
* value part of the tangents as derivatives over the segment's normalised time.
|
|
63
|
+
*/
|
|
64
|
+
export declare function sampleDAESampler(sampler: DAESampler, time: number, out?: Float64Array): Float64Array;
|
|
65
|
+
/** The transform values a node's channels give at `time` (copies; the node's own stack is left as is). */
|
|
66
|
+
export declare function animateTransforms(transforms: readonly DAETransform[], channels: readonly DAEAnimationChannel[], time: number): DAETransform[];
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
// COLLADA <library_animations> and <library_animation_clips>: samplers (LINEAR, STEP, BEZIER, HERMITE) and channels
|
|
2
|
+
// that address node transform elements or morph weights. Written from the COLLADA 1.4.1 and 1.5 specifications.
|
|
3
|
+
import { LoaderError } from './types.js';
|
|
4
|
+
import { block, F, kid, kids, numbers, ref, sourcesOf, TRANSFORM_SIZES } from './dae-common.js';
|
|
5
|
+
const SELECTORS = {
|
|
6
|
+
X: 0,
|
|
7
|
+
Y: 1,
|
|
8
|
+
Z: 2,
|
|
9
|
+
W: 3,
|
|
10
|
+
ANGLE: 3,
|
|
11
|
+
R: 0,
|
|
12
|
+
G: 1,
|
|
13
|
+
B: 2,
|
|
14
|
+
A: 3,
|
|
15
|
+
U: 0,
|
|
16
|
+
V: 1,
|
|
17
|
+
S: 0,
|
|
18
|
+
T: 1,
|
|
19
|
+
P: 2,
|
|
20
|
+
Q: 3,
|
|
21
|
+
};
|
|
22
|
+
/** A target's last part: the sid or id, and the value index its `.member` or `(i)(j)` selector names. */
|
|
23
|
+
function selector(part, rows = 4) {
|
|
24
|
+
const match = /^([^.(]*)(?:\.([A-Za-z]+)|((?:\(\d+\))+))?$/.exec(part);
|
|
25
|
+
if (!match)
|
|
26
|
+
return;
|
|
27
|
+
const [, name, member, array] = match;
|
|
28
|
+
if (member !== undefined) {
|
|
29
|
+
const index = SELECTORS[member.toUpperCase()];
|
|
30
|
+
return index === undefined ? undefined : { name, index };
|
|
31
|
+
}
|
|
32
|
+
if (array === undefined)
|
|
33
|
+
return { name };
|
|
34
|
+
const indices = [...array.matchAll(/\((\d+)\)/g)].map((m) => Number(m[1]));
|
|
35
|
+
if (indices.length > 2)
|
|
36
|
+
return;
|
|
37
|
+
return { name, index: indices.length === 2 ? indices[0] * rows + indices[1] : indices[0] };
|
|
38
|
+
}
|
|
39
|
+
const KINDS = ['LINEAR', 'STEP', 'BEZIER', 'HERMITE'];
|
|
40
|
+
function readSampler(sampler, source, what, warn) {
|
|
41
|
+
const inputs = new Map(kids(sampler, 'input').map((i) => [i.attributes.semantic, i.attributes.source]));
|
|
42
|
+
if (!inputs.has('INPUT') || !inputs.has('OUTPUT'))
|
|
43
|
+
throw new LoaderError(F, 'syntax', `${what} sampler needs INPUT and OUTPUT inputs`);
|
|
44
|
+
const input = source(inputs.get('INPUT')), output = source(inputs.get('OUTPUT')), count = input.count || input.data.length, times = new Float64Array(count);
|
|
45
|
+
for (let k = 0; k < count; k++) {
|
|
46
|
+
const time = input.data[input.offset + k * input.stride];
|
|
47
|
+
if (!Number.isFinite(time) || (k && time < times[k - 1]))
|
|
48
|
+
throw new LoaderError(F, 'syntax', `${what} key ${k} time ${time} is not finite and increasing`);
|
|
49
|
+
times[k] = time;
|
|
50
|
+
}
|
|
51
|
+
if (!count)
|
|
52
|
+
throw new LoaderError(F, 'syntax', `${what} sampler has no keys`);
|
|
53
|
+
const stride = output.stride, values = new Float64Array(count * stride);
|
|
54
|
+
if (output.count < count)
|
|
55
|
+
throw new LoaderError(F, 'truncated', `${what} has ${count} key times but ${output.count} output values`);
|
|
56
|
+
for (let k = 0; k < count; k++)
|
|
57
|
+
values.set(block(output, k, stride), k * stride);
|
|
58
|
+
const names = inputs.has('INTERPOLATION') ? (source(inputs.get('INTERPOLATION')).names ?? []) : [];
|
|
59
|
+
let tangents = false;
|
|
60
|
+
const interpolations = Array.from({ length: count }, (_, k) => {
|
|
61
|
+
const name = names[k] ?? names[0] ?? 'LINEAR';
|
|
62
|
+
if (KINDS.includes(name)) {
|
|
63
|
+
tangents ||= name === 'BEZIER' || name === 'HERMITE';
|
|
64
|
+
return name;
|
|
65
|
+
}
|
|
66
|
+
warn(`${what}: ${name.slice(0, 16)} interpolation is sampled as LINEAR`);
|
|
67
|
+
return 'LINEAR';
|
|
68
|
+
});
|
|
69
|
+
const result = { input: times, output: values, stride, interpolations };
|
|
70
|
+
if (!tangents)
|
|
71
|
+
return result;
|
|
72
|
+
if (!inputs.has('IN_TANGENT') || !inputs.has('OUT_TANGENT')) {
|
|
73
|
+
warn(`${what}: BEZIER or HERMITE keys without IN_TANGENT and OUT_TANGENT are sampled as LINEAR`);
|
|
74
|
+
result.interpolations = interpolations.map((i) => (i === 'STEP' ? i : 'LINEAR'));
|
|
75
|
+
return result;
|
|
76
|
+
}
|
|
77
|
+
const inTangent = source(inputs.get('IN_TANGENT')), outTangent = source(inputs.get('OUT_TANGENT')), tangentStride = inTangent.stride;
|
|
78
|
+
if (outTangent.stride !== tangentStride ||
|
|
79
|
+
(tangentStride !== stride && tangentStride !== stride * 2) ||
|
|
80
|
+
inTangent.count < count ||
|
|
81
|
+
outTangent.count < count)
|
|
82
|
+
throw new LoaderError(F, 'syntax', `${what} tangents must have ${stride} or ${stride * 2} values per key`);
|
|
83
|
+
result.inTangents = new Float64Array(count * tangentStride);
|
|
84
|
+
result.outTangents = new Float64Array(count * tangentStride);
|
|
85
|
+
for (let k = 0; k < count; k++) {
|
|
86
|
+
result.inTangents.set(block(inTangent, k, tangentStride), k * tangentStride);
|
|
87
|
+
result.outTangents.set(block(outTangent, k, tangentStride), k * tangentStride);
|
|
88
|
+
}
|
|
89
|
+
result.tangentStride = tangentStride;
|
|
90
|
+
return result;
|
|
91
|
+
}
|
|
92
|
+
/** Reads every `<animation>` (nested ones too) and `<animation_clip>`. */
|
|
93
|
+
export function readAnimations(root, ids, morphs, warn) {
|
|
94
|
+
const animations = [], index = new Map(), morphOf = new Map();
|
|
95
|
+
morphs.forEach((morph, i) => {
|
|
96
|
+
morphOf.set(morph.id, i);
|
|
97
|
+
if (morph.weightsSource)
|
|
98
|
+
morphOf.set(morph.weightsSource, i);
|
|
99
|
+
});
|
|
100
|
+
const channelOf = (target, sampler) => {
|
|
101
|
+
const slash = target.indexOf('/'), first = slash < 0 ? target : target.slice(0, slash), rest = slash < 0 ? '' : target.slice(slash + 1);
|
|
102
|
+
// A weights channel: "morph/weights(i)" or "weightsSource(i)".
|
|
103
|
+
const direct = slash < 0 ? selector(first) : undefined, morph = morphOf.get(direct ? direct.name : first);
|
|
104
|
+
if (morph !== undefined) {
|
|
105
|
+
const part = direct ?? selector(rest);
|
|
106
|
+
if (!part)
|
|
107
|
+
return;
|
|
108
|
+
const width = part.index === undefined ? morphs[morph].weights.length : 1;
|
|
109
|
+
if ((part.index ?? 0) >= morphs[morph].weights.length || sampler.stride !== width)
|
|
110
|
+
return;
|
|
111
|
+
const channel = { target, morph, sampler };
|
|
112
|
+
if (part.index !== undefined)
|
|
113
|
+
channel.index = part.index;
|
|
114
|
+
return channel;
|
|
115
|
+
}
|
|
116
|
+
const node = ids.get(first.startsWith('./') ? first.slice(2) : first);
|
|
117
|
+
if (node?.name !== 'node' || !rest || rest.includes('/'))
|
|
118
|
+
return;
|
|
119
|
+
const part = selector(rest), element = part && node.children.find((c) => c.attributes.sid === part.name && Object.hasOwn(TRANSFORM_SIZES, c.name));
|
|
120
|
+
if (!part || !element)
|
|
121
|
+
return;
|
|
122
|
+
const size = TRANSFORM_SIZES[element.name];
|
|
123
|
+
if ((part.index ?? 0) >= size || sampler.stride !== (part.index === undefined ? size : 1))
|
|
124
|
+
return;
|
|
125
|
+
const channel = { target, node: first, transform: part.name, sampler };
|
|
126
|
+
if (part.index !== undefined)
|
|
127
|
+
channel.index = part.index;
|
|
128
|
+
return channel;
|
|
129
|
+
};
|
|
130
|
+
const read = (e, parent, depth) => {
|
|
131
|
+
if (depth > 64)
|
|
132
|
+
throw new LoaderError(F, 'limit', 'animations nest deeper than 64');
|
|
133
|
+
const animation = { parent, channels: [] }, at = animations.length, what = `animation "${e.attributes.id ?? e.attributes.name ?? at}"`, source = sourcesOf(e, ids, what);
|
|
134
|
+
if (e.attributes.id !== undefined)
|
|
135
|
+
animation.id = e.attributes.id;
|
|
136
|
+
if (e.attributes.name !== undefined)
|
|
137
|
+
animation.name = e.attributes.name;
|
|
138
|
+
animations.push(animation);
|
|
139
|
+
index.set(e, at);
|
|
140
|
+
const samplers = new Map();
|
|
141
|
+
for (const channel of kids(e, 'channel')) {
|
|
142
|
+
const id = ref(channel.attributes.source), target = channel.attributes.target ?? '', element = kids(e, 'sampler').find((s) => s.attributes.id === id);
|
|
143
|
+
if (!element) {
|
|
144
|
+
warn(`${what} channel "${target}" names no <sampler> "${id}"`);
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
let sampler = samplers.get(id);
|
|
148
|
+
if (!sampler)
|
|
149
|
+
samplers.set(id, (sampler = readSampler(element, source, `${what} sampler "${id}"`, warn)));
|
|
150
|
+
const loaded = channelOf(target, sampler);
|
|
151
|
+
if (loaded)
|
|
152
|
+
animation.channels.push(loaded);
|
|
153
|
+
else
|
|
154
|
+
warn(`${what} channel "${target}" is not imported: it names no node transform or morph weight it fits`);
|
|
155
|
+
}
|
|
156
|
+
for (const child of kids(e, 'animation'))
|
|
157
|
+
read(child, at, depth + 1);
|
|
158
|
+
};
|
|
159
|
+
for (const e of kids(kid(root, 'library_animations'), 'animation'))
|
|
160
|
+
read(e, -1, 0);
|
|
161
|
+
const clips = [];
|
|
162
|
+
for (const e of kids(kid(root, 'library_animation_clips'), 'animation_clip')) {
|
|
163
|
+
const number = (name) => {
|
|
164
|
+
const text = e.attributes[name];
|
|
165
|
+
if (text === undefined)
|
|
166
|
+
return;
|
|
167
|
+
const value = numbers(text, `animation_clip ${name}`)[0];
|
|
168
|
+
if (!Number.isFinite(value))
|
|
169
|
+
throw new LoaderError(F, 'syntax', `animation_clip ${name} must be a finite number`);
|
|
170
|
+
return value;
|
|
171
|
+
};
|
|
172
|
+
const clip = { start: number('start') ?? 0, animations: [] }, end = number('end');
|
|
173
|
+
if (e.attributes.id !== undefined)
|
|
174
|
+
clip.id = e.attributes.id;
|
|
175
|
+
if (e.attributes.name !== undefined)
|
|
176
|
+
clip.name = e.attributes.name;
|
|
177
|
+
if (end !== undefined)
|
|
178
|
+
clip.end = end;
|
|
179
|
+
for (const instance of kids(e, 'instance_animation')) {
|
|
180
|
+
const found = ids.get(ref(instance.attributes.url)), at = found && index.get(found);
|
|
181
|
+
if (at === undefined)
|
|
182
|
+
warn(`animation_clip instance_animation "${instance.attributes.url}" names no <animation>`);
|
|
183
|
+
else
|
|
184
|
+
clip.animations.push(at);
|
|
185
|
+
}
|
|
186
|
+
clips.push(clip);
|
|
187
|
+
}
|
|
188
|
+
return { animations, clips };
|
|
189
|
+
}
|
|
190
|
+
/** x(s) or y(s) of a cubic Bezier with control values a, b, c, d. */
|
|
191
|
+
const bezier = (a, b, c, d, s) => {
|
|
192
|
+
const u = 1 - s;
|
|
193
|
+
return u * u * u * a + 3 * u * u * s * b + 3 * u * s * s * c + s * s * s * d;
|
|
194
|
+
};
|
|
195
|
+
/**
|
|
196
|
+
* A sampler's values at `time` seconds: the first key's before it, the last key's after it, else the segment's
|
|
197
|
+
* interpolation. BEZIER segments are 2D curves through (time, value) control points: OUT_TANGENT of the key and
|
|
198
|
+
* IN_TANGENT of the next one, solved for the time (value-only tangents sit at a third of the segment). HERMITE uses the
|
|
199
|
+
* value part of the tangents as derivatives over the segment's normalised time.
|
|
200
|
+
*/
|
|
201
|
+
export function sampleDAESampler(sampler, time, out) {
|
|
202
|
+
if (typeof time !== 'number' || !Number.isFinite(time))
|
|
203
|
+
throw new RangeError(`DAE sample time ${time} is invalid; pass finite seconds`);
|
|
204
|
+
const { input, output, stride } = sampler, last = input.length - 1;
|
|
205
|
+
out ??= new Float64Array(stride);
|
|
206
|
+
if (out.length < stride)
|
|
207
|
+
throw new RangeError(`DAE sample target must hold ${stride} numbers; pass a larger array`);
|
|
208
|
+
let a = 0;
|
|
209
|
+
if (time >= input[last])
|
|
210
|
+
a = last;
|
|
211
|
+
else if (time > input[0])
|
|
212
|
+
for (let hi = last; a + 1 < hi;) {
|
|
213
|
+
const mid = (a + hi) >> 1;
|
|
214
|
+
if (input[mid] <= time)
|
|
215
|
+
a = mid;
|
|
216
|
+
else
|
|
217
|
+
hi = mid;
|
|
218
|
+
}
|
|
219
|
+
const kind = sampler.interpolations[a];
|
|
220
|
+
if (a === last || time <= input[a] || kind === 'STEP' || input[a + 1] <= input[a]) {
|
|
221
|
+
out.set(output.subarray(a * stride, a * stride + stride));
|
|
222
|
+
return out;
|
|
223
|
+
}
|
|
224
|
+
const t0 = input[a], t1 = input[a + 1], dt = t1 - t0, s = (time - t0) / dt, ts = sampler.tangentStride ?? 0, pairs = ts === stride * 2, inT = sampler.inTangents, outT = sampler.outTangents;
|
|
225
|
+
for (let c = 0; c < stride; c++) {
|
|
226
|
+
const p0 = output[a * stride + c], p1 = output[(a + 1) * stride + c];
|
|
227
|
+
if (kind === 'LINEAR' || !inT || !outT) {
|
|
228
|
+
out[c] = p0 + (p1 - p0) * s;
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
const o = (a * ts + (pairs ? c * 2 : c)) | 0, i = ((a + 1) * ts + (pairs ? c * 2 : c)) | 0;
|
|
232
|
+
if (kind === 'HERMITE') {
|
|
233
|
+
const m0 = pairs ? outT[o + 1] : outT[o], m1 = pairs ? inT[i + 1] : inT[i], s2 = s * s, s3 = s2 * s;
|
|
234
|
+
out[c] = (2 * s3 - 3 * s2 + 1) * p0 + (s3 - 2 * s2 + s) * m0 + (-2 * s3 + 3 * s2) * p1 + (s3 - s2) * m1;
|
|
235
|
+
continue;
|
|
236
|
+
}
|
|
237
|
+
const x1 = pairs ? outT[o] : t0 + dt / 3, y1 = pairs ? outT[o + 1] : outT[o], x2 = pairs ? inT[i] : t1 - dt / 3, y2 = pairs ? inT[i + 1] : inT[i];
|
|
238
|
+
// Solve x(u) = time on [0, 1]; x is monotonic for well-formed curves, so bisection always converges.
|
|
239
|
+
let lo = 0, hi = 1, u = s;
|
|
240
|
+
for (let n = 0; n < 60; n++) {
|
|
241
|
+
u = (lo + hi) / 2;
|
|
242
|
+
if (bezier(t0, x1, x2, t1, u) < time)
|
|
243
|
+
lo = u;
|
|
244
|
+
else
|
|
245
|
+
hi = u;
|
|
246
|
+
}
|
|
247
|
+
out[c] = bezier(p0, y1, y2, p1, u);
|
|
248
|
+
}
|
|
249
|
+
return out;
|
|
250
|
+
}
|
|
251
|
+
/** The transform values a node's channels give at `time` (copies; the node's own stack is left as is). */
|
|
252
|
+
export function animateTransforms(transforms, channels, time) {
|
|
253
|
+
const out = transforms.map((t) => ({ ...t, values: [...t.values] }));
|
|
254
|
+
for (const channel of channels) {
|
|
255
|
+
const transform = out.find((t) => t.sid === channel.transform);
|
|
256
|
+
if (!transform)
|
|
257
|
+
continue;
|
|
258
|
+
const value = sampleDAESampler(channel.sampler, time);
|
|
259
|
+
if (channel.index === undefined)
|
|
260
|
+
for (let k = 0; k < transform.values.length; k++)
|
|
261
|
+
transform.values[k] = value[k];
|
|
262
|
+
else
|
|
263
|
+
transform.values[channel.index] = value[0];
|
|
264
|
+
}
|
|
265
|
+
return out;
|
|
266
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import type { XMLElement } from './xml.js';
|
|
2
|
+
export declare const F = "DAE";
|
|
3
|
+
export declare const kid: (e: XMLElement | undefined, name: string) => XMLElement | undefined;
|
|
4
|
+
export declare const kids: (e: XMLElement | undefined, name: string) => XMLElement[];
|
|
5
|
+
export declare const ref: (url: string | undefined) => string;
|
|
6
|
+
export declare function numbers(text: string, what: string): number[];
|
|
7
|
+
export declare function integer(value: string | undefined, fallback: number, what: string): number;
|
|
8
|
+
/** A `<source>`: its float array (or name/IDREF tokens) read through the accessor. */
|
|
9
|
+
export interface Source {
|
|
10
|
+
id: string;
|
|
11
|
+
data: number[];
|
|
12
|
+
/** Tokens of a `Name_array` or `IDREF_array`. */
|
|
13
|
+
names?: string[];
|
|
14
|
+
/** The array element: `float_array`, `Name_array`, `IDREF_array` or `int_array`. */
|
|
15
|
+
kind: string;
|
|
16
|
+
stride: number;
|
|
17
|
+
offset: number;
|
|
18
|
+
count: number;
|
|
19
|
+
/** Offset inside a stride of each named `<param>`. */
|
|
20
|
+
params: number[];
|
|
21
|
+
}
|
|
22
|
+
/** Reads a source; `names` allows `Name_array` and `IDREF_array` (else only number arrays are accepted). */
|
|
23
|
+
export declare function readSource(e: XMLElement, ids: Map<string, XMLElement>, names?: boolean): Source;
|
|
24
|
+
/** `width` consecutive values of element `index` of a source, from the start of its stride. */
|
|
25
|
+
export declare function block(source: Source, index: number, width: number): number[];
|
|
26
|
+
/** The sources of a `<skin>`, `<morph>` or `<animation>` by id, read lazily. */
|
|
27
|
+
export declare function sourcesOf(e: XMLElement, ids: Map<string, XMLElement>, what: string): (url: string | undefined) => Source;
|
|
28
|
+
export type Matrix = number[];
|
|
29
|
+
export declare const IDENTITY: Matrix;
|
|
30
|
+
/** Column-major a * b. */
|
|
31
|
+
export declare function multiply(a: ArrayLike<number>, b: ArrayLike<number>): Matrix;
|
|
32
|
+
/** Column-major matrix from 16 row-major values (COLLADA writes matrices row by row). */
|
|
33
|
+
export declare const fromRows: (rows: ArrayLike<number>) => Matrix;
|
|
34
|
+
/** Column-major inverse, or undefined when singular. */
|
|
35
|
+
export declare function invert(m: ArrayLike<number>): Matrix | undefined;
|
|
36
|
+
/** One element of a node's transform stack, as written (the file's axes and units). */
|
|
37
|
+
export interface DAETransform {
|
|
38
|
+
type: 'matrix' | 'translate' | 'rotate' | 'scale' | 'lookat' | 'skew';
|
|
39
|
+
/** The element's `sid`, which animation channels address. */
|
|
40
|
+
sid?: string;
|
|
41
|
+
/**
|
|
42
|
+
* The numbers as written: 16 row-major for `matrix`, xyz for translate and scale, axis and degrees for rotate, eye,
|
|
43
|
+
* interest point and up for lookat, degrees, rotation axis and translation axis for skew.
|
|
44
|
+
*/
|
|
45
|
+
values: number[];
|
|
46
|
+
}
|
|
47
|
+
export declare const TRANSFORM_SIZES: {
|
|
48
|
+
readonly matrix: 16;
|
|
49
|
+
readonly translate: 3;
|
|
50
|
+
readonly rotate: 4;
|
|
51
|
+
readonly scale: 3;
|
|
52
|
+
readonly lookat: 9;
|
|
53
|
+
readonly skew: 7;
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* A `<lookat>` (eye, interest point, up) as the matrix that places a frame looking down its -Z at the eye: columns
|
|
57
|
+
* side = forward x up, true up = side x forward, -forward and the eye, forward being interest - eye. Undefined when
|
|
58
|
+
* the eye is the interest point or up is parallel to the view.
|
|
59
|
+
*/
|
|
60
|
+
export declare function lookatMatrix(v: ArrayLike<number>): Matrix | undefined;
|
|
61
|
+
/**
|
|
62
|
+
* A `<skew>` (degrees, rotation axis a, translation axis t) as the RenderMan skew the spec adopts: points slide
|
|
63
|
+
* parallel to t, so that a turns by the angle toward t in their common plane: M = I + k t d^T, d being the unit part
|
|
64
|
+
* of a perpendicular to t and k = cot(phi - angle) - cot(phi) for phi the angle from t to a (tan(angle) when they are
|
|
65
|
+
* perpendicular). Undefined when t is zero, a is parallel to t, or a would turn onto or past t.
|
|
66
|
+
*/
|
|
67
|
+
export declare function skewMatrix(v: ArrayLike<number>): Matrix | undefined;
|
|
68
|
+
/**
|
|
69
|
+
* The stack in document order, each element post-multiplying. A degenerate `lookat` keeps only its eye translation
|
|
70
|
+
* and a degenerate `skew` is skipped (parseDAE warns about both).
|
|
71
|
+
*/
|
|
72
|
+
export declare function stackMatrix(transforms: readonly DAETransform[]): Matrix;
|
|
73
|
+
export declare const AXES: {
|
|
74
|
+
readonly Y_UP: readonly [1, 0, 0, 0, 1, 0, 0, 0, 1];
|
|
75
|
+
readonly Z_UP: readonly [1, 0, 0, 0, 0, 1, 0, -1, 0];
|
|
76
|
+
readonly X_UP: readonly [0, -1, 0, 1, 0, 0, 0, 0, 1];
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* M' = C M C^-1 with C = meter * R: rotations keep their axes in Y-up, translations scale to meters. The same
|
|
80
|
+
* conversion serves node transforms, bind shape and inverse bind matrices, so skinning stays consistent.
|
|
81
|
+
*/
|
|
82
|
+
export declare function converter(upAxis: keyof typeof AXES, meter: number): (m: ArrayLike<number>) => Matrix;
|
|
83
|
+
/** Cofactors of a column-major matrix's 3x3 part (the inverse transpose times the determinant), and the determinant. */
|
|
84
|
+
export declare function cofactors(m: ArrayLike<number>): {
|
|
85
|
+
n: number[];
|
|
86
|
+
determinant: number;
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* Transforms xyz points (and normals) by a column-major matrix into `out` arrays at `at`; normals use the cofactor
|
|
90
|
+
* matrix, renormalised, with the determinant's sign removed.
|
|
91
|
+
*/
|
|
92
|
+
export declare function transformInto(m: ArrayLike<number>, positions: ArrayLike<number>, normals: ArrayLike<number> | undefined, i: number, outPositions: Float32Array, outNormals: Float32Array | undefined, at: number, co?: {
|
|
93
|
+
n: number[];
|
|
94
|
+
determinant: number;
|
|
95
|
+
}): void;
|