Yuka.js库中AABB(轴对齐包围盒)的applyMatrix4方法可用于将指定的矩阵应用于AABB的八个顶点,并返回新的AABB。
applyMatrix4(matrix4: Matrix4): AABB
AABB - 应用矩阵变换后的新AABB。
const aabb = new YUKA.AABB();
const matrix = new YUKA.Matrix4().rotationY(Math.PI/2); // 绕y轴旋转90度
const transformedAABB = aabb.applyMatrix4(matrix);
console.log(transformedAABB); // 输出变换后的AABB
直接用矩阵乘法来将AABB的八个顶点与矩阵相乘即可。
const applyMatrix4 = function (matrix4) {
const min = this.min;
const max = this.max;
const newMin = new Vector3(Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY);
const newMax = new Vector3(Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY);
const localPoints = this._localPoints;
// 将AABB的八个点乘以矩阵
for (let i = 0, l = localPoints.length; i < l; i++) {
const point = localPoints[i].clone().applyMatrix4(matrix4);
newMin.x = Math.min(newMin.x, point.x);
newMin.y = Math.min(newMin.y, point.y);
newMin.z = Math.min(newMin.z, point.z);
newMax.x = Math.max(newMax.x, point.x);
newMax.y = Math.max(newMax.y, point.y);
newMax.z = Math.max(newMax.z, point.z);
}
// 新建一个计算出的AABB并返回
const aabb = new AABB(newMin, newMax);
return aabb;
};