@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
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { type Matrix } from './dae-common.js';
|
|
2
|
+
import type { XMLElement } from './xml.js';
|
|
3
|
+
/**
|
|
4
|
+
* A `<skin>` controller in Y-up meters. Influences are per `<vertices>` position (the geometry's
|
|
5
|
+
* `positionIndices` values), four each: the strongest four of the file's, renormalised to sum 1.
|
|
6
|
+
*/
|
|
7
|
+
export interface DAESkin {
|
|
8
|
+
id: string;
|
|
9
|
+
name?: string;
|
|
10
|
+
/** Index into `LoadedDAE.geometries` of the skinned mesh (the morph's base mesh when it skins a morph). */
|
|
11
|
+
geometry: number;
|
|
12
|
+
/** Index into `LoadedDAE.morphs` when the skin's source is a morph controller. */
|
|
13
|
+
morph?: number;
|
|
14
|
+
/** `bind_shape_matrix`, column-major, converted to Y-up meters; applied to the mesh before skinning. */
|
|
15
|
+
bindShapeMatrix: number[];
|
|
16
|
+
/** Joint names as written: sids (`Name_array`) or node ids (`IDREF_array`). */
|
|
17
|
+
joints: string[];
|
|
18
|
+
/** 16 column-major numbers per joint, converted to Y-up meters. */
|
|
19
|
+
inverseBindMatrices: Float64Array;
|
|
20
|
+
/** Four joint indices per position, into `joints`; -1 is the bind shape itself (the mesh unmoved by joints). */
|
|
21
|
+
vertexJoints: Int32Array;
|
|
22
|
+
/** Four weights per position matching `vertexJoints`, summing to 1; all zero for a position without weights. */
|
|
23
|
+
vertexWeights: Float32Array;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* A `<morph>` controller: `NORMALIZED` gives (1 - sum w) base + sum w target, `RELATIVE` gives base + sum w target,
|
|
27
|
+
* position by position (targets share the base mesh's `<vertices>` numbering).
|
|
28
|
+
*/
|
|
29
|
+
export interface DAEMorph {
|
|
30
|
+
id: string;
|
|
31
|
+
name?: string;
|
|
32
|
+
method: 'NORMALIZED' | 'RELATIVE';
|
|
33
|
+
/** Index into `LoadedDAE.geometries` of the base mesh. */
|
|
34
|
+
base: number;
|
|
35
|
+
/** Index into `LoadedDAE.geometries` of each target mesh. */
|
|
36
|
+
targets: number[];
|
|
37
|
+
/** Default weight of each target (`MORPH_WEIGHT`). */
|
|
38
|
+
weights: number[];
|
|
39
|
+
/** Id of the `MORPH_WEIGHT` source, which animation channels may address instead of the controller. */
|
|
40
|
+
weightsSource?: string;
|
|
41
|
+
}
|
|
42
|
+
/** What an `<instance_controller>` resolves to. */
|
|
43
|
+
export interface ControllerRef {
|
|
44
|
+
geometry: number;
|
|
45
|
+
skin?: number;
|
|
46
|
+
morph?: number;
|
|
47
|
+
/** The skin's joints are node ids (`IDREF_array`), not sids. */
|
|
48
|
+
idref?: boolean;
|
|
49
|
+
}
|
|
50
|
+
/** Highest position index a geometry uses, plus one. */
|
|
51
|
+
export declare function positionCount(indices: Uint32Array): number;
|
|
52
|
+
/** Reads `library_controllers`: morphs first, so a skin may wrap one. */
|
|
53
|
+
export declare function readControllers(root: XMLElement, ids: Map<string, XMLElement>, geometryIndex: Map<string, number>, positionIndices: (geometry: number) => Uint32Array, convert: (m: ArrayLike<number>) => Matrix, warn: (message: string) => void): {
|
|
54
|
+
skins: DAESkin[];
|
|
55
|
+
morphs: DAEMorph[];
|
|
56
|
+
controllers: Map<string, ControllerRef>;
|
|
57
|
+
};
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
// COLLADA <controller> reading: <skin> (bind shape, joints, inverse bind matrices, vertex weights) and <morph>
|
|
2
|
+
// (NORMALIZED and RELATIVE targets with default weights). Written from the COLLADA 1.4.1 and 1.5 specifications.
|
|
3
|
+
import { LoaderError } from './types.js';
|
|
4
|
+
import { block, F, fromRows, integer, kid, kids, numbers, ref, sourcesOf, } from './dae-common.js';
|
|
5
|
+
/** Most influences kept per vertex, as the engine's skinning takes them. */
|
|
6
|
+
const MAX_INFLUENCES = 4;
|
|
7
|
+
/** Highest position index a geometry uses, plus one. */
|
|
8
|
+
export function positionCount(indices) {
|
|
9
|
+
let count = 0;
|
|
10
|
+
for (let i = 0; i < indices.length; i++)
|
|
11
|
+
if (indices[i] >= count)
|
|
12
|
+
count = indices[i] + 1;
|
|
13
|
+
return count;
|
|
14
|
+
}
|
|
15
|
+
function readMorph(controller, morph, ids, geometryIndex, positions, warn) {
|
|
16
|
+
const id = controller.attributes.id ?? '', what = `morph "${id}"`, base = geometryIndex.get(ref(morph.attributes.source));
|
|
17
|
+
if (base === undefined) {
|
|
18
|
+
warn(`${what} is not imported: its source "${morph.attributes.source}" is not an imported mesh`);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
const method = morph.attributes.method ?? 'NORMALIZED';
|
|
22
|
+
if (method !== 'NORMALIZED' && method !== 'RELATIVE')
|
|
23
|
+
throw new LoaderError(F, 'syntax', `${what} method "${method.slice(0, 32)}" is not NORMALIZED or RELATIVE`);
|
|
24
|
+
const source = sourcesOf(morph, ids, what), inputs = kids(kid(morph, 'targets'), 'input'), targetInput = inputs.find((i) => i.attributes.semantic === 'MORPH_TARGET'), weightInput = inputs.find((i) => i.attributes.semantic === 'MORPH_WEIGHT');
|
|
25
|
+
if (!targetInput || !weightInput)
|
|
26
|
+
throw new LoaderError(F, 'syntax', `${what} <targets> needs MORPH_TARGET and MORPH_WEIGHT inputs`);
|
|
27
|
+
const names = source(targetInput.attributes.source).names ?? [], weightSource = source(weightInput.attributes.source), weights = weightSource.data.slice(weightSource.offset, weightSource.offset + names.length);
|
|
28
|
+
if (weights.length !== names.length || !weights.every(Number.isFinite))
|
|
29
|
+
throw new LoaderError(F, 'syntax', `${what} has ${names.length} targets but ${weights.length} finite weights`);
|
|
30
|
+
const targets = [];
|
|
31
|
+
for (const name of names) {
|
|
32
|
+
const target = geometryIndex.get(ref(name));
|
|
33
|
+
if (target === undefined) {
|
|
34
|
+
warn(`${what} is not imported: target "${name}" is not an imported mesh`);
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
if (positions(target) > positions(base)) {
|
|
38
|
+
warn(`${what} is not imported: target "${name}" has more positions than the base mesh`);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
targets.push(target);
|
|
42
|
+
}
|
|
43
|
+
const result = { id, method, base, targets, weights };
|
|
44
|
+
if (controller.attributes.name !== undefined)
|
|
45
|
+
result.name = controller.attributes.name;
|
|
46
|
+
if (weightSource.id)
|
|
47
|
+
result.weightsSource = weightSource.id;
|
|
48
|
+
return result;
|
|
49
|
+
}
|
|
50
|
+
function readSkin(controller, skin, ids, base, count, convert) {
|
|
51
|
+
const id = controller.attributes.id ?? '', what = `skin "${id}"`, source = sourcesOf(skin, ids, what), matrix = kid(skin, 'bind_shape_matrix'), bindShape = matrix ? numbers(matrix.text, `${what} bind_shape_matrix`) : undefined;
|
|
52
|
+
if (bindShape && (bindShape.length !== 16 || !bindShape.every(Number.isFinite)))
|
|
53
|
+
throw new LoaderError(F, 'syntax', `${what} bind_shape_matrix needs 16 finite numbers, got ${bindShape.length}`);
|
|
54
|
+
const jointInputs = kids(kid(skin, 'joints'), 'input'), jointInput = jointInputs.find((i) => i.attributes.semantic === 'JOINT'), bindInput = jointInputs.find((i) => i.attributes.semantic === 'INV_BIND_MATRIX');
|
|
55
|
+
if (!jointInput || !bindInput)
|
|
56
|
+
throw new LoaderError(F, 'syntax', `${what} <joints> needs JOINT and INV_BIND_MATRIX inputs`);
|
|
57
|
+
const jointSource = source(jointInput.attributes.source), names = jointSource.names ?? [], joints = names.slice(jointSource.offset, jointSource.offset + (jointSource.count || names.length)), idref = jointSource.kind === 'IDREF_array', binds = source(bindInput.attributes.source);
|
|
58
|
+
if (!joints.length)
|
|
59
|
+
throw new LoaderError(F, 'syntax', `${what} has no joints`);
|
|
60
|
+
if (binds.count < joints.length || binds.stride < 16)
|
|
61
|
+
throw new LoaderError(F, 'syntax', `${what} has ${joints.length} joints but ${binds.count} inverse bind matrices`);
|
|
62
|
+
const inverseBindMatrices = new Float64Array(joints.length * 16);
|
|
63
|
+
for (let j = 0; j < joints.length; j++) {
|
|
64
|
+
const rows = block(binds, j, 16);
|
|
65
|
+
if (!rows.every(Number.isFinite))
|
|
66
|
+
throw new LoaderError(F, 'syntax', `${what} inverse bind matrix ${j} is not finite`);
|
|
67
|
+
inverseBindMatrices.set(convert(fromRows(rows)), j * 16);
|
|
68
|
+
}
|
|
69
|
+
const weightsElement = kid(skin, 'vertex_weights');
|
|
70
|
+
if (!weightsElement)
|
|
71
|
+
throw new LoaderError(F, 'syntax', `${what} has no <vertex_weights>`);
|
|
72
|
+
const inputs = kids(weightsElement, 'input'), stride = Math.max(0, ...inputs.map((i) => integer(i.attributes.offset, 0, `${what} input offset`) + 1)), jointAt = inputs.find((i) => i.attributes.semantic === 'JOINT'), weightAt = inputs.find((i) => i.attributes.semantic === 'WEIGHT');
|
|
73
|
+
if (!jointAt || !weightAt)
|
|
74
|
+
throw new LoaderError(F, 'syntax', `${what} <vertex_weights> needs JOINT and WEIGHT inputs`);
|
|
75
|
+
const jointOffset = integer(jointAt.attributes.offset, 0, `${what} input offset`), weightOffset = integer(weightAt.attributes.offset, 0, `${what} input offset`), weightSource = source(weightAt.attributes.source), vertices = integer(weightsElement.attributes.count, 0, `${what} vertex_weights count`), vcount = numbers(kid(weightsElement, 'vcount')?.text ?? '', `${what} <vcount>`), v = numbers(kid(weightsElement, 'v')?.text ?? '', `${what} <v>`);
|
|
76
|
+
if (vcount.length !== vertices)
|
|
77
|
+
throw new LoaderError(F, 'syntax', `${what} <vcount> has ${vcount.length} entries for ${vertices} vertices`);
|
|
78
|
+
if (vertices < count)
|
|
79
|
+
throw new LoaderError(F, 'range', `${what} weights ${vertices} vertices but its mesh has ${count} positions`);
|
|
80
|
+
const vertexJoints = new Int32Array(count * MAX_INFLUENCES), vertexWeights = new Float32Array(count * MAX_INFLUENCES), pairs = [];
|
|
81
|
+
let at = 0;
|
|
82
|
+
for (let vertex = 0; vertex < vertices; vertex++) {
|
|
83
|
+
const n = vcount[vertex];
|
|
84
|
+
if (!Number.isSafeInteger(n) || n < 0 || (at + n) * stride > v.length)
|
|
85
|
+
throw new LoaderError(F, 'syntax', `${what} <vcount> does not match its <v> indices`);
|
|
86
|
+
pairs.length = 0;
|
|
87
|
+
for (let k = 0; k < n; k++, at++) {
|
|
88
|
+
const joint = v[at * stride + jointOffset], index = v[at * stride + weightOffset];
|
|
89
|
+
if (!(Number.isInteger(joint) && joint >= -1 && joint < joints.length))
|
|
90
|
+
throw new LoaderError(F, 'range', `${what} joint index ${joint} is outside -1..${joints.length - 1}`);
|
|
91
|
+
if (!(Number.isInteger(index) && index >= 0 && index < weightSource.count))
|
|
92
|
+
throw new LoaderError(F, 'range', `${what} weight index ${index} is outside 0..${weightSource.count - 1}`);
|
|
93
|
+
const weight = weightSource.data[weightSource.offset + index * weightSource.stride];
|
|
94
|
+
if (Number.isFinite(weight) && weight > 0)
|
|
95
|
+
pairs.push([joint, weight]);
|
|
96
|
+
}
|
|
97
|
+
if (vertex >= count)
|
|
98
|
+
continue;
|
|
99
|
+
pairs.sort((a, b) => b[1] - a[1]);
|
|
100
|
+
let sum = 0;
|
|
101
|
+
for (let k = 0; k < MAX_INFLUENCES && k < pairs.length; k++)
|
|
102
|
+
sum += pairs[k][1];
|
|
103
|
+
for (let k = 0; k < MAX_INFLUENCES; k++) {
|
|
104
|
+
const pair = pairs[k];
|
|
105
|
+
vertexJoints[vertex * MAX_INFLUENCES + k] = pair ? pair[0] : 0;
|
|
106
|
+
vertexWeights[vertex * MAX_INFLUENCES + k] = pair && sum > 0 ? pair[1] / sum : 0;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
const result = {
|
|
110
|
+
id,
|
|
111
|
+
geometry: base.geometry,
|
|
112
|
+
bindShapeMatrix: bindShape
|
|
113
|
+
? convert(fromRows(bindShape))
|
|
114
|
+
: convert([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]),
|
|
115
|
+
joints,
|
|
116
|
+
inverseBindMatrices,
|
|
117
|
+
vertexJoints,
|
|
118
|
+
vertexWeights,
|
|
119
|
+
};
|
|
120
|
+
if (base.morph !== undefined)
|
|
121
|
+
result.morph = base.morph;
|
|
122
|
+
if (controller.attributes.name !== undefined)
|
|
123
|
+
result.name = controller.attributes.name;
|
|
124
|
+
return { skin: result, idref };
|
|
125
|
+
}
|
|
126
|
+
/** Reads `library_controllers`: morphs first, so a skin may wrap one. */
|
|
127
|
+
export function readControllers(root, ids, geometryIndex, positionIndices, convert, warn) {
|
|
128
|
+
const all = kids(kid(root, 'library_controllers'), 'controller'), skins = [], morphs = [], controllers = new Map(), counts = new Map(), positions = (geometry) => {
|
|
129
|
+
let count = counts.get(geometry);
|
|
130
|
+
if (count === undefined)
|
|
131
|
+
counts.set(geometry, (count = positionCount(positionIndices(geometry))));
|
|
132
|
+
return count;
|
|
133
|
+
};
|
|
134
|
+
for (const controller of all) {
|
|
135
|
+
const morph = kid(controller, 'morph');
|
|
136
|
+
if (!morph)
|
|
137
|
+
continue;
|
|
138
|
+
const loaded = readMorph(controller, morph, ids, geometryIndex, positions, warn);
|
|
139
|
+
if (!loaded)
|
|
140
|
+
continue;
|
|
141
|
+
controllers.set(loaded.id, { geometry: loaded.base, morph: morphs.length });
|
|
142
|
+
morphs.push(loaded);
|
|
143
|
+
}
|
|
144
|
+
for (const controller of all) {
|
|
145
|
+
const skin = kid(controller, 'skin'), id = controller.attributes.id ?? '';
|
|
146
|
+
if (!skin) {
|
|
147
|
+
if (!kid(controller, 'morph'))
|
|
148
|
+
warn(`controller "${id}" is neither a skin nor a morph and is not imported`);
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
const sourceId = ref(skin.attributes.source), geometry = geometryIndex.get(sourceId), wrapped = controllers.get(sourceId), base = geometry !== undefined ? { geometry } : wrapped?.morph !== undefined ? wrapped : undefined;
|
|
152
|
+
if (!base) {
|
|
153
|
+
warn(`skin "${id}" is not imported: its source "${skin.attributes.source}" is not an imported mesh or morph`);
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
const { skin: loaded, idref } = readSkin(controller, skin, ids, base, positions(base.geometry), convert);
|
|
157
|
+
const entry = { geometry: base.geometry, skin: skins.length, idref };
|
|
158
|
+
if (base.morph !== undefined)
|
|
159
|
+
entry.morph = base.morph;
|
|
160
|
+
controllers.set(id, entry);
|
|
161
|
+
skins.push(loaded);
|
|
162
|
+
}
|
|
163
|
+
return { skins, morphs, controllers };
|
|
164
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import type { MutableVec3 } from '@margoengine/math';
|
|
2
|
+
import { type Matrix } from './dae-common.js';
|
|
3
|
+
import type { DAEGeometry, DAEGroup, DAEInstance, DAENode, LoadedDAE } from './dae.js';
|
|
4
|
+
/** A parsed COLLADA scene posed at one time by {@link poseDAE}. */
|
|
5
|
+
export interface DAEPose {
|
|
6
|
+
/** Local matrix of every node (column-major, Y-up meters): animated where channels address it, else `node.matrix`. */
|
|
7
|
+
local: Map<DAENode, number[]>;
|
|
8
|
+
/** World matrix of every node. */
|
|
9
|
+
world: Map<DAENode, number[]>;
|
|
10
|
+
/** Weights of every morph, by index into `LoadedDAE.morphs`: animated where channels address them, else defaults. */
|
|
11
|
+
weights: number[][];
|
|
12
|
+
}
|
|
13
|
+
/** Options of {@link poseDAE} and {@link daeKeyframeClip}. */
|
|
14
|
+
export interface DAEPoseOptions {
|
|
15
|
+
/** Play only this `LoadedDAE.clips` entry, with time 0 at its start (held at its end). Default: every animation. */
|
|
16
|
+
clip?: number;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* A mesh with normals, ready for a materials `createMesh` or `mesh.setGeometry`. Its groups number the primitives
|
|
20
|
+
* (`materialIndex` 0, 1, ...) for a material array; a single material ignores them.
|
|
21
|
+
*/
|
|
22
|
+
export type DAEMeshGeometry = Omit<DAEGeometry, 'groups'> & {
|
|
23
|
+
normals: Float32Array;
|
|
24
|
+
groups: (DAEGroup & {
|
|
25
|
+
materialIndex: number;
|
|
26
|
+
})[];
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Every node's local and world matrix and every morph's weights at `time` seconds, from the document's animation
|
|
30
|
+
* channels (or one clip's). Channels replace values of the node's transform stack as written, which is then converted
|
|
31
|
+
* to Y-up meters like `node.matrix`.
|
|
32
|
+
*/
|
|
33
|
+
export declare function poseDAE(dae: LoadedDAE, time: number, options?: DAEPoseOptions): DAEPose;
|
|
34
|
+
/** The scene at rest: `node.matrix` everywhere and the morphs' default weights. */
|
|
35
|
+
export declare function restPose(dae: LoadedDAE): DAEPose;
|
|
36
|
+
/**
|
|
37
|
+
* A copy of a morph's base mesh with its targets blended at `weights` (default: the morph's own weights), position
|
|
38
|
+
* by position: `NORMALIZED` (1 - sum w) base + sum w target, `RELATIVE` base + sum w target. Normals blend the same
|
|
39
|
+
* way and are renormalised (computed when the base has none). Vertex order is the base mesh's, so the result can
|
|
40
|
+
* replace a mesh's geometry every frame.
|
|
41
|
+
*/
|
|
42
|
+
export declare function morphDAE(dae: LoadedDAE, morph: number, weights?: ArrayLike<number>): DAEMeshGeometry;
|
|
43
|
+
/**
|
|
44
|
+
* @internal An instance's mesh in world space at a pose: morphed by the pose's weights, then skinned by its joints'
|
|
45
|
+
* world matrices (sum w joint * inverseBind * bindShape) or moved by `world`. `flip` is set when the transform
|
|
46
|
+
* mirrors, so the caller can keep outward winding.
|
|
47
|
+
*/
|
|
48
|
+
export declare function deformInstance(dae: LoadedDAE, instance: DAEInstance, world: Matrix, pose: DAEPose): {
|
|
49
|
+
positions: Float32Array;
|
|
50
|
+
normals?: Float32Array;
|
|
51
|
+
flip: boolean;
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* A unique name for every node, depth first: its id, else its name, else `node`, with `#2`, `#3`... added to repeats
|
|
55
|
+
* (copies made by `instance_node`). Bones of {@link bindDAESkin} and tracks of {@link daeKeyframeClip} use these.
|
|
56
|
+
*/
|
|
57
|
+
export declare function daeNodeNames(dae: LoadedDAE): Map<DAENode, string>;
|
|
58
|
+
/** One bone for the animation package's `Skeleton`: a node's local transform split into TRS. */
|
|
59
|
+
export interface DAEBone {
|
|
60
|
+
/** The node's name from {@link daeNodeNames}, which keyframe tracks target. */
|
|
61
|
+
name: string;
|
|
62
|
+
/** Index of the parent bone, -1 for the root. */
|
|
63
|
+
parent: number;
|
|
64
|
+
translation: MutableVec3;
|
|
65
|
+
rotation: [number, number, number, number];
|
|
66
|
+
scale: MutableVec3;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* A skinned instance ready for the animation package: `new Skeleton(bones, inverseBindMatrices)` and
|
|
70
|
+
* `Skin.attach(mesh, skeleton, joints, weights)` on a mesh made from `geometry`. Bones live in scene (world) space,
|
|
71
|
+
* so put the mesh at the scene root with an identity transform.
|
|
72
|
+
*/
|
|
73
|
+
export interface DAESkinBinding {
|
|
74
|
+
/** The node holding the `<instance_controller>`. */
|
|
75
|
+
node: DAENode;
|
|
76
|
+
instance: DAEInstance;
|
|
77
|
+
/** The mesh in its bind shape (`bind_shape_matrix` applied, morphs at the given weights), in Y-up meters. */
|
|
78
|
+
geometry: DAEMeshGeometry;
|
|
79
|
+
/** Every joint node and its ancestors in scene order; an identity root bone is added when needed. */
|
|
80
|
+
bones: DAEBone[];
|
|
81
|
+
/** 16 column-major numbers per bone: the skin's for joints, the inverse rest world matrix for other bones. */
|
|
82
|
+
inverseBindMatrices: Float64Array;
|
|
83
|
+
/** Four bone indices per vertex of `geometry`. */
|
|
84
|
+
joints: Uint32Array;
|
|
85
|
+
/** Four weights per vertex, summing to 1 (all zero follows the root bone). */
|
|
86
|
+
weights: Float32Array;
|
|
87
|
+
}
|
|
88
|
+
/** Options of {@link bindDAESkin}. */
|
|
89
|
+
export interface DAESkinBindingOptions {
|
|
90
|
+
/** Weights for a skin over a morph (default: the morph's own). */
|
|
91
|
+
weights?: ArrayLike<number>;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Skeleton and skin data of a skinned instance (default: the first one in the scene) for the animation package's
|
|
95
|
+
* `Skeleton` and `Skin`. Throws a `LoaderError` (`unsupported`) when the document has no skinned instance.
|
|
96
|
+
*/
|
|
97
|
+
export declare function bindDAESkin(dae: LoadedDAE, instance?: DAEInstance, options?: DAESkinBindingOptions): DAESkinBinding;
|
|
98
|
+
/** One key of a {@link DAEKeyframeTrack}: the shape of the animation package's `Keyframe`. */
|
|
99
|
+
export interface DAEKeyframe {
|
|
100
|
+
/** Seconds from the clip's start. */
|
|
101
|
+
time: number;
|
|
102
|
+
/** [x, y, z] translation or scale, an [x, y, z, w] quaternion, or one weight per morph target. */
|
|
103
|
+
value: number[];
|
|
104
|
+
interpolation: 'linear' | 'discrete';
|
|
105
|
+
}
|
|
106
|
+
/** Keys for one property of one node: the shape of the animation package's `KeyframeTrackData`. */
|
|
107
|
+
export interface DAEKeyframeTrack {
|
|
108
|
+
/** The node's name from {@link daeNodeNames} (a bone name of {@link bindDAESkin}). */
|
|
109
|
+
target: string;
|
|
110
|
+
path: 'translation' | 'rotation' | 'scale' | 'weights';
|
|
111
|
+
keys: DAEKeyframe[];
|
|
112
|
+
}
|
|
113
|
+
/** A clip in the shape of the animation package's `KeyframeClipData`, for `createKeyframeClip`. */
|
|
114
|
+
export interface DAEKeyframeClip {
|
|
115
|
+
name: string;
|
|
116
|
+
tracks: DAEKeyframeTrack[];
|
|
117
|
+
}
|
|
118
|
+
/** Options of {@link daeKeyframeClip}. */
|
|
119
|
+
export interface DAEKeyframeClipOptions extends DAEPoseOptions {
|
|
120
|
+
/** Samples per second between keys. Default 30. */
|
|
121
|
+
fps?: number;
|
|
122
|
+
/** Clip name. Default: the clip's name or id, else `''`. */
|
|
123
|
+
name?: string;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* The document's animation (or one clip) as keyframe tracks: translation, rotation and scale for every animated node
|
|
127
|
+
* and weights for every node whose morph is animated. COLLADA channels address single transform elements, so each
|
|
128
|
+
* node is sampled at every key time of its channels and every `1 / fps` second between them, then split into TRS
|
|
129
|
+
* (exact at those times, linear between); a node whose channels all STEP keeps only its keys, held (`discrete`).
|
|
130
|
+
*/
|
|
131
|
+
export declare function daeKeyframeClip(dae: LoadedDAE, options?: DAEKeyframeClipOptions): DAEKeyframeClip;
|