@galacean/engine-math 2.0.0-alpha.5 → 2.0.0-alpha.51

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/dist/main.js CHANGED
@@ -469,13 +469,16 @@ function _create_class(Constructor, protoProps, staticProps) {
469
469
  * Converts the vector into a unit vector.
470
470
  * @param a - The vector to normalize
471
471
  * @param out - The normalized vector
472
+ * @returns True if the vector has a unit form, otherwise false and `out` is left untouched
472
473
  */ Vector3.normalize = function normalize(a, out) {
473
474
  var _x = a._x, _y = a._y, _z = a._z;
474
475
  var len = Math.sqrt(_x * _x + _y * _y + _z * _z);
475
- if (len > MathUtil.zeroTolerance) {
476
- len = 1 / len;
477
- out.set(_x * len, _y * len, _z * len);
476
+ if (len > MathUtil.zeroTolerance && Number.isFinite(len)) {
477
+ var inv = 1 / len;
478
+ out.set(_x * inv, _y * inv, _z * inv);
479
+ return true;
478
480
  }
481
+ return false;
479
482
  };
480
483
  /**
481
484
  * Scale a vector by the given value.
@@ -841,20 +844,16 @@ BoundingSphere._tempVec30 = new Vector3();
841
844
  * @param matrix - The transform to apply to the bounding box
842
845
  * @param out - The transformed bounding box
843
846
  */ BoundingBox.transform = function transform(source, matrix, out) {
844
- // https://zeux.io/2010/10/17/aabb-from-obb-with-component-wise-abs/
845
- var center = BoundingBox._tempVec30;
846
- var extent = BoundingBox._tempVec31;
847
- source.getCenter(center);
848
- source.getExtent(extent);
849
- Vector3.transformCoordinate(center, matrix, center);
850
- var x = extent.x, y = extent.y, z = extent.z;
847
+ // Arvo's min/max method: for each matrix element, positive values multiply min for new min (max for new max),
848
+ // negative values multiply max for new min (min for new max), then add translation.
849
+ // Zero check avoids 0 * Infinity = NaN
850
+ var _source_min = source.min, minX = _source_min.x, minY = _source_min.y, minZ = _source_min.z;
851
+ var _source_max = source.max, maxX = _source_max.x, maxY = _source_max.y, maxZ = _source_max.z;
851
852
  var e = matrix.elements;
852
853
  // prettier-ignore
853
854
  var e0 = e[0], e1 = e[1], e2 = e[2], e4 = e[4], e5 = e[5], e6 = e[6], e8 = e[8], e9 = e[9], e10 = e[10];
854
- extent.set((e0 === 0 ? 0 : Math.abs(x * e0)) + (e4 === 0 ? 0 : Math.abs(y * e4)) + (e8 === 0 ? 0 : Math.abs(z * e8)), (e1 === 0 ? 0 : Math.abs(x * e1)) + (e5 === 0 ? 0 : Math.abs(y * e5)) + (e9 === 0 ? 0 : Math.abs(z * e9)), (e2 === 0 ? 0 : Math.abs(x * e2)) + (e6 === 0 ? 0 : Math.abs(y * e6)) + (e10 === 0 ? 0 : Math.abs(z * e10)));
855
- // set min、max
856
- Vector3.subtract(center, extent, out.min);
857
- Vector3.add(center, extent, out.max);
855
+ out.min.set((e0 > 0 ? e0 * minX : e0 < 0 ? e0 * maxX : 0) + (e4 > 0 ? e4 * minY : e4 < 0 ? e4 * maxY : 0) + (e8 > 0 ? e8 * minZ : e8 < 0 ? e8 * maxZ : 0) + e[12], (e1 > 0 ? e1 * minX : e1 < 0 ? e1 * maxX : 0) + (e5 > 0 ? e5 * minY : e5 < 0 ? e5 * maxY : 0) + (e9 > 0 ? e9 * minZ : e9 < 0 ? e9 * maxZ : 0) + e[13], (e2 > 0 ? e2 * minX : e2 < 0 ? e2 * maxX : 0) + (e6 > 0 ? e6 * minY : e6 < 0 ? e6 * maxY : 0) + (e10 > 0 ? e10 * minZ : e10 < 0 ? e10 * maxZ : 0) + e[14]);
856
+ out.max.set((e0 > 0 ? e0 * maxX : e0 < 0 ? e0 * minX : 0) + (e4 > 0 ? e4 * maxY : e4 < 0 ? e4 * minY : 0) + (e8 > 0 ? e8 * maxZ : e8 < 0 ? e8 * minZ : 0) + e[12], (e1 > 0 ? e1 * maxX : e1 < 0 ? e1 * minX : 0) + (e5 > 0 ? e5 * maxY : e5 < 0 ? e5 * minY : 0) + (e9 > 0 ? e9 * maxZ : e9 < 0 ? e9 * minZ : 0) + e[13], (e2 > 0 ? e2 * maxX : e2 < 0 ? e2 * minX : 0) + (e6 > 0 ? e6 * maxY : e6 < 0 ? e6 * minY : 0) + (e10 > 0 ? e10 * maxZ : e10 < 0 ? e10 * minZ : 0) + e[14]);
858
857
  };
859
858
  /**
860
859
  * Calculate a bounding box that is as large as the total combined area of the two specified boxes.
@@ -869,8 +868,6 @@ BoundingSphere._tempVec30 = new Vector3();
869
868
  };
870
869
  return BoundingBox;
871
870
  }();
872
- BoundingBox._tempVec30 = new Vector3();
873
- BoundingBox._tempVec31 = new Vector3();
874
871
 
875
872
  /**
876
873
  * Contains static methods to help in determining intersections, containment, etc.
@@ -985,11 +982,15 @@ BoundingBox._tempVec31 = new Vector3();
985
982
  var normal = plane.normal;
986
983
  var zeroTolerance = MathUtil.zeroTolerance;
987
984
  var dir = Vector3.dot(normal, ray.direction);
985
+ var position = Vector3.dot(normal, ray.origin);
988
986
  // Parallel
989
987
  if (Math.abs(dir) < zeroTolerance) {
988
+ // Check if ray origin is on the plane
989
+ if (Math.abs(position + plane.distance) < zeroTolerance) {
990
+ return 0;
991
+ }
990
992
  return -1;
991
993
  }
992
- var position = Vector3.dot(normal, ray.origin);
993
994
  var distance = (-plane.distance - position) / dir;
994
995
  if (distance < 0) {
995
996
  if (distance < -zeroTolerance) {
@@ -1362,7 +1363,7 @@ CollisionUtil._tempVec32 = new Vector3();
1362
1363
  /**
1363
1364
  * Get the plane by the given frustum face.
1364
1365
  * @param face - The frustum face
1365
- * @returns The plane get
1366
+ * @returns The plane, or null for an invalid face
1366
1367
  */ _proto.getPlane = function getPlane(face) {
1367
1368
  switch(face){
1368
1369
  case FrustumFace.Near:
@@ -1855,6 +1856,7 @@ CollisionUtil._tempVec32 = new Vector3();
1855
1856
  * Calculate the inverse of the specified matrix.
1856
1857
  * @param a - The matrix whose inverse is to be calculated
1857
1858
  * @param out - The inverse of the specified matrix
1859
+ * @returns Null if the matrix is singular; otherwise undefined
1858
1860
  */ Matrix3x3.invert = function invert(a, out) {
1859
1861
  var ae = a.elements;
1860
1862
  var oe = out.elements;
@@ -3326,6 +3328,7 @@ CollisionUtil._tempVec32 = new Vector3();
3326
3328
  * Calculate the inverse of the specified matrix.
3327
3329
  * @param a - The matrix whose inverse is to be calculated
3328
3330
  * @param out - The inverse of the specified matrix
3331
+ * @returns Null if the matrix is singular; otherwise undefined
3329
3332
  */ Matrix.invert = function invert(a, out) {
3330
3333
  var ae = a.elements;
3331
3334
  var oe = out.elements;
@@ -3663,6 +3666,23 @@ Matrix._tempMat30 = new Matrix3x3();
3663
3666
  Vector3.scale(this.direction, distance, out);
3664
3667
  return out.add(this.origin);
3665
3668
  };
3669
+ /**
3670
+ * Creates a clone of this ray.
3671
+ * @returns A clone of this ray
3672
+ */ _proto.clone = function clone() {
3673
+ var out = new Ray();
3674
+ out.copyFrom(this);
3675
+ return out;
3676
+ };
3677
+ /**
3678
+ * Copy this ray from the specified ray.
3679
+ * @param source - The specified ray
3680
+ * @returns This ray
3681
+ */ _proto.copyFrom = function copyFrom(source) {
3682
+ this.origin.copyFrom(source.origin);
3683
+ this.direction.copyFrom(source.direction);
3684
+ return this;
3685
+ };
3666
3686
  return Ray;
3667
3687
  }();
3668
3688