|
@ -1,6 +1,6 @@ |
|
|
import { EPSILON } from "../common"; |
|
|
import { EPSILON } from "../common"; |
|
|
|
|
|
|
|
|
export class Vec3 { |
|
|
|
|
|
|
|
|
export class vec3 { |
|
|
/** |
|
|
/** |
|
|
* @var |
|
|
* @var |
|
|
*/ |
|
|
*/ |
|
@ -50,8 +50,8 @@ export class Vec3 { |
|
|
this.z = z; |
|
|
this.z = z; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public copy(dest: Vec3 | null = null): Vec3 { |
|
|
|
|
|
if (!dest) dest = new Vec3(); |
|
|
|
|
|
|
|
|
public copy(dest: vec3 | null = null): vec3 { |
|
|
|
|
|
if (!dest) dest = new vec3(); |
|
|
|
|
|
|
|
|
dest.x = this.x; |
|
|
dest.x = this.x; |
|
|
dest.y = this.y; |
|
|
dest.y = this.y; |
|
@ -65,7 +65,7 @@ export class Vec3 { |
|
|
* @param dest |
|
|
* @param dest |
|
|
* @returns |
|
|
* @returns |
|
|
*/ |
|
|
*/ |
|
|
public negate(dest: Vec3 | null = null): Vec3 { |
|
|
|
|
|
|
|
|
public negate(dest: vec3 | null = null): vec3 { |
|
|
if (!dest) dest = this; |
|
|
if (!dest) dest = this; |
|
|
|
|
|
|
|
|
dest.x = -this.x; |
|
|
dest.x = -this.x; |
|
@ -81,7 +81,7 @@ export class Vec3 { |
|
|
* @param threshold |
|
|
* @param threshold |
|
|
* @returns |
|
|
* @returns |
|
|
*/ |
|
|
*/ |
|
|
public equals(vector: Vec3, threshold = EPSILON): boolean { |
|
|
|
|
|
|
|
|
public equals(vector: vec3, threshold = EPSILON): boolean { |
|
|
if (Math.abs(this.x - vector.x) > threshold) |
|
|
if (Math.abs(this.x - vector.x) > threshold) |
|
|
return false; |
|
|
return false; |
|
|
|
|
|
|
|
@ -117,7 +117,7 @@ export class Vec3 { |
|
|
* @param vector |
|
|
* @param vector |
|
|
* @returns |
|
|
* @returns |
|
|
*/ |
|
|
*/ |
|
|
add(vector: Vec3): Vec3 { |
|
|
|
|
|
|
|
|
add(vector: vec3): vec3 { |
|
|
this.x += vector.x; |
|
|
this.x += vector.x; |
|
|
this.y += vector.y; |
|
|
this.y += vector.y; |
|
|
this.z += vector.z; |
|
|
this.z += vector.z; |
|
@ -130,7 +130,7 @@ export class Vec3 { |
|
|
* @param vector |
|
|
* @param vector |
|
|
* @returns |
|
|
* @returns |
|
|
*/ |
|
|
*/ |
|
|
subtract(vector: Vec3): Vec3 { |
|
|
|
|
|
|
|
|
subtract(vector: vec3): vec3 { |
|
|
this.x -= vector.x; |
|
|
this.x -= vector.x; |
|
|
this.y -= vector.y; |
|
|
this.y -= vector.y; |
|
|
this.z -= vector.z; |
|
|
this.z -= vector.z; |
|
@ -145,7 +145,7 @@ export class Vec3 { |
|
|
* @param dest |
|
|
* @param dest |
|
|
* @returns |
|
|
* @returns |
|
|
*/ |
|
|
*/ |
|
|
public scale(value: number, dest: Vec3 | null = null): Vec3 { |
|
|
|
|
|
|
|
|
public scale(value: number, dest: vec3 | null = null): vec3 { |
|
|
if (!dest) { |
|
|
if (!dest) { |
|
|
dest = this; |
|
|
dest = this; |
|
|
} else { |
|
|
} else { |
|
@ -158,4 +158,138 @@ export class Vec3 { |
|
|
|
|
|
|
|
|
return dest; |
|
|
return dest; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 向量单位化 |
|
|
|
|
|
* @param dest |
|
|
|
|
|
* @returns |
|
|
|
|
|
*/ |
|
|
|
|
|
public normalize(dest: vec3 | null = null): vec3 { |
|
|
|
|
|
if (!dest) { |
|
|
|
|
|
dest = this; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let length = this.length; |
|
|
|
|
|
|
|
|
|
|
|
if (length === 1) { |
|
|
|
|
|
return this; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (length === 0) { |
|
|
|
|
|
dest.x = 0 |
|
|
|
|
|
dest.y = 0; |
|
|
|
|
|
dest.z = 0; |
|
|
|
|
|
|
|
|
|
|
|
return dest; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
dest.x /= length; |
|
|
|
|
|
dest.y /= length; |
|
|
|
|
|
dest.z /= length; |
|
|
|
|
|
|
|
|
|
|
|
return dest; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 向量单位化 |
|
|
|
|
|
* @returns |
|
|
|
|
|
*/ |
|
|
|
|
|
public normalize2(): number { |
|
|
|
|
|
let length = this.length; |
|
|
|
|
|
this.x /= length; |
|
|
|
|
|
this.y /= length; |
|
|
|
|
|
this.z /= length; |
|
|
|
|
|
return length; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 向量缩放 |
|
|
|
|
|
* @param vector |
|
|
|
|
|
* @param value |
|
|
|
|
|
* @param dest |
|
|
|
|
|
* @returns |
|
|
|
|
|
*/ |
|
|
|
|
|
public static multiplyScalar(vector: vec3, value: number, dest: vec3 | null = null): vec3 { |
|
|
|
|
|
if (!dest) dest = new vec3(); |
|
|
|
|
|
dest.x *= value; |
|
|
|
|
|
dest.y *= value; |
|
|
|
|
|
dest.z *= value; |
|
|
|
|
|
return dest; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 向量叉乘 |
|
|
|
|
|
* @param vector |
|
|
|
|
|
* @param vector2 |
|
|
|
|
|
* @param dest |
|
|
|
|
|
* @returns |
|
|
|
|
|
*/ |
|
|
|
|
|
public static cross(vector: vec3, vector2: vec3, dest: vec3 | null = null): vec3 { |
|
|
|
|
|
if (!dest) dest = new vec3(); |
|
|
|
|
|
|
|
|
|
|
|
let x = vector.x, |
|
|
|
|
|
y = vector.y, |
|
|
|
|
|
z = vector.z; |
|
|
|
|
|
|
|
|
|
|
|
let x2 = vector2.x, |
|
|
|
|
|
y2 = vector2.y, |
|
|
|
|
|
z2 = vector2.z; |
|
|
|
|
|
|
|
|
|
|
|
dest.x = y * z2 - z * y2; |
|
|
|
|
|
dest.y = z * x2 - x * z2; |
|
|
|
|
|
dest.z = x * y2 - y * x2; |
|
|
|
|
|
|
|
|
|
|
|
return dest; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 向量点乘 |
|
|
|
|
|
* @param vector |
|
|
|
|
|
* @param vector2 |
|
|
|
|
|
* @returns |
|
|
|
|
|
*/ |
|
|
|
|
|
public static dot(vector: vec3, vector2: vec3): number { |
|
|
|
|
|
let x = vector.x, |
|
|
|
|
|
y = vector.y, |
|
|
|
|
|
z = vector.z; |
|
|
|
|
|
|
|
|
|
|
|
let x2 = vector2.x, |
|
|
|
|
|
y2 = vector2.y, |
|
|
|
|
|
z2 = vector2.z; |
|
|
|
|
|
|
|
|
|
|
|
return (x * x2 + y * y2 + z * z2); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static sum(vector: vec3, vector2: vec3, dest: vec3 | null = null): vec3 { |
|
|
|
|
|
if (!dest) dest = new vec3(); |
|
|
|
|
|
|
|
|
|
|
|
dest.x = vector.x + vector2.x; |
|
|
|
|
|
dest.y = vector.y + vector2.y; |
|
|
|
|
|
dest.z = vector.z + vector2.z; |
|
|
|
|
|
|
|
|
|
|
|
return dest; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static difference(vector: vec3, vector2: vec3, dest: vec3 | null = null): vec3 { |
|
|
|
|
|
if (!dest) dest = new vec3(); |
|
|
|
|
|
|
|
|
|
|
|
dest.x = vector.x - vector2.x; |
|
|
|
|
|
dest.y = vector.y - vector2.y; |
|
|
|
|
|
dest.z = vector.z - vector2.z; |
|
|
|
|
|
|
|
|
|
|
|
return dest; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static readonly up = new vec3([0, 1, 0]); |
|
|
|
|
|
static readonly down = new vec3([0, -1, 0]); |
|
|
|
|
|
static readonly right = new vec3([1, 0, 0]); |
|
|
|
|
|
static readonly left = new vec3([-1, 0, 0]); |
|
|
|
|
|
static readonly forward = new vec3([0, 0, 1]); |
|
|
|
|
|
static readonly backward = new vec3([0, 0, -1]); |
|
|
|
|
|
|
|
|
|
|
|
static readonly zero = new vec3([0, 0, 0]); |
|
|
|
|
|
|
|
|
|
|
|
static v0 = new vec3([0, 0, 0]); |
|
|
|
|
|
static v1 = new vec3([0, 0, 0]); |
|
|
|
|
|
static v2 = new vec3([0, 0, 0]); |
|
|
} |
|
|
} |