@es3d/es3d 1.0.0 → 1.0.1

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.
Files changed (79) hide show
  1. package/LICENSE +201 -201
  2. package/README.md +86 -86
  3. package/docs/SPEC.md +417 -417
  4. package/package.json +1 -1
  5. package/scripts/es3d-cli.mjs +377 -377
  6. package/src/cache/geometry-store.js +392 -392
  7. package/src/cache/mesh-bundle.js +123 -123
  8. package/src/cache/sha256.js +73 -73
  9. package/src/format/ast.js +238 -238
  10. package/src/format/colors.js +93 -93
  11. package/src/format/compact-parser.js +171 -171
  12. package/src/format/compact-tokenizer.js +165 -165
  13. package/src/format/errors.js +247 -247
  14. package/src/format/json.js +55 -55
  15. package/src/format/limits.js +14 -14
  16. package/src/format/parse.js +92 -92
  17. package/src/format/schema.js +617 -617
  18. package/src/format/serializer.js +333 -333
  19. package/src/format/validator.js +330 -330
  20. package/src/format/value-error.js +12 -12
  21. package/src/format/value-format.js +314 -314
  22. package/src/format/values.js +459 -459
  23. package/src/format/xml-parser.js +136 -136
  24. package/src/format/xml-tokenizer.js +237 -237
  25. package/src/geometry/contour.js +86 -86
  26. package/src/geometry/extrude.js +175 -175
  27. package/src/geometry/gear.js +85 -85
  28. package/src/geometry/geometry-cache.js +159 -159
  29. package/src/geometry/lod-error.js +178 -178
  30. package/src/geometry/mesh.js +458 -458
  31. package/src/geometry/primitives.js +299 -299
  32. package/src/geometry/registry.js +645 -645
  33. package/src/geometry/revolve.js +136 -136
  34. package/src/geometry/sweep.js +441 -441
  35. package/src/index.js +47 -47
  36. package/src/io/compression.js +67 -67
  37. package/src/io/convert.js +236 -236
  38. package/src/io/download.js +13 -13
  39. package/src/io/external-scene.js +127 -127
  40. package/src/io/file-loader.js +28 -28
  41. package/src/io/gltf.js +630 -630
  42. package/src/io/mesh-import.js +104 -104
  43. package/src/io/obj.js +352 -352
  44. package/src/io/step-brep.js +439 -439
  45. package/src/io/step-exact.js +1149 -1149
  46. package/src/io/step.js +1418 -1418
  47. package/src/io/stl.js +193 -193
  48. package/src/io/svg-import.js +787 -787
  49. package/src/material/materials.js +154 -154
  50. package/src/math/mat4.js +475 -475
  51. package/src/math/random.js +30 -30
  52. package/src/math/trig.js +65 -65
  53. package/src/math/vec3.js +81 -81
  54. package/src/optimize/optimizer.js +579 -579
  55. package/src/path/path-flatten.js +322 -322
  56. package/src/path/path-parser.js +332 -332
  57. package/src/path/shapes.js +129 -129
  58. package/src/path/triangulate.js +517 -517
  59. package/src/pipeline.js +74 -74
  60. package/src/render/camera.js +168 -168
  61. package/src/render/frame-builder.js +362 -362
  62. package/src/render/geometry-pool.js +149 -149
  63. package/src/render/lighting.js +149 -149
  64. package/src/render/orbit-controls.js +237 -237
  65. package/src/render/renderer.js +258 -249
  66. package/src/render/scroll-timeline.js +104 -104
  67. package/src/render/shaders/glsl.js +285 -285
  68. package/src/render/shaders/wgsl.js +252 -252
  69. package/src/render/stage.js +364 -352
  70. package/src/render/texture-cache.js +40 -40
  71. package/src/render/textures.js +223 -223
  72. package/src/render/webgl2/webgl2-backend.js +586 -532
  73. package/src/render/webgpu/webgpu-backend.js +482 -480
  74. package/src/scene/flatten.js +754 -754
  75. package/src/scene/picking.js +161 -161
  76. package/src/scene/scene-graph.js +320 -320
  77. package/src/transform/transform.js +211 -211
  78. package/src/wasm/kernels-binary.js +3 -3
  79. package/src/wasm/kernels.js +385 -385
