Files
node-metaverse/lib/tsm/vec4.ts

487 lines
9.1 KiB
TypeScript
Raw Normal View History

/**
* @fileoverview TSM - A TypeScript vector and matrix math library
* @author Matthias Ferch
* @version 0.6
*/
/*
* Copyright (c) 2012 Matthias Ferch
*
* Project homepage: https://github.com/matthiasferch/tsm
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not
* be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*/
///<reference path='./common.ts' />
2017-11-24 01:00:56 +00:00
import {mat4} from './mat4';
2017-11-24 01:00:56 +00:00
export class vec4
{
2017-11-24 01:00:56 +00:00
static zero = new vec4([0, 0, 0, 1]);
2017-11-24 01:00:56 +00:00
private values = new Float32Array(4);
static mix(vector: vec4, vector2: vec4, time: number, dest: vec4 | null = null): vec4
{
2017-11-24 01:00:56 +00:00
if (!dest)
{
dest = new vec4();
}
2017-11-24 01:00:56 +00:00
dest.x = vector.x + time * (vector2.x - vector.x);
dest.y = vector.y + time * (vector2.y - vector.y);
dest.z = vector.z + time * (vector2.z - vector.z);
dest.w = vector.w + time * (vector2.w - vector.w);
2017-11-24 01:00:56 +00:00
return dest;
}
static sum(vector: vec4, vector2: vec4, dest: vec4 | null = null): vec4
{
if (!dest)
{
2017-11-24 01:00:56 +00:00
dest = new vec4();
}
2017-11-24 01:00:56 +00:00
dest.x = vector.x + vector2.x,
dest.y = vector.y + vector2.y,
dest.z = vector.z + vector2.z,
dest.w = vector.w + vector2.w
2017-11-24 01:00:56 +00:00
return dest;
}
2017-11-24 01:00:56 +00:00
static difference(vector: vec4, vector2: vec4, dest: vec4 | null = null): vec4
{
if (!dest)
{
2017-11-24 01:00:56 +00:00
dest = new vec4();
}
2017-11-24 01:00:56 +00:00
dest.x = vector.x - vector2.x,
dest.y = vector.y - vector2.y,
dest.z = vector.z - vector2.z,
dest.w = vector.w - vector2.w
2017-11-24 01:00:56 +00:00
return dest;
}
2017-11-24 01:00:56 +00:00
static product(vector: vec4, vector2: vec4, dest: vec4 | null = null): vec4
{
if (!dest)
{
2017-11-24 01:00:56 +00:00
dest = new vec4();
}
2017-11-24 01:00:56 +00:00
dest.x = vector.x * vector2.x,
dest.y = vector.y * vector2.y,
dest.z = vector.z * vector2.z,
dest.w = vector.w * vector2.w
2017-11-24 01:00:56 +00:00
return dest;
}
2017-11-24 01:00:56 +00:00
static quotient(vector: vec4, vector2: vec4, dest: vec4 | null = null): vec4
{
if (!dest)
{
2017-11-24 01:00:56 +00:00
dest = new vec4();
}
2017-11-24 01:00:56 +00:00
dest.x = vector.x / vector2.x,
dest.y = vector.y / vector2.y,
dest.z = vector.z / vector2.z,
dest.w = vector.w / vector2.w
2017-11-24 01:00:56 +00:00
return dest;
}
2017-11-24 01:00:56 +00:00
get x(): number
{
return this.values[0];
}
2017-11-24 01:00:56 +00:00
get y(): number
{
return this.values[1];
}
2017-11-24 01:00:56 +00:00
get z(): number
{
return this.values[2];
}
2017-11-24 01:00:56 +00:00
get w(): number
{
return this.values[3];
}
2017-11-24 01:00:56 +00:00
get xy(): number[]
{
return [
this.values[0],
this.values[1]
];
}
2017-11-24 01:00:56 +00:00
get xyz(): number[]
{
return [
this.values[0],
this.values[1],
this.values[2]
];
}
2017-11-24 01:00:56 +00:00
get xyzw(): number[]
{
return [
this.values[0],
this.values[1],
this.values[2],
this.values[3]
];
}
2017-11-24 01:00:56 +00:00
set x(value: number)
{
this.values[0] = value;
}
2017-11-24 01:00:56 +00:00
set y(value: number)
{
this.values[1] = value;
}
2017-11-24 01:00:56 +00:00
set z(value: number)
{
this.values[2] = value;
}
2017-11-24 01:00:56 +00:00
set w(value: number)
{
this.values[3] = value;
}
2017-11-24 01:00:56 +00:00
set xy(values: number[])
{
this.values[0] = values[0];
this.values[1] = values[1];
}
2017-11-24 01:00:56 +00:00
set xyz(values: number[])
{
this.values[0] = values[0];
this.values[1] = values[1];
this.values[2] = values[2];
}
2017-11-24 01:00:56 +00:00
set xyzw(values: number[])
{
this.values[0] = values[0];
this.values[1] = values[1];
this.values[2] = values[2];
this.values[3] = values[3];
}
2017-11-24 01:00:56 +00:00
get r(): number
{
return this.values[0];
}
2017-11-24 01:00:56 +00:00
get g(): number
{
return this.values[1];
}
2017-11-24 01:00:56 +00:00
get b(): number
{
return this.values[2];
}
2017-11-24 01:00:56 +00:00
get a(): number
{
return this.values[3];
}
2017-11-24 01:00:56 +00:00
get rg(): number[]
{
return [
this.values[0],
this.values[1]
];
}
2017-11-24 01:00:56 +00:00
get rgb(): number[]
{
return [
this.values[0],
this.values[1],
this.values[2]
];
}
2017-11-24 01:00:56 +00:00
get rgba(): number[]
{
return [
this.values[0],
this.values[1],
this.values[2],
this.values[3]
];
}
2017-11-24 01:00:56 +00:00
set r(value: number)
{
this.values[0] = value;
}
2017-11-24 01:00:56 +00:00
set g(value: number)
{
this.values[1] = value;
}
2017-11-24 01:00:56 +00:00
set b(value: number)
{
this.values[2] = value;
}
2017-11-24 01:00:56 +00:00
set a(value: number)
{
this.values[3] = value;
}
2017-11-24 01:00:56 +00:00
set rg(values: number[])
{
this.values[0] = values[0];
this.values[1] = values[1];
}
2017-11-24 01:00:56 +00:00
set rgb(values: number[])
{
this.values[0] = values[0];
this.values[1] = values[1];
this.values[2] = values[2];
}
2017-11-24 01:00:56 +00:00
set rgba(values: number[])
{
this.values[0] = values[0];
this.values[1] = values[1];
this.values[2] = values[2];
this.values[3] = values[3];
}
constructor(values: number[] | null = null)
{
if (values)
{
2017-11-24 01:00:56 +00:00
this.xyzw = values;
}
2017-11-24 01:00:56 +00:00
}
at(index: number): number
{
return this.values[index];
}
reset(): void
{
this.x = 0;
this.y = 0;
this.z = 0;
this.w = 0;
}
2017-11-24 01:00:56 +00:00
copy(dest: vec4 | null = null): vec4
{
if (!dest)
{
2017-11-24 01:00:56 +00:00
dest = new vec4();
}
2017-11-24 01:00:56 +00:00
dest.x = this.x;
dest.y = this.y;
dest.z = this.z;
dest.w = this.w;
return dest;
}
negate(dest: vec4 | null = null): vec4
{
if (!dest)
{
2017-11-24 01:00:56 +00:00
dest = this;
}
2017-11-24 01:00:56 +00:00
dest.x = -this.x;
dest.y = -this.y;
dest.z = -this.z;
dest.w = -this.w;
return dest;
}
equals(vector: vec4, threshold = EPSILON): boolean
{
if (Math.abs(this.x - vector.x) > threshold)
{
2017-11-24 01:00:56 +00:00
return false;
}
2017-11-24 01:00:56 +00:00
if (Math.abs(this.y - vector.y) > threshold)
{
2017-11-24 01:00:56 +00:00
return false;
}
2017-11-24 01:00:56 +00:00
if (Math.abs(this.z - vector.z) > threshold)
{
2017-11-24 01:00:56 +00:00
return false;
}
2017-11-24 01:00:56 +00:00
if (Math.abs(this.w - vector.w) > threshold)
{
2017-11-24 01:00:56 +00:00
return false;
}
2017-11-24 01:00:56 +00:00
return true;
}
2017-11-24 01:00:56 +00:00
length(): number
{
return Math.sqrt(this.squaredLength());
}
2017-11-24 01:00:56 +00:00
squaredLength(): number
{
const x = this.x,
y = this.y,
z = this.z,
w = this.w;
2017-11-24 01:00:56 +00:00
return (x * x + y * y + z * z + w * w);
}
2017-11-24 01:00:56 +00:00
add(vector: vec4): vec4
{
this.x += vector.x;
this.y += vector.y;
this.z += vector.z;
this.w += vector.w;
2017-11-24 01:00:56 +00:00
return this;
}
2017-11-24 01:00:56 +00:00
subtract(vector: vec4): vec4
{
this.x -= vector.x;
this.y -= vector.y;
this.z -= vector.z;
this.w -= vector.w;
2017-11-24 01:00:56 +00:00
return this;
}
2017-11-24 01:00:56 +00:00
multiply(vector: vec4): vec4
{
this.x *= vector.x;
this.y *= vector.y;
this.z *= vector.z;
this.w *= vector.w;
2017-11-24 01:00:56 +00:00
return this;
}
2017-11-24 01:00:56 +00:00
divide(vector: vec4): vec4
{
this.x /= vector.x;
this.y /= vector.y;
this.z /= vector.z;
this.w /= vector.w;
2017-11-24 01:00:56 +00:00
return this;
}
2017-11-24 01:00:56 +00:00
scale(value: number, dest: vec4 | null = null): vec4
{
if (!dest)
{
2017-11-24 01:00:56 +00:00
dest = this;
}
2017-11-24 01:00:56 +00:00
dest.x *= value;
dest.y *= value;
dest.z *= value;
dest.w *= value;
2017-11-24 01:00:56 +00:00
return dest;
}
2017-11-24 01:00:56 +00:00
normalize(dest: vec4 | null = null): vec4
{
if (!dest)
{
2017-11-24 01:00:56 +00:00
dest = this;
}
let length = this.length();
2017-11-24 01:00:56 +00:00
if (length === 1)
{
return this;
}
2017-11-24 01:00:56 +00:00
if (length === 0)
{
2017-11-24 01:00:56 +00:00
dest.x *= 0;
dest.y *= 0;
dest.z *= 0;
dest.w *= 0;
return dest;
}
2017-11-24 01:00:56 +00:00
length = 1.0 / length;
2017-11-24 01:00:56 +00:00
dest.x *= length;
dest.y *= length;
dest.z *= length;
dest.w *= length;
2017-11-24 01:00:56 +00:00
return dest;
}
2017-11-24 01:00:56 +00:00
multiplyMat4(matrix: mat4, dest: vec4 | null = null): vec4
{
if (!dest)
{
2017-11-24 01:00:56 +00:00
dest = this;
}
2017-11-24 01:00:56 +00:00
return matrix.multiplyVec4(this, dest);
}
}