mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-19 14:52:40 +08:00
@@ -34,8 +34,10 @@ const temp_vec4 = legacyCC.v4();
|
||||
/**
|
||||
* @en
|
||||
* Basic Geometry: Plane.
|
||||
* Plane Equation: a*x + b*y + c*z - d = 0.
|
||||
* @zh
|
||||
* 基础几何:平面。
|
||||
* 平面方程: a*x + b*y + c*z - d = 0。
|
||||
*/
|
||||
|
||||
export class Plane {
|
||||
@@ -223,9 +225,9 @@ export class Plane {
|
||||
public transform (mat: Mat4): void {
|
||||
Mat4.invert(temp_mat, mat);
|
||||
Mat4.transpose(temp_mat, temp_mat);
|
||||
Vec4.set(temp_vec4, this.n.x, this.n.y, this.n.z, this.d);
|
||||
Vec4.set(temp_vec4, this.n.x, this.n.y, this.n.z, -this.d);
|
||||
Vec4.transformMat4(temp_vec4, temp_vec4, temp_mat);
|
||||
Vec3.set(this.n, temp_vec4.x, temp_vec4.y, temp_vec4.z);
|
||||
this.d = temp_vec4.w;
|
||||
this.d = -temp_vec4.w;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -236,7 +236,7 @@ export class Sphere {
|
||||
|
||||
/**
|
||||
* @en
|
||||
* Transforms this sphere by a 4x4 matrix and a quternion, stores the result to the `out` parameter.
|
||||
* Transforms this sphere by a 4x4 matrix and a quaternion, stores the result to the `out` parameter.
|
||||
* @zh
|
||||
* 使用一个 4x4 矩阵和一个四元数变换此球体,并将结果存储在 `out` 参数中。
|
||||
* @param m @en The 4x4 transform matrix. @zh 4x4 变换矩阵。
|
||||
|
||||
@@ -59,7 +59,8 @@ export class AffineTransform {
|
||||
|
||||
/**
|
||||
* @en Concatenate a transform matrix to another. The results are reflected in the out `AffineTransform`.
|
||||
* @zh 将两个矩阵相乘的结果赋值给输出矩阵。
|
||||
* First apply t1, then t2: out * v = t2 * (t1 * v).
|
||||
* @zh 将两个矩阵相乘的结果赋值给输出矩阵,先应用t1再应用t2: out * v = t2 * (t1 * v)。
|
||||
* @param out Out object to store the concat result
|
||||
* @param t1 The first transform object.
|
||||
* @param t2 The transform object to concatenate.
|
||||
@@ -198,7 +199,8 @@ export class AffineTransform {
|
||||
* @param rect The rect object to apply transform.
|
||||
* @param anAffineTransform transform matrix.
|
||||
*/
|
||||
public static transformObb (out_bl: Vec2, out_tl: Vec2, out_tr: Vec2, out_br: Vec2, rect: Rect, anAffineTransform: AffineTransform) {
|
||||
public static transformObb (out_bl: Vec2, out_tl: Vec2, out_tr: Vec2, out_br: Vec2, rect: Rect,
|
||||
anAffineTransform: AffineTransform, flipY = true) {
|
||||
const tx = anAffineTransform.a * rect.x + anAffineTransform.c * rect.y + anAffineTransform.tx;
|
||||
const ty = anAffineTransform.b * rect.x + anAffineTransform.d * rect.y + anAffineTransform.ty;
|
||||
const xa = anAffineTransform.a * rect.width;
|
||||
@@ -206,16 +208,33 @@ export class AffineTransform {
|
||||
const yc = anAffineTransform.c * rect.height;
|
||||
const yd = anAffineTransform.d * rect.height;
|
||||
|
||||
out_tl.x = tx;
|
||||
out_tl.y = ty;
|
||||
out_tr.x = xa + tx;
|
||||
out_tr.y = xb + ty;
|
||||
out_bl.x = yc + tx;
|
||||
out_bl.y = yd + ty;
|
||||
out_br.x = xa + yc + tx;
|
||||
out_br.y = xb + yd + ty;
|
||||
if (flipY) {
|
||||
out_tl.x = tx;
|
||||
out_tl.y = ty;
|
||||
out_tr.x = xa + tx;
|
||||
out_tr.y = xb + ty;
|
||||
out_bl.x = yc + tx;
|
||||
out_bl.y = yd + ty;
|
||||
out_br.x = xa + yc + tx;
|
||||
out_br.y = xb + yd + ty;
|
||||
} else {
|
||||
out_bl.x = tx;
|
||||
out_bl.y = ty;
|
||||
out_br.x = xa + tx;
|
||||
out_br.y = xb + ty;
|
||||
out_tl.x = yc + tx;
|
||||
out_tl.y = yd + ty;
|
||||
out_tr.x = xa + yc + tx;
|
||||
out_tr.y = xb + yd + ty;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* matrix layout
|
||||
* |a c tx|
|
||||
* |b d ty|
|
||||
* |0 0 1 |
|
||||
*/
|
||||
public declare a: number;
|
||||
public declare b: number;
|
||||
public declare c: number;
|
||||
|
||||
+36
-22
@@ -74,12 +74,12 @@ export class Mat3 extends ValueType {
|
||||
public static set <Out extends IMat3Like> (
|
||||
out: Out,
|
||||
m00: number, m01: number, m02: number,
|
||||
m10: number, m11: number, m12: number,
|
||||
m20: number, m21: number, m22: number,
|
||||
m03: number, m04: number, m05: number,
|
||||
m06: number, m07: number, m08: number,
|
||||
) {
|
||||
out.m00 = m00; out.m01 = m01; out.m02 = m02;
|
||||
out.m03 = m10; out.m04 = m11; out.m05 = m12;
|
||||
out.m06 = m20; out.m07 = m21; out.m08 = m22;
|
||||
out.m03 = m03; out.m04 = m04; out.m05 = m05;
|
||||
out.m06 = m06; out.m07 = m07; out.m08 = m08;
|
||||
return out;
|
||||
}
|
||||
|
||||
@@ -180,8 +180,8 @@ export class Mat3 extends ValueType {
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Multiply two matrices explicitly and save the results to out matrix
|
||||
* @zh 矩阵乘法
|
||||
* @en Multiply two matrices explicitly and save the results to out matrix: a * b
|
||||
* @zh 矩阵乘法:a * b
|
||||
*/
|
||||
public static multiply <Out extends IMat3Like> (out: Out, a: Out, b: Out) {
|
||||
const a00 = a.m00; const a01 = a.m01; const a02 = a.m02;
|
||||
@@ -207,8 +207,8 @@ export class Mat3 extends ValueType {
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Take the first third order of the fourth order matrix and multiply by the third order matrix
|
||||
* @zh 取四阶矩阵的前三阶,与三阶矩阵相乘
|
||||
* @en Take the first third order of the fourth order matrix and multiply by the third order matrix: a * b
|
||||
* @zh 取四阶矩阵的前三阶,与三阶矩阵相乘:a * b
|
||||
*/
|
||||
public static multiplyMat4 <Out extends IMat3Like> (out: Out, a: Out, b: IMat4Like) {
|
||||
const a00 = a.m00; const a01 = a.m01; const a02 = a.m02;
|
||||
@@ -234,10 +234,21 @@ export class Mat3 extends ValueType {
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Multiply a matrix with a translation vector given by a translation offset.
|
||||
* @zh 在给定矩阵变换基础上加入变换
|
||||
* @en Multiply a matrix with a translation vector given by a translation offset, first translate, then transform:a * T(v).
|
||||
* @zh 在给定矩阵变换基础上加入位移变换,先位移,再变换,即a * T(v)。
|
||||
*/
|
||||
/**
|
||||
* @deprecated since v3.8.0, the function name is misleading, please use translate instead.
|
||||
*/
|
||||
public static transform <Out extends IMat3Like, VecLike extends IVec3Like> (out: Out, a: Out, v: VecLike) {
|
||||
this.translate(out, a, v);
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Multiply a matrix with a translation vector given by a translation offset, first translate, then transform:a * T(v).
|
||||
* @zh 在给定矩阵变换基础上加入位移变换,先位移,再变换,即a * T(v)。
|
||||
*/
|
||||
public static translate <Out extends IMat3Like, VecLike extends IVec3Like> (out: Out, a: Out, v: VecLike) {
|
||||
const a00 = a.m00; const a01 = a.m01; const a02 = a.m02;
|
||||
const a10 = a.m03; const a11 = a.m04; const a12 = a.m05;
|
||||
const a20 = a.m06; const a21 = a.m07; const a22 = a.m08;
|
||||
@@ -258,8 +269,8 @@ export class Mat3 extends ValueType {
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Multiply a matrix with a scale matrix given by a scale vector and save the results to out matrix
|
||||
* @zh 在给定矩阵变换基础上加入新缩放变换
|
||||
* @en Multiply a matrix with a scale matrix given by a scale vector and save the results to out matrix, first scale, then transform:a * S(v).
|
||||
* @zh 在给定矩阵变换基础上加入新缩放变换,先缩放,再变换,即a * S(v)。
|
||||
*/
|
||||
public static scale <Out extends IMat3Like, VecLike extends IVec3Like> (out: Out, a: Out, v: VecLike) {
|
||||
const x = v.x; const y = v.y;
|
||||
@@ -279,9 +290,9 @@ export class Mat3 extends ValueType {
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Rotates the transform by the given angle and save the results into the out matrix
|
||||
* @zh 在给定矩阵变换基础上加入新旋转变换
|
||||
* @param rad radius of rotation
|
||||
* @en Rotates the transform by the given angle and save the results into the out matrix, first rotate, then transform:a * R(rad).
|
||||
* @zh 在给定矩阵变换基础上加入新旋转变换,先旋转,再变换,即a * R(rad)。
|
||||
* @param rad radian of rotation
|
||||
*/
|
||||
public static rotate <Out extends IMat3Like> (out: Out, a: Out, rad: number) {
|
||||
const a00 = a.m00; const a01 = a.m01; const a02 = a.m02;
|
||||
@@ -445,6 +456,9 @@ export class Mat3 extends ValueType {
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated since v3.8.0, this function is too complicated, and should be split into several functions.
|
||||
*/
|
||||
/**
|
||||
* @en Calculates the upper-left 3x3 matrix of a 4x4 matrix's inverse transpose
|
||||
* @zh 计算指定四维矩阵的逆转置三维矩阵
|
||||
@@ -625,8 +639,8 @@ export class Mat3 extends ValueType {
|
||||
|
||||
/**
|
||||
* @en Convert Matrix to euler angle, resulting angle y, z in the range of [-PI, PI],
|
||||
* x in the range of [-PI/2, PI/2], the rotation order is YXZ.
|
||||
* @zh 将矩阵转换成欧拉角, 返回角度 y,z 在 [-PI, PI] 区间内, x 在 [-PI/2, PI/2] 区间内,旋转顺序为 YXZ.
|
||||
* x in the range of [-PI/2, PI/2], the rotation order is YXZ, first rotate around Y, then around X, and finally around Z.
|
||||
* @zh 将矩阵转换成欧拉角, 返回角度 y,z 在 [-PI, PI] 区间内, x 在 [-PI/2, PI/2] 区间内,旋转顺序为 YXZ,即先绕Y旋转,再绕X,最后绕Z旋转。
|
||||
*/
|
||||
public static toEuler (matrix: Mat3, v: Vec3): boolean {
|
||||
//a[col][row]
|
||||
@@ -991,8 +1005,8 @@ export class Mat3 extends ValueType {
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Multiply the current matrix with a scale matrix given by a scale vector.
|
||||
* @zh 将当前矩阵左乘缩放矩阵的结果赋值给当前矩阵,缩放矩阵由各个轴的缩放给出。
|
||||
* @en Multiply the current matrix with a scale matrix given by a scale vector, that is M * S(vec).
|
||||
* @zh 将当前矩阵左乘缩放矩阵的结果赋值给当前矩阵,缩放矩阵由各个轴的缩放给出,即M * S(vec)。
|
||||
* @param vec vector to scale by
|
||||
*/
|
||||
public scale (vec: Vec3) {
|
||||
@@ -1013,9 +1027,9 @@ export class Mat3 extends ValueType {
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Rotates the current matrix by the given angle.
|
||||
* @zh 将当前矩阵左乘旋转矩阵的结果赋值给当前矩阵,旋转矩阵由旋转轴和旋转角度给出。
|
||||
* @param rad radius of rotation
|
||||
* @en Rotates the current matrix by the given angle, that is M * R(rad).
|
||||
* @zh 将当前矩阵左乘旋转矩阵的结果赋值给当前矩阵,旋转矩阵由旋转轴和旋转角度给出,即M * R(rad)。
|
||||
* @param rad radian of rotation
|
||||
*/
|
||||
public rotate (rad: number) {
|
||||
const a00 = this.m00; const a01 = this.m01; const a02 = this.m02;
|
||||
|
||||
+19
-17
@@ -116,7 +116,7 @@ export class Quat extends ValueType {
|
||||
* @zh 获取四元数的旋转轴和旋转弧度
|
||||
* @param outAxis output axis
|
||||
* @param q input quaternion
|
||||
* @return radius of rotation
|
||||
* @return radian of rotation
|
||||
*/
|
||||
public static getAxisAngle<Out extends IQuatLike, VecLike extends IVec3Like> (outAxis: VecLike, q: Out) {
|
||||
const rad = Math.acos(q.w) * 2.0;
|
||||
@@ -135,8 +135,8 @@ export class Quat extends ValueType {
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Quaternion multiplication and save the results to out quaternion
|
||||
* @zh 四元数乘法
|
||||
* @en Quaternion multiplication and save the results to out quaternion, that is a * b.
|
||||
* @zh 四元数乘法,即a * b。
|
||||
*/
|
||||
public static multiply<Out extends IQuatLike, QuatLike_1 extends IQuatLike, QuatLike_2 extends IQuatLike> (out: Out, a: QuatLike_1, b: QuatLike_2) {
|
||||
const x = a.x * b.w + a.w * b.x + a.y * b.z - a.z * b.y;
|
||||
@@ -177,7 +177,7 @@ export class Quat extends ValueType {
|
||||
/**
|
||||
* @en Sets the out quaternion to represent a radian rotation around x axis
|
||||
* @zh 绕 X 轴旋转指定四元数
|
||||
* @param rad radius of rotation
|
||||
* @param rad radian of rotation
|
||||
*/
|
||||
public static rotateX<Out extends IQuatLike> (out: Out, a: Out, rad: number) {
|
||||
rad *= 0.5;
|
||||
@@ -196,7 +196,7 @@ export class Quat extends ValueType {
|
||||
/**
|
||||
* @en Sets the out quaternion to represent a radian rotation around y axis
|
||||
* @zh 绕 Y 轴旋转指定四元数
|
||||
* @param rad radius of rotation
|
||||
* @param rad radian of rotation
|
||||
*/
|
||||
public static rotateY<Out extends IQuatLike> (out: Out, a: Out, rad: number) {
|
||||
rad *= 0.5;
|
||||
@@ -215,7 +215,7 @@ export class Quat extends ValueType {
|
||||
/**
|
||||
* @en Sets the out quaternion to represent a radian rotation around z axis
|
||||
* @zh 绕 Z 轴旋转指定四元数
|
||||
* @param rad radius of rotation
|
||||
* @param rad radian of rotation
|
||||
*/
|
||||
public static rotateZ<Out extends IQuatLike> (out: Out, a: Out, rad: number) {
|
||||
rad *= 0.5;
|
||||
@@ -235,7 +235,7 @@ export class Quat extends ValueType {
|
||||
* @en Sets the out quaternion to represent a radian rotation around a given rotation axis in world space
|
||||
* @zh 绕世界空间下指定轴旋转四元数
|
||||
* @param axis axis of rotation, normalized by default
|
||||
* @param rad radius of rotation
|
||||
* @param rad radian of rotation
|
||||
*/
|
||||
public static rotateAround<Out extends IQuatLike, VecLike extends IVec3Like> (out: Out, rot: Out, axis: VecLike, rad: number) {
|
||||
// get inv-axis (local to rot)
|
||||
@@ -251,7 +251,7 @@ export class Quat extends ValueType {
|
||||
* @en Sets the out quaternion to represent a radian rotation around a given rotation axis in local space
|
||||
* @zh 绕本地空间下指定轴旋转四元数
|
||||
* @param axis axis of rotation
|
||||
* @param rad radius of rotation
|
||||
* @param rad radian of rotation
|
||||
*/
|
||||
public static rotateAroundLocal<Out extends IQuatLike, VecLike extends IVec3Like> (out: Out, rot: Out, axis: VecLike, rad: number) {
|
||||
Quat.fromAxisAngle(qt_1, axis, rad);
|
||||
@@ -457,7 +457,7 @@ export class Quat extends ValueType {
|
||||
* @en Calculates the quaternion with the three-dimensional transform matrix, considering no scale included in the matrix
|
||||
* @zh 根据三维矩阵信息计算四元数,默认输入矩阵不含有缩放信息
|
||||
*/
|
||||
public static fromMat3<Out extends IQuatLike> (out: Out, m: Mat3) {
|
||||
public static fromMat3<Out extends IQuatLike> (out: Out, m: Mat3) {
|
||||
const {
|
||||
m00, m03: m01, m06: m02,
|
||||
m01: m10, m04: m11, m07: m12,
|
||||
@@ -500,8 +500,8 @@ export class Quat extends ValueType {
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Calculates the quaternion with Euler angles, the rotation order is YZX
|
||||
* @zh 根据欧拉角信息计算四元数,旋转顺序为 YZX
|
||||
* @en Calculates the quaternion with Euler angles, the rotation order is YZX, first rotate around Y, then around Z, and finally around X.
|
||||
* @zh 根据欧拉角信息计算四元数,旋转顺序为 YZX,即先绕Y旋转,再绕Z,最后绕X旋转。
|
||||
*/
|
||||
public static fromEuler<Out extends IQuatLike> (out: Out, x: number, y: number, z: number) {
|
||||
x *= halfToRad;
|
||||
@@ -547,7 +547,7 @@ export class Quat extends ValueType {
|
||||
const fz = 2.0 * q.z;
|
||||
out.x = 1.0 - fy * q.y - fz * q.z;
|
||||
out.y = fy * q.x + fz * q.w;
|
||||
out.z = fz * q.x + fy * q.w;
|
||||
out.z = fz * q.x - fy * q.w;
|
||||
|
||||
return out;
|
||||
}
|
||||
@@ -575,7 +575,7 @@ export class Quat extends ValueType {
|
||||
const fx = 2.0 * q.x;
|
||||
const fy = 2.0 * q.y;
|
||||
const fz = 2.0 * q.z;
|
||||
out.x = fz * q.x - fy * q.w;
|
||||
out.x = fz * q.x + fy * q.w;
|
||||
out.y = fz * q.y - fx * q.w;
|
||||
out.z = 1.0 - fx * q.x - fy * q.y;
|
||||
|
||||
@@ -583,8 +583,9 @@ export class Quat extends ValueType {
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Converts the quaternion to angles, result angle x, y in the range of [-180, 180], z in the range of [-90, 90] interval, the rotation order is YZX
|
||||
* @zh 根据四元数计算欧拉角,返回角度 x, y 在 [-180, 180] 区间内, z 默认在 [-90, 90] 区间内,旋转顺序为 YZX
|
||||
* @en Converts the quaternion to angles, result angle x, y in the range of [-180, 180], z in the range of [-90, 90] interval,
|
||||
* the rotation order is YZX, first rotate around Y, then around Z, and finally around X
|
||||
* @zh 根据四元数计算欧拉角,返回角度 x, y 在 [-180, 180] 区间内, z 默认在 [-90, 90] 区间内,旋转顺序为 YZX,即先绕Y旋转,再绕Z,最后绕X旋转。
|
||||
* @param outerZ change z value range to [-180, -90] U [90, 180]
|
||||
*/
|
||||
public static toEuler (out: IVec3Like, q: IQuatLike, outerZ?: boolean) {
|
||||
@@ -619,8 +620,9 @@ export class Quat extends ValueType {
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Converts the quaternion to euler angles, result angle y, z in the range of [-180, 180], x in the range of [-90, 90], the rotation order is YXZ
|
||||
* @zh 根据四元数计算欧拉角,返回角度 yz 在 [-180, 180], x 在 [-90, 90],旋转顺序为 YXZ
|
||||
* @en Converts the quaternion to euler angles, result angle y, z in the range of [-180, 180], x in the range of [-90, 90],
|
||||
* the rotation order is YXZ, first rotate around Y, then around X, and finally around Z.
|
||||
* @zh 根据四元数计算欧拉角,返回角度 yz 在 [-180, 180], x 在 [-90, 90],旋转顺序为 YXZ,即先绕Y旋转,再绕X,最后绕Z旋转。
|
||||
*/
|
||||
public static toEulerInYXZOrder (out: Vec3, q: IQuatLike) {
|
||||
Mat3.fromQuat(m3_1, q);
|
||||
|
||||
+11
-8
@@ -297,10 +297,13 @@ export class Vec2 extends ValueType {
|
||||
* @override (a:Vec2, b:Vec2) => number
|
||||
* @override [deprecated] (out:Vec3, a:Vec2, b:Vec2) => Vec3
|
||||
*/
|
||||
/**
|
||||
* @deprecated since v3.8.0, There is no physical meaning.
|
||||
*/
|
||||
public static cross (a: IVec2Like, b: IVec2Like): number;
|
||||
|
||||
/**
|
||||
* @deprecated Consider use another overrides please.
|
||||
* @deprecated since v3.8.0, There is no physical meaning.
|
||||
*/
|
||||
public static cross <Out extends IVec2Like> (out: Vec3, a: Out, b: Out): Vec3;
|
||||
|
||||
@@ -315,7 +318,7 @@ export class Vec2 extends ValueType {
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Calculates the linear interpolation between two vectors with a given ratio
|
||||
* @en Calculates the linear interpolation between two vectors with a given ratio: A + t * (B - A)
|
||||
* @zh 逐元素向量线性插值: A + t * (B - A)
|
||||
*/
|
||||
public static lerp <Out extends IVec2Like> (out: Out, a: Out, b: Out, t: number) {
|
||||
@@ -716,6 +719,9 @@ export class Vec2 extends ValueType {
|
||||
* @param other specified vector
|
||||
* @return `out`
|
||||
*/
|
||||
/**
|
||||
* @deprecated since v3.8.0, There is no physical meaning.
|
||||
*/
|
||||
public cross (other: Vec2) {
|
||||
return this.x * other.y - this.y * other.x;
|
||||
}
|
||||
@@ -787,9 +793,9 @@ export class Vec2 extends ValueType {
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Rotates the current vector by an angle in radian value
|
||||
* @zh 将当前向量的旋转
|
||||
* @param radians radius of rotation
|
||||
* @en Rotates the current vector by an angle in radian value. Counterclockwise is the positive direction.
|
||||
* @zh 将当前向量进行旋转,逆时针为正方向。
|
||||
* @param radians radians of rotation.
|
||||
*/
|
||||
public rotate (radians: number) {
|
||||
const x = this.x;
|
||||
@@ -829,9 +835,6 @@ export class Vec2 extends ValueType {
|
||||
}
|
||||
}
|
||||
|
||||
const v2_1 = new Vec2();
|
||||
const v2_2 = new Vec2();
|
||||
|
||||
CCClass.fastDefine('cc.Vec2', Vec2, { x: 0, y: 0 });
|
||||
legacyCC.Vec2 = Vec2;
|
||||
|
||||
|
||||
+45
-9
@@ -27,9 +27,8 @@ import { CCClass } from '../data/class';
|
||||
import { ValueType } from '../value-types/value-type';
|
||||
import { Mat4 } from './mat4';
|
||||
import { IMat3Like, IMat4Like, IQuatLike, IVec3Like } from './type-define';
|
||||
import { approx, clamp, EPSILON, lerp, random } from './utils';
|
||||
import { clamp, EPSILON, lerp, random } from './utils';
|
||||
import { legacyCC } from '../global-exports';
|
||||
import type { Quat } from './quat';
|
||||
|
||||
/**
|
||||
* @en Representation of 3D vectors and points.
|
||||
@@ -349,7 +348,7 @@ export class Vec3 extends ValueType {
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Calculates the linear interpolation between two vectors with a given ratio
|
||||
* @en Calculates the linear interpolation between two vectors with a given ratio: A + t * (B - A)
|
||||
* @zh 逐元素向量线性插值: A + t * (B - A)
|
||||
*/
|
||||
public static lerp<Out extends IVec3Like> (out: Out, a: IVec3Like, b: IVec3Like, t: number) {
|
||||
@@ -457,6 +456,8 @@ export class Vec3 extends ValueType {
|
||||
const y = a.y;
|
||||
const z = a.z;
|
||||
let rhw = m.m03 * x + m.m07 * y + m.m11 * z + m.m15;
|
||||
// Important note by stanley:
|
||||
// Math.abs needs to be removed later, because the operation will generate a wrong homogeneous coordinate
|
||||
rhw = rhw ? Math.abs(1 / rhw) : 1;
|
||||
out.x = (m.m00 * x + m.m04 * y + m.m08 * z + m.m12) * rhw;
|
||||
out.y = (m.m01 * x + m.m05 * y + m.m09 * z + m.m13) * rhw;
|
||||
@@ -473,6 +474,8 @@ export class Vec3 extends ValueType {
|
||||
const y = a.y;
|
||||
const z = a.z;
|
||||
let rhw = m.m03 * x + m.m07 * y + m.m11 * z;
|
||||
// Important note by stanley:
|
||||
// Math.abs needs to be removed later, because the operation will generate a wrong homogeneous coordinate
|
||||
rhw = rhw ? Math.abs(1 / rhw) : 1;
|
||||
out.x = (m.m00 * x + m.m04 * y + m.m08 * z) * rhw;
|
||||
out.y = (m.m01 * x + m.m05 * y + m.m09 * z) * rhw;
|
||||
@@ -509,8 +512,8 @@ export class Vec3 extends ValueType {
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Vector quaternion multiplication
|
||||
* @zh 向量四元数乘法
|
||||
* @en Vector quaternion multiplication: q*a*q^{-1}.
|
||||
* @zh 向量四元数乘法:q*a*q^{-1}。
|
||||
*/
|
||||
public static transformQuat<Out extends IVec3Like> (out: Out, a: IVec3Like, q: IQuatLike) {
|
||||
// benchmarks: http://jsperf.com/quaternion-transform-Vec3-implementations
|
||||
@@ -569,7 +572,7 @@ export class Vec3 extends ValueType {
|
||||
* @zh 绕 X 轴旋转向量指定弧度
|
||||
* @param v rotation vector
|
||||
* @param o center of rotation
|
||||
* @param a radius of rotation
|
||||
* @param a radiance of rotation
|
||||
*/
|
||||
public static rotateX<Out extends IVec3Like> (out: Out, v: IVec3Like, o: IVec3Like, a: number) {
|
||||
// Translate point to the origin
|
||||
@@ -597,7 +600,7 @@ export class Vec3 extends ValueType {
|
||||
* @zh 绕 Y 轴旋转向量指定弧度
|
||||
* @param v rotation vector
|
||||
* @param o center of rotation
|
||||
* @param a radius of rotation
|
||||
* @param a radiance of rotation
|
||||
*/
|
||||
public static rotateY<Out extends IVec3Like> (out: Out, v: IVec3Like, o: IVec3Like, a: number) {
|
||||
// Translate point to the origin
|
||||
@@ -625,7 +628,7 @@ export class Vec3 extends ValueType {
|
||||
* @zh 绕 Z 轴旋转向量指定弧度
|
||||
* @param v rotation vector
|
||||
* @param o center of rotation
|
||||
* @param a radius of rotation
|
||||
* @param a radiance of rotation
|
||||
*/
|
||||
public static rotateZ<Out extends IVec3Like> (out: Out, v: IVec3Like, o: IVec3Like, a: number) {
|
||||
// Translate point to the origin
|
||||
@@ -648,6 +651,39 @@ export class Vec3 extends ValueType {
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Rotates the vector with specified angle around any n axis
|
||||
* @zh 绕任意 n 轴旋转向量指定弧度
|
||||
* @param v rotation vector
|
||||
* @param o center of rotation
|
||||
* @param n axis of rotation
|
||||
* @param a radiance of rotation
|
||||
*/
|
||||
public static rotateN<Out extends IVec3Like> (out: Out, v: IVec3Like, o: IVec3Like, n: IVec3Like, a: number) {
|
||||
// Translate point to the origin
|
||||
const x = v.x - o.x;
|
||||
const y = v.y - o.y;
|
||||
const z = v.z - o.z;
|
||||
|
||||
// perform rotation
|
||||
const nx = n.x;
|
||||
const ny = n.y;
|
||||
const nz = n.z;
|
||||
|
||||
const cos = Math.cos(a);
|
||||
const sin = Math.sin(a);
|
||||
const rx = x * (nx * nx * (1.0 - cos) + cos) + y * (nx * ny * (1.0 - cos) - nx * sin) + z * (nx * nz * (1.0 - cos) + ny * sin);
|
||||
const ry = x * (nx * ny * (1.0 - cos) + nz * sin) + y * (ny * ny * (1.0 - cos) + cos) + z * (ny * nz * (1.0 - cos) - nx * sin);
|
||||
const rz = x * (nx * nz * (1.0 - cos) - ny * sin) + y * (ny * nz * (1.0 - cos) + nx * sin) + z * (nz * nz * (1.0 - cos) + cos);
|
||||
|
||||
// translate to correct position
|
||||
out.x = rx + o.x;
|
||||
out.y = ry + o.y;
|
||||
out.z = rz + o.z;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Converts the given vector to an array
|
||||
* @zh 向量转数组
|
||||
@@ -730,7 +766,7 @@ export class Vec3 extends ValueType {
|
||||
* @en Calculates the projection on the specified vector
|
||||
* @zh 计算向量在指定向量上的投影
|
||||
* @param a projection vector
|
||||
* @param n target vector
|
||||
* @param b target vector
|
||||
*/
|
||||
public static project<Out extends IVec3Like> (out: Out, a: IVec3Like, b: IVec3Like) {
|
||||
const sqrLen = Vec3.lengthSqr(b);
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
import { log } from '../../test.log';
|
||||
import { AffineTransform } from '../../../cocos/core/math/affine-transform';
|
||||
import { Vec2 } from '../../../cocos/core/math/vec2';
|
||||
import { Size } from '../../../cocos/core/math/size';
|
||||
import { Rect } from '../../../cocos/core/math/rect';
|
||||
|
||||
// test Vec2
|
||||
describe('Test AffineTransform', () => {
|
||||
test('concat', () => {
|
||||
const t1 = new AffineTransform(2, 0, 0, 3, 0, 0);
|
||||
const t2 = new AffineTransform(1, 0, 0, 1, 5, 7);
|
||||
const t12 = AffineTransform.identity();
|
||||
AffineTransform.concat(t12, t1, t2);
|
||||
log('concat: ', t12);
|
||||
expect(t12.a).toBeCloseTo(2);
|
||||
expect(t12.b).toBeCloseTo(0);
|
||||
expect(t12.c).toBeCloseTo(0);
|
||||
expect(t12.d).toBeCloseTo(3);
|
||||
expect(t12.tx).toBeCloseTo(5);
|
||||
expect(t12.ty).toBeCloseTo(7);
|
||||
});
|
||||
|
||||
test('invert', () => {
|
||||
const t1 = new AffineTransform(2, 0, 0, 3, 5, 7);
|
||||
const t2 = AffineTransform.identity();
|
||||
const t12 = AffineTransform.identity();
|
||||
AffineTransform.invert(t2, t1);
|
||||
AffineTransform.concat(t12, t1, t2);
|
||||
console.log('invert: ', t12);
|
||||
expect(t12.a).toBeCloseTo(1);
|
||||
expect(t12.b).toBeCloseTo(0);
|
||||
expect(t12.c).toBeCloseTo(0);
|
||||
expect(t12.d).toBeCloseTo(1);
|
||||
expect(t12.tx).toBeCloseTo(0);
|
||||
expect(t12.ty).toBeCloseTo(0);
|
||||
});
|
||||
|
||||
test('transformVec2', () => {
|
||||
const t1 = new AffineTransform(2, 0, 0, 3, 5, 7);
|
||||
const v0 = new Vec2(1, 2);
|
||||
const expect0 = new Vec2(7, 13);
|
||||
AffineTransform.transformVec2(v0, v0, t1);
|
||||
log('transformVec2: ', v0);
|
||||
expect(Vec2.equals(v0, expect0)).toBe(true);
|
||||
});
|
||||
|
||||
test('transformSize', () => {
|
||||
const t1 = new AffineTransform(2, 0, 0, 3, 5, 7);
|
||||
const s0 = new Size(1, 2);
|
||||
const expect0 = new Size(2, 6);
|
||||
AffineTransform.transformSize(s0, s0, t1);
|
||||
log('transformSize: ', s0);
|
||||
expect(Vec2.equals(s0, expect0)).toBe(true);
|
||||
});
|
||||
|
||||
test('transformRect', () => {
|
||||
const t1 = new AffineTransform(2, 0, 0, 3, 5, 7);
|
||||
const r0 = new Rect(1, 2, 3, 4);
|
||||
const expect0 = new Rect(7, 13, 6, 12);
|
||||
AffineTransform.transformRect(r0, r0, t1);
|
||||
log('transformRect: ', r0);
|
||||
expect(Vec2.equals(r0, expect0)).toBe(true);
|
||||
});
|
||||
|
||||
test('transformObb', () => {
|
||||
const t1 = new AffineTransform(2, 0, 0, 3, 5, 7);
|
||||
const r0 = new Rect(1, 2, 3, 4);
|
||||
const bl = new Vec2();
|
||||
const tl = new Vec2();
|
||||
const tr = new Vec2();
|
||||
const br = new Vec2();
|
||||
const expect0 = new Vec2(7, 13);
|
||||
const expect1 = new Vec2(7, 25);
|
||||
const expect2 = new Vec2(13, 25);
|
||||
const expect3 = new Vec2(13, 13);
|
||||
AffineTransform.transformObb(bl, tl, tr, br, r0, t1, false);
|
||||
log('transformObb 0: ', bl);
|
||||
log('transformObb 1: ', tl);
|
||||
log('transformObb 2: ', tr);
|
||||
log('transformObb 3: ', br);
|
||||
expect(Vec2.equals(bl, expect0)).toBe(true);
|
||||
expect(Vec2.equals(tl, expect1)).toBe(true);
|
||||
expect(Vec2.equals(tr, expect2)).toBe(true);
|
||||
expect(Vec2.equals(br, expect3)).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,141 @@
|
||||
import { log } from '../../test.log';
|
||||
import { Vec3 } from '../../../cocos/core/math/vec3';
|
||||
import { Mat3 } from '../../../cocos/core/math/mat3';
|
||||
import { Mat4 } from '../../../cocos/core/math/mat4';
|
||||
import { Quat } from '../../../cocos/core/math/quat';
|
||||
|
||||
describe('Test Mat3', () => {
|
||||
test('transpose', () => {
|
||||
const m0 = new Mat3(1, 2, 3, 4, 5, 6, 7, 8, 9);
|
||||
const m1 = new Mat3(1, 2, 3, 4, 5, 6, 7, 8, 9);
|
||||
const expect0 = new Mat3(1, 4, 7, 2, 5, 8, 3, 6, 9);
|
||||
Mat3.transpose(m0, m0);
|
||||
m1.transpose();
|
||||
log('transpose 0: ', m0);
|
||||
log('transpose 1: ', m1);
|
||||
expect(Mat3.equals(m0, expect0)).toBe(true);
|
||||
expect(Mat3.equals(m1, expect0)).toBe(true);
|
||||
});
|
||||
|
||||
test('invert', () => {
|
||||
const cos = Math.cos(Math.PI / 6);
|
||||
const sin = Math.sin(Math.PI / 6);
|
||||
const m0 = new Mat3(cos, sin, 0, -sin, cos, 0, 0, 0, 1);
|
||||
const m1 = new Mat3(cos, sin, 0, -sin, cos, 0, 0, 0, 1);
|
||||
const expect0 = new Mat3(cos, -sin, 0, sin, cos, 0, 0, 0, 1);
|
||||
Mat3.invert(m0, m0);
|
||||
m1.invert();
|
||||
log('invert 0: ', m0);
|
||||
log('invert 1: ', m1);
|
||||
expect(Mat3.equals(m0, expect0)).toBe(true);
|
||||
expect(Mat3.equals(m1, expect0)).toBe(true);
|
||||
});
|
||||
|
||||
test('determinant', () => {
|
||||
const cos = Math.cos(Math.PI / 6);
|
||||
const sin = Math.sin(Math.PI / 6);
|
||||
const m0 = new Mat3(cos, sin, 0, -sin, cos, 0, 0, 0, 1);
|
||||
const expect0 = 1;
|
||||
const d0 = Mat3.determinant(m0);
|
||||
const d1 = m0.determinant();
|
||||
log('determinant 0: ', d0);
|
||||
log('determinant 1: ', d1);
|
||||
expect(d0).toBe(expect0);
|
||||
expect(d1).toBe(expect0);
|
||||
});
|
||||
|
||||
test('multiply', () => {
|
||||
const cos = Math.cos(Math.PI / 6);
|
||||
const sin = Math.sin(Math.PI / 6);
|
||||
const cos2 = Math.cos(Math.PI / 3);
|
||||
const sin2 = Math.sin(Math.PI / 3);
|
||||
const m0 = new Mat3(cos, sin, 0, -sin, cos, 0, 0, 0, 1);
|
||||
const m1 = new Mat3(cos, sin, 0, -sin, cos, 0, 0, 0, 1);
|
||||
const expect0 = new Mat3(cos2, sin2, 0, -sin2, cos2, 0, 0, 0, 1);
|
||||
Mat3.multiply(m0, m0, m1);
|
||||
log('multiply: ', m0);
|
||||
expect(Mat3.equals(m0, expect0)).toBe(true);
|
||||
});
|
||||
|
||||
test('multiplyMat4', () => {
|
||||
const cos = Math.cos(Math.PI / 6);
|
||||
const sin = Math.sin(Math.PI / 6);
|
||||
const cos2 = Math.cos(Math.PI / 3);
|
||||
const sin2 = Math.sin(Math.PI / 3);
|
||||
const m0 = new Mat3(cos, sin, 0, -sin, cos, 0, 0, 0, 1);
|
||||
const m1 = new Mat4(cos, sin, 0, 0, -sin, cos, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
|
||||
const expect0 = new Mat3(cos2, sin2, 0, -sin2, cos2, 0, 0, 0, 1);
|
||||
Mat3.multiplyMat4(m0, m0, m1);
|
||||
log('multiplyMat4: ', m0);
|
||||
expect(Mat3.equals(m0, expect0)).toBe(true);
|
||||
});
|
||||
|
||||
test('translate', () => {
|
||||
const m0 = new Mat3(2, 0, 0, 0, 3, 0, 0, 0, 1);
|
||||
const v0 = new Vec3(1, 2, 1);
|
||||
const v1 = new Vec3(2, 3, 1);
|
||||
const expect0 = new Vec3(6, 15, 1);
|
||||
Mat3.translate(m0, m0, v1);
|
||||
Vec3.transformMat3(v0, v0, m0);
|
||||
log('translate: ', v0);
|
||||
expect(Vec3.equals(v0, expect0)).toBe(true);
|
||||
});
|
||||
|
||||
test('scale', () => {
|
||||
const m0 = new Mat3(2, 0, 0, 0, 3, 0, 0, 0, 1);
|
||||
const m1 = new Mat3(2, 0, 0, 0, 3, 0, 0, 0, 1);
|
||||
const v0 = new Vec3(1, 2, 1);
|
||||
const v1 = new Vec3(2, 3, 1);
|
||||
const expect0 = new Vec3(4, 18, 1);
|
||||
Mat3.scale(m0, m0, v1);
|
||||
m1.scale(v1);
|
||||
const s0 = Vec3.transformMat3(new Vec3(), v0, m0);
|
||||
const s1 = Vec3.transformMat3(new Vec3(), v0, m1);
|
||||
log('scale 0: ', s0);
|
||||
log('scale 1: ', s1);
|
||||
expect(Vec3.equals(s0, expect0)).toBe(true);
|
||||
expect(Vec3.equals(s1, expect0)).toBe(true);
|
||||
});
|
||||
|
||||
test('rotate', () => {
|
||||
const m0 = new Mat3(2, 0, 0, 0, 3, 0, 0, 0, 1);
|
||||
const m1 = new Mat3(2, 0, 0, 0, 3, 0, 0, 0, 1);
|
||||
const v0 = new Vec3(1, 2, 1);
|
||||
const expect0 = new Vec3(-4, 3, 1);
|
||||
Mat3.rotate(m0, m0, Math.PI / 2);
|
||||
m1.rotate(Math.PI / 2);
|
||||
const r0 = Vec3.transformMat3(new Vec3(), v0, m0);
|
||||
const r1 = Vec3.transformMat3(new Vec3(), v0, m1);
|
||||
log('rotate 0: ', r0);
|
||||
log('rotate 1: ', r1);
|
||||
expect(Vec3.equals(r0, expect0)).toBe(true);
|
||||
expect(Vec3.equals(r1, expect0)).toBe(true);
|
||||
});
|
||||
|
||||
test('fromQuat', () => {
|
||||
const m0 = new Mat3();
|
||||
const m1 = new Mat3();
|
||||
const q0 = Quat.fromAxisAngle(new Quat(), new Vec3(1, 0, 0), Math.PI);
|
||||
const v0 = new Vec3(0, 1, 0);
|
||||
const expect0 = new Vec3(0, -1, 0);
|
||||
Mat3.fromQuat(m0, q0);
|
||||
m1.fromQuat(q0);
|
||||
const r0 = Vec3.transformMat3(new Vec3(), v0, m0);
|
||||
const r1 = Vec3.transformMat3(new Vec3(), v0, m1);
|
||||
log('fromQuat 0: ', r0);
|
||||
log('fromQuat 1: ', r1);
|
||||
expect(Vec3.equals(r0, expect0)).toBe(true);
|
||||
expect(Vec3.equals(r1, expect0)).toBe(true);
|
||||
});
|
||||
|
||||
test('toEuler', () => {
|
||||
const m0 = new Mat3();
|
||||
const q0 = Quat.fromAxisAngle(new Quat(), new Vec3(0, 0, 1), Math.PI);
|
||||
const v0 = new Vec3();
|
||||
const expect0 = new Vec3(0, 0, Math.PI);
|
||||
Mat3.fromQuat(m0, q0);
|
||||
Mat3.toEuler(m0, v0);
|
||||
log('toEuler: ', v0);
|
||||
expect(Vec3.equals(v0, expect0)).toBe(true);
|
||||
});
|
||||
});
|
||||
+113
-13
@@ -1,18 +1,118 @@
|
||||
import { Mat3, Quat } from "../../../cocos/core";
|
||||
import { log } from '../../test.log';
|
||||
import { Mat3 } from '../../../cocos/core/math/mat3';
|
||||
import { Quat } from '../../../cocos/core/math/quat';
|
||||
import { Vec3 } from '../../../cocos/core/math/vec3';
|
||||
|
||||
// test Quat
|
||||
describe('Test Quat', () => {
|
||||
test('rotationTo', () => {
|
||||
const q0 = new Quat();
|
||||
const v0 = new Vec3(1, 0, 0);
|
||||
const v1 = new Vec3(0, 1, 0);
|
||||
const expect0 = new Quat(0, 0, Math.sin(Math.PI / 4), Math.cos(Math.PI / 4));
|
||||
Quat.rotationTo(q0, v0, v1);
|
||||
log('rotationTo: ', q0);
|
||||
expect(Quat.equals(q0, expect0)).toBe(true);
|
||||
});
|
||||
|
||||
test('getAxisAngle', () => {
|
||||
const axis = new Vec3();
|
||||
const q0 = new Quat(0, 0, Math.sin(Math.PI / 4), Math.cos(Math.PI / 4));
|
||||
const angle = Quat.getAxisAngle(axis, q0);
|
||||
const expect0 = new Vec3(0, 0, 1);
|
||||
const expect1 = Math.PI / 2;
|
||||
log('getAxisAngle 0: ', axis);
|
||||
log('getAxisAngle 1: ', angle);
|
||||
expect(Vec3.equals(axis, expect0)).toBe(true);
|
||||
expect(angle).toBeCloseTo(expect1);
|
||||
});
|
||||
|
||||
test('multiply', () => {
|
||||
const q0 = new Quat(Math.sin(Math.PI / 4), 0, 0, Math.cos(Math.PI / 4));
|
||||
const expect0 = new Quat(Math.sin(Math.PI / 2), 0, 0, Math.cos(Math.PI / 2));
|
||||
Quat.multiply(q0, q0, q0);
|
||||
log('multiply: ', q0);
|
||||
expect(Quat.equals(q0, expect0)).toBe(true);
|
||||
});
|
||||
|
||||
test('slerp', () => {
|
||||
const q0 = Quat.identity(new Quat());
|
||||
const q1 = new Quat(Math.sin(Math.PI / 4), 0, 0, Math.cos(Math.PI / 4));
|
||||
const expect0 = new Quat(Math.sin(Math.PI / 8), 0, 0, Math.cos(Math.PI / 8));
|
||||
const q2 = Quat.slerp(new Quat(), q0, q1, 0.5);
|
||||
log('slerp: ', q2);
|
||||
expect(Quat.equals(q2, expect0)).toBe(true);
|
||||
});
|
||||
|
||||
test('invert', () => {
|
||||
const q0 = new Quat(Math.sin(Math.PI / 4), 0, 0, Math.cos(Math.PI / 4));
|
||||
const expect0 = new Quat(-Math.sin(Math.PI / 4), 0, 0, Math.cos(-Math.PI / 4));
|
||||
const q1 = Quat.invert(new Quat(), q0);
|
||||
log('invert: ', q1);
|
||||
expect(Quat.equals(q1, expect0)).toBe(true);
|
||||
});
|
||||
|
||||
test('conjugate', () => {
|
||||
const q0 = new Quat(Math.sin(Math.PI / 4), 0, 0, Math.cos(Math.PI / 4));
|
||||
const expect0 = new Quat(-Math.sin(Math.PI / 4), 0, 0, Math.cos(-Math.PI / 4));
|
||||
const q1 = Quat.conjugate(new Quat(), q0);
|
||||
log('conjugate: ', q1);
|
||||
expect(Quat.equals(q1, expect0)).toBe(true);
|
||||
});
|
||||
|
||||
test('normalize', () => {
|
||||
const q0 = new Quat(Math.sin(Math.PI / 4) * 3, 0, 0, Math.cos(Math.PI / 4) * 3);
|
||||
const expect0 = new Quat(Math.sin(Math.PI / 4), 0, 0, Math.cos(Math.PI / 4));
|
||||
const q1 = Quat.normalize(new Quat(), q0);
|
||||
log('normalize: ', q1);
|
||||
expect(Quat.equals(q1, expect0)).toBe(true);
|
||||
});
|
||||
|
||||
test('fromAxes', () => {
|
||||
const x = new Vec3(1, 0, 0);
|
||||
const y = new Vec3(0, 1, 0);
|
||||
const z = new Vec3(0, 0, 1);
|
||||
const q0 = Quat.fromAxes(new Quat(), x, y, z);
|
||||
const x1 = Quat.toAxisX(new Vec3(), q0)
|
||||
const y1 = Quat.toAxisY(new Vec3(), q0)
|
||||
const z1 = Quat.toAxisZ(new Vec3(), q0)
|
||||
log('fromAxes 0: ', x1);
|
||||
log('fromAxes 1: ', y1);
|
||||
log('fromAxes 2: ', z1);
|
||||
expect(Vec3.equals(x, x1)).toBe(true);
|
||||
expect(Vec3.equals(y, y1)).toBe(true);
|
||||
expect(Vec3.equals(z, z1)).toBe(true);
|
||||
});
|
||||
|
||||
test('fromAxisAngle', () => {
|
||||
const axis = new Vec3(1, 0, 0);
|
||||
const angle = Math.PI / 3;
|
||||
const q0 = Quat.fromAxisAngle(new Quat(), axis, angle);
|
||||
const expect0 = new Quat(Math.sin(Math.PI / 6), 0, 0, Math.cos(Math.PI / 6));
|
||||
log('fromAxisAngle: ', q0);
|
||||
expect(Quat.equals(q0, expect0)).toBe(true);
|
||||
});
|
||||
|
||||
test('fromMat3', () => {
|
||||
const mat3 = new Mat3(1, 0, 0,
|
||||
0, 0, -1,
|
||||
0, 1, 0);
|
||||
let quat = new Quat;
|
||||
Quat.fromMat3(quat, mat3);
|
||||
expect(quat).toMatchObject({
|
||||
x: -0.7071067811865475,
|
||||
y: 0,
|
||||
z: 0,
|
||||
w: 0.7071067811865476
|
||||
});
|
||||
const m0 = Mat3.fromRotation(new Mat3(), Math.PI / 3);
|
||||
const q0 = Quat.fromMat3(new Quat(), m0);
|
||||
const expect0 = new Quat(0, 0, Math.sin(Math.PI / 6), Math.cos(Math.PI / 6));
|
||||
log('fromMat3: ', q0);
|
||||
expect(Quat.equals(q0, expect0)).toBe(true);
|
||||
});
|
||||
|
||||
test('fromEuler', () => {
|
||||
const q0 = Quat.fromEuler(new Quat(), 0, 0, 60);
|
||||
const expect0 = new Quat(0, 0, Math.sin(Math.PI / 6), Math.cos(Math.PI / 6));
|
||||
log('fromEuler: ', q0);
|
||||
expect(Quat.equals(q0, expect0)).toBe(true);
|
||||
});
|
||||
|
||||
test('toEuler', () => {
|
||||
const q0 = new Quat(0, 0, Math.sin(Math.PI / 6), Math.cos(Math.PI / 6));
|
||||
const v0 = new Vec3();
|
||||
const expect0 = new Vec3(0, 0, 60);
|
||||
Quat.toEuler(v0, q0);
|
||||
log('toEuler: ', v0);
|
||||
expect(Vec3.equals(v0, expect0)).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,74 @@
|
||||
import { log } from '../../test.log';
|
||||
import { Vec2 } from '../../../cocos/core/math/vec2';
|
||||
import { Vec3 } from '../../../cocos/core/math/vec3';
|
||||
import { Mat3 } from '../../../cocos/core/math/mat3';
|
||||
import { Mat4 } from '../../../cocos/core/math/mat4';
|
||||
|
||||
describe('Test Vec2', () => {
|
||||
test('normalize', () => {
|
||||
const v0 = new Vec2(3, 4);
|
||||
const v1 = new Vec2(3, 4);
|
||||
const expect0 = new Vec2(0.6, 0.8);
|
||||
Vec2.normalize(v0, v0);
|
||||
v1.normalize();
|
||||
log('normalize 0: ', v0);
|
||||
log('normalize 1: ', v1);
|
||||
expect(Vec2.equals(v0, expect0)).toBe(true);
|
||||
expect(Vec2.equals(v1, expect0)).toBe(true);
|
||||
});
|
||||
|
||||
test('dot', () => {
|
||||
const v0 = new Vec2(3, 4);
|
||||
const v1 = new Vec2(-4, 3);
|
||||
const value0 = Vec2.dot(v0, v1);
|
||||
const value1 = v0.dot(v1);
|
||||
const expect0 = 0;
|
||||
const expect1 = 0;
|
||||
log('dot 0: ', value0);
|
||||
log('dot 1: ', value1);
|
||||
expect(value0).toBe(expect0);
|
||||
expect(value1).toBe(expect1);
|
||||
});
|
||||
|
||||
test('transformMat3', () => {
|
||||
const v0 = new Vec2(3, 4);
|
||||
const m0 = Mat3.fromTranslation(new Mat3(), new Vec2(1, 2));
|
||||
const expect0 = new Vec2(4, 6);
|
||||
Vec2.transformMat3(v0, v0, m0);
|
||||
log('transformMat3: ', v0);
|
||||
expect(Vec2.equals(v0, expect0)).toBe(true);
|
||||
});
|
||||
|
||||
test('transformMat4', () => {
|
||||
const v0 = new Vec2(3, 4);
|
||||
const v1 = new Vec2(3, 4);
|
||||
const m0 = Mat4.fromScaling(new Mat4(), new Vec3(1, 2, 3));
|
||||
const expect0 = new Vec2(3, 8);
|
||||
Vec2.transformMat4(v0, v0, m0);
|
||||
v1.transformMat4(m0);
|
||||
log('transformMat4 0: ', v0);
|
||||
log('transformMat4 1: ', v1);
|
||||
expect(Vec2.equals(v0, expect0)).toBe(true);
|
||||
expect(Vec2.equals(v1, expect0)).toBe(true);
|
||||
});
|
||||
|
||||
test('angle', () => {
|
||||
const v0 = new Vec2(3, 4);
|
||||
const v1 = new Vec2(-6, -8);
|
||||
const expect0 = Math.PI;
|
||||
const value0 = Vec2.angle(v0, v1);
|
||||
const value1 = v0.angle(v1);
|
||||
log('angle 0: ', value0);
|
||||
log('angle 1: ', value1);
|
||||
expect(value0).toBe(expect0);
|
||||
expect(value1).toBe(expect0);
|
||||
});
|
||||
|
||||
test('rotate', () => {
|
||||
const v0 = new Vec2(3, 4);
|
||||
const expect0 = new Vec2(-4, 3);
|
||||
v0.rotate(Math.PI / 2);
|
||||
log('rotate: ', v0);
|
||||
expect(Vec2.equals(v0, expect0)).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,163 @@
|
||||
import { log } from '../../test.log';
|
||||
import { Vec3 } from '../../../cocos/core/math/vec3';
|
||||
import { Mat3 } from '../../../cocos/core/math/mat3';
|
||||
import { Mat4 } from '../../../cocos/core/math/mat4';
|
||||
import { Quat } from '../../../cocos/core/math/quat';
|
||||
|
||||
describe('Test Vec3', () => {
|
||||
test('normalize', () => {
|
||||
const v0 = new Vec3(3, 4, 12);
|
||||
const v1 = new Vec3(3, 4, 12);
|
||||
const expect0 = new Vec3(0.2307692, 0.3076923, 0.9230769);
|
||||
Vec3.normalize(v0, v0);
|
||||
v1.normalize();
|
||||
log('normalize 0: ', v0);
|
||||
log('normalize 1: ', v1);
|
||||
expect(Vec3.equals(v0, expect0)).toBe(true);
|
||||
expect(Vec3.equals(v1, expect0)).toBe(true);
|
||||
});
|
||||
|
||||
test('dot', () => {
|
||||
const v0 = new Vec3(3, 4, 0);
|
||||
const v1 = new Vec3(-4, 3, 0);
|
||||
const value0 = Vec3.dot(v0, v1);
|
||||
const value1 = v0.dot(v1);
|
||||
const expect0 = 0;
|
||||
const expect1 = 0;
|
||||
log('dot 0: ', value0);
|
||||
log('dot 1: ', value1);
|
||||
expect(value0).toBe(expect0);
|
||||
expect(value1).toBe(expect1);
|
||||
});
|
||||
|
||||
test('cross', () => {
|
||||
const v0 = new Vec3(3, 4, 0);
|
||||
const v1 = new Vec3(3, 4, 0);
|
||||
const value0 = Vec3.cross(new Vec3(), v0, v1);
|
||||
const value1 = v0.cross(v1);
|
||||
const expect0 = new Vec3(0, 0, 0);
|
||||
log('cross 0: ', value0);
|
||||
log('cross 1: ', value1);
|
||||
expect(Vec3.equals(value0, expect0)).toBe(true);
|
||||
expect(Vec3.equals(value1, expect0)).toBe(true);
|
||||
});
|
||||
|
||||
test('transformMat4', () => {
|
||||
const v0 = new Vec3(3, 4, 5);
|
||||
const v1 = new Vec3(3, 4, 5);
|
||||
const m0 = Mat4.fromScaling(new Mat4(), new Vec3(1, 2, 3));
|
||||
const m1 = Mat4.fromTranslation(new Mat4(), new Vec3(1, 2, 3));
|
||||
Mat4.multiply(m0, m1, m0);
|
||||
const expect0 = new Vec3(4, 10, 18);
|
||||
Vec3.transformMat4(v0, v0, m0);
|
||||
v1.transformMat4(m0);
|
||||
log('transformMat4 0: ', v0);
|
||||
log('transformMat4 1: ', v1);
|
||||
expect(Vec3.equals(v0, expect0)).toBe(true);
|
||||
expect(Vec3.equals(v1, expect0)).toBe(true);
|
||||
});
|
||||
|
||||
test('transformMat4Normal', () => {
|
||||
const v0 = new Vec3(3, 4, 5);
|
||||
const m0 = Mat4.fromScaling(new Mat4(), new Vec3(1, 2, 3));
|
||||
const expect0 = new Vec3(3, 8, 15);
|
||||
Vec3.transformMat4Normal(v0, v0, m0);
|
||||
log('transformMat4Normal: ', v0);
|
||||
expect(Vec3.equals(v0, expect0)).toBe(true);
|
||||
});
|
||||
|
||||
test('transformMat3', () => {
|
||||
const v0 = new Vec3(3, 4, 5);
|
||||
const m0 = new Mat3();
|
||||
m0.m00 = 1;
|
||||
m0.m04 = 2;
|
||||
m0.m08 = 3;
|
||||
const expect0 = new Vec3(3, 8, 15);
|
||||
Vec3.transformMat3(v0, v0, m0);
|
||||
log('transformMat3: ', v0);
|
||||
expect(Vec3.equals(v0, expect0)).toBe(true);
|
||||
});
|
||||
|
||||
test('transformAffine', () => {
|
||||
const v0 = new Vec3(3, 4, 5);
|
||||
const m0 = Mat4.fromScaling(new Mat4(), new Vec3(1, 2, 3));
|
||||
const expect0 = new Vec3(3, 8, 15);
|
||||
Vec3.transformAffine(v0, v0, m0);
|
||||
log('transformAffine: ', v0);
|
||||
expect(Vec3.equals(v0, expect0)).toBe(true);
|
||||
});
|
||||
|
||||
test('transformQuat', () => {
|
||||
const v0 = new Vec3(3, 4, 5);
|
||||
const q0 = Quat.fromAxisAngle(new Quat(), new Vec3(0, 0, 1), Math.PI / 2);
|
||||
const expect0 = new Vec3(-4, 3, 5);
|
||||
Vec3.transformQuat(v0, v0, q0);
|
||||
log('transformQuat: ', v0);
|
||||
expect(Vec3.equals(v0, expect0)).toBe(true);
|
||||
});
|
||||
|
||||
test('transformRTS', () => {
|
||||
const v0 = new Vec3(1, 1, 1);
|
||||
const s0 = new Vec3(2, 3, 4);
|
||||
const t0 = new Vec3(1, 2, 3);
|
||||
const q0 = Quat.fromAxisAngle(new Quat(), new Vec3(0, 0, 1), Math.PI / 2);
|
||||
const expect0 = new Vec3(-2, 4, 7);
|
||||
Vec3.transformRTS(v0, v0, q0, t0, s0);
|
||||
log('transformRTS: ', v0);
|
||||
expect(Vec3.equals(v0, expect0)).toBe(true);
|
||||
});
|
||||
|
||||
test('transformInverseRTS', () => {
|
||||
const v0 = new Vec3(-2, 4, 7);
|
||||
const s0 = new Vec3(2, 3, 4);
|
||||
const t0 = new Vec3(1, 2, 3);
|
||||
const q0 = Quat.fromAxisAngle(new Quat(), new Vec3(0, 0, 1), Math.PI / 2);
|
||||
const expect0 = new Vec3(1, 1, 1);
|
||||
Vec3.transformInverseRTS(v0, v0, q0, t0, s0);
|
||||
log('transformInverseRTS: ', v0);
|
||||
expect(Vec3.equals(v0, expect0)).toBe(true);
|
||||
});
|
||||
|
||||
test('rotateX', () => {
|
||||
const v0 = new Vec3(1, 2, 3);
|
||||
const expect0 = new Vec3(1, -3, 2);
|
||||
Vec3.rotateX(v0, v0, Vec3.ZERO, Math.PI / 2);
|
||||
log('rotateX: ', v0);
|
||||
expect(Vec3.equals(v0, expect0)).toBe(true);
|
||||
});
|
||||
|
||||
test('rotateY', () => {
|
||||
const v0 = new Vec3(1, 2, 3);
|
||||
const expect0 = new Vec3(3, 2, -1);
|
||||
Vec3.rotateY(v0, v0, Vec3.ZERO, Math.PI / 2);
|
||||
log('rotateY: ', v0);
|
||||
expect(Vec3.equals(v0, expect0)).toBe(true);
|
||||
});
|
||||
|
||||
test('rotateZ', () => {
|
||||
const v0 = new Vec3(1, 2, 3);
|
||||
const expect0 = new Vec3(-2, 1, 3);
|
||||
Vec3.rotateZ(v0, v0, Vec3.ZERO, Math.PI / 2);
|
||||
log('rotateZ: ', v0);
|
||||
expect(Vec3.equals(v0, expect0)).toBe(true);
|
||||
});
|
||||
|
||||
test('rotateN', () => {
|
||||
const v0 = new Vec3(1, 1, 0);
|
||||
const axis = new Vec3(-1, 1, 0);
|
||||
axis.normalize();
|
||||
const expect0 = new Vec3(-1, -1, 0);
|
||||
Vec3.rotateN(v0, v0, Vec3.ZERO, axis, Math.PI);
|
||||
log('rotateN: ', v0);
|
||||
expect(Vec3.equals(v0, expect0)).toBe(true);
|
||||
});
|
||||
|
||||
test('angle', () => {
|
||||
const v0 = new Vec3(3, 4, 0);
|
||||
const v1 = new Vec3(-6, -8, 0);
|
||||
const expect0 = Math.PI;
|
||||
const value0 = Vec3.angle(v0, v1);
|
||||
log('angle: ', value0);
|
||||
expect(value0).toBe(expect0);
|
||||
});
|
||||
});
|
||||
@@ -1,21 +0,0 @@
|
||||
import { Vec2 } from '../../../cocos/core/math/vec2';
|
||||
import { Vec3 } from '../../../cocos/core/math/vec3';
|
||||
|
||||
// test Vec2
|
||||
describe('Test Vec2', () => {
|
||||
test('cross', () => {
|
||||
// cross and prototype.cross
|
||||
const v2_0 = new Vec2(1, 0);
|
||||
const v2_1 = new Vec2(0, 1);
|
||||
const result = v2_0.x * v2_1.y - v2_0.y * v2_1.x;
|
||||
expect(Vec2.cross(v2_0, v2_1)).toBe(result);
|
||||
expect(v2_0.cross(v2_1)).toBe(result);
|
||||
|
||||
const v3_0 = new Vec3(1, 1, 1);
|
||||
const out = Vec2.cross(v3_0, v2_0, v2_1);
|
||||
expect(out).toBe(v3_0);
|
||||
expect(v3_0.x).toBe(0);
|
||||
expect(v3_0.y).toBe(0);
|
||||
expect(v3_0.z).toBe(result);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,7 @@
|
||||
const logEnabled = false;
|
||||
|
||||
export function log(message?: any, ...optionalParams: any[]): void {
|
||||
if (logEnabled) {
|
||||
console.log(message, ...optionalParams);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user