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

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/module.js CHANGED
@@ -465,13 +465,16 @@ function _create_class(Constructor, protoProps, staticProps) {
465
465
  * Converts the vector into a unit vector.
466
466
  * @param a - The vector to normalize
467
467
  * @param out - The normalized vector
468
+ * @returns True if the vector has a unit form, otherwise false and `out` is left untouched
468
469
  */ Vector3.normalize = function normalize(a, out) {
469
470
  var _x = a._x, _y = a._y, _z = a._z;
470
471
  var len = Math.sqrt(_x * _x + _y * _y + _z * _z);
471
- if (len > MathUtil.zeroTolerance) {
472
- len = 1 / len;
473
- out.set(_x * len, _y * len, _z * len);
472
+ if (len > MathUtil.zeroTolerance && Number.isFinite(len)) {
473
+ var inv = 1 / len;
474
+ out.set(_x * inv, _y * inv, _z * inv);
475
+ return true;
474
476
  }
477
+ return false;
475
478
  };
476
479
  /**
477
480
  * Scale a vector by the given value.
@@ -837,20 +840,16 @@ BoundingSphere._tempVec30 = new Vector3();
837
840
  * @param matrix - The transform to apply to the bounding box
838
841
  * @param out - The transformed bounding box
839
842
  */ BoundingBox.transform = function transform(source, matrix, out) {
840
- // https://zeux.io/2010/10/17/aabb-from-obb-with-component-wise-abs/
841
- var center = BoundingBox._tempVec30;
842
- var extent = BoundingBox._tempVec31;
843
- source.getCenter(center);
844
- source.getExtent(extent);
845
- Vector3.transformCoordinate(center, matrix, center);
846
- var x = extent.x, y = extent.y, z = extent.z;
843
+ // Arvo's min/max method: for each matrix element, positive values multiply min for new min (max for new max),
844
+ // negative values multiply max for new min (min for new max), then add translation.
845
+ // Zero check avoids 0 * Infinity = NaN
846
+ var _source_min = source.min, minX = _source_min.x, minY = _source_min.y, minZ = _source_min.z;
847
+ var _source_max = source.max, maxX = _source_max.x, maxY = _source_max.y, maxZ = _source_max.z;
847
848
  var e = matrix.elements;
848
849
  // prettier-ignore
849
850
  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];
850
- 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)));
851
- // set min、max
852
- Vector3.subtract(center, extent, out.min);
853
- Vector3.add(center, extent, out.max);
851
+ 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]);
852
+ 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]);
854
853
  };
855
854
  /**
856
855
  * Calculate a bounding box that is as large as the total combined area of the two specified boxes.
@@ -865,8 +864,6 @@ BoundingSphere._tempVec30 = new Vector3();
865
864
  };
866
865
  return BoundingBox;
867
866
  }();
868
- BoundingBox._tempVec30 = new Vector3();
869
- BoundingBox._tempVec31 = new Vector3();
870
867
 
871
868
  /**
872
869
  * Contains static methods to help in determining intersections, containment, etc.
@@ -981,11 +978,15 @@ BoundingBox._tempVec31 = new Vector3();
981
978
  var normal = plane.normal;
982
979
  var zeroTolerance = MathUtil.zeroTolerance;
983
980
  var dir = Vector3.dot(normal, ray.direction);
981
+ var position = Vector3.dot(normal, ray.origin);
984
982
  // Parallel
985
983
  if (Math.abs(dir) < zeroTolerance) {
984
+ // Check if ray origin is on the plane
985
+ if (Math.abs(position + plane.distance) < zeroTolerance) {
986
+ return 0;
987
+ }
986
988
  return -1;
987
989
  }
988
- var position = Vector3.dot(normal, ray.origin);
989
990
  var distance = (-plane.distance - position) / dir;
990
991
  if (distance < 0) {
991
992
  if (distance < -zeroTolerance) {
@@ -1358,7 +1359,7 @@ CollisionUtil._tempVec32 = new Vector3();
1358
1359
  /**
1359
1360
  * Get the plane by the given frustum face.
1360
1361
  * @param face - The frustum face
1361
- * @returns The plane get
1362
+ * @returns The plane, or null for an invalid face
1362
1363
  */ _proto.getPlane = function getPlane(face) {
1363
1364
  switch(face){
1364
1365
  case FrustumFace.Near:
@@ -1851,6 +1852,7 @@ CollisionUtil._tempVec32 = new Vector3();
1851
1852
  * Calculate the inverse of the specified matrix.
1852
1853
  * @param a - The matrix whose inverse is to be calculated
1853
1854
  * @param out - The inverse of the specified matrix
1855
+ * @returns Null if the matrix is singular; otherwise undefined
1854
1856
  */ Matrix3x3.invert = function invert(a, out) {
1855
1857
  var ae = a.elements;
1856
1858
  var oe = out.elements;
@@ -3322,6 +3324,7 @@ CollisionUtil._tempVec32 = new Vector3();
3322
3324
  * Calculate the inverse of the specified matrix.
3323
3325
  * @param a - The matrix whose inverse is to be calculated
3324
3326
  * @param out - The inverse of the specified matrix
3327
+ * @returns Null if the matrix is singular; otherwise undefined
3325
3328
  */ Matrix.invert = function invert(a, out) {
3326
3329
  var ae = a.elements;
3327
3330
  var oe = out.elements;
@@ -3659,6 +3662,23 @@ Matrix._tempMat30 = new Matrix3x3();
3659
3662
  Vector3.scale(this.direction, distance, out);
3660
3663
  return out.add(this.origin);
3661
3664
  };
3665
+ /**
3666
+ * Creates a clone of this ray.
3667
+ * @returns A clone of this ray
3668
+ */ _proto.clone = function clone() {
3669
+ var out = new Ray();
3670
+ out.copyFrom(this);
3671
+ return out;
3672
+ };
3673
+ /**
3674
+ * Copy this ray from the specified ray.
3675
+ * @param source - The specified ray
3676
+ * @returns This ray
3677
+ */ _proto.copyFrom = function copyFrom(source) {
3678
+ this.origin.copyFrom(source.origin);
3679
+ this.direction.copyFrom(source.direction);
3680
+ return this;
3681
+ };
3662
3682
  return Ray;
3663
3683
  }();
3664
3684