@@ -1,211 +1,211 @@
1
- // The ES3D transform system.
2
- //
3
- // transform="translate(0,2,0) rotate(0,45,0) scale(2)" is read exactly like
4
- // SVG: functions are applied left to right as nested coordinate systems, so the
5
- // resulting matrix is T1 * T2 * T3 (the rightmost function acts on the object
6
- // first). Supported functions:
7
- //
8
- // translate(x[,y[,z]]) rotate(x,y,z) Euler degrees (X, then Y, then Z)
9
- // rotateX(a) rotateY(a) rotateZ(a) rotate(a, ax,ay,az) axis-angle
10
- // scale(s) | scale(x,y,z) quat(x,y,z,w)
11
- // matrix(m0..m15) 16 numbers, column-major (like CSS matrix3d)
12
- //
13
- // Elements additionally accept position / rotation / scale attributes. The
14
- // local matrix of an element is transform * T(position) * R(rotation) * S(scale)
15
- // mirroring SVG <use>, where x/y are applied after (inside) the transform.
16
-
17
- import * as mat4 from '../math/mat4.js';
18
- import { ValueError } from '../format/value-error.js';
19
- import { suggest } from '../format/errors.js';
20
- import { readNumber, numberValue } from '../path/path-parser.js';
21
-
22
- const FUNCS = {
23
- translate: [1, 3],
24
- rotate: [3, 4],
25
- rotateX: [1, 1],
26
- rotateY: [1, 1],
27
- rotateZ: [1, 1],
28
- scale: [1, 3],
29
- matrix: [16, 16],
30
- quat: [4, 4],
31
- };
32
-
33
- function isWs(c) {
34
- return c === 32 || c === 9 || c === 10 || c === 13 || c === 44; // comma acts as separator between functions
35
- }
36
-
37
- /** Parse a transform list into operations (validated, with offsets). */
38
- export function parseTransformOps(str) {
39
- const ops = [];
40
- const n = str.length;
41
- let i = 0;
42
- while (true) {
43
- while (i < n && isWs(str.charCodeAt(i))) i++;
44
- if (i >= n) break;
45
- const nameStart = i;
46
- while (i < n && /[A-Za-z0-9]/.test(str[i])) i++;
47
- const fn = str.slice(nameStart, i);
48
- if (!fn) throw new ValueError(`Unexpected character '${str[i]}' in transform.`, i, 1);
49
- if (!(fn in FUNCS)) {
50
- throw new ValueError(`Unknown transform function '${fn}'.`, nameStart, fn.length, {
51
- suggestion: suggest(fn, Object.keys(FUNCS)),
52
- hint: 'Valid functions: translate, rotate, rotateX, rotateY, rotateZ, scale, matrix, quat.',
53
- });
54
- }
55
- while (i < n && (str[i] === ' ' || str[i] === '\t')) i++;
56
- if (str[i] !== '(') throw new ValueError(`Expected '(' after '${fn}'.`, i, 1);
57
- i++;
58
- const args = [];
59
- for (;;) {
60
- while (i < n && /[\s,]/.test(str[i])) i++;
61
- if (str[i] === ')') {
62
- i++;
63
- break;
64
- }
65
- if (i >= n) throw new ValueError(`Missing ')' to close '${fn}('.`, nameStart, fn.length + 1);
66
- const end = readNumber(str, i);
67
- if (end < 0) throw new ValueError(`Expected a number in '${fn}()' but found '${str[i]}'.`, i, 1);
68
- args.push(numberValue);
69
- i = end;
70
- }
71
- const [min, max] = FUNCS[fn];
72
- if (args.length < min || args.length > max) {
73
- let hint;
74
- if (fn === 'rotate' && args.length === 1) hint = 'rotate() takes Euler angles rotate(x,y,z) or an axis-angle rotate(a,ax,ay,az); use rotateY(a) for a single angle.';
75
- if (fn === 'rotate' && args.length === 2) hint = 'rotate() takes 3 Euler angles in degrees, e.g. rotate(0,45,0).';
76
- throw new ValueError(
77
- `${fn}() takes ${min === max ? min : `${min} to ${max}`} numbers but ${args.length} ${args.length === 1 ? 'was' : 'were'} given.`,
78
- nameStart,
79
- i - nameStart,
80
- { hint },
81
- );
82
- }
83
- if (fn === 'rotate' && args.length === 4 && Math.hypot(args[1], args[2], args[3]) < 1e-12) {
84
- throw new ValueError('rotate(angle, ax, ay, az): the rotation axis must not be zero.', nameStart, i - nameStart);
85
- }
86
- if (fn === 'scale' && args.length === 2) {
87
- throw new ValueError('scale() takes 1 (uniform) or 3 numbers.', nameStart, i - nameStart);
88
- }
89
- ops.push({ fn, args, offset: nameStart });
90
- }
91
- return ops;
92
- }
93
-
94
- /** Matrix of a single transform operation. */
95
- export function opMatrix(op, out = mat4.createF64()) {
96
- const a = op.args;
97
- switch (op.fn) {
98
- case 'translate':
99
- return mat4.fromTranslation(out, a[0], a[1] || 0, a[2] || 0);
100
- case 'rotate':
101
- if (a.length === 4) return mat4.fromAxisAngle(out, a[1], a[2], a[3], a[0] * mat4.DEG);
102
- return mat4.fromEulerDeg(out, a[0], a[1], a[2]);
103
- case 'rotateX':
104
- return mat4.fromRotationX(out, a[0] * mat4.DEG);
105
- case 'rotateY':
106
- return mat4.fromRotationY(out, a[0] * mat4.DEG);
107
- case 'rotateZ':
108
- return mat4.fromRotationZ(out, a[0] * mat4.DEG);
109
- case 'scale':
110
- return a.length === 1 ? mat4.fromScaling(out, a[0], a[0], a[0]) : mat4.fromScaling(out, a[0], a[1], a[2]);
111
- case 'matrix':
112
- for (let k = 0; k < 16; k++) out[k] = a[k];
113
- return out;
114
- case 'quat':
115
- return mat4.fromQuat(out, a[0], a[1], a[2], a[3]);
116
- default:
117
- throw new Error(`unknown transform ${op.fn}`);
118
- }
119
- }
120
-
121
- export function opsToMatrix(ops) {
122
- const m = mat4.createF64();
123
- const t = mat4.createF64();
124
- for (const op of ops) {
125
- opMatrix(op, t);
126
- mat4.multiply(m, m, t);
127
- }
128
- return m;
129
- }
130
-
131
- /**
132
- * {ops, matrix} for already separated operations (binary decoding), applying
133
- * the same rules as the text parser; null when an operation is invalid.
134
- */
135
- export function transformFromOps(ops) {
136
- for (const op of ops) {
137
- const range = FUNCS[op.fn];
138
- if (!range) return null;
139
- const n = op.args.length;
140
- if (n < range[0] || n > range[1] || !op.args.every(Number.isFinite)) return null;
141
- if (op.fn === 'rotate' && n === 4 && Math.hypot(op.args[1], op.args[2], op.args[3]) < 1e-12) return null;
142
- if (op.fn === 'scale' && n === 2) return null;
143
- }
144
- return { ops, matrix: opsToMatrix(ops) };
145
- }
146
-
147
- /** Parse a transform attribute value into {ops, matrix}. */
148
- export function parseTransform(str) {
149
- const ops = parseTransformOps(str);
150
- return { ops, matrix: opsToMatrix(ops) };
151
- }
152
-
153
- const ZERO = [0, 0, 0];
154
- const ONE = [1, 1, 1];
155
-
156
- /**
157
- * Local matrix of an element from its typed values:
158
- * transform * T(position) * R(rotation) * S(scale)
159
- */
160
- export function localMatrix(values, out = mat4.createF64(), overrides = null) {
161
- const position = (overrides && overrides.position) || values.position || ZERO;
162
- const rotation = (overrides && overrides.rotation) || values.rotation || ZERO;
163
- const scale = (overrides && overrides.scale) || values.scale || ONE;
164
- mat4.compose(out, position, rotation, scale);
165
- const tr = values.transform;
166
- if (tr && tr.ops.length) mat4.multiply(out, tr.matrix, out);
167
- return out;
168
- }
169
-
170
- /** Format a matrix as a transform string (used by serializers / expansion). */
171
- export function matrixToTransformString(m, fmt) {
172
- const d = mat4.decompose(m);
173
- if (d) {
174
- const parts = [];
175
- const [tx, ty, tz] = d.translation;
176
- if (Math.abs(tx) > 1e-9 || Math.abs(ty) > 1e-9 || Math.abs(tz) > 1e-9) parts.push(`translate(${fmt(tx)},${fmt(ty)},${fmt(tz)})`);
177
- const [qx, qy, qz, qw] = d.rotation;
178
- if (Math.abs(qx) > 1e-9 || Math.abs(qy) > 1e-9 || Math.abs(qz) > 1e-9) parts.push(`quat(${fmt(qx)},${fmt(qy)},${fmt(qz)},${fmt(qw)})`);
179
- const [sx, sy, sz] = d.scale;
180
- if (Math.abs(sx - 1) > 1e-9 || Math.abs(sy - 1) > 1e-9 || Math.abs(sz - 1) > 1e-9) {
181
- if (Math.abs(sx - sy) < 1e-9 && Math.abs(sy - sz) < 1e-9) parts.push(`scale(${fmt(sx)})`);
182
- else parts.push(`scale(${fmt(sx)},${fmt(sy)},${fmt(sz)})`);
183
- }
184
- return parts.join(' ');
185
- }
186
- return `matrix(${Array.from(m, fmt).join(',')})`;
187
- }
188
-
189
- /**
190
- * Decompose a shear-free affine matrix into translation, ES3D Euler angles
191
- * (degrees, R = Rz * Ry * Rx) and scale. Returns null when shear is present.
192
- */
193
- export function matrixToEuler(m) {
194
- const d = mat4.decompose(m);
195
- if (!d) return null;
196
- const [sx, sy, sz] = d.scale;
197
- const r = [m[0] / sx, m[1] / sx, m[2] / sx, m[4] / sy, m[5] / sy, m[6] / sy, m[8] / sz, m[9] / sz, m[10] / sz];
198
- const R2 = Math.max(-1, Math.min(1, -r[2]));
199
- const y = Math.asin(R2);
200
- let x, z;
201
- if (Math.abs(R2) < 0.9999999) {
202
- x = Math.atan2(r[5], r[8]);
203
- z = Math.atan2(r[1], r[0]);
204
- } else {
205
- // gimbal lock: rotation about X and Z are coupled; put everything into Z
206
- x = 0;
207
- z = Math.atan2(-r[3], r[4]);
208
- }
209
- const deg = (v) => (v * 180) / Math.PI;
210
- return { translation: d.translation, rotation: [deg(x), deg(y), deg(z)], scale: d.scale };
211
- }
1
+ // The ES3D transform system.
2
+ //
3
+ // transform="translate(0,2,0) rotate(0,45,0) scale(2)" is read exactly like
4
+ // SVG: functions are applied left to right as nested coordinate systems, so the
5
+ // resulting matrix is T1 * T2 * T3 (the rightmost function acts on the object
6
+ // first). Supported functions:
7
+ //
8
+ // translate(x[,y[,z]]) rotate(x,y,z) Euler degrees (X, then Y, then Z)
9
+ // rotateX(a) rotateY(a) rotateZ(a) rotate(a, ax,ay,az) axis-angle
10
+ // scale(s) | scale(x,y,z) quat(x,y,z,w)
11
+ // matrix(m0..m15) 16 numbers, column-major (like CSS matrix3d)
12
+ //
13
+ // Elements additionally accept position / rotation / scale attributes. The
14
+ // local matrix of an element is transform * T(position) * R(rotation) * S(scale)
15
+ // mirroring SVG <use>, where x/y are applied after (inside) the transform.
16
+
17
+ import * as mat4 from '../math/mat4.js';
18
+ import { ValueError } from '../format/value-error.js';
19
+ import { suggest } from '../format/errors.js';
20
+ import { readNumber, numberValue } from '../path/path-parser.js';
21
+
22
+ const FUNCS = {
23
+ translate: [1, 3],
24
+ rotate: [3, 4],
25
+ rotateX: [1, 1],
26
+ rotateY: [1, 1],
27
+ rotateZ: [1, 1],
28
+ scale: [1, 3],
29
+ matrix: [16, 16],
30
+ quat: [4, 4],
31
+ };
32
+
33
+ function isWs(c) {
34
+ return c === 32 || c === 9 || c === 10 || c === 13 || c === 44; // comma acts as separator between functions
35
+ }
36
+
37
+ /** Parse a transform list into operations (validated, with offsets). */
38
+ export function parseTransformOps(str) {
39
+ const ops = [];
40
+ const n = str.length;
41
+ let i = 0;
42
+ while (true) {
43
+ while (i < n && isWs(str.charCodeAt(i))) i++;
44
+ if (i >= n) break;
45
+ const nameStart = i;
46
+ while (i < n && /[A-Za-z0-9]/.test(str[i])) i++;
47
+ const fn = str.slice(nameStart, i);
48
+ if (!fn) throw new ValueError(`Unexpected character '${str[i]}' in transform.`, i, 1);
49
+ if (!(fn in FUNCS)) {
50
+ throw new ValueError(`Unknown transform function '${fn}'.`, nameStart, fn.length, {
51
+ suggestion: suggest(fn, Object.keys(FUNCS)),
52
+ hint: 'Valid functions: translate, rotate, rotateX, rotateY, rotateZ, scale, matrix, quat.',
53
+ });
54
+ }
55
+ while (i < n && (str[i] === ' ' || str[i] === '\t')) i++;
56
+ if (str[i] !== '(') throw new ValueError(`Expected '(' after '${fn}'.`, i, 1);
57
+ i++;
58
+ const args = [];
59
+ for (;;) {
60
+ while (i < n && /[\s,]/.test(str[i])) i++;
61
+ if (str[i] === ')') {
62
+ i++;
63
+ break;
64
+ }
65
+ if (i >= n) throw new ValueError(`Missing ')' to close '${fn}('.`, nameStart, fn.length + 1);
66
+ const end = readNumber(str, i);
67
+ if (end < 0) throw new ValueError(`Expected a number in '${fn}()' but found '${str[i]}'.`, i, 1);
68
+ args.push(numberValue);
69
+ i = end;
70
+ }
71
+ const [min, max] = FUNCS[fn];
72
+ if (args.length < min || args.length > max) {
73
+ let hint;
74
+ if (fn === 'rotate' && args.length === 1) hint = 'rotate() takes Euler angles rotate(x,y,z) or an axis-angle rotate(a,ax,ay,az); use rotateY(a) for a single angle.';
75
+ if (fn === 'rotate' && args.length === 2) hint = 'rotate() takes 3 Euler angles in degrees, e.g. rotate(0,45,0).';
76
+ throw new ValueError(
77
+ `${fn}() takes ${min === max ? min : `${min} to ${max}`} numbers but ${args.length} ${args.length === 1 ? 'was' : 'were'} given.`,
78
+ nameStart,
79
+ i - nameStart,
80
+ { hint },
81
+ );
82
+ }
83
+ if (fn === 'rotate' && args.length === 4 && Math.hypot(args[1], args[2], args[3]) < 1e-12) {
84
+ throw new ValueError('rotate(angle, ax, ay, az): the rotation axis must not be zero.', nameStart, i - nameStart);
85
+ }
86
+ if (fn === 'scale' && args.length === 2) {
87
+ throw new ValueError('scale() takes 1 (uniform) or 3 numbers.', nameStart, i - nameStart);
88
+ }
89
+ ops.push({ fn, args, offset: nameStart });
90
+ }
91
+ return ops;
92
+ }
93
+
94
+ /** Matrix of a single transform operation. */
95
+ export function opMatrix(op, out = mat4.createF64()) {
96
+ const a = op.args;
97
+ switch (op.fn) {
98
+ case 'translate':
99
+ return mat4.fromTranslation(out, a[0], a[1] || 0, a[2] || 0);
100
+ case 'rotate':
101
+ if (a.length === 4) return mat4.fromAxisAngle(out, a[1], a[2], a[3], a[0] * mat4.DEG);
102
+ return mat4.fromEulerDeg(out, a[0], a[1], a[2]);
103
+ case 'rotateX':
104
+ return mat4.fromRotationX(out, a[0] * mat4.DEG);
105
+ case 'rotateY':
106
+ return mat4.fromRotationY(out, a[0] * mat4.DEG);
107
+ case 'rotateZ':
108
+ return mat4.fromRotationZ(out, a[0] * mat4.DEG);
109
+ case 'scale':
110
+ return a.length === 1 ? mat4.fromScaling(out, a[0], a[0], a[0]) : mat4.fromScaling(out, a[0], a[1], a[2]);
111
+ case 'matrix':
112
+ for (let k = 0; k < 16; k++) out[k] = a[k];
113
+ return out;
114
+ case 'quat':
115
+ return mat4.fromQuat(out, a[0], a[1], a[2], a[3]);
116
+ default:
117
+ throw new Error(`unknown transform ${op.fn}`);
118
+ }
119
+ }
120
+
121
+ export function opsToMatrix(ops) {
122
+ const m = mat4.createF64();
123
+ const t = mat4.createF64();
124
+ for (const op of ops) {
125
+ opMatrix(op, t);
126
+ mat4.multiply(m, m, t);
127
+ }
128
+ return m;
129
+ }
130
+
131
+ /**
132
+ * {ops, matrix} for already separated operations (binary decoding), applying
133
+ * the same rules as the text parser; null when an operation is invalid.
134
+ */
135
+ export function transformFromOps(ops) {
136
+ for (const op of ops) {
137
+ const range = FUNCS[op.fn];
138
+ if (!range) return null;
139
+ const n = op.args.length;
140
+ if (n < range[0] || n > range[1] || !op.args.every(Number.isFinite)) return null;
141
+ if (op.fn === 'rotate' && n === 4 && Math.hypot(op.args[1], op.args[2], op.args[3]) < 1e-12) return null;
142
+ if (op.fn === 'scale' && n === 2) return null;
143
+ }
144
+ return { ops, matrix: opsToMatrix(ops) };
145
+ }
146
+
147
+ /** Parse a transform attribute value into {ops, matrix}. */
148
+ export function parseTransform(str) {
149
+ const ops = parseTransformOps(str);
150
+ return { ops, matrix: opsToMatrix(ops) };
151
+ }
152
+
153
+ const ZERO = [0, 0, 0];
154
+ const ONE = [1, 1, 1];
155
+
156
+ /**
157
+ * Local matrix of an element from its typed values:
158
+ * transform * T(position) * R(rotation) * S(scale)
159
+ */
160
+ export function localMatrix(values, out = mat4.createF64(), overrides = null) {
161
+ const position = (overrides && overrides.position) || values.position || ZERO;
162
+ const rotation = (overrides && overrides.rotation) || values.rotation || ZERO;
163
+ const scale = (overrides && overrides.scale) || values.scale || ONE;
164
+ mat4.compose(out, position, rotation, scale);
165
+ const tr = values.transform;
166
+ if (tr && tr.ops.length) mat4.multiply(out, tr.matrix, out);
167
+ return out;
168
+ }
169
+
170
+ /** Format a matrix as a transform string (used by serializers / expansion). */
171
+ export function matrixToTransformString(m, fmt) {
172
+ const d = mat4.decompose(m);
173
+ if (d) {
174
+ const parts = [];
175
+ const [tx, ty, tz] = d.translation;
176
+ if (Math.abs(tx) > 1e-9 || Math.abs(ty) > 1e-9 || Math.abs(tz) > 1e-9) parts.push(`translate(${fmt(tx)},${fmt(ty)},${fmt(tz)})`);
177
+ const [qx, qy, qz, qw] = d.rotation;
178
+ if (Math.abs(qx) > 1e-9 || Math.abs(qy) > 1e-9 || Math.abs(qz) > 1e-9) parts.push(`quat(${fmt(qx)},${fmt(qy)},${fmt(qz)},${fmt(qw)})`);
179
+ const [sx, sy, sz] = d.scale;
180
+ if (Math.abs(sx - 1) > 1e-9 || Math.abs(sy - 1) > 1e-9 || Math.abs(sz - 1) > 1e-9) {
181
+ if (Math.abs(sx - sy) < 1e-9 && Math.abs(sy - sz) < 1e-9) parts.push(`scale(${fmt(sx)})`);
182
+ else parts.push(`scale(${fmt(sx)},${fmt(sy)},${fmt(sz)})`);
183
+ }
184
+ return parts.join(' ');
185
+ }
186
+ return `matrix(${Array.from(m, fmt).join(',')})`;
187
+ }
188
+
189
+ /**
190
+ * Decompose a shear-free affine matrix into translation, ES3D Euler angles
191
+ * (degrees, R = Rz * Ry * Rx) and scale. Returns null when shear is present.
192
+ */
193
+ export function matrixToEuler(m) {
194
+ const d = mat4.decompose(m);
195
+ if (!d) return null;
196
+ const [sx, sy, sz] = d.scale;
197
+ const r = [m[0] / sx, m[1] / sx, m[2] / sx, m[4] / sy, m[5] / sy, m[6] / sy, m[8] / sz, m[9] / sz, m[10] / sz];
198
+ const R2 = Math.max(-1, Math.min(1, -r[2]));
199
+ const y = Math.asin(R2);
200
+ let x, z;
201
+ if (Math.abs(R2) < 0.9999999) {
202
+ x = Math.atan2(r[5], r[8]);
203
+ z = Math.atan2(r[1], r[0]);
204
+ } else {
205
+ // gimbal lock: rotation about X and Z are coupled; put everything into Z
206
+ x = 0;
207
+ z = Math.atan2(-r[3], r[4]);
208
+ }
209
+ const deg = (v) => (v * 180) / Math.PI;
210
+ return { translation: d.translation, rotation: [deg(x), deg(y), deg(z)], scale: d.scale };
211
+ }