nowjs-core
Version:
NowCanDo Javascript Core [nowjs-core] is a library written by TypeScript code maintains under Apache 2.0 licence
209 lines (208 loc) • 5.66 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const index_1 = require("./index");
const NumericSquareMatrix_1 = require("./NumericSquareMatrix");
class Matrix4x4 extends NumericSquareMatrix_1.NumericSquareMatrix {
constructor(...rows) {
super(4, ...rows);
}
frustum(l, r, b, t, n, f) {
const m = this.arr;
m[0][0] = (2 * n) / (r - l);
m[0][1] = 0;
m[0][2] = (r + l) / (r - l);
m[0][3] = 0;
m[1][0] = 0;
m[1][1] = (2 * n) / (t - b);
m[1][2] = (t + b) / (t - b);
m[1][3] = 0;
m[2][0] = 0;
m[2][1] = 0;
m[2][2] = -(f + n) / (f - n);
m[2][3] = (-2 * f * n) / (f - n);
m[3][0] = 0;
m[3][1] = 0;
m[3][2] = -1;
m[3][3] = 0;
return this;
}
ortho(l, r, b, t, n, f) {
const m = this.arr;
m[0][0] = 2 / (r - l);
m[0][1] = 0;
m[0][2] = 0;
m[0][3] = -(r + l) / (r - l);
m[1][0] = 0;
m[1][1] = 2 / (t - b);
m[1][2] = 0;
m[1][3] = -(t + b) / (t - b);
m[2][0] = 0;
m[2][1] = 0;
m[2][2] = -2 / (f - n);
m[2][3] = -(f + n) / (f - n);
m[3][0] = 0;
m[3][1] = 0;
m[3][2] = 0;
m[3][3] = -1;
return this;
}
scale(arg1, arg2, arg3) {
const m = this.arr;
let x = 0, y = 0, z = 0;
if (arg1 instanceof index_1.Vector3D) {
x = arg1.X;
y = arg1.Y;
z = arg1.Z;
}
else {
x = arg1;
y = arg2;
z = arg3;
}
m[0][0] = x;
m[0][1] = 0;
m[0][2] = 0;
m[0][3] = 0;
m[1][0] = 0;
m[1][1] = y;
m[1][2] = 0;
m[1][3] = 0;
m[2][0] = 0;
m[2][1] = 0;
m[2][2] = z;
m[2][3] = 0;
m[3][0] = 0;
m[3][1] = 0;
m[3][2] = 0;
m[3][3] = 1;
return this;
}
translate(arg1, arg2, arg3) {
const m = this.arr;
let x = 0, y = 0, z = 0;
if (arg1 instanceof index_1.Vector3D) {
x = arg1.X;
y = arg1.Y;
z = arg1.Z;
}
else {
x = arg1;
y = arg2;
z = arg3;
}
m[0][0] = 1;
m[0][1] = 0;
m[0][2] = 0;
m[0][3] = x;
m[1][0] = 0;
m[1][1] = 1;
m[1][2] = 0;
m[1][3] = y;
m[2][0] = 0;
m[2][1] = 0;
m[2][2] = 1;
m[2][3] = z;
m[3][0] = 0;
m[3][1] = 0;
m[3][2] = 0;
m[3][3] = 1;
return this;
}
rotate(a, arg1, arg2, arg3) {
const m = this.arr;
let x = 0, y = 0, z = 0;
if (arg1 instanceof index_1.Vector3D) {
x = arg1.X;
y = arg1.Y;
z = arg1.Z;
}
else {
x = arg1;
y = arg2;
z = arg3;
}
const d = Math.sqrt(x * x + y * y + z * z);
a *= Math.PI / 180;
x /= d;
y /= d;
z /= d;
const c = Math.cos(a), s = Math.sin(a), t = 1 - c;
m[0][0] = x * x * t + c;
m[0][1] = x * y * t - z * s;
m[0][2] = x * z * t + y * s;
m[0][3] = 0;
m[1][0] = y * x * t + z * s;
m[1][1] = y * y * t + c;
m[1][2] = y * z * t - x * s;
m[1][3] = 0;
m[2][0] = z * x * t - y * s;
m[2][1] = z * y * t + x * s;
m[2][2] = z * z * t + c;
m[2][3] = 0;
m[3][0] = 0;
m[3][1] = 0;
m[3][2] = 0;
m[3][3] = 1;
return this;
}
lookAt(e, c, u) {
const m = this.arr;
const f = e.subtract(c).unit();
const s = u.cross(f).unit();
const t = f.cross(s).unit();
m[0][0] = s.X;
m[0][1] = s.Y;
m[0][2] = s.Z;
m[0][3] = -s.dot(e);
m[1][0] = t.X;
m[1][1] = t.Y;
m[1][2] = t.Z;
m[1][3] = -t.dot(e);
m[2][0] = f.X;
m[2][1] = f.Y;
m[2][2] = f.Z;
m[2][3] = -f.dot(e);
m[3][0] = 0;
m[3][1] = 0;
m[3][2] = 0;
m[3][3] = 1;
return this;
}
perspective(fov, aspect, near, far) {
const result = new Matrix4x4();
const y = Math.tan((fov * Math.PI) / 360) * near;
const x = y * aspect;
return this.frustum(-x, x, -y, y, near, far);
}
transformPoint(v) {
const m = this.arr;
return new index_1.Vector3D(m[0][0] * v.X + m[0][1] * v.Y + m[0][2] * v.Z + m[0][3], m[1][0] * v.X + [1][1] * v.Y + [1][2] * v.Z + [1][3], m[2][0] * v.X + m[2][1] * v.Y + m[2][2] * v.Z + m[2][3]).divide(m[3][0] * v.X + m[3][1] * v.Y + m[3][2] * v.Z + m[3][3]);
}
transformVector(v) {
const m = this.arr;
return new index_1.Vector3D(m[0][0] * v.X + m[0][1] * v.Y + m[0][2] * v.Z, m[1][0] * v.X + m[1][1] * v.Y + m[1][2] * v.Z, m[2][0] * v.X + m[2][1] * v.Y + m[2][2] * v.Z);
}
transpose() {
const result = new Matrix4x4();
const m = this.arr;
const r = result.arr;
r[0][0] = m[0][0];
r[0][1] = m[1][0];
r[0][2] = m[2][0];
r[0][3] = m[3][0];
r[1][0] = m[0][1];
r[1][1] = m[1][1];
r[1][2] = m[2][1];
r[1][3] = m[3][1];
r[2][0] = m[0][2];
r[2][1] = m[1][2];
r[2][2] = m[2][2];
r[2][3] = m[3][2];
r[3][0] = m[0][3];
r[3][1] = m[1][3];
r[3][2] = m[2][3];
r[3][3] = m[3][3];
return result;
}
}
exports.Matrix4x4 = Matrix4x4;