-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvector-math.js
205 lines (178 loc) · 6.02 KB
/
vector-math.js
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
'use strict';
function Matrix4x3() {
this.d = new Float32Array(16);
this.d[0] = 1;
this.d[5] = 1;
this.d[10] = 1;
this.d[15] = 1;
}
Matrix4x3.prototype = {
make: function(x1, x2, x3, y1, y2, y3, z1, z2, z3, t1, t2, t3) {
this.d[0] = x1;
this.d[1] = x2;
this.d[2] = x3;
this.d[4] = y1;
this.d[5] = y2;
this.d[6] = y3;
this.d[8] = z1;
this.d[9] = z2;
this.d[10] = z3;
this.d[12] = t1;
this.d[13] = t2;
this.d[14] = t3;
return this;
},
makeIdentity: function() {
return this.make(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0);
},
makeRotate: function(angle, x, y, z) {
var invLen = 1 / Math.sqrt(x * x + y * y + z * z);
var n = { x: invLen * x, y: invLen * y, z: invLen * z };
var s = Math.sin(angle);
var c = Math.cos(angle);
var t = 1 - c;
this.d[0] = t * n.x * n.x + c;
this.d[1] = t * n.x * n.y + s * n.z;
this.d[2] = t * n.x * n.z - s * n.y;
this.d[4] = t * n.x * n.y - s * n.z;
this.d[5] = t * n.y * n.y + c;
this.d[6] = t * n.y * n.z + s * n.x;
this.d[8] = t * n.x * n.z + s * n.y;
this.d[9] = t * n.y * n.z - s * n.x;
this.d[10] = t * n.z * n.z + c;
this.d[12] = this.d[13] = this.d[14] = 0;
return this;
},
makeRotateXYZ: function(ax, ay, az) {
var sx = Math.sin(ax);
var cx = Math.cos(ax);
var sy = Math.sin(ay);
var cy = Math.cos(ay);
var sz = Math.sin(az);
var cz = Math.cos(az);
this.d[0] = cy * cz;
this.d[1] = -cy * sz;
this.d[2] = sy;
this.d[4] = cz * sx * sy + cx * sz;
this.d[5] = cx * cz - sx * sy * sz;
this.d[6] = -cy * sx;
this.d[8] = -cx * cz * sy + sx * sz;
this.d[9] = cz * sx + cx * sy * sz;
this.d[10] = cx * cy;
this.d[12] = this.d[13] = this.d[14] = 0;
return this;
},
multiply: function(m) {
this.make(this.d[0] * m.d[0] + this.d[4] * m.d[1] + this.d[8] * m.d[2],
this.d[1] * m.d[0] + this.d[5] * m.d[1] + this.d[9] * m.d[2],
this.d[2] * m.d[0] + this.d[6] * m.d[1] + this.d[10] * m.d[2],
this.d[0] * m.d[4] + this.d[4] * m.d[5] + this.d[8] * m.d[6],
this.d[1] * m.d[4] + this.d[5] * m.d[5] + this.d[9] * m.d[6],
this.d[2] * m.d[4] + this.d[6] * m.d[5] + this.d[10] * m.d[6],
this.d[0]*m.d[8]+this.d[4]*m.d[9]+this.d[ 8]*m.d[10],
this.d[1]*m.d[8]+this.d[5]*m.d[9]+this.d[ 9]*m.d[10],
this.d[2]*m.d[8]+this.d[6]*m.d[9]+this.d[10]*m.d[10],
this.d[0]*m.d[12]+this.d[4]*m.d[13]+this.d[ 8]*m.d[14] + this.d[12],
this.d[1]*m.d[12]+this.d[5]*m.d[13]+this.d[ 9]*m.d[14] + this.d[13],
this.d[2]*m.d[12]+this.d[6]*m.d[13]+this.d[10]*m.d[14] + this.d[14]);
return this;
},
makeInverseRigidBody: function(m) {
// Inv(M) = Inv(Rot*Trans) = Inv(Rot) * Inv(Trans) = Transpose(Rot) * -T
// Invert translation
var it0 = -m.d[12];
var it1 = -m.d[13];
var it2 = -m.d[14];
// Calculate the translation
this.d[12] = m.d[0] * it0 + m.d[1] * it1 + m.d[2] * it2;
this.d[13] = m.d[4] * it0 + m.d[5] * it1 + m.d[6] * it2;
this.d[14] = m.d[8] * it0 + m.d[9] * it1 + m.d[10] * it2;
// Calculate the rotation (transpose)
this.d[0] = m.d[0];
this.d[1] = m.d[4];
this.d[2] = m.d[8];
this.d[4] = m.d[1];
this.d[5] = m.d[5];
this.d[6] = m.d[9];
this.d[8] = m.d[2];
this.d[9] = m.d[6];
this.d[10] = m.d[10];
return this;
}
};
function Matrix4x4() {
this.d = new Float32Array(16);
this.d[0] = 1;
this.d[5] = 1;
this.d[10] = 1;
this.d[15] = 1;
}
Matrix4x4.prototype = {
make : function(x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4, t1, t2, t3, t4) {
this.d[0] = x1;
this.d[1] = x2;
this.d[2] = x3;
this.d[3] = x4;
this.d[4] = y1;
this.d[5] = y2;
this.d[6] = y3;
this.d[7] = y4;
this.d[8] = z1;
this.d[9] = z2;
this.d[10] = z3;
this.d[11] = z4;
this.d[12] = t1;
this.d[13] = t2;
this.d[14] = t3;
this.d[15] = t4;
return this;
},
makeIdentity : function() {
return this.make(1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1);
},
makePerspective : function(fovy, aspect, znear, zfar) {
var top = znear * Math.tan(fovy * Math.PI / 360.0);
var bottom = -top;
var left = bottom * aspect;
var right = top * aspect;
var X = 2 * znear / (right - left);
var Y = 2 * znear / (top - bottom);
var A = (right + left) / (right - left);
var B = (top + bottom) / (top - bottom);
var C = -(zfar + znear) / (zfar - znear);
var D = -2 * zfar * znear / (zfar - znear);
this.make(X, 0, 0, 0, 0, Y, 0, 0, A, B, C, -1, 0, 0, D, 0);
return this;
}
};
var matrixState = {
modelMatrix: [new Matrix4x3(), new Matrix4x3()],
projMatrix: new Matrix4x4().makePerspective(45, 1, 0.01, 100),
viewMatrix: new Matrix4x3(),
modelStackTop: 0
};
function modelMatrix() {
return matrixState.modelMatrix[matrixState.modelStackTop];
}
function projMatrix() {
return matrixState.projMatrix;
}
function viewMatrix() {
return matrixState.viewMatrix;
}
function pushModelMatrix() {
++matrixState.modelStackTop;
if (matrixState.modelStackTop == matrixState.modelMatrix.length) {
var l = matrixState.modelMatrix.length;
matrixState.modelMatrix[l] = new Matrix4x3();
}
var top = matrixState.modelMatrix[matrixState.modelStackTop];
var parent = matrixState.modelMatrix[matrixState.modelStackTop-1];
for (var j = 0; j < 16; j++) {
top.d[j] = parent.d[j];
}
return top;
}
function popModelMatrix() {
--matrixState.modelStackTop;
}