swf2js 0.1.0 → 0.1.2

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 (50) hide show
  1. package/README.md +9 -9
  2. package/package.json +3 -2
  3. package/src/cli.js +15 -6
  4. package/src/emitter/bundle-emitter.js +56 -16
  5. package/src/runtime/avm1-runtime.js +329 -84
  6. package/src/runtime/avm2-runtime.js +56 -6
  7. package/src/runtime/embedded-movie-runtime.js +6 -1
  8. package/src/runtime/index.js +2 -6
  9. package/src/runtime/movie-runtime.js +2 -1
  10. package/src/runtime/swflib/assemble.js +34 -0
  11. package/src/runtime/swflib/bitmap-image.js +4 -0
  12. package/src/runtime/swflib/bitmap.js +1004 -0
  13. package/src/runtime/swflib/builtins.js +156 -0
  14. package/src/runtime/swflib/bytearray.js +949 -0
  15. package/src/runtime/swflib/compiled-swf.js +3 -0
  16. package/src/runtime/swflib/context-menu.js +245 -0
  17. package/src/runtime/swflib/cursor-flag.js +12 -0
  18. package/src/runtime/swflib/dictionary.js +121 -0
  19. package/src/runtime/swflib/display.js +823 -0
  20. package/src/runtime/swflib/events.js +754 -0
  21. package/src/runtime/swflib/filters.js +298 -0
  22. package/src/runtime/swflib/flash.js +130 -0
  23. package/src/runtime/swflib/geom.js +759 -0
  24. package/src/runtime/swflib/glyph-metrics.js +14 -0
  25. package/src/runtime/swflib/graphics.js +735 -0
  26. package/src/runtime/swflib/host.js +2 -0
  27. package/src/runtime/swflib/inherit.js +22 -0
  28. package/src/runtime/swflib/input.js +1085 -0
  29. package/src/runtime/swflib/loader.js +687 -0
  30. package/src/runtime/swflib/marks.js +16 -0
  31. package/src/runtime/swflib/matrix3d.js +144 -0
  32. package/src/runtime/swflib/pointer-bridge.js +8 -0
  33. package/src/runtime/swflib/projection.js +158 -0
  34. package/src/runtime/swflib/render.js +1240 -0
  35. package/src/runtime/swflib/sound.js +1166 -0
  36. package/src/runtime/swflib/sprite-drag.js +116 -0
  37. package/src/runtime/swflib/stage-ref.js +9 -0
  38. package/src/runtime/swflib/stage.js +484 -0
  39. package/src/runtime/swflib/system.js +987 -0
  40. package/src/runtime/swflib/text-field.js +519 -0
  41. package/src/runtime/swflib/text-paint.js +797 -0
  42. package/src/runtime/swflib/text.js +1387 -0
  43. package/src/runtime/swflib/transform.js +133 -0
  44. package/src/runtime/swflib/xml.js +268 -0
  45. package/src/transpiler/avm1/avm1-ir.js +2 -1
  46. package/src/transpiler/avm1/avm1-rewrite.js +23 -2
  47. package/src/transpiler/avm1/avm1-transpiler.js +4 -1
  48. package/src/transpiler/avm2/avm2-transpiler.js +38 -7
  49. package/src/transpiler/timeline/timeline-codegen.js +366 -123
  50. package/src/runtime/swflib.js +0 -14050
