summaryrefslogtreecommitdiff
path: root/public/gl/math
diff options
context:
space:
mode:
authorTyler Murphy <tylermurphy534@gmail.com>2023-01-18 22:43:02 -0500
committerTyler Murphy <tylermurphy534@gmail.com>2023-01-18 22:43:02 -0500
commit30cf48cd70f1c52f89362245891be355af110afe (patch)
tree5cfee873428a8519c5d117917f3abbb3659b631b /public/gl/math
parentinitial (diff)
downloadwebgl-30cf48cd70f1c52f89362245891be355af110afe.tar.gz
webgl-30cf48cd70f1c52f89362245891be355af110afe.tar.bz2
webgl-30cf48cd70f1c52f89362245891be355af110afe.zip
cube
Diffstat (limited to 'public/gl/math')
-rw-r--r--public/gl/math/Mat4.js157
-rw-r--r--public/gl/math/Vec2.js78
-rw-r--r--public/gl/math/Vec3.js88
3 files changed, 323 insertions, 0 deletions
diff --git a/public/gl/math/Mat4.js b/public/gl/math/Mat4.js
new file mode 100644
index 0000000..a342c0a
--- /dev/null
+++ b/public/gl/math/Mat4.js
@@ -0,0 +1,157 @@
+export class Mat4 {
+
+ constructor() {
+ this.data = [
+ 1, 0, 0, 0,
+ 0, 1, 0, 0,
+ 0, 0, 1, 0,
+ 0, 0, 0, 1
+ ]
+ }
+
+ set(m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33) {
+ const d = this.data
+ d[0] = m00; d[1] = m01; d[2] = m02; d[3] = m03;
+ d[4] = m10; d[5] = m11; d[6] = m12; d[7] = m13;
+ d[8] = m20; d[9] = m21; d[10] = m22; d[11] = m23;
+ d[12] = m30; d[13] = m31; d[14] = m32; d[15] = m33;
+ return this
+ }
+
+ clone() {
+ return new Mat4().arr(this.da)
+ }
+
+ arr(arr, offset = 0) {
+ for(let i = 0; i < 16; i++) {
+ this.data[i] = arr[i + offset]
+ }
+ return this
+ }
+
+ identity() {
+ this.set(
+ 1, 0, 0, 0,
+ 0, 1, 0, 0,
+ 0, 0, 1, 0,
+ 0, 0, 0, 1
+ )
+ return this
+ }
+
+ copy(m) {
+ const d = this.data
+ const o = m.data
+ d[0] = o[0]; d[1] = o[1]; d[2] = o[2]; d[3] = o[3];
+ d[4] = o[4]; d[5] = o[5]; d[6] = o[6]; d[7] = o[7];
+ d[8] = o[8]; d[9] = o[9]; d[10] = o[10]; d[11] = o[11];
+ d[12] = o[12]; d[13] = o[13]; d[14] = o[14]; d[15] = o[15];
+ return this
+ }
+
+ mult(v) {
+ return this.multmat(this.clone(), v)
+ }
+
+ premult(v) {
+ return this.multmat(v, this.clone())
+ }
+
+ multmat(ma, mb) {
+ const a = ma.data;
+ const b = mb.data
+ const d = this.data;
+
+ const a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3];
+ const a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7];
+ const a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11];
+ const a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15];
+
+ const b00 = b[0], b01 = b[1], b02 = b[2], b03 = a[3];
+ const b10 = b[4], b11 = b[5], b12 = b[6], b13 = a[7];
+ const b20 = b[8], b21 = b[9], b22 = b[10], b23 = a[11];
+ const b30 = b[12], b31 = b[13], b32 = b[14], b33 = a[15];
+
+ d[0] = a00 * b00 + a01 * b10 + a02 * b20 + a03 * b30;
+ d[1] = a00 * b01 + a01 * b11 + a02 * b21 + a03 * b31;
+ d[2] = a00 * b02 + a01 * b12 + a02 * b22 + a03 * b32;
+ d[3] = a00 * b03 + a01 * b13 + a02 * b23 + a03 * b33;
+
+ d[4] = a10 * b00 + a11 * b10 + a12 * b20 + a13 * b30;
+ d[5] = a10 * b01 + a11 * b11 + a12 * b21 + a13 * b31;
+ d[6] = a10 * b02 + a11 * b12 + a12 * b22 + a13 * b32;
+ d[7] = a10 * b03 + a11 * b13 + a12 * b23 + a13 * b33;
+
+ d[8] = a20 * b00 + a21 * b10 + a22 * b20 + a23 * b30;
+ d[9] = a20 * b01 + a21 * b11 + a22 * b21 + a23 * b31;
+ d[10] = a20 * b02 + a21 * b12 + a22 * b22 + a23 * b32;
+ d[11] = a20 * b03 + a21 * b13 + a22 * b23 + a23 * b33;
+
+ d[12] = a30 * b00 + a31 * b10 + a32 * b20 + a33 * b30;
+ d[13] = a30 * b01 + a31 * b11 + a32 * b21 + a33 * b31;
+ d[14] = a30 * b02 + a31 * b12 + a32 * b22 + a33 * b32;
+ d[15] = a30 * b03 + a31 * b13 + a32 * b23 + a33 * b33;
+
+ return this;
+ }
+
+ pos(v) {
+ const d = this.data
+
+ const m30 = d[0] * v.x + d[4] * v.y + d[8] * v.z;
+ const m31 = d[1] * v.x + d[5] * v.y + d[9] * v.z;
+ const m32 = d[2] * v.x + d[6] * v.y + d[10] * v.z;
+ const m33 = d[3] * v.x + d[7] * v.y + d[11] * v.z;
+
+ d[3] += m30;
+ d[7] += m31;
+ d[11] += m32;
+ d[15] += m33;
+ return this;
+ }
+
+ rot(v) {
+ const da = this.data;
+ const c3 = Math.cos(v.z)
+ const s3 = Math.sin(v.z)
+ const c2 = Math.cos(v.x)
+ const s2 = Math.sin(v.x)
+ const c1 = Math.cos(v.y)
+ const s1 = Math.sin(v.y)
+
+ da[0] = c1 * c3 + s1 * s2 * s3
+ da[4] = c2 * s3
+ da[8] = c1 * s2 * s3 - c3 * s1
+
+ da[1] = c3 * s1 * s2 - c1 * s3
+ da[5] = c2 * c3
+ da[9] = c1 * c3 * s2 + s1 * s3
+
+ da[2] = c2 * s1
+ da[6] = -s2
+ da[10] = c1 * c2
+
+ return this
+ }
+
+ scale( v ) {
+ const d = this.data;
+
+ d[0] *= v.x; d[1] *= v.y; d[2] *= v.z;
+ d[4] *= v.x; d[5] *= v.y; d[6] *= v.z;
+ d[8] *= v.x; d[9] *= v.y; d[10] *= v.z;
+ d[12] *= v.x; d[13] *= v.y; d[14] *= v.z;
+
+ return this;
+ }
+
+ get() {
+ const d = this.data
+ return [
+ d[0], d[4], d[8], d[12],
+ d[1], d[5], d[9], d[13],
+ d[2], d[6], d[10], d[14],
+ d[3], d[7], d[11], d[15]
+ ]
+ }
+} \ No newline at end of file
diff --git a/public/gl/math/Vec2.js b/public/gl/math/Vec2.js
new file mode 100644
index 0000000..6156e40
--- /dev/null
+++ b/public/gl/math/Vec2.js
@@ -0,0 +1,78 @@
+export class Vec2 {
+
+ constructor(x = 0, y = 0) {
+ this.x = x
+ this.y = y
+ }
+
+ set(x,y) {
+ this.x = x
+ this.y = y
+ return this
+ }
+
+ clone() {
+ return new Vec2(this.x, this.y)
+ }
+
+ add(v) {
+ this.x += v.x
+ this.y += v.y
+ return this
+ }
+
+ sub(v) {
+ this.x -= v.x
+ this.y -= v.y
+ return this
+ }
+
+ copy(v) {
+ this.x = v.x
+ this.y = v.y
+ return this
+ }
+
+ multV(v) {
+ this.x *= v.x
+ this.y *= v.y
+ return this
+ }
+
+ multS(s) {
+ this.x *= s
+ this.y *= s
+ return this
+ }
+
+ divV(v) {
+ this.x *= (1 / v.x)
+ this.y *= (1 / v.y)
+ return this
+ }
+
+ divS(s) {
+ this.x *= (1 / s)
+ this.y *= (1 / s)
+ return this
+ }
+
+ invert() {
+ this.x = this.x == 0 ? 0 : (1 / this.x)
+ this.y = this.y == 0 ? 0 : (1 / this.y)
+ return this
+ }
+
+ normalize() {
+ return this.divS(this.length() || 1)
+ }
+
+ dot(v) {
+ return this.x * v.x + this.y * v.y;
+ }
+
+ length() {
+ return Math.sqrt(this.x * this.x + this.y * this.y);
+ }
+
+} \ No newline at end of file
diff --git a/public/gl/math/Vec3.js b/public/gl/math/Vec3.js
new file mode 100644
index 0000000..d77d49d
--- /dev/null
+++ b/public/gl/math/Vec3.js
@@ -0,0 +1,88 @@
+export class Vec3 {
+
+ constructor(x = 0, y = 0, z = 0) {
+ this.x = x
+ this.y = y
+ this.z = z
+ }
+
+ set(x,y,z) {
+ this.x = x
+ this.y = y
+ this.z = z
+ return this
+ }
+
+ clone() {
+ return new Vec3(this.x, this.y, this.z)
+ }
+
+ add(v) {
+ this.x += v.x
+ this.y += v.y
+ this.x += v.x
+ return this
+ }
+
+ sub(v) {
+ this.x -= v.x
+ this.y -= v.y
+ this.x -= v.z
+ return this
+ }
+
+ copy(v) {
+ this.x = v.x
+ this.y = v.y
+ this.z = v.z
+ return this
+ }
+
+ multV(v) {
+ this.x *= v.x
+ this.y *= v.y
+ this.z *= v.z
+ return this
+ }
+
+ multS(s) {
+ this.x *= s
+ this.y *= s
+ this.z *= s
+ return this
+ }
+
+ divV(v) {
+ this.x *= (1 / v.x)
+ this.y *= (1 / v.y)
+ this.z *= (1 / v.z)
+ return this
+ }
+
+ divS(s) {
+ this.x *= (1 / s)
+ this.y *= (1 / s)
+ this.z *= (1 / s)
+ return this
+ }
+
+ invert() {
+ this.x = this.x == 0 ? 0 : (1 / this.x)
+ this.y = this.y == 0 ? 0 : (1 / this.y)
+ this.z = this.z == 0 ? 0 : (1 / this.z)
+ return this
+ }
+
+ normalize() {
+ return this.divS(this.length() || 1)
+ }
+
+ dot(v) {
+ return this.x * v.x + this.y * v.y + this.z * v.z;
+ }
+
+ length() {
+ return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
+ }
+
+} \ No newline at end of file