@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
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import { LoaderError } from './types.js';
|
|
2
|
+
import { AXES, F, IDENTITY, kid, multiply, numbers, ref } from './dae-common.js';
|
|
3
|
+
const rotation = (upAxis) => {
|
|
4
|
+
const axis = AXES[upAxis], m = [...IDENTITY];
|
|
5
|
+
for (let r = 0; r < 3; r++)
|
|
6
|
+
for (let c = 0; c < 3; c++)
|
|
7
|
+
m[c * 4 + r] = axis[r * 3 + c];
|
|
8
|
+
return m;
|
|
9
|
+
};
|
|
10
|
+
const normalized = (x, y, z) => {
|
|
11
|
+
const length = Math.hypot(x, y, z) || 1;
|
|
12
|
+
return [x / length, y / length, z / length];
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* A node's camera or light frame in Y-up meters, column-major: the node's world matrix (from `pose`, else at rest)
|
|
16
|
+
* times the up-axis rotation, so the file's local -Z view axis and +Y up stay the frame's -Z and +Y. Throws a
|
|
17
|
+
* TypeError when the node is not in the document.
|
|
18
|
+
*/
|
|
19
|
+
export function daeFrame(dae, node, pose) {
|
|
20
|
+
let world = pose?.world.get(node);
|
|
21
|
+
if (!world) {
|
|
22
|
+
const find = (n, parent) => {
|
|
23
|
+
const w = multiply(parent, n.matrix);
|
|
24
|
+
if (n === node)
|
|
25
|
+
return w;
|
|
26
|
+
for (const child of n.children) {
|
|
27
|
+
const found = find(child, w);
|
|
28
|
+
if (found)
|
|
29
|
+
return found;
|
|
30
|
+
}
|
|
31
|
+
return undefined;
|
|
32
|
+
};
|
|
33
|
+
for (const root of dae.nodes)
|
|
34
|
+
if ((world = find(root, IDENTITY)))
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
if (!world)
|
|
38
|
+
throw new TypeError('daeFrame needs a node of this document; pass camera.node or light.node');
|
|
39
|
+
return multiply(world, rotation(dae.upAxis));
|
|
40
|
+
}
|
|
41
|
+
function value(e, what, fallback) {
|
|
42
|
+
if (!e)
|
|
43
|
+
return fallback;
|
|
44
|
+
const v = numbers(e.text, what);
|
|
45
|
+
if (v.length !== 1 || !Number.isFinite(v[0]))
|
|
46
|
+
throw new LoaderError(F, 'syntax', `${what} must be one finite number; write it as a plain number`);
|
|
47
|
+
return v[0];
|
|
48
|
+
}
|
|
49
|
+
function readOptics(camera, meter, warn) {
|
|
50
|
+
const id = camera.attributes.id ?? camera.attributes.name ?? '', common = kid(kid(camera, 'optics'), 'technique_common'), perspective = kid(common, 'perspective'), e = perspective ?? kid(common, 'orthographic');
|
|
51
|
+
if (!e) {
|
|
52
|
+
warn(`camera "${id}" has no technique_common <perspective> or <orthographic> and is not imported`);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
const what = `camera "${id}" <${e.name}>`, read = (name) => value(kid(e, name), `${what} <${name}>`), near = read('znear'), far = read('zfar');
|
|
56
|
+
if (near === undefined || far === undefined)
|
|
57
|
+
throw new LoaderError(F, 'syntax', `${what} needs <znear> and <zfar>; add both`);
|
|
58
|
+
if (!(far > near) || (perspective && !(near > 0)))
|
|
59
|
+
throw new LoaderError(F, 'range', `${what} znear ${near} and zfar ${far} are invalid; give ${perspective ? '0 < znear' : 'znear'} < zfar`);
|
|
60
|
+
const [xName, yName] = perspective ? ['xfov', 'yfov'] : ['xmag', 'ymag'], x = read(xName), y = read(yName);
|
|
61
|
+
let aspect = read('aspect_ratio');
|
|
62
|
+
if (x === undefined && y === undefined)
|
|
63
|
+
throw new LoaderError(F, 'syntax', `${what} needs <${xName}> or <${yName}>; add one (and <aspect_ratio> if wanted)`);
|
|
64
|
+
if (x !== undefined && y !== undefined && aspect !== undefined) {
|
|
65
|
+
warn(`${what} gives ${xName}, ${yName} and aspect_ratio; aspect_ratio is ignored (give only two of them)`);
|
|
66
|
+
aspect = undefined;
|
|
67
|
+
}
|
|
68
|
+
for (const [name, v] of [
|
|
69
|
+
[xName, x],
|
|
70
|
+
[yName, y],
|
|
71
|
+
])
|
|
72
|
+
if (v !== undefined && !(v > 0 && (!perspective || v < 180)))
|
|
73
|
+
throw new LoaderError(F, 'range', `${what} <${name}> ${v} is invalid; give ${perspective ? 'degrees between 0 and 180' : 'a size > 0'}`);
|
|
74
|
+
if (aspect !== undefined && !(aspect > 0))
|
|
75
|
+
throw new LoaderError(F, 'range', `${what} <aspect_ratio> ${aspect} is invalid; give width / height > 0`);
|
|
76
|
+
const scale = perspective ? 1 : meter, optics = { znear: near * meter, zfar: far * meter };
|
|
77
|
+
if (x !== undefined)
|
|
78
|
+
optics[xName] = x * scale;
|
|
79
|
+
if (y !== undefined)
|
|
80
|
+
optics[yName] = y * scale;
|
|
81
|
+
if (aspect !== undefined)
|
|
82
|
+
optics.aspectRatio = aspect;
|
|
83
|
+
return perspective ? { type: 'perspective', perspective: optics } : { type: 'orthographic', orthographic: optics };
|
|
84
|
+
}
|
|
85
|
+
const LIGHTS = ['ambient', 'directional', 'point', 'spot'];
|
|
86
|
+
function readLight(light, meter, warn) {
|
|
87
|
+
const id = light.attributes.id ?? light.attributes.name ?? '', e = kid(light, 'technique_common')?.children.find((c) => LIGHTS.includes(c.name));
|
|
88
|
+
if (!e) {
|
|
89
|
+
warn(`light "${id}" has no technique_common ambient, directional, point or spot light and is not imported`);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
const what = `light "${id}" <${e.name}>`, colorElement = kid(e, 'color'), color = colorElement ? numbers(colorElement.text, `${what} <color>`) : [1, 1, 1];
|
|
93
|
+
if (color.length !== 3 || !color.every((c) => Number.isFinite(c) && c >= 0))
|
|
94
|
+
throw new LoaderError(F, 'syntax', `${what} <color> needs 3 finite numbers >= 0 (r g b), got ${color.length}`);
|
|
95
|
+
const result = { type: e.name, color: [color[0], color[1], color[2]] };
|
|
96
|
+
if (light.attributes.id !== undefined)
|
|
97
|
+
result.id = light.attributes.id;
|
|
98
|
+
if (light.attributes.name !== undefined)
|
|
99
|
+
result.name = light.attributes.name;
|
|
100
|
+
if (result.type === 'point' || result.type === 'spot') {
|
|
101
|
+
const read = (name, fallback) => {
|
|
102
|
+
const v = value(kid(e, name), `${what} <${name}>`, fallback);
|
|
103
|
+
if (!(v >= 0))
|
|
104
|
+
throw new LoaderError(F, 'range', `${what} <${name}> ${v} is negative; give a number >= 0`);
|
|
105
|
+
return v;
|
|
106
|
+
};
|
|
107
|
+
const constant = read('constant_attenuation', 1), linear = read('linear_attenuation', 0), quadratic = read('quadratic_attenuation', 0);
|
|
108
|
+
if (!(constant || linear || quadratic))
|
|
109
|
+
throw new LoaderError(F, 'range', `${what} attenuation is all zero; give constant_attenuation 1 (or any term > 0)`);
|
|
110
|
+
result.constantAttenuation = constant;
|
|
111
|
+
result.linearAttenuation = linear / meter;
|
|
112
|
+
result.quadraticAttenuation = quadratic / (meter * meter);
|
|
113
|
+
}
|
|
114
|
+
if (result.type === 'spot') {
|
|
115
|
+
const angle = value(kid(e, 'falloff_angle'), `${what} <falloff_angle>`, 180), exponent = value(kid(e, 'falloff_exponent'), `${what} <falloff_exponent>`, 0);
|
|
116
|
+
if (!(angle > 0 && angle <= 360))
|
|
117
|
+
throw new LoaderError(F, 'range', `${what} <falloff_angle> ${angle} is invalid; give degrees in (0, 360]`);
|
|
118
|
+
if (!(exponent >= 0))
|
|
119
|
+
throw new LoaderError(F, 'range', `${what} <falloff_exponent> ${exponent} is negative; give a number >= 0`);
|
|
120
|
+
result.falloffAngle = angle;
|
|
121
|
+
result.falloffExponent = exponent;
|
|
122
|
+
}
|
|
123
|
+
return result;
|
|
124
|
+
}
|
|
125
|
+
/** @internal Resolves the scene's camera and light instances into data at rest. */
|
|
126
|
+
export function placeScene(dae, ids, placements, warn) {
|
|
127
|
+
const cameras = [], lights = [], optics = new Map(), sources = new Map();
|
|
128
|
+
for (const { node, kind, url } of placements) {
|
|
129
|
+
const e = ids.get(ref(url));
|
|
130
|
+
if (e?.name !== kind) {
|
|
131
|
+
warn(`instance_${kind} "${url}" names no <${kind}>`);
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
const frame = daeFrame(dae, node);
|
|
135
|
+
if (kind === 'camera') {
|
|
136
|
+
if (!optics.has(e))
|
|
137
|
+
optics.set(e, readOptics(e, dae.meter, warn));
|
|
138
|
+
const read = optics.get(e);
|
|
139
|
+
if (!read)
|
|
140
|
+
continue;
|
|
141
|
+
const camera = { node, ...structuredClone(read), matrix: frame };
|
|
142
|
+
if (e.attributes.id !== undefined)
|
|
143
|
+
camera.id = e.attributes.id;
|
|
144
|
+
if (e.attributes.name !== undefined)
|
|
145
|
+
camera.name = e.attributes.name;
|
|
146
|
+
cameras.push(camera);
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
if (!sources.has(e))
|
|
150
|
+
sources.set(e, readLight(e, dae.meter, warn));
|
|
151
|
+
const read = sources.get(e);
|
|
152
|
+
if (!read)
|
|
153
|
+
continue;
|
|
154
|
+
lights.push({
|
|
155
|
+
...structuredClone(read),
|
|
156
|
+
node,
|
|
157
|
+
position: [frame[12], frame[13], frame[14]],
|
|
158
|
+
direction: normalized(-frame[8], -frame[9], -frame[10]),
|
|
159
|
+
matrix: frame,
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return { cameras, lights };
|
|
164
|
+
}
|
|
165
|
+
const tan = (degrees) => Math.tan((degrees * Math.PI) / 360);
|
|
166
|
+
const atan = (t) => (Math.atan(t) * 360) / Math.PI;
|
|
167
|
+
/**
|
|
168
|
+
* How an engine camera views through `dae.cameras[camera]` (default the first), from its node's frame at rest (or at
|
|
169
|
+
* `options.pose`). Perspective follows the spec's combinations: `yfov` is the vertical angle; `xfov` with
|
|
170
|
+
* `aspect_ratio` gives yfov = 2 atan(tan(xfov / 2) / aspect); `xfov` with `yfov` fixes the aspect as
|
|
171
|
+
* tan(xfov / 2) / tan(yfov / 2) and keeps yfov; `xfov` alone takes `options.aspect`. Orthographic: `ymag` is the
|
|
172
|
+
* half-height and the width follows the viewport; `xmag` alone is the half-width (the height follows the viewport);
|
|
173
|
+
* `xmag` with `aspect_ratio` gives a half-height of xmag / aspect. Throws a RangeError when the camera does not exist
|
|
174
|
+
* or `xfov` alone has no valid `options.aspect`.
|
|
175
|
+
*/
|
|
176
|
+
export function daeCameraView(dae, camera = 0, options = {}) {
|
|
177
|
+
const def = Number.isInteger(camera) ? dae.cameras[camera] : undefined;
|
|
178
|
+
if (!def)
|
|
179
|
+
throw new RangeError(`DAE camera ${camera} does not exist; pass an index below ${dae.cameras.length}`);
|
|
180
|
+
const m = options.pose ? daeFrame(dae, def.node, options.pose) : def.matrix, forward = normalized(-m[8], -m[9], -m[10]), view = {
|
|
181
|
+
position: [m[12], m[13], m[14]],
|
|
182
|
+
target: [m[12] + forward[0], m[13] + forward[1], m[14] + forward[2]],
|
|
183
|
+
up: normalized(m[4], m[5], m[6]),
|
|
184
|
+
near: 0,
|
|
185
|
+
far: 0,
|
|
186
|
+
project: undefined,
|
|
187
|
+
};
|
|
188
|
+
if (def.perspective) {
|
|
189
|
+
const { xfov, yfov, aspectRatio, znear, zfar } = def.perspective;
|
|
190
|
+
let aspect = xfov !== undefined && yfov !== undefined ? tan(xfov) / tan(yfov) : aspectRatio;
|
|
191
|
+
if (aspect !== undefined)
|
|
192
|
+
view.aspect = aspect;
|
|
193
|
+
if (yfov === undefined && aspect === undefined) {
|
|
194
|
+
aspect = options.aspect;
|
|
195
|
+
if (typeof aspect !== 'number' || !(aspect > 0) || !Number.isFinite(aspect))
|
|
196
|
+
throw new RangeError(`DAE camera ${camera} gives only xfov; pass {aspect: viewport width / height}`);
|
|
197
|
+
}
|
|
198
|
+
view.fov = yfov ?? atan(tan(xfov) / aspect);
|
|
199
|
+
view.near = znear;
|
|
200
|
+
view.far = zfar;
|
|
201
|
+
return view;
|
|
202
|
+
}
|
|
203
|
+
const { xmag, ymag, aspectRatio, znear: near, zfar: far } = def.orthographic, depth = far - near, fixed = ymag ?? (aspectRatio !== undefined ? xmag / aspectRatio : undefined);
|
|
204
|
+
view.near = near;
|
|
205
|
+
view.far = far;
|
|
206
|
+
if (xmag !== undefined && ymag !== undefined)
|
|
207
|
+
view.aspect = xmag / ymag;
|
|
208
|
+
else if (aspectRatio !== undefined)
|
|
209
|
+
view.aspect = aspectRatio;
|
|
210
|
+
// Column-major; WebGPU depth is reversed (near 1, far 0) like core's perspective, WebGL's runs -1 to 1.
|
|
211
|
+
view.project = (out, aspect, webgpu) => {
|
|
212
|
+
const halfHeight = fixed ?? xmag / aspect;
|
|
213
|
+
out.fill(0);
|
|
214
|
+
out[0] = 1 / (halfHeight * aspect);
|
|
215
|
+
out[5] = 1 / halfHeight;
|
|
216
|
+
out[10] = webgpu ? 1 / depth : -2 / depth;
|
|
217
|
+
out[14] = webgpu ? far / depth : -(far + near) / depth;
|
|
218
|
+
out[15] = 1;
|
|
219
|
+
};
|
|
220
|
+
return view;
|
|
221
|
+
}
|
|
222
|
+
/** A color split into its brightest channel (times `scale`) and the color divided by it. */
|
|
223
|
+
const split = (c, scale) => {
|
|
224
|
+
const peak = Math.max(c[0], c[1], c[2]);
|
|
225
|
+
return peak > 0
|
|
226
|
+
? { color: [c[0] / peak, c[1] / peak, c[2] / peak], intensity: peak * scale }
|
|
227
|
+
: { color: [0, 0, 0], intensity: 0 };
|
|
228
|
+
};
|
|
229
|
+
/**
|
|
230
|
+
* The document's lights (at rest, or at `options.pose`) in the shapes the engine takes. Colors are split into a unit
|
|
231
|
+
* color and an intensity (the brightest channel). The first directional light is core's sun, the others are for
|
|
232
|
+
* @margoengine/clustered/directional; ambient lights add up into core's ambient. Point and spot attenuation
|
|
233
|
+
* `1 / (c + l d + q d^2)` is approximated by its strongest set term: q gives inverse-square (`decay` 2,
|
|
234
|
+
* intensity / q), else l gives `decay` 1 (intensity / l), else c gives `decay` 0 (intensity / c, `range` 0). The
|
|
235
|
+
* dropped terms make the light brighter near its position than the file's, and the same far away. `range` ends where
|
|
236
|
+
* intensity / d^decay is 1/256. A spot's full `falloff_angle` becomes `outerAngle` (half of it, at most a
|
|
237
|
+
* hemisphere); its `falloff_exponent` e (cos^e fading within the cone) becomes an `innerAngle` where cos^e is 1/2, a
|
|
238
|
+
* hard edge for e = 0. Throws a RangeError for a negative or non-finite `intensity`.
|
|
239
|
+
*/
|
|
240
|
+
export function daeEngineLights(dae, options = {}) {
|
|
241
|
+
const scale = options.intensity ?? 1;
|
|
242
|
+
if (typeof scale !== 'number' || !Number.isFinite(scale) || scale < 0)
|
|
243
|
+
throw new RangeError(`DAE light intensity ${scale} is invalid; pass a finite number >= 0`);
|
|
244
|
+
const out = { directional: [], local: [] }, ambient = [0, 0, 0];
|
|
245
|
+
let ambients = 0;
|
|
246
|
+
for (const light of dae.lights) {
|
|
247
|
+
if (light.type === 'ambient') {
|
|
248
|
+
ambients++;
|
|
249
|
+
for (let c = 0; c < 3; c++)
|
|
250
|
+
ambient[c] += light.color[c];
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
const frame = options.pose ? daeFrame(dae, light.node, options.pose) : light.matrix, direction = normalized(-frame[8], -frame[9], -frame[10]), { color, intensity } = split(light.color, scale);
|
|
254
|
+
if (light.type === 'directional') {
|
|
255
|
+
if (!out.sun)
|
|
256
|
+
out.sun = { toward: [-direction[0], -direction[1], -direction[2]], color, intensity };
|
|
257
|
+
else
|
|
258
|
+
out.directional.push({ direction, color, intensity });
|
|
259
|
+
continue;
|
|
260
|
+
}
|
|
261
|
+
const q = light.quadraticAttenuation ?? 0, l = light.linearAttenuation ?? 0, [decay, term] = q > 0 ? [2, q] : l > 0 ? [1, l] : [0, light.constantAttenuation ?? 1], strength = intensity / term, local = {
|
|
262
|
+
position: [frame[12], frame[13], frame[14]],
|
|
263
|
+
range: decay ? (256 * strength) ** (1 / decay) : 0,
|
|
264
|
+
color,
|
|
265
|
+
intensity: strength,
|
|
266
|
+
decay,
|
|
267
|
+
};
|
|
268
|
+
if (light.type === 'spot') {
|
|
269
|
+
const outer = Math.min(((light.falloffAngle ?? 180) * Math.PI) / 360, Math.PI / 2), e = light.falloffExponent ?? 0;
|
|
270
|
+
local.direction = direction;
|
|
271
|
+
local.outerAngle = outer;
|
|
272
|
+
local.innerAngle = e > 0 ? Math.min(outer, Math.acos(0.5 ** (1 / e))) : outer;
|
|
273
|
+
}
|
|
274
|
+
out.local.push(local);
|
|
275
|
+
}
|
|
276
|
+
if (ambients)
|
|
277
|
+
out.ambient = split(ambient, scale);
|
|
278
|
+
return out;
|
|
279
|
+
}
|
package/dist/dae.d.ts
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { type LoadedGeometry, type LoadedGroup, type ParseLimits } from './types.js';
|
|
2
|
+
import { type BinaryInput } from './util.js';
|
|
3
|
+
import { type XMLElement, type XMLExternalOptions } from './xml.js';
|
|
4
|
+
import { type DAETransform } from './dae-common.js';
|
|
5
|
+
import { type DAEMorph, type DAESkin } from './dae-controllers.js';
|
|
6
|
+
import { type DAEAnimation, type DAEClip } from './dae-animation.js';
|
|
7
|
+
import { type DAEPose } from './dae-pose.js';
|
|
8
|
+
import { type DAECamera, type DAELight } from './dae-scene.js';
|
|
9
|
+
export type { DAETransform } from './dae-common.js';
|
|
10
|
+
export type { DAESkin, DAEMorph } from './dae-controllers.js';
|
|
11
|
+
export type { DAECamera, DAELight } from './dae-scene.js';
|
|
12
|
+
export type { DAEAnimation, DAEAnimationChannel, DAEClip, DAEInterpolation, DAESampler, } from './dae-animation.js';
|
|
13
|
+
/** A primitive's triangle range and the material symbol it binds. */
|
|
14
|
+
export interface DAEGroup extends LoadedGroup {
|
|
15
|
+
/** The primitive's `material` attribute: a symbol that each instance binds to a material. */
|
|
16
|
+
symbol?: string;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* One `<geometry>` mesh in Y-up meters. Every distinct corner (vertex, normal, UV and color index) is one vertex;
|
|
20
|
+
* `positionIndices` maps it back to its `<vertices>` entry. Polygons are fan-triangulated: face f with
|
|
21
|
+
* n = `faceSizes[f]` corners c0..cn-1 gives triangles (c0, ck, ck+1). `uvs` are the lowest TEXCOORD set, with
|
|
22
|
+
* v flipped to Margo's convention (1 - t).
|
|
23
|
+
*/
|
|
24
|
+
export interface DAEGeometry extends LoadedGeometry {
|
|
25
|
+
kind: 'mesh';
|
|
26
|
+
id: string;
|
|
27
|
+
name?: string;
|
|
28
|
+
indices: Uint32Array;
|
|
29
|
+
groups: DAEGroup[];
|
|
30
|
+
faceSizes: Uint32Array;
|
|
31
|
+
positionIndices: Uint32Array;
|
|
32
|
+
/** The mesh's `<lines>` and `<linestrips>`, when it has any, with vertices of their own. */
|
|
33
|
+
lines?: DAELines;
|
|
34
|
+
}
|
|
35
|
+
/** Line primitives of a mesh: their own corners (position and colour), and segments into them. */
|
|
36
|
+
export interface DAELines {
|
|
37
|
+
positions: Float32Array;
|
|
38
|
+
/** Linear rgb per vertex, when a line primitive has a COLOR input. */
|
|
39
|
+
colors?: Float32Array;
|
|
40
|
+
/** The `<vertices>` entry each vertex came from. */
|
|
41
|
+
positionIndices: Uint32Array;
|
|
42
|
+
/** Segments, two vertex indices each: a `<lines>` pair is one, a `<linestrips>` strip of n corners gives n - 1. */
|
|
43
|
+
indices: Uint32Array;
|
|
44
|
+
/** One range of `indices` per line primitive, with its material symbol. */
|
|
45
|
+
groups: DAEGroup[];
|
|
46
|
+
}
|
|
47
|
+
/** A material channel: a color, or a texture read through a named UV set. */
|
|
48
|
+
export interface DAEChannel {
|
|
49
|
+
/** RGBA as written. */
|
|
50
|
+
color?: [number, number, number, number];
|
|
51
|
+
/** Image path relative to the .dae folder, and the effect's `texcoord` name. */
|
|
52
|
+
texture?: {
|
|
53
|
+
path: string;
|
|
54
|
+
texcoord?: string;
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
/** A `<material>` with its `profile_COMMON` effect resolved. */
|
|
58
|
+
export interface DAEMaterial {
|
|
59
|
+
id: string;
|
|
60
|
+
name?: string;
|
|
61
|
+
shading: 'constant' | 'lambert' | 'phong' | 'blinn';
|
|
62
|
+
emission?: DAEChannel;
|
|
63
|
+
ambient?: DAEChannel;
|
|
64
|
+
diffuse?: DAEChannel;
|
|
65
|
+
specular?: DAEChannel;
|
|
66
|
+
reflective?: DAEChannel;
|
|
67
|
+
transparent?: DAEChannel;
|
|
68
|
+
/** `opaque` mode of `transparent`: `'A_ONE'` (default), `'A_ZERO'`, `'RGB_ONE'` or `'RGB_ZERO'`. */
|
|
69
|
+
opaque?: string;
|
|
70
|
+
shininess?: number;
|
|
71
|
+
reflectivity?: number;
|
|
72
|
+
transparency?: number;
|
|
73
|
+
indexOfRefraction?: number;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* An `<instance_geometry>`, or an `<instance_controller>` (then `skin` and/or `morph` are set and `geometry` is the
|
|
77
|
+
* controller's base mesh).
|
|
78
|
+
*/
|
|
79
|
+
export interface DAEInstance {
|
|
80
|
+
/** Index into `LoadedDAE.geometries`. */
|
|
81
|
+
geometry: number;
|
|
82
|
+
/** Index into `LoadedDAE.materials` by primitive symbol, in a null-prototype object. */
|
|
83
|
+
materials: Record<string, number>;
|
|
84
|
+
/** Index into `LoadedDAE.skins`; set only when every joint resolved to a node (`joints`). */
|
|
85
|
+
skin?: number;
|
|
86
|
+
/** Index into `LoadedDAE.morphs`. */
|
|
87
|
+
morph?: number;
|
|
88
|
+
/** The node of each of the skin's `joints`, found under the instance's `<skeleton>` roots (else the whole scene). */
|
|
89
|
+
joints?: DAENode[];
|
|
90
|
+
}
|
|
91
|
+
/** A scene node. `instance_node` references are expanded into copies. */
|
|
92
|
+
export interface DAENode {
|
|
93
|
+
id?: string;
|
|
94
|
+
name?: string;
|
|
95
|
+
/** The node's `sid`, which skin joints name. */
|
|
96
|
+
sid?: string;
|
|
97
|
+
/**
|
|
98
|
+
* Local transform (the node's matrix, translate, rotate, scale, lookat and skew stack), column-major 4x4, in Y-up
|
|
99
|
+
* meters.
|
|
100
|
+
*/
|
|
101
|
+
matrix: number[];
|
|
102
|
+
/** The transform stack as written (the file's axes and units), which animation channels address by sid. */
|
|
103
|
+
transforms: DAETransform[];
|
|
104
|
+
instances: DAEInstance[];
|
|
105
|
+
children: DAENode[];
|
|
106
|
+
}
|
|
107
|
+
/** A parsed COLLADA document, converted to Y-up and meters. */
|
|
108
|
+
export interface LoadedDAE {
|
|
109
|
+
/** The file's `up_axis`; geometry and matrices are already converted to Y-up. */
|
|
110
|
+
upAxis: 'X_UP' | 'Y_UP' | 'Z_UP';
|
|
111
|
+
/** The file's `unit meter`; geometry and translations are already scaled to meters. */
|
|
112
|
+
meter: number;
|
|
113
|
+
geometries: DAEGeometry[];
|
|
114
|
+
materials: DAEMaterial[];
|
|
115
|
+
/** Image paths the materials use, relative to the .dae folder, without duplicates: give each to the resolver. */
|
|
116
|
+
images: string[];
|
|
117
|
+
/** Root nodes of the instanced visual scene. */
|
|
118
|
+
nodes: DAENode[];
|
|
119
|
+
/** `<skin>` controllers, in Y-up meters. */
|
|
120
|
+
skins: DAESkin[];
|
|
121
|
+
/** `<morph>` controllers. */
|
|
122
|
+
morphs: DAEMorph[];
|
|
123
|
+
/** Every `<animation>`, nested ones included, with the channels that address imported nodes and morphs. */
|
|
124
|
+
animations: DAEAnimation[];
|
|
125
|
+
/** `<animation_clip>`s. */
|
|
126
|
+
clips: DAEClip[];
|
|
127
|
+
/** Every `<instance_camera>` in scene order, with its optics and frame at rest. */
|
|
128
|
+
cameras: DAECamera[];
|
|
129
|
+
/** Every `<instance_light>` in scene order, with its color, attenuation and frame at rest. */
|
|
130
|
+
lights: DAELight[];
|
|
131
|
+
/** What was skipped: holes, unresolved references, channels and controllers that do not fit. */
|
|
132
|
+
warnings: string[];
|
|
133
|
+
}
|
|
134
|
+
/** Options of `parseDAE`. */
|
|
135
|
+
export interface DAEParseOptions extends ParseLimits, XMLExternalOptions {
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Parses a COLLADA 1.4.1 or 1.5 document (`.dae`): mesh geometries (`<triangles>`, `<polylist>`, `<polygons>`,
|
|
139
|
+
* `<trifans>`, `<tristrips>` with VERTEX, POSITION, NORMAL, TEXCOORD and COLOR inputs), `profile_COMMON` materials
|
|
140
|
+
* (constant, lambert, phong, blinn; colors and textures through sampler and surface params), images, and the
|
|
141
|
+
* instanced visual scene with matrix/translate/rotate/scale stacks, `bind_material` symbols and `instance_node`.
|
|
142
|
+
* Controllers (`<skin>` with up to four influences per vertex, `<morph>` NORMALIZED or RELATIVE) and
|
|
143
|
+
* `<instance_controller>` with `<skeleton>` roots, animations (nested, LINEAR, STEP, BEZIER and HERMITE samplers on
|
|
144
|
+
* node transform elements and morph weights) and animation clips are read too. `up_axis` and `unit` are converted to
|
|
145
|
+
* Y-up meters, alike for geometry, node, bind shape and inverse bind matrices. `lookat` and `skew` join the transform
|
|
146
|
+
* stack; `<instance_camera>` and `<instance_light>` give `cameras` and `lights` (technique_common optics and lights)
|
|
147
|
+
* with their frames. `<lines>` and `<linestrips>` come back as each geometry's `lines`. Polygon holes, and cameras,
|
|
148
|
+
* lights, channels or controllers that do not fit, are not imported; each is reported in `warnings`. Image paths are
|
|
149
|
+
* relative to the .dae folder, for the caller's resolver. Joints are read by `parseDAEKinematics` of
|
|
150
|
+
* `@margoengine/loaders/dae-kinematics`.
|
|
151
|
+
*/
|
|
152
|
+
export declare function parseDAE(input: BinaryInput | string, options?: DAEParseOptions): LoadedDAE;
|
|
153
|
+
/** The document's `<COLLADA>` element. */
|
|
154
|
+
export declare function daeRoot(input: BinaryInput | string, options: DAEParseOptions): XMLElement;
|
|
155
|
+
/** {@link parseDAE} of a document already read, for entries that also read other libraries of it. */
|
|
156
|
+
export declare function readDAE(root: XMLElement, options: DAEParseOptions): LoadedDAE;
|
|
157
|
+
/**
|
|
158
|
+
* Bakes a parsed COLLADA scene into one `LoadedGeometry` for `createMeshes` and `toGeometry`: every node instance's
|
|
159
|
+
* geometry transformed by its world matrix (mirroring transforms keep outward winding), one group per instance and
|
|
160
|
+
* primitive, named after the node and colored with the bound material's diffuse color. Morphed instances use the
|
|
161
|
+
* pose's weights and skinned ones are skinned by their joints' world matrices, so `flattenDAE(dae, poseDAE(dae, t))`
|
|
162
|
+
* bakes the animated scene at time t; without a pose the scene is at rest.
|
|
163
|
+
*/
|
|
164
|
+
export declare function flattenDAE(dae: LoadedDAE, pose?: DAEPose): LoadedGeometry & {
|
|
165
|
+
faceSizes: Uint32Array;
|
|
166
|
+
};
|