Repository navigation
Expand file tree
/
Copy pathMat4.js
More file actions
143 lines (134 loc) · 3.76 KB
/
Copy pathMat4.js
File metadata and controls
143 lines (134 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
class Mat4 {
constructor(
e11, e21, e31, e41,
e12, e22, e32, e42,
e13, e23, e33, e43,
e14, e24, e34, e44
) {
this.plainArr = [
e11, e21, e31, e41,
e12, e22, e32, e42,
e13, e23, e33, e43,
e14, e24, e34, e44
];
}
static fromArray(arr) {
return new Mat4(...arr);
}
get(i, j) {
if (i < 1 || i > 4 || j < 1 || j > 4)
throw new RangeError(
"trying to access index that is out of range [1, 4]",
);
const index = (j - 1) * 4 + (i - 1);
return this.plainArr[index];
}
set(i, j, value) {
if (i < 1 || i > 4 || j < 1 || j > 4)
throw new RangeError(
"trying to access index that is out of range [1, 4]",
);
const index = (j - 1) * 4 + (i - 1);
this.plainArr[index] = value;
}
add(that) {
return Mat4.fromArray(this.plainArr.map((val, i) => val + that.plainArr[i]));
}
sub(that) {
return Mat4.fromArray(this.plainArr.map((val, i) => val - that.plainArr[i]));
}
scale(scale) {
return Mat4.fromArray(this.plainArr.map((val, i) => val * scale));
}
mul(that) {
let c = new Array(16);
for (let i = 1; i <= 4; i++) {
for (let j = 1; j <= 4; j++) {
const index = (j - 1) * 4 + (i - 1);
c[index] = 0;
for (let k = 1; k <= 4; k++) {
c[index] += this.get(i, k) * that.get(k, j);
}
}
}
return Mat4.fromArray(c);
}
applyTo(vec4) {
let arr = [vec4.x, vec4.y, vec4.z, vec4.w];
let res = [0, 0, 0, 0];
for (let j = 1; j <= 4; j++) {
for (let i = 1; i <= 4; i++) {
res[i - 1] += arr[j - 1] * this.get(i, j);
}
}
return Vec4.fromArray(res);
}
static identity() {
return new Mat4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
}
static translation(tx, ty, tz) {
return new Mat4(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
tx, ty, tz, 1
);
}
static scaling(sx, sy, sz) {
return new Mat4(
sx, 0, 0, 0,
0, sy, 0, 0,
0, 0, sz, 0,
0, 0, 0, 1
);
}
static fromBasis(right, up, forward) {
return new Mat4(
right.x, right.y, right.z, 0,
up.x, up.y, up.z, 0,
forward.x, forward.y, forward.z, 0,
0, 0, 0, 1
);
}
static lookAt(eye, target, up) {
const zAxis = eye.sub(target).normalize();
const xAxis = up.cross(zAxis).normalize();
const yAxis = zAxis.cross(xAxis);
return new Mat4(
xAxis.x, yAxis.x, zAxis.x, 0,
xAxis.y, yAxis.y, zAxis.y, 0,
xAxis.z, yAxis.z, zAxis.z, 0,
-xAxis.dot(eye), -yAxis.dot(eye), -zAxis.dot(eye), 1
);
}
static perspective(fovRad, aspect, near, far) {
let f = 1.0 / Math.tan(fovRad / 2);
let nf = 1 / (near - far);
return Mat4.fromArray([
f / aspect, 0, 0, 0,
0, f, 0, 0,
0, 0, (far + near) * nf, -1,
0, 0, 2 * far * near * nf, 0
]);
}
static rotationX(rad) {
let c = Math.cos(rad);
let s = Math.sin(rad);
return new Mat4(
1, 0, 0, 0,
0, c, s, 0,
0, -s, c, 0,
0, 0, 0, 1
);
}
static rotationY(rad) {
let c = Math.cos(rad);
let s = Math.sin(rad);
return new Mat4(
c, 0, -s, 0,
0, 1, 0, 0,
s, 0, c, 0,
0, 0, 0, 1
);
}
}