@woosh/meep-engine 2.92.23 → 2.92.24

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.
@@ -21,6 +21,9 @@ const forward = new Vector3();
21
21
  const up = new Vector3();
22
22
  const right = new Vector3();
23
23
 
24
+ const sin = Math.sin;
25
+ const cos = Math.cos;
26
+
24
27
  class Quaternion {
25
28
  /**
26
29
  *
@@ -56,6 +59,8 @@ class Quaternion {
56
59
  this.onChanged = new Signal();
57
60
  }
58
61
 
62
+ // Making Quaternion comply to array interface
63
+
59
64
  get 0() {
60
65
  return this.x;
61
66
  }
@@ -88,6 +93,19 @@ class Quaternion {
88
93
  this.w = v;
89
94
  }
90
95
 
96
+ /**
97
+ * Making quaternion iterable
98
+ */
99
+ * [Symbol.iterator]() {
100
+
101
+ yield this.x;
102
+ yield this.y;
103
+ yield this.z;
104
+ yield this.w;
105
+
106
+ }
107
+
108
+
91
109
  /**
92
110
  *
93
111
  * @param {number} fx forward vector
@@ -128,58 +146,22 @@ class Quaternion {
128
146
 
129
147
  // construct partial transform matrix
130
148
  const m00 = right.x;
131
- const m01 = right.y;
132
- const m02 = right.z;
133
- const m10 = up.x;
134
- const m11 = up.y;
135
- const m12 = up.z;
136
- const m20 = forward.x;
137
- const m21 = forward.y;
138
- const m22 = forward.z;
139
-
140
-
141
- const num8 = (m00 + m11) + m22;
142
-
143
- let _x, _y, _z, _w;
144
-
145
- if (num8 > 0) {
146
- let num = Math.sqrt(num8 + 1);
147
- _w = num * 0.5;
148
- num = 0.5 / num;
149
- _x = (m12 - m21) * num;
150
- _y = (m20 - m02) * num;
151
- _z = (m01 - m10) * num;
152
- } else if ((m00 >= m11) && (m00 >= m22)) {
153
-
154
- const num7 = Math.sqrt(((1 + m00) - m11) - m22);
155
- const num4 = 0.5 / num7;
156
-
157
- _x = 0.5 * num7;
158
- _y = (m01 + m10) * num4;
159
- _z = (m02 + m20) * num4;
160
- _w = (m12 - m21) * num4;
161
-
162
- } else if (m11 > m22) {
149
+ const m10 = right.y;
150
+ const m20 = right.z;
163
151
 
164
- const num6 = Math.sqrt(((1 + m11) - m00) - m22);
165
- const num3 = 0.5 / num6;
166
-
167
- _x = (m10 + m01) * num3;
168
- _y = 0.5 * num6;
169
- _z = (m21 + m12) * num3;
170
- _w = (m20 - m02) * num3;
171
- } else {
172
-
173
- const num5 = Math.sqrt(((1 + m22) - m00) - m11);
174
- const num2 = 0.5 / num5;
152
+ const m01 = up.x;
153
+ const m11 = up.y;
154
+ const m21 = up.z;
175
155
 
176
- _x = (m20 + m02) * num2;
177
- _y = (m21 + m12) * num2;
178
- _z = 0.5 * num5;
179
- _w = (m01 - m10) * num2;
180
- }
156
+ const m02 = forward.x;
157
+ const m12 = forward.y;
158
+ const m22 = forward.z;
181
159
 
182
- this.set(_x, _y, _z, _w);
160
+ this.__setFromRotationMatrix(
161
+ m00, m01, m02,
162
+ m10, m11, m12,
163
+ m20, m21, m22
164
+ );
183
165
  }
184
166
 
185
167
  /**
@@ -187,13 +169,24 @@ class Quaternion {
187
169
  * @param {Vector3} vForward
188
170
  * @param {Vector3} [vUp=Vector3.up]
189
171
  *
190
- * @source http://answers.unity3d.com/questions/467614/what-is-the-source-code-of-quaternionlookrotation.html
191
172
  */
192
173
  lookRotation(vForward, vUp = Vector3.up) {
193
174
 
194
175
  this._lookRotation(vForward.x, vForward.y, vForward.z, vUp.x, vUp.y, vUp.z);
195
176
  }
196
177
 
178
+ /**
179
+ *
180
+ * @param {Quaternion} other
181
+ * @return {number}
182
+ */
183
+ dot(other) {
184
+ return this.x * other.x
185
+ + this.y * other.y
186
+ + this.z * other.z
187
+ + this.w * other.w
188
+ ;
189
+ }
197
190
 
198
191
  /**
199
192
  *
@@ -213,14 +206,14 @@ class Quaternion {
213
206
  const z = this.z;
214
207
  const w = this.w;
215
208
 
216
- const dot = x * x + y * y + z * z + w * w;
209
+ const length_sqr = x * x + y * y + z * z + w * w;
217
210
 
218
- if (dot === 0) {
211
+ if (length_sqr === 0) {
219
212
  this.set(0, 0, 0, 0);
220
213
  return;
221
214
  }
222
215
 
223
- const invDot = 1.0 / dot;
216
+ const invDot = 1.0 / length_sqr;
224
217
 
225
218
  const _x = -x * invDot;
226
219
  const _y = -y * invDot;
@@ -274,8 +267,8 @@ class Quaternion {
274
267
  _fromAxisAngle(ax, ay, az, angle) {
275
268
  const halfAngle = angle * 0.5;
276
269
 
277
- const sinA2 = Math.sin(halfAngle);
278
- const cosA2 = Math.cos(halfAngle);
270
+ const sinA2 = sin(halfAngle);
271
+ const cosA2 = cos(halfAngle);
279
272
 
280
273
  const qx = ax * sinA2;
281
274
  const qy = ay * sinA2;
@@ -357,7 +350,7 @@ class Quaternion {
357
350
  toAxisAngle(axis) {
358
351
  const rad = Math.acos(this.w) * 2.0;
359
352
 
360
- const s = Math.sin(rad * 0.5);
353
+ const s = sin(rad * 0.5);
361
354
 
362
355
  if (Math.abs(s) > EPSILON) {
363
356
  axis.set(
@@ -400,7 +393,6 @@ class Quaternion {
400
393
  }
401
394
 
402
395
  /**
403
- * @see http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.htm
404
396
  * @param {Quaternion} other
405
397
  */
406
398
  multiply(other) {
@@ -440,6 +432,8 @@ class Quaternion {
440
432
  */
441
433
  _multiplyQuaternions(ax, ay, az, aw, bx, by, bz, bw) {
442
434
 
435
+ // see http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.htm
436
+
443
437
  const x = ax * bw + aw * bx + ay * bz - az * by;
444
438
  const y = ay * bw + aw * by + az * bx - ax * bz;
445
439
  const z = az * bw + aw * bz + ax * by - ay * bx;
@@ -529,7 +523,7 @@ class Quaternion {
529
523
  * @param {number} x
530
524
  * @param {number} y
531
525
  * @param {number} z
532
- * @param {String} order a combination of capital letters X,Y,Z. Examples: XYZ, YXZ
526
+ * @param {String} [order] a combination of capital letters X,Y,Z. Examples: XYZ, YXZ
533
527
  * @returns {Quaternion}
534
528
  */
535
529
  __setFromEuler(x, y, z, order = 'XYZ') {
@@ -560,6 +554,8 @@ class Quaternion {
560
554
 
561
555
  this.fromEulerAnglesXZY(x, y, z);
562
556
 
557
+ } else {
558
+ throw new Error(`Invalid order '${order}', bust be 3 capital letters consisting of X,Y and Z`);
563
559
  }
564
560
 
565
561
  return this;
@@ -684,9 +680,6 @@ class Quaternion {
684
680
  const half_y = y * 0.5;
685
681
  const half_z = z * 0.5;
686
682
 
687
- const sin = Math.sin;
688
- const cos = Math.cos;
689
-
690
683
  const s1 = sin(half_x);
691
684
  const s2 = sin(half_y);
692
685
  const s3 = sin(half_z);
@@ -717,9 +710,6 @@ class Quaternion {
717
710
  const scaled_y = y * 0.5;
718
711
  const scaled_z = z * 0.5;
719
712
 
720
- const sin = Math.sin;
721
- const cos = Math.cos;
722
-
723
713
  const s1 = sin(scaled_x);
724
714
  const s2 = sin(scaled_y);
725
715
  const s3 = sin(scaled_z);
@@ -750,9 +740,6 @@ class Quaternion {
750
740
  const scaled_y = y * 0.5;
751
741
  const scaled_z = z * 0.5;
752
742
 
753
- const sin = Math.sin;
754
- const cos = Math.cos;
755
-
756
743
  const s1 = sin(scaled_x);
757
744
  const s2 = sin(scaled_y);
758
745
  const s3 = sin(scaled_z);
@@ -783,9 +770,6 @@ class Quaternion {
783
770
  const scaled_y = y * 0.5;
784
771
  const scaled_z = z * 0.5;
785
772
 
786
- const sin = Math.sin;
787
- const cos = Math.cos;
788
-
789
773
  const s1 = sin(scaled_x);
790
774
  const s2 = sin(scaled_y);
791
775
  const s3 = sin(scaled_z);
@@ -816,9 +800,6 @@ class Quaternion {
816
800
  const scaled_y = y * 0.5;
817
801
  const scaled_z = z * 0.5;
818
802
 
819
- const sin = Math.sin;
820
- const cos = Math.cos;
821
-
822
803
  const s1 = sin(scaled_x);
823
804
  const s2 = sin(scaled_y);
824
805
  const s3 = sin(scaled_z);
@@ -849,9 +830,6 @@ class Quaternion {
849
830
  const scaled_y = y * 0.5;
850
831
  const scaled_z = z * 0.5;
851
832
 
852
- const sin = Math.sin;
853
- const cos = Math.cos;
854
-
855
833
  const s1 = sin(scaled_x);
856
834
  const s2 = sin(scaled_y);
857
835
  const s3 = sin(scaled_z);
@@ -1036,7 +1014,7 @@ class Quaternion {
1036
1014
  }
1037
1015
 
1038
1016
  /**
1039
- *
1017
+ * @deprecated
1040
1018
  * @param {Matrix4} m
1041
1019
  */
1042
1020
  setFromRotationMatrix(m) {
@@ -1210,9 +1188,9 @@ class Quaternion {
1210
1188
  if ((1.0 - cosom) > EPSILON) {
1211
1189
  // standard case (slerp)
1212
1190
  omega = Math.acos(cosom);
1213
- sinom = Math.sin(omega);
1214
- scale0 = Math.sin((1.0 - t) * omega) / sinom;
1215
- scale1 = Math.sin(t * omega) / sinom;
1191
+ sinom = sin(omega);
1192
+ scale0 = sin((1.0 - t) * omega) / sinom;
1193
+ scale1 = sin(t * omega) / sinom;
1216
1194
  } else {
1217
1195
  // "from" and "to" quaternions are very close
1218
1196
  // ... so we can do a linear interpolation
@@ -1477,15 +1455,6 @@ class Quaternion {
1477
1455
  && epsilonEquals(this.w, w, tolerance);
1478
1456
  }
1479
1457
 
1480
- * [Symbol.iterator]() {
1481
-
1482
- yield this.x;
1483
- yield this.y;
1484
- yield this.z;
1485
- yield this.w;
1486
-
1487
- }
1488
-
1489
1458
  /**
1490
1459
  * Based on http://planning.cs.uiuc.edu/node198.html
1491
1460
  * @param {function():number} [random]
@@ -1502,10 +1471,10 @@ class Quaternion {
1502
1471
  const u3 = 2 * Math.PI * random();
1503
1472
 
1504
1473
  return this.set(
1505
- sqrt1u1 * Math.cos(u2),
1506
- sqrtu1 * Math.sin(u3),
1507
- sqrtu1 * Math.cos(u3),
1508
- sqrt1u1 * Math.sin(u2),
1474
+ sqrt1u1 * cos(u2),
1475
+ sqrtu1 * sin(u3),
1476
+ sqrtu1 * cos(u3),
1477
+ sqrt1u1 * sin(u2),
1509
1478
  );
1510
1479
  }
1511
1480
 
@@ -1554,6 +1523,7 @@ class Quaternion {
1554
1523
  const angle = from.angleTo(to);
1555
1524
 
1556
1525
  if (angle === 0) {
1526
+ // we're already where we need tobe. Also - avoid division by 0
1557
1527
  result.copy(to);
1558
1528
  } else {
1559
1529
  // clamp to 1, to make sure we don't overshoot
@@ -1584,10 +1554,4 @@ const axis = new Vector3();
1584
1554
 
1585
1555
  const tempvec3 = new Vector3();
1586
1556
 
1587
- /**
1588
- * Used in UINT32 packing
1589
- * @type {number}
1590
- */
1591
- const K_CONST = 0.70710678118; // 1/sqrt(2)
1592
-
1593
1557
  export default Quaternion;
@@ -119,9 +119,10 @@ test("extreme tangent interpolation", () => {
119
119
  const curve = new AnimationCurve();
120
120
 
121
121
  curve.add(Keyframe.from(0, 1, 0, -1));
122
- curve.add(Keyframe.from(1, 2, 1, 0));
122
+ curve.add(Keyframe.from(1, 2, -1, 0));
123
123
 
124
- expect(curve.evaluate(0.5)).toBe(0.5);
124
+ expect(curve.evaluate(0.1)).toBeLessThan(1);
125
+ expect(curve.evaluate(0.9)).toBeGreaterThan(2);
125
126
 
126
127
  });
127
128