@@ -0,0 +1,759 @@
1
+ export class ColorTransform {
2
+ constructor(rm, gm, bm, am, ro, go, bo, ao) {
3
+ this.redMultiplier = rm == null ? 1 : rm;
4
+ this.greenMultiplier = gm == null ? 1 : gm;
5
+ this.blueMultiplier = bm == null ? 1 : bm;
6
+ this.alphaMultiplier = am == null ? 1 : am;
7
+ this.redOffset = ro == null ? 0 : ro;
8
+ this.greenOffset = go == null ? 0 : go;
9
+ this.blueOffset = bo == null ? 0 : bo;
10
+ this.alphaOffset = ao == null ? 0 : ao;
11
+ }
12
+
13
+ get color() {
14
+ return ((this.redOffset << 16) | (this.greenOffset << 8) | this.blueOffset) >>> 0;
15
+ }
16
+
17
+ set color(value) {
18
+ var rgb = value >>> 0;
19
+ this.redOffset = rgb >>> 16 & 255;
20
+ this.greenOffset = rgb >>> 8 & 255;
21
+ this.blueOffset = rgb & 255;
22
+ this.redMultiplier = this.greenMultiplier = this.blueMultiplier = 0;
23
+ }
24
+
25
+ concat(second) {
26
+ if (!second) return this;
27
+ this.redMultiplier *= second.redMultiplier;
28
+ this.greenMultiplier *= second.greenMultiplier;
29
+ this.blueMultiplier *= second.blueMultiplier;
30
+ this.alphaMultiplier *= second.alphaMultiplier;
31
+ this.redOffset = this.redOffset * second.redMultiplier + second.redOffset;
32
+ this.greenOffset = this.greenOffset * second.greenMultiplier + second.greenOffset;
33
+ this.blueOffset = this.blueOffset * second.blueMultiplier + second.blueOffset;
34
+ this.alphaOffset = this.alphaOffset * second.alphaMultiplier + second.alphaOffset;
35
+ return this;
36
+ }
37
+ }
38
+
39
+ export function copyColorTransform(value) {
40
+ return new ColorTransform(value.redMultiplier, value.greenMultiplier, value.blueMultiplier,
41
+ value.alphaMultiplier, value.redOffset, value.greenOffset, value.blueOffset, value.alphaOffset);
42
+ }
43
+
44
+ export class Matrix {
45
+ constructor(a, b, c, d, tx, ty) {
46
+ this._a = a == null ? 1 : +a;
47
+ this._b = b == null ? 0 : +b;
48
+ this._c = c == null ? 0 : +c;
49
+ this._d = d == null ? 1 : +d;
50
+ this._tx = tx == null ? 0 : +tx;
51
+ this._ty = ty == null ? 0 : +ty;
52
+ this._changeListener = null;
53
+ }
54
+
55
+ identity() {
56
+ this.a = 1; this.b = 0; this.c = 0; this.d = 1; this.tx = 0; this.ty = 0;
57
+ return this;
58
+ }
59
+
60
+ clone() {
61
+ return new Matrix(this.a, this.b, this.c, this.d, this.tx, this.ty);
62
+ }
63
+
64
+ copyFrom(m) {
65
+ if (!m) return this;
66
+ this.a = m.a; this.b = m.b; this.c = m.c; this.d = m.d; this.tx = m.tx; this.ty = m.ty;
67
+ return this;
68
+ }
69
+
70
+ concat(m) {
71
+ if (!m) return this;
72
+ var a = this.a * m.a + this.b * m.c;
73
+ var b = this.a * m.b + this.b * m.d;
74
+ var c = this.c * m.a + this.d * m.c;
75
+ var d = this.c * m.b + this.d * m.d;
76
+ var tx = this.tx * m.a + this.ty * m.c + m.tx;
77
+ var ty = this.tx * m.b + this.ty * m.d + m.ty;
78
+ this.a = a; this.b = b; this.c = c; this.d = d; this.tx = tx; this.ty = ty;
79
+ return this;
80
+ }
81
+
82
+ translate(x, y) {
83
+ this.tx += +x || 0;
84
+ this.ty += +y || 0;
85
+ return this;
86
+ }
87
+
88
+ scale(sx, sy) {
89
+ sx = sx == null ? 1 : +sx;
90
+ sy = sy == null ? sx : +sy;
91
+ this.a *= sx; this.b *= sy; this.c *= sx; this.d *= sy;
92
+ this.tx *= sx; this.ty *= sy;
93
+ return this;
94
+ }
95
+
96
+ rotate(angle) {
97
+ angle = +angle || 0;
98
+ var cos = Math.cos(angle);
99
+ var sin = Math.sin(angle);
100
+ var a = this.a * cos - this.b * sin;
101
+ var b = this.a * sin + this.b * cos;
102
+ var c = this.c * cos - this.d * sin;
103
+ var d = this.c * sin + this.d * cos;
104
+ var tx = this.tx * cos - this.ty * sin;
105
+ var ty = this.tx * sin + this.ty * cos;
106
+ this.a = a; this.b = b; this.c = c; this.d = d; this.tx = tx; this.ty = ty;
107
+ return this;
108
+ }
109
+
110
+ invert() {
111
+ var a = this.a, b = this.b, c = this.c, d = this.d, tx = this.tx, ty = this.ty;
112
+ var det = a * d - b * c;
113
+ if (!det) {
114
+ this.identity();
115
+ return this;
116
+ }
117
+ det = 1 / det;
118
+ this.a = d * det;
119
+ this.b = -b * det;
120
+ this.c = -c * det;
121
+ this.d = a * det;
122
+ this.tx = (c * ty - d * tx) * det;
123
+ this.ty = (b * tx - a * ty) * det;
124
+ return this;
125
+ }
126
+
127
+ transformPoint(p) {
128
+ p = p || new Point();
129
+ return new Point(this.a * p.x + this.c * p.y + this.tx, this.b * p.x + this.d * p.y + this.ty);
130
+ }
131
+
132
+ deltaTransformPoint(p) {
133
+ p = p || new Point();
134
+ return new Point(this.a * p.x + this.c * p.y, this.b * p.x + this.d * p.y);
135
+ }
136
+
137
+ createBox(scaleX, scaleY, rotation, tx, ty) {
138
+ this.identity();
139
+ if (rotation) this.rotate(rotation);
140
+ this.scale(scaleX == null ? 1 : scaleX, scaleY == null ? 1 : scaleY);
141
+ this.translate(tx || 0, ty || 0);
142
+ return this;
143
+ }
144
+
145
+ createGradientBox(width, height, rotation, tx, ty) {
146
+ this.createBox((width || 0) / 1638.4, (height || 0) / 1638.4, rotation || 0,
147
+ (tx || 0) + (width || 0) / 2, (ty || 0) + (height || 0) / 2);
148
+ return this;
149
+ }
150
+ }
151
+
152
+ export function matrixField(name) {
153
+ return {
154
+ enumerable: true, configurable: true,
155
+ get: function () { return this['_' + name]; },
156
+ set: function (value) {
157
+ this['_' + name] = +value || 0;
158
+ if (this._changeListener) this._changeListener(name);
159
+ },
160
+ };
161
+ }
162
+ // Keep these enumerable. Class accessors are not, and for-in walks them.
163
+ Object.defineProperties(Matrix.prototype, {
164
+ a: matrixField('a'), b: matrixField('b'), c: matrixField('c'),
165
+ d: matrixField('d'), tx: matrixField('tx'), ty: matrixField('ty'),
166
+ });
167
+
168
+ export class Point {
169
+ constructor(x, y) {
170
+ this.x = +x || 0;
171
+ this.y = +y || 0;
172
+ }
173
+
174
+ get length() {
175
+ return Math.sqrt(this.x * this.x + this.y * this.y);
176
+ }
177
+
178
+ clone() { return new Point(this.x, this.y); }
179
+ setTo(x, y) { this.x = +x || 0; this.y = +y || 0; return this; }
180
+ offset(dx, dy) { this.x += +dx || 0; this.y += +dy || 0; return this; }
181
+ equals(p) { return !!p && this.x === p.x && this.y === p.y; }
182
+ add(p) { return new Point(this.x + ((p && p.x) || 0), this.y + ((p && p.y) || 0)); }
183
+ subtract(p) { return new Point(this.x - ((p && p.x) || 0), this.y - ((p && p.y) || 0)); }
184
+
185
+ normalize(thickness) {
186
+ var len = this.length;
187
+ if (len) {
188
+ thickness = thickness == null ? 1 : +thickness;
189
+ this.x = (this.x / len) * thickness;
190
+ this.y = (this.y / len) * thickness;
191
+ }
192
+ return this;
193
+ }
194
+
195
+ static distance(a, b) {
196
+ var dx = ((a && a.x) || 0) - ((b && b.x) || 0);
197
+ var dy = ((a && a.y) || 0) - ((b && b.y) || 0);
198
+ return Math.sqrt(dx * dx + dy * dy);
199
+ }
200
+
201
+ static interpolate(a, b, f) {
202
+ f = +f || 0;
203
+ return new Point(((a && a.x) || 0) + ((((b && b.x) || 0) - ((a && a.x) || 0)) * f),
204
+ ((a && a.y) || 0) + ((((b && b.y) || 0) - ((a && a.y) || 0)) * f));
205
+ }
206
+
207
+ static polar(len, angle) {
208
+ return new Point((+len || 0) * Math.cos(+angle || 0), (+len || 0) * Math.sin(+angle || 0));
209
+ }
210
+ }
211
+
212
+ export class Rectangle {
213
+ constructor(x, y, w, h) {
214
+ this.x = +x || 0; this.y = +y || 0; this.width = +w || 0; this.height = +h || 0;
215
+ }
216
+
217
+ get left() { return this.x; }
218
+ set left(v) { this.width += this.x - v; this.x = v; }
219
+ get top() { return this.y; }
220
+ set top(v) { this.height += this.y - v; this.y = v; }
221
+ get right() { return this.x + this.width; }
222
+ set right(v) { this.width = v - this.x; }
223
+ get bottom() { return this.y + this.height; }
224
+ set bottom(v) { this.height = v - this.y; }
225
+ get topLeft() { return new Point(this.x, this.y); }
226
+ set topLeft(p) { if (p) { this.x = p.x; this.y = p.y; } }
227
+ get bottomRight() { return new Point(this.right, this.bottom); }
228
+ set bottomRight(p) { if (p) { this.right = p.x; this.bottom = p.y; } }
229
+ get size() { return new Point(this.width, this.height); }
230
+ set size(p) { if (p) { this.width = p.x; this.height = p.y; } }
231
+
232
+ clone() {
233
+ return new Rectangle(this.x, this.y, this.width, this.height);
234
+ }
235
+
236
+ setTo(x, y, w, h) {
237
+ this.x = +x || 0; this.y = +y || 0; this.width = +w || 0; this.height = +h || 0;
238
+ return this;
239
+ }
240
+
241
+ copyFrom(r) {
242
+ if (!r) return this;
243
+ return this.setTo(r.x, r.y, r.width, r.height);
244
+ }
245
+
246
+ contains(x, y) {
247
+ return x >= this.x && y >= this.y && x < this.x + this.width && y < this.y + this.height;
248
+ }
249
+
250
+ containsPoint(p) {
251
+ return !!p && this.contains(p.x, p.y);
252
+ }
253
+
254
+ containsRect(r) {
255
+ return !!r && r.x >= this.x && r.y >= this.y &&
256
+ r.x + r.width <= this.x + this.width && r.y + r.height <= this.y + this.height;
257
+ }
258
+
259
+ isEmpty() {
260
+ return this.width <= 0 || this.height <= 0;
261
+ }
262
+
263
+ offset(dx, dy) {
264
+ this.x += +dx || 0; this.y += +dy || 0;
265
+ return this;
266
+ }
267
+
268
+ offsetPoint(p) {
269
+ if (p) this.offset(p.x, p.y);
270
+ return this;
271
+ }
272
+
273
+ inflate(dx, dy) {
274
+ this.x -= +dx || 0; this.width += 2 * (+dx || 0);
275
+ this.y -= +dy || 0; this.height += 2 * (+dy || 0);
276
+ return this;
277
+ }
278
+
279
+ union(r) {
280
+ if (!r || r.isEmpty()) return this.clone();
281
+ if (this.isEmpty()) return r.clone();
282
+ var x = Math.min(this.x, r.x);
283
+ var y = Math.min(this.y, r.y);
284
+ return new Rectangle(x, y,
285
+ Math.max(this.right, r.x + r.width) - x,
286
+ Math.max(this.bottom, r.y + r.height) - y);
287
+ }
288
+
289
+ intersection(r) {
290
+ if (!r) return new Rectangle();
291
+ var x = Math.max(this.x, r.x);
292
+ var y = Math.max(this.y, r.y);
293
+ var w = Math.min(this.right, r.x + r.width) - x;
294
+ var h = Math.min(this.bottom, r.y + r.height) - y;
295
+ if (w <= 0 || h <= 0) return new Rectangle();
296
+ return new Rectangle(x, y, w, h);
297
+ }
298
+
299
+ intersects(r) {
300
+ return !this.intersection(r).isEmpty();
301
+ }
302
+ }
303
+
304
+ /**
305
+ * AS3 `Vector.<Object>` — Array subclass with fixed-length + type tags.
306
+ * Callable without `new` (Flash cast / construction style).
307
+ */
308
+ class VectorBase extends Array {
309
+ constructor(length = 0, fixed = false) {
310
+ const count = length == null ? 0 : Number(length) | 0;
311
+ if (count < 0) throw new RangeError('Vector length must not be negative');
312
+ super(count);
313
+ this.fixed = !!fixed;
314
+ Object.defineProperty(this, '__swoofVectorType', {
315
+ value: 'Object', writable: true, configurable: true, enumerable: false,
316
+ });
317
+ Object.defineProperty(this, '__swoofVectorTypeName', {
318
+ value: 'Object', writable: true, configurable: true, enumerable: false,
319
+ });
320
+ }
321
+ }
322
+ Object.defineProperty(VectorBase, '__swoofVector', { value: true });
323
+
324
+ export const Vector = new Proxy(VectorBase, {
325
+ apply(_target, _thisArg, args) {
326
+ return Reflect.construct(VectorBase, args);
327
+ },
328
+ construct(_target, args) {
329
+ return Reflect.construct(VectorBase, args);
330
+ },
331
+ get(target, prop, receiver) {
332
+ if (prop === 'prototype') return VectorBase.prototype;
333
+ return Reflect.get(target, prop, receiver);
334
+ },
335
+ });
336
+
337
+ export class Vector3D {
338
+ constructor(x, y, z, w) {
339
+ this.x = +x || 0; this.y = +y || 0; this.z = +z || 0; this.w = w == null ? 0 : +w;
340
+ }
341
+
342
+ get length() {
343
+ return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
344
+ }
345
+
346
+ get lengthSquared() {
347
+ return this.x * this.x + this.y * this.y + this.z * this.z;
348
+ }
349
+
350
+ clone() { return new Vector3D(this.x, this.y, this.z, this.w); }
351
+
352
+ setTo(x, y, z) {
353
+ this.x = +x || 0; this.y = +y || 0; this.z = +z || 0;
354
+ return this;
355
+ }
356
+
357
+ copyFrom(source) {
358
+ if (source) { this.x = source.x; this.y = source.y; this.z = source.z; this.w = source.w; }
359
+ }
360
+
361
+ add(v) {
362
+ return new Vector3D(this.x + v.x, this.y + v.y, this.z + v.z);
363
+ }
364
+
365
+ subtract(v) {
366
+ return new Vector3D(this.x - v.x, this.y - v.y, this.z - v.z);
367
+ }
368
+
369
+ normalize() {
370
+ var len = this.length;
371
+ if (len) { this.x /= len; this.y /= len; this.z /= len; }
372
+ return len;
373
+ }
374
+
375
+ scaleBy(s) {
376
+ s = +s || 0;
377
+ this.x *= s; this.y *= s; this.z *= s;
378
+ return this;
379
+ }
380
+
381
+ incrementBy(v) {
382
+ if (v) { this.x += v.x; this.y += v.y; this.z += v.z; }
383
+ return this;
384
+ }
385
+
386
+ decrementBy(v) {
387
+ if (v) { this.x -= v.x; this.y -= v.y; this.z -= v.z; }
388
+ return this;
389
+ }
390
+
391
+ dotProduct(v) {
392
+ return v ? this.x * v.x + this.y * v.y + this.z * v.z : 0;
393
+ }
394
+
395
+ crossProduct(v) {
396
+ v = v || new Vector3D();
397
+ return new Vector3D(
398
+ this.y * v.z - this.z * v.y,
399
+ this.z * v.x - this.x * v.z,
400
+ this.x * v.y - this.y * v.x
401
+ );
402
+ }
403
+
404
+ negate() {
405
+ this.x = -this.x; this.y = -this.y; this.z = -this.z;
406
+ }
407
+
408
+ project() {
409
+ this.x /= this.w; this.y /= this.w; this.z /= this.w;
410
+ }
411
+
412
+ equals(v, allFour) {
413
+ return !!v && this.x === v.x && this.y === v.y && this.z === v.z && (!allFour || this.w === v.w);
414
+ }
415
+
416
+ nearEquals(v, tolerance, allFour) {
417
+ if (!v) return false;
418
+ tolerance = Math.abs(+tolerance || 0);
419
+ return Math.abs(this.x - v.x) < tolerance && Math.abs(this.y - v.y) < tolerance &&
420
+ Math.abs(this.z - v.z) < tolerance && (!allFour || Math.abs(this.w - v.w) < tolerance);
421
+ }
422
+
423
+ toString() {
424
+ return 'Vector3D(' + this.x + ', ' + this.y + ', ' + this.z + ', ' + this.w + ')';
425
+ }
426
+
427
+ static angleBetween(a, b) {
428
+ if (!a || !b) return NaN;
429
+ var denominator = a.length * b.length;
430
+ if (!denominator) return NaN;
431
+ return Math.acos(Math.max(-1, Math.min(1, a.dotProduct(b) / denominator)));
432
+ }
433
+
434
+ static distance(a, b) {
435
+ var dx = ((a && a.x) || 0) - ((b && b.x) || 0);
436
+ var dy = ((a && a.y) || 0) - ((b && b.y) || 0);
437
+ var dz = ((a && a.z) || 0) - ((b && b.z) || 0);
438
+ return Math.sqrt(dx * dx + dy * dy + dz * dz);
439
+ }
440
+ }
441
+ Vector3D.X_AXIS = new Vector3D(1, 0, 0);
442
+ Vector3D.Y_AXIS = new Vector3D(0, 1, 0);
443
+ Vector3D.Z_AXIS = new Vector3D(0, 0, 1);
444
+
445
+ export function _mat4Mul(a, b) {
446
+ var o = new Array(16);
447
+ for (var c = 0; c < 4; c++) {
448
+ for (var r = 0; r < 4; r++) {
449
+ o[c * 4 + r] =
450
+ a[0 * 4 + r] * b[c * 4 + 0] +
451
+ a[1 * 4 + r] * b[c * 4 + 1] +
452
+ a[2 * 4 + r] * b[c * 4 + 2] +
453
+ a[3 * 4 + r] * b[c * 4 + 3];
454
+ }
455
+ }
456
+ return o;
457
+ }
458
+
459
+ export function matrix3DRotation(degrees, axis, pivot) {
460
+ var x = +(axis && axis.x) || 0, y = +(axis && axis.y) || 0, z = +(axis && axis.z) || 0;
461
+ var length = Math.sqrt(x * x + y * y + z * z);
462
+ if (!length) return new Matrix3D();
463
+ x /= length; y /= length; z /= length;
464
+ var angle = (+degrees || 0) * Math.PI / 180, c = Math.cos(angle), s = Math.sin(angle), t = 1 - c;
465
+ var r = new Matrix3D([
466
+ t*x*x+c, t*x*y+s*z, t*x*z-s*y, 0,
467
+ t*x*y-s*z, t*y*y+c, t*y*z+s*x, 0,
468
+ t*x*z+s*y, t*y*z-s*x, t*z*z+c, 0,
469
+ 0, 0, 0, 1,
470
+ ]);
471
+ if (pivot) {
472
+ var px = +pivot.x || 0, py = +pivot.y || 0, pz = +pivot.z || 0;
473
+ r.prependTranslation(-px, -py, -pz);
474
+ r.appendTranslation(px, py, pz);
475
+ }
476
+ return r;
477
+ }
478
+
479
+ export function matrix3DDeterminant(rawData) {
480
+ if (!rawData || rawData.length !== 16) return NaN;
481
+ var rows = new Array(4);
482
+ for (var r = 0; r < 4; r++) rows[r] = [rawData[r], rawData[4 + r], rawData[8 + r], rawData[12 + r]];
483
+ var sign = 1, det = 1;
484
+ for (var c = 0; c < 4; c++) {
485
+ var pivot = c;
486
+ for (var rr = c + 1; rr < 4; rr++) if (Math.abs(rows[rr][c]) > Math.abs(rows[pivot][c])) pivot = rr;
487
+ var pv = rows[pivot][c];
488
+ if (!Number.isFinite(pv)) return NaN;
489
+ if (pv === 0) return 0;
490
+ if (pivot !== c) { var swap = rows[c]; rows[c] = rows[pivot]; rows[pivot] = swap; sign = -sign; }
491
+ pv = rows[c][c];
492
+ det *= pv;
493
+ for (var rr2 = c + 1; rr2 < 4; rr2++) {
494
+ var factor = rows[rr2][c] / pv;
495
+ for (var cc = c + 1; cc < 4; cc++) rows[rr2][cc] -= factor * rows[c][cc];
496
+ }
497
+ }
498
+ return sign * det;
499
+ }
500
+
501
+ export function matrix3DQuaternion(r00, r10, r20, r01, r11, r21, r02, r12, r22) {
502
+ // Extract a normalized quaternion using the largest stable diagonal branch.
503
+ var qw, qx, qy, qz, trace = r00+r11+r22, s;
504
+ if (trace > 0) { s=Math.sqrt(trace+1)*2; qw=0.25*s; qx=(r21-r12)/s; qy=(r02-r20)/s; qz=(r10-r01)/s; }
505
+ else if (r00 > r11 && r00 > r22) { s=Math.sqrt(1+r00-r11-r22)*2; qw=(r21-r12)/s; qx=0.25*s; qy=(r01+r10)/s; qz=(r02+r20)/s; }
506
+ else if (r11 > r22) { s=Math.sqrt(1+r11-r00-r22)*2; qw=(r02-r20)/s; qx=(r01+r10)/s; qy=0.25*s; qz=(r12+r21)/s; }
507
+ else { s=Math.sqrt(1+r22-r00-r11)*2; qw=(r10-r01)/s; qx=(r02+r20)/s; qy=(r12+r21)/s; qz=0.25*s; }
508
+ var length = Math.hypot(qx, qy, qz, qw);
509
+ return length ? new Vector3D(qx / length, qy / length, qz / length, qw / length) : new Vector3D(0, 0, 0, 1);
510
+ }
511
+
512
+ export function matrix3DSlerp(a, b, percent) {
513
+ var dot = a.x*b.x + a.y*b.y + a.z*b.z + a.w*b.w;
514
+ var direction = dot < 0 ? -1 : 1;
515
+ dot = Math.min(1, Math.abs(dot));
516
+ var fromWeight = 1 - percent, toWeight = percent;
517
+ if (dot <= 0.9995) {
518
+ var angle = Math.acos(dot), arc = Math.sin(angle);
519
+ fromWeight = Math.sin(fromWeight * angle) / arc;
520
+ toWeight = Math.sin(toWeight * angle) / arc;
521
+ }
522
+ toWeight *= direction;
523
+ return new Vector3D(
524
+ a.x*fromWeight + b.x*toWeight, a.y*fromWeight + b.y*toWeight,
525
+ a.z*fromWeight + b.z*toWeight, a.w*fromWeight + b.w*toWeight);
526
+ }
527
+
528
+ export function matrix3DInterpolateInto(output, from, to, percent) {
529
+ percent = +percent;
530
+ var a = from.rawData, b = to.rawData;
531
+ if (!Number.isFinite(percent) || !a.every(Number.isFinite) || !b.every(Number.isFinite) ||
532
+ !from.decompose() || !to.decompose()) return false;
533
+ // Flash extracts orientations from the scaled basis and recomposes at unit scale.
534
+ var fromRotation = matrix3DQuaternion(a[0], a[1], a[2], a[4], a[5], a[6], a[8], a[9], a[10]);
535
+ var toRotation = matrix3DQuaternion(b[0], b[1], b[2], b[4], b[5], b[6], b[8], b[9], b[10]);
536
+ function lerp(x, y) { return x + (y - x) * percent; }
537
+ return output.recompose([
538
+ new Vector3D(lerp(a[12], b[12]), lerp(a[13], b[13]), lerp(a[14], b[14])),
539
+ matrix3DSlerp(fromRotation, toRotation, percent),
540
+ new Vector3D(1, 1, 1),
541
+ ], 'quaternion');
542
+ }
543
+
544
+ export class Matrix3D {
545
+ constructor(v) {
546
+ this.rawData = v && v.length === 16 ? v.slice() : [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
547
+ }
548
+
549
+ clone() { return new Matrix3D(this.rawData.slice()); }
550
+
551
+ identity() {
552
+ this.rawData = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
553
+ return this;
554
+ }
555
+
556
+ append(m) {
557
+ if (m && m.rawData) this.rawData = _mat4Mul(m.rawData, this.rawData);
558
+ return this;
559
+ }
560
+
561
+ prepend(m) {
562
+ if (m && m.rawData) this.rawData = _mat4Mul(this.rawData, m.rawData);
563
+ return this;
564
+ }
565
+
566
+ appendTranslation(x, y, z) {
567
+ return this.append(new Matrix3D([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, +x || 0, +y || 0, +z || 0, 1]));
568
+ }
569
+
570
+ prependTranslation(x, y, z) {
571
+ return this.prepend(new Matrix3D([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, +x || 0, +y || 0, +z || 0, 1]));
572
+ }
573
+
574
+ appendScale(x, y, z) {
575
+ return this.append(new Matrix3D([+x || 0, 0, 0, 0, 0, +y || 0, 0, 0, 0, 0, +z || 0, 0, 0, 0, 0, 1]));
576
+ }
577
+
578
+ prependScale(x, y, z) {
579
+ return this.prepend(new Matrix3D([+x || 0, 0, 0, 0, 0, +y || 0, 0, 0, 0, 0, +z || 0, 0, 0, 0, 0, 1]));
580
+ }
581
+
582
+ appendRotation(degrees, axis, pivotPoint) {
583
+ return this.append(matrix3DRotation(degrees, axis, pivotPoint));
584
+ }
585
+
586
+ prependRotation(degrees, axis, pivotPoint) {
587
+ return this.prepend(matrix3DRotation(degrees, axis, pivotPoint));
588
+ }
589
+
590
+ copyFrom(m) {
591
+ if (m && m.rawData) this.rawData = m.rawData.slice();
592
+ return this;
593
+ }
594
+
595
+ copyRawDataFrom(v) {
596
+ if (v && v.length) {
597
+ for (var i = 0; i < 16; i++) this.rawData[i] = +v[i] || 0;
598
+ }
599
+ return this;
600
+ }
601
+
602
+ copyRawDataTo(v) {
603
+ if (!v) return;
604
+ for (var i = 0; i < 16; i++) v[i] = this.rawData[i];
605
+ }
606
+
607
+ transpose() {
608
+ var d = this.rawData, transposed = new Array(16);
609
+ for (var row = 0; row < 4; row++) for (var col = 0; col < 4; col++) transposed[col * 4 + row] = d[row * 4 + col];
610
+ this.rawData = transposed;
611
+ }
612
+
613
+ invert() {
614
+ var raw = this.rawData;
615
+ if (!raw || raw.length !== 16) return false;
616
+ var rows = new Array(4);
617
+ for (var r = 0; r < 4; r++) {
618
+ rows[r] = [raw[r], raw[4 + r], raw[8 + r], raw[12 + r], r === 0 ? 1 : 0, r === 1 ? 1 : 0, r === 2 ? 1 : 0, r === 3 ? 1 : 0];
619
+ }
620
+ for (var c = 0; c < 4; c++) {
621
+ var pivot = c;
622
+ for (var rr = c + 1; rr < 4; rr++) if (Math.abs(rows[rr][c]) > Math.abs(rows[pivot][c])) pivot = rr;
623
+ var pv = rows[pivot][c];
624
+ if (!Number.isFinite(pv) || pv === 0) return false;
625
+ if (pivot !== c) { var swap = rows[c]; rows[c] = rows[pivot]; rows[pivot] = swap; }
626
+ pv = rows[c][c];
627
+ for (var j = 0; j < 8; j++) rows[c][j] /= pv;
628
+ for (var other = 0; other < 4; other++) {
629
+ if (other === c) continue;
630
+ var factor = rows[other][c];
631
+ for (var k = 0; k < 8; k++) rows[other][k] -= factor * rows[c][k];
632
+ }
633
+ }
634
+ var inverse = new Array(16);
635
+ for (var row = 0; row < 4; row++) for (var col = 0; col < 4; col++) inverse[col * 4 + row] = rows[row][4 + col];
636
+ if (!inverse.every(Number.isFinite)) return false;
637
+ this.rawData = inverse;
638
+ return true;
639
+ }
640
+
641
+ transformVector(v) {
642
+ var d = this.rawData, x = +(v && v.x) || 0, y = +(v && v.y) || 0;
643
+ var z = +(v && v.z) || 0, w = 1;
644
+ return new Vector3D(d[0]*x+d[4]*y+d[8]*z+d[12]*w,
645
+ d[1]*x+d[5]*y+d[9]*z+d[13]*w,
646
+ d[2]*x+d[6]*y+d[10]*z+d[14]*w,
647
+ d[3]*x+d[7]*y+d[11]*z+d[15]*w);
648
+ }
649
+
650
+ transformVectors(vin, vout) {
651
+ if (!vin || !vout || typeof vin.length !== 'number' || typeof vout.length !== 'number') return;
652
+ var d = this.rawData, length = vin.length - (vin.length % 3);
653
+ for (var i = 0; i < length; i += 3) {
654
+ var x = +vin[i] || 0, y = +vin[i + 1] || 0, z = +vin[i + 2] || 0;
655
+ vout[i] = d[0]*x+d[4]*y+d[8]*z+d[12];
656
+ vout[i + 1] = d[1]*x+d[5]*y+d[9]*z+d[13];
657
+ vout[i + 2] = d[2]*x+d[6]*y+d[10]*z+d[14];
658
+ }
659
+ }
660
+
661
+ decompose(orientationStyle) {
662
+ var d = this.rawData;
663
+ var sx = Math.hypot(d[0], d[1], d[2]), sy = Math.hypot(d[4], d[5], d[6]), sz = Math.hypot(d[8], d[9], d[10]);
664
+ orientationStyle = orientationStyle || 'eulerAngles';
665
+ if (!(sx && sy && sz) || ['eulerAngles', 'axisAngle', 'quaternion'].indexOf(orientationStyle) < 0 ||
666
+ Math.abs(d[3]) > 1e-8 || Math.abs(d[7]) > 1e-8 || Math.abs(d[11]) > 1e-8 || Math.abs(d[15] - 1) > 1e-8) return null;
667
+ var det3 = d[0]*(d[5]*d[10]-d[9]*d[6]) - d[4]*(d[1]*d[10]-d[9]*d[2]) + d[8]*(d[1]*d[6]-d[5]*d[2]);
668
+ if (det3 < 0) sx = -sx;
669
+ var r00=d[0]/sx, r10=d[1]/sx, r20=d[2]/sx;
670
+ var r01=d[4]/sy, r11=d[5]/sy, r21=d[6]/sy;
671
+ var r02=d[8]/sz, r12=d[9]/sz, r22=d[10]/sz;
672
+ if (Math.abs(r00*r01+r10*r11+r20*r21) > 1e-4 ||
673
+ Math.abs(r00*r02+r10*r12+r20*r22) > 1e-4 ||
674
+ Math.abs(r01*r02+r11*r12+r21*r22) > 1e-4) return null;
675
+ var rotation;
676
+ if (orientationStyle === 'eulerAngles') {
677
+ var ry = Math.asin(Math.max(-1, Math.min(1, -r20))), rx, rz;
678
+ if (Math.abs(r20) < 0.999999) { rx = Math.atan2(r21, r22); rz = Math.atan2(r10, r00); }
679
+ else { rx = Math.atan2(-r12, r11); rz = 0; }
680
+ rotation = new Vector3D(rx*180/Math.PI, ry*180/Math.PI, rz*180/Math.PI);
681
+ } else {
682
+ rotation = matrix3DQuaternion(r00, r10, r20, r01, r11, r21, r02, r12, r22);
683
+ if (orientationStyle === 'axisAngle') {
684
+ var qx = rotation.x, qy = rotation.y, qz = rotation.z, qw = rotation.w;
685
+ if (qw < 0) { qx=-qx; qy=-qy; qz=-qz; qw=-qw; }
686
+ var angle=2*Math.acos(Math.max(-1,Math.min(1,qw))), axisLen=Math.hypot(qx,qy,qz);
687
+ rotation = axisLen > 1e-10 ? new Vector3D(qx/axisLen,qy/axisLen,qz/axisLen,angle) : new Vector3D(0,0,1,0);
688
+ }
689
+ }
690
+ var parts = new Vector(3, false);
691
+ parts[0] = new Vector3D(d[12], d[13], d[14]);
692
+ parts[1] = rotation;
693
+ parts[2] = new Vector3D(sx, sy, sz);
694
+ return parts;
695
+ }
696
+
697
+ recompose(components, orientationStyle) {
698
+ orientationStyle = orientationStyle || 'eulerAngles';
699
+ if (!components || components.length < 3 || ['eulerAngles', 'axisAngle', 'quaternion'].indexOf(orientationStyle) < 0) return false;
700
+ var translation=components[0], rotation=components[1], scale=components[2];
701
+ if (!translation || !rotation || !scale) return false;
702
+ var cx, sx, cy, sy, cz, sz;
703
+ if (orientationStyle === 'eulerAngles') {
704
+ var x=(+rotation.x||0)*Math.PI/180, y=(+rotation.y||0)*Math.PI/180, z=(+rotation.z||0)*Math.PI/180;
705
+ cx=Math.cos(x); sx=Math.sin(x); cy=Math.cos(y); sy=Math.sin(y); cz=Math.cos(z); sz=Math.sin(z);
706
+ } else {
707
+ var qx, qy, qz, qw;
708
+ if (orientationStyle === 'quaternion') {
709
+ qx=+rotation.x||0; qy=+rotation.y||0; qz=+rotation.z||0; qw=Number.isFinite(+rotation.w)?+rotation.w:1;
710
+ var qlen=Math.hypot(qx,qy,qz,qw);
711
+ if (qlen > 1e-12) { qx/=qlen; qy/=qlen; qz/=qlen; qw/=qlen; }
712
+ else { qx=0; qy=0; qz=0; qw=1; }
713
+ } else {
714
+ var ax=+rotation.x||0, ay=+rotation.y||0, az=+rotation.z||0, angle=Number.isFinite(+rotation.w)?+rotation.w:0;
715
+ var axisLen=Math.hypot(ax,ay,az);
716
+ if (axisLen > 1e-12 && Number.isFinite(angle)) { var sh=Math.sin(angle/2)/axisLen; qx=ax*sh; qy=ay*sh; qz=az*sh; qw=Math.cos(angle/2); }
717
+ else { qx=0; qy=0; qz=0; qw=1; }
718
+ }
719
+ // Quaternion to the same XYZ-Euler matrix basis used below.
720
+ var r00=1-2*(qy*qy+qz*qz), r10=2*(qx*qy+qz*qw), r20=2*(qx*qz-qy*qw);
721
+ var r01=2*(qx*qy-qz*qw), r11=1-2*(qx*qx+qz*qz), r21=2*(qy*qz+qx*qw);
722
+ var r02=2*(qx*qz+qy*qw), r12=2*(qy*qz-qx*qw), r22=1-2*(qx*qx+qy*qy);
723
+ this.rawData=[r00*(+scale.x||0),r10*(+scale.x||0),r20*(+scale.x||0),0,
724
+ r01*(+scale.y||0),r11*(+scale.y||0),r21*(+scale.y||0),0,
725
+ r02*(+scale.z||0),r12*(+scale.z||0),r22*(+scale.z||0),0,
726
+ +translation.x||0,+translation.y||0,+translation.z||0,1];
727
+ return true;
728
+ }
729
+ this.rawData = [
730
+ cz*cy*(+scale.x||0), sz*cy*(+scale.x||0), -sy*(+scale.x||0), 0,
731
+ (cz*sy*sx-sz*cx)*(+scale.y||0), (sz*sy*sx+cz*cx)*(+scale.y||0), cy*sx*(+scale.y||0), 0,
732
+ (cz*sy*cx+sz*sx)*(+scale.z||0), (sz*sy*cx-cz*sx)*(+scale.z||0), cy*cx*(+scale.z||0), 0,
733
+ +translation.x||0, +translation.y||0, +translation.z||0, 1,
734
+ ];
735
+ return true;
736
+ }
737
+
738
+ interpolateTo(toMat, percent) {
739
+ if (toMat == null) throw new TypeError('Error #2007: Parameter toMat must be non-null.');
740
+ matrix3DInterpolateInto(this, this, toMat, percent);
741
+ }
742
+
743
+ static interpolate(thisMat, toMat, percent) {
744
+ if (thisMat == null) throw new TypeError('Error #2007: Parameter fromMat must be non-null.');
745
+ if (toMat == null) throw new TypeError('Error #2007: Parameter toMat must be non-null.');
746
+ var output = thisMat.clone();
747
+ matrix3DInterpolateInto(output, thisMat, toMat, percent);
748
+ return output;
749
+ }
750
+ }
751
+ Object.defineProperty(Matrix3D.prototype, 'determinant', {
752
+ enumerable: true,
753
+ get: function () { return matrix3DDeterminant(this.rawData); },
754
+ });
755
+ Object.defineProperty(Matrix3D.prototype, 'position', {
756
+ enumerable: true, configurable: true,
757
+ get: function () { return new Vector3D(this.rawData[12], this.rawData[13], this.rawData[14]); },
758
+ set: function (v) { if (v) { this.rawData[12]=+v.x||0; this.rawData[13]=+v.y||0; this.rawData[14]=+v.z||0; } },
759
+ });