Files
esengine/packages/math/src/Vector3.ts

441 lines
10 KiB
TypeScript
Raw Normal View History

2025-09-26 17:45:52 +08:00
/**
* 3D向量类
*
* 3D向量运算功能
* -
* -
* -
* -
*/
export class Vector3 {
/** X分量 */
public x: number;
2025-09-26 17:45:52 +08:00
/** Y分量 */
public y: number;
2025-09-26 17:45:52 +08:00
/** Z分量 */
public z: number;
2025-09-26 17:45:52 +08:00
/**
2025-09-26 17:45:52 +08:00
* 3D向量
* @param x X分量0
* @param y Y分量0
* @param z Z分量0
*/
constructor(x: number = 0, y: number = 0, z: number = 0) {
this.x = x;
this.y = y;
this.z = z;
}
2025-09-26 17:45:52 +08:00
// 静态常量
/** 零向量 (0, 0, 0) */
static readonly ZERO = new Vector3(0, 0, 0);
2025-09-26 17:45:52 +08:00
/** 单位向量 (1, 1, 1) */
static readonly ONE = new Vector3(1, 1, 1);
2025-09-26 17:45:52 +08:00
/** 右方向向量 (1, 0, 0) */
static readonly RIGHT = new Vector3(1, 0, 0);
2025-09-26 17:45:52 +08:00
/** 左方向向量 (-1, 0, 0) */
static readonly LEFT = new Vector3(-1, 0, 0);
2025-09-26 17:45:52 +08:00
/** 上方向向量 (0, 1, 0) */
static readonly UP = new Vector3(0, 1, 0);
2025-09-26 17:45:52 +08:00
/** 下方向向量 (0, -1, 0) */
static readonly DOWN = new Vector3(0, -1, 0);
2025-09-26 17:45:52 +08:00
/** 前方向向量 (0, 0, 1) */
static readonly FORWARD = new Vector3(0, 0, 1);
2025-09-26 17:45:52 +08:00
/** 后方向向量 (0, 0, -1) */
static readonly BACK = new Vector3(0, 0, -1);
2025-09-26 17:45:52 +08:00
// 基础属性
2025-09-26 17:45:52 +08:00
/**
2025-09-26 17:45:52 +08:00
*
*/
get length(): number {
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
}
2025-09-26 17:45:52 +08:00
/**
2025-09-26 17:45:52 +08:00
*
*/
get lengthSquared(): number {
return this.x * this.x + this.y * this.y + this.z * this.z;
}
2025-09-26 17:45:52 +08:00
/**
2025-09-26 17:45:52 +08:00
*
*/
get isZero(): boolean {
return this.x === 0 && this.y === 0 && this.z === 0;
}
2025-09-26 17:45:52 +08:00
/**
2025-09-26 17:45:52 +08:00
*
*/
get isUnit(): boolean {
const lenSq = this.lengthSquared;
return Math.abs(lenSq - 1) < Number.EPSILON;
}
2025-09-26 17:45:52 +08:00
// 基础运算
2025-09-26 17:45:52 +08:00
/**
2025-09-26 17:45:52 +08:00
*
* @param x X分量
* @param y Y分量
* @param z Z分量
* @returns
*/
set(x: number, y: number, z: number): this {
this.x = x;
this.y = y;
this.z = z;
return this;
}
2025-09-26 17:45:52 +08:00
/**
2025-09-26 17:45:52 +08:00
*
* @param other
* @returns
*/
copy(other: Vector3): this {
this.x = other.x;
this.y = other.y;
this.z = other.z;
return this;
}
2025-09-26 17:45:52 +08:00
/**
2025-09-26 17:45:52 +08:00
*
* @returns
*/
clone(): Vector3 {
return new Vector3(this.x, this.y, this.z);
}
2025-09-26 17:45:52 +08:00
/**
2025-09-26 17:45:52 +08:00
*
* @param other
* @returns
*/
add(other: Vector3): this {
this.x += other.x;
this.y += other.y;
this.z += other.z;
return this;
}
2025-09-26 17:45:52 +08:00
/**
2025-09-26 17:45:52 +08:00
*
* @param other
* @returns
*/
subtract(other: Vector3): this {
this.x -= other.x;
this.y -= other.y;
this.z -= other.z;
return this;
}
2025-09-26 17:45:52 +08:00
/**
2025-09-26 17:45:52 +08:00
*
* @param scalar
* @returns
*/
multiply(scalar: number): this {
this.x *= scalar;
this.y *= scalar;
this.z *= scalar;
return this;
}
2025-09-26 17:45:52 +08:00
/**
2025-09-26 17:45:52 +08:00
*
* @param scalar
* @returns
*/
divide(scalar: number): this {
if (scalar === 0) {
throw new Error('不能除以零');
}
this.x /= scalar;
this.y /= scalar;
this.z /= scalar;
return this;
2025-09-26 17:45:52 +08:00
}
/**
2025-09-26 17:45:52 +08:00
*
* @returns
*/
negate(): this {
this.x = -this.x;
this.y = -this.y;
this.z = -this.z;
return this;
}
2025-09-26 17:45:52 +08:00
// 向量运算
2025-09-26 17:45:52 +08:00
/**
2025-09-26 17:45:52 +08:00
*
* @param other
* @returns
*/
dot(other: Vector3): number {
return this.x * other.x + this.y * other.y + this.z * other.z;
}
2025-09-26 17:45:52 +08:00
/**
2025-09-26 17:45:52 +08:00
*
* @param other
* @returns
*/
cross(other: Vector3): Vector3 {
return new Vector3(
this.y * other.z - this.z * other.y,
this.z * other.x - this.x * other.z,
this.x * other.y - this.y * other.x
);
}
2025-09-26 17:45:52 +08:00
/**
2025-09-26 17:45:52 +08:00
*
* @returns
*/
normalize(): this {
const len = this.length;
if (len === 0) {
return this;
}
return this.divide(len);
2025-09-26 17:45:52 +08:00
}
/**
2025-09-26 17:45:52 +08:00
*
* @returns
*/
normalized(): Vector3 {
return this.clone().normalize();
}
2025-09-26 17:45:52 +08:00
// 几何运算
2025-09-26 17:45:52 +08:00
/**
2025-09-26 17:45:52 +08:00
*
* @param other
* @returns
*/
distanceTo(other: Vector3): number {
const dx = this.x - other.x;
const dy = this.y - other.y;
const dz = this.z - other.z;
return Math.sqrt(dx * dx + dy * dy + dz * dz);
}
2025-09-26 17:45:52 +08:00
/**
2025-09-26 17:45:52 +08:00
*
* @param other
* @returns
*/
distanceToSquared(other: Vector3): number {
const dx = this.x - other.x;
const dy = this.y - other.y;
const dz = this.z - other.z;
return dx * dx + dy * dy + dz * dz;
}
2025-09-26 17:45:52 +08:00
/**
2025-09-26 17:45:52 +08:00
*
* @param other
* @returns 0π
*/
angleTo(other: Vector3): number {
const dot = this.dot(other);
const lenProduct = this.length * other.length;
if (lenProduct === 0) return 0;
return Math.acos(Math.max(-1, Math.min(1, dot / lenProduct)));
}
2025-09-26 17:45:52 +08:00
/**
2025-09-26 17:45:52 +08:00
*
* @param onto
* @returns
*/
projectOnto(onto: Vector3): Vector3 {
const dot = this.dot(onto);
const lenSq = onto.lengthSquared;
if (lenSq === 0) return new Vector3();
return onto.clone().multiply(dot / lenSq);
}
2025-09-26 17:45:52 +08:00
// 插值和限制
2025-09-26 17:45:52 +08:00
/**
2025-09-26 17:45:52 +08:00
* 线
* @param target
* @param t 01
* @returns
*/
lerp(target: Vector3, t: number): this {
this.x += (target.x - this.x) * t;
this.y += (target.y - this.y) * t;
this.z += (target.z - this.z) * t;
return this;
}
2025-09-26 17:45:52 +08:00
/**
2025-09-26 17:45:52 +08:00
*
* @param maxLength
* @returns
*/
clampLength(maxLength: number): this {
const lenSq = this.lengthSquared;
if (lenSq > maxLength * maxLength) {
return this.normalize().multiply(maxLength);
}
return this;
2025-09-26 17:45:52 +08:00
}
// 比较操作
2025-09-26 17:45:52 +08:00
/**
2025-09-26 17:45:52 +08:00
*
* @param other
* @param epsilon Number.EPSILON
* @returns
*/
equals(other: Vector3, epsilon: number = Number.EPSILON): boolean {
return Math.abs(this.x - other.x) < epsilon &&
2025-09-26 17:45:52 +08:00
Math.abs(this.y - other.y) < epsilon &&
Math.abs(this.z - other.z) < epsilon;
}
2025-09-26 17:45:52 +08:00
/**
2025-09-26 17:45:52 +08:00
*
* @param other
* @returns
*/
exactEquals(other: Vector3): boolean {
return this.x === other.x && this.y === other.y && this.z === other.z;
}
2025-09-26 17:45:52 +08:00
// 静态方法
2025-09-26 17:45:52 +08:00
/**
2025-09-26 17:45:52 +08:00
*
* @param a a
* @param b b
* @returns
*/
static add(a: Vector3, b: Vector3): Vector3 {
return new Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
}
2025-09-26 17:45:52 +08:00
/**
2025-09-26 17:45:52 +08:00
*
* @param a a
* @param b b
* @returns
*/
static subtract(a: Vector3, b: Vector3): Vector3 {
return new Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
}
2025-09-26 17:45:52 +08:00
/**
2025-09-26 17:45:52 +08:00
*
* @param vector
* @param scalar
* @returns
*/
static multiply(vector: Vector3, scalar: number): Vector3 {
return new Vector3(vector.x * scalar, vector.y * scalar, vector.z * scalar);
}
2025-09-26 17:45:52 +08:00
/**
2025-09-26 17:45:52 +08:00
*
* @param a a
* @param b b
* @returns
*/
static dot(a: Vector3, b: Vector3): number {
return a.x * b.x + a.y * b.y + a.z * b.z;
}
2025-09-26 17:45:52 +08:00
/**
2025-09-26 17:45:52 +08:00
*
* @param a a
* @param b b
* @returns
*/
static cross(a: Vector3, b: Vector3): Vector3 {
return new Vector3(
a.y * b.z - a.z * b.y,
a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.x
);
}
2025-09-26 17:45:52 +08:00
/**
2025-09-26 17:45:52 +08:00
*
* @param a a
* @param b b
* @returns
*/
static distance(a: Vector3, b: Vector3): number {
const dx = a.x - b.x;
const dy = a.y - b.y;
const dz = a.z - b.z;
return Math.sqrt(dx * dx + dy * dy + dz * dz);
}
2025-09-26 17:45:52 +08:00
/**
2025-09-26 17:45:52 +08:00
* 线
* @param a
* @param b
* @param t 01
* @returns
*/
static lerp(a: Vector3, b: Vector3, t: number): Vector3 {
return new Vector3(
a.x + (b.x - a.x) * t,
a.y + (b.y - a.y) * t,
a.z + (b.z - a.z) * t
);
}
2025-09-26 17:45:52 +08:00
// 字符串转换
2025-09-26 17:45:52 +08:00
/**
2025-09-26 17:45:52 +08:00
*
* @returns
*/
toString(): string {
return `Vector3(${this.x.toFixed(3)}, ${this.y.toFixed(3)}, ${this.z.toFixed(3)})`;
}
2025-09-26 17:45:52 +08:00
/**
2025-09-26 17:45:52 +08:00
*
* @returns [x, y, z]
*/
toArray(): [number, number, number] {
return [this.x, this.y, this.z];
}
2025-09-26 17:45:52 +08:00
/**
2025-09-26 17:45:52 +08:00
*
* @returns {x, y, z}
*/
toObject(): { x: number; y: number; z: number } {
return { x: this.x, y: this.y, z: this.z };
}
}