bg2e-js 2.3.11 → 2.3.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bg2e-js.js +356 -326
- package/dist/bg2e-js.js.map +1 -1
- package/package.json +56 -56
- package/src/app/AppController.ts +39 -39
- package/src/app/Bg2KeyboardEvent.ts +54 -54
- package/src/app/Bg2MouseEvent.ts +82 -82
- package/src/app/Bg2TouchEvent.ts +18 -18
- package/src/app/Canvas.ts +108 -108
- package/src/app/EventBase.ts +10 -10
- package/src/app/MainLoop.ts +273 -273
- package/src/app/index.ts +24 -24
- package/src/base/Color.ts +134 -134
- package/src/base/Environment.ts +183 -183
- package/src/base/Light.ts +192 -192
- package/src/base/Material.ts +620 -620
- package/src/base/PolyList.ts +365 -365
- package/src/base/Texture.ts +620 -620
- package/src/base/index.ts +81 -81
- package/src/db/Bg2LoaderPlugin.ts +143 -143
- package/src/db/DBPluginApi.ts +48 -48
- package/src/db/Loader.ts +116 -116
- package/src/db/LoaderPlugin.ts +34 -34
- package/src/db/MtlParser.ts +7 -7
- package/src/db/ObjLoaderPlugin.ts +54 -54
- package/src/db/ObjParser.ts +252 -252
- package/src/db/ObjWriterPlugin.ts +18 -18
- package/src/db/VitscnjLoaderPlugin.ts +112 -112
- package/src/db/Writer.ts +52 -52
- package/src/db/WriterPlugin.ts +22 -22
- package/src/db/index.ts +44 -44
- package/src/debug/DebugRenderer.ts +173 -173
- package/src/debug/WebGLTextureViewer.ts +75 -75
- package/src/debug/index.ts +6 -6
- package/src/index.html +11 -11
- package/src/index.ts +33 -33
- package/src/manipulation/SelectionBuffer.ts +81 -81
- package/src/manipulation/SelectionHighlight.ts +105 -84
- package/src/manipulation/SelectionIdAssignVisitor.ts +96 -96
- package/src/manipulation/SelectionManager.ts +196 -188
- package/src/manipulation/SelectionMode.ts +6 -6
- package/src/math/Mat3.ts +259 -259
- package/src/math/Mat4.ts +710 -710
- package/src/math/MatrixStrategy.ts +25 -25
- package/src/math/Quat.ts +65 -65
- package/src/math/Vec.ts +753 -753
- package/src/math/constants.ts +46 -46
- package/src/math/functions.ts +103 -103
- package/src/math/index.ts +74 -74
- package/src/phsics/joint.ts +137 -137
- package/src/primitives/arrow.ts +57 -57
- package/src/primitives/cone.ts +138 -138
- package/src/primitives/cube.ts +60 -60
- package/src/primitives/cylinder.ts +216 -216
- package/src/primitives/index.ts +13 -13
- package/src/primitives/plane.ts +31 -31
- package/src/primitives/sphere.ts +809 -809
- package/src/react/useBg2e.ts +69 -69
- package/src/render/BRDFIntegrationMap.ts +4 -4
- package/src/render/Environment.ts +135 -135
- package/src/render/FrameBuffer.ts +35 -35
- package/src/render/MaterialRenderer.ts +34 -34
- package/src/render/Pipeline.ts +108 -108
- package/src/render/PolyListRenderer.ts +47 -47
- package/src/render/RenderBuffer.ts +197 -197
- package/src/render/RenderQueue.ts +198 -198
- package/src/render/RenderState.ts +116 -116
- package/src/render/Renderer.ts +248 -248
- package/src/render/SceneAppController.ts +250 -250
- package/src/render/SceneRenderer.ts +387 -387
- package/src/render/Shader.ts +32 -32
- package/src/render/ShadowRenderer.ts +176 -176
- package/src/render/SkyCube.ts +105 -105
- package/src/render/SkySphere.ts +117 -117
- package/src/render/TextureMergerRenderer.ts +70 -70
- package/src/render/TextureRenderer.ts +34 -34
- package/src/render/index.ts +67 -67
- package/src/render/webgl/FrameBuffer.ts +9 -9
- package/src/render/webgl/MaterialRenderer.ts +112 -112
- package/src/render/webgl/Pipeline.ts +88 -88
- package/src/render/webgl/PolyListRenderer.ts +260 -260
- package/src/render/webgl/RenderBuffer.ts +226 -226
- package/src/render/webgl/Renderer.ts +262 -262
- package/src/render/webgl/SceneRenderer.ts +67 -67
- package/src/render/webgl/ShaderProgram.ts +424 -424
- package/src/render/webgl/ShadowRenderer.ts +6 -6
- package/src/render/webgl/SkyCube.ts +15 -15
- package/src/render/webgl/SkySphere.ts +15 -15
- package/src/render/webgl/State.ts +152 -152
- package/src/render/webgl/TextureRenderer.ts +167 -167
- package/src/render/webgl/VertexBuffer.ts +137 -137
- package/src/render/webgl/index.ts +35 -35
- package/src/scene/Camera.ts +458 -458
- package/src/scene/Chain.ts +44 -44
- package/src/scene/ChainJoint.ts +58 -58
- package/src/scene/Component.ts +177 -177
- package/src/scene/ComponentMap.ts +106 -106
- package/src/scene/Drawable.ts +154 -154
- package/src/scene/EnvironmentComponent.ts +141 -141
- package/src/scene/FindNodeVisitor.ts +59 -59
- package/src/scene/LightComponent.ts +154 -154
- package/src/scene/MatrixState.ts +46 -46
- package/src/scene/Node.ts +328 -328
- package/src/scene/NodeVisitor.ts +15 -15
- package/src/scene/OrbitCameraController.ts +450 -450
- package/src/scene/SmoothOrbitCameraController.ts +99 -99
- package/src/scene/Transform.ts +73 -73
- package/src/scene/index.ts +60 -60
- package/src/shaders/BasicDiffuseColorShader.ts +111 -111
- package/src/shaders/BasicPBRLightShader.ts +276 -276
- package/src/shaders/DebugRenderShader.ts +97 -97
- package/src/shaders/DepthRenderShader.ts +127 -127
- package/src/shaders/IrradianceMapCubeShader.ts +115 -115
- package/src/shaders/PBRLightIBLShader.ts +486 -486
- package/src/shaders/PickSelectionShader.ts +101 -101
- package/src/shaders/PresentDebugFramebufferShader.ts +118 -118
- package/src/shaders/PresentTextureShader.ts +99 -99
- package/src/shaders/SelectionHighlightShader.ts +143 -127
- package/src/shaders/ShaderFunction.ts +318 -318
- package/src/shaders/SkyCubeShader.ts +93 -93
- package/src/shaders/SkySphereShader.ts +102 -102
- package/src/shaders/SpecularMapCubeShader.ts +164 -164
- package/src/shaders/TextureMergerShader.ts +171 -171
- package/src/shaders/index.ts +36 -36
- package/src/shaders/webgl/color_correction.glsl +47 -47
- package/src/shaders/webgl/constants.glsl +6 -6
- package/src/shaders/webgl/index.ts +70 -70
- package/src/shaders/webgl/normal_map.glsl +9 -9
- package/src/shaders/webgl/pbr.glsl +173 -173
- package/src/shaders/webgl/uniforms.glsl +91 -91
- package/src/shaders/webgl_shader_lib.ts +213 -213
- package/src/tools/BinaryResourceProvider.ts +14 -14
- package/src/tools/ImageResourceProvider.ts +66 -66
- package/src/tools/MaterialModifier.ts +446 -446
- package/src/tools/Resource.ts +203 -203
- package/src/tools/ResourceProvider.ts +69 -69
- package/src/tools/TextResourceProvider.ts +24 -24
- package/src/tools/TextureCache.ts +51 -51
- package/src/tools/TextureResourceDatabase.ts +100 -100
- package/src/tools/UserAgent.ts +362 -362
- package/src/tools/VideoResourceProvider.ts +50 -50
- package/src/tools/WriteStrategy.ts +22 -22
- package/src/tools/base64.ts +11 -11
- package/src/tools/crypto.ts +19 -19
- package/src/tools/endiantess.ts +13 -13
- package/src/tools/image.ts +18 -18
- package/src/tools/index.ts +41 -41
- package/src/tools/processType.ts +39 -39
- package/src/vite-env.d.ts +12 -12
package/src/math/Mat4.ts
CHANGED
|
@@ -1,710 +1,710 @@
|
|
|
1
|
-
import { NumericArray, PI, checkArray } from "./constants";
|
|
2
|
-
import Vec from "./Vec";
|
|
3
|
-
import Mat3 from "./Mat3";
|
|
4
|
-
import { equals, isZero } from "./functions";
|
|
5
|
-
|
|
6
|
-
export default class Mat4 extends NumericArray {
|
|
7
|
-
constructor();
|
|
8
|
-
constructor(m: ArrayLike<number>);
|
|
9
|
-
constructor(m0: number, m1: number, m2: number, m3: number, m4: number, m5: number, m6: number, m7: number, m8: number, m9: number, m10: number, m11: number, m12: number, m13: number, m14: number, m15: number);
|
|
10
|
-
constructor(...args: any[]) {
|
|
11
|
-
const inMatrix = [
|
|
12
|
-
0, 0, 0, 0,
|
|
13
|
-
0, 0, 0, 0,
|
|
14
|
-
0, 0, 0, 0,
|
|
15
|
-
0, 0, 0, 0
|
|
16
|
-
];
|
|
17
|
-
|
|
18
|
-
// Create from matrix3
|
|
19
|
-
if (args.length === 9) {
|
|
20
|
-
inMatrix[0] = args[0];
|
|
21
|
-
inMatrix[1] = args[1];
|
|
22
|
-
inMatrix[2] = args[2];
|
|
23
|
-
|
|
24
|
-
inMatrix[4] = args[3];
|
|
25
|
-
inMatrix[5] = args[4];
|
|
26
|
-
inMatrix[6] = args[5];
|
|
27
|
-
|
|
28
|
-
inMatrix[8] = args[6];
|
|
29
|
-
inMatrix[9] = args[7];
|
|
30
|
-
inMatrix[10] = args[8];
|
|
31
|
-
|
|
32
|
-
inMatrix[15] = 1;
|
|
33
|
-
}
|
|
34
|
-
else if (args.length === 1 && args[0].length === 9) {
|
|
35
|
-
inMatrix[0] = args[0][0];
|
|
36
|
-
inMatrix[1] = args[0][1];
|
|
37
|
-
inMatrix[2] = args[0][2];
|
|
38
|
-
|
|
39
|
-
inMatrix[4] = args[0][3];
|
|
40
|
-
inMatrix[5] = args[0][4];
|
|
41
|
-
inMatrix[6] = args[0][5];
|
|
42
|
-
|
|
43
|
-
inMatrix[8] = args[0][6];
|
|
44
|
-
inMatrix[9] = args[0][7];
|
|
45
|
-
inMatrix[10] = args[0][8];
|
|
46
|
-
|
|
47
|
-
inMatrix[15] = 1;
|
|
48
|
-
}
|
|
49
|
-
// Create from matrix4
|
|
50
|
-
else if (args.length === 16) {
|
|
51
|
-
inMatrix[0 ] = args[0];
|
|
52
|
-
inMatrix[1 ] = args[1 ];
|
|
53
|
-
inMatrix[2 ] = args[2 ];
|
|
54
|
-
inMatrix[3 ] = args[3 ];
|
|
55
|
-
|
|
56
|
-
inMatrix[4 ] = args[4 ];
|
|
57
|
-
inMatrix[5 ] = args[5 ];
|
|
58
|
-
inMatrix[6 ] = args[6 ];
|
|
59
|
-
inMatrix[7 ] = args[7 ];
|
|
60
|
-
|
|
61
|
-
inMatrix[8 ] = args[8 ];
|
|
62
|
-
inMatrix[9 ] = args[9 ];
|
|
63
|
-
inMatrix[10] = args[10];
|
|
64
|
-
inMatrix[11] = args[11];
|
|
65
|
-
|
|
66
|
-
inMatrix[12] = args[12];
|
|
67
|
-
inMatrix[13] = args[13];
|
|
68
|
-
inMatrix[14] = args[14];
|
|
69
|
-
inMatrix[15] = args[15];
|
|
70
|
-
}
|
|
71
|
-
else if (args.length === 1 && args[0].length === 16) {
|
|
72
|
-
inMatrix[0 ] = args[0][0];
|
|
73
|
-
inMatrix[1 ] = args[0][1 ];
|
|
74
|
-
inMatrix[2 ] = args[0][2 ];
|
|
75
|
-
inMatrix[3 ] = args[0][3 ];
|
|
76
|
-
|
|
77
|
-
inMatrix[4 ] = args[0][4 ];
|
|
78
|
-
inMatrix[5 ] = args[0][5 ];
|
|
79
|
-
inMatrix[6 ] = args[0][6 ];
|
|
80
|
-
inMatrix[7 ] = args[0][7 ];
|
|
81
|
-
|
|
82
|
-
inMatrix[8 ] = args[0][8 ];
|
|
83
|
-
inMatrix[9 ] = args[0][9 ];
|
|
84
|
-
inMatrix[10] = args[0][10];
|
|
85
|
-
inMatrix[11] = args[0][11];
|
|
86
|
-
|
|
87
|
-
inMatrix[12] = args[0][12];
|
|
88
|
-
inMatrix[13] = args[0][13];
|
|
89
|
-
inMatrix[14] = args[0][14];
|
|
90
|
-
inMatrix[15] = args[0][15];
|
|
91
|
-
}
|
|
92
|
-
else if (args.length != 0) {
|
|
93
|
-
throw new Error(`Invalid parameter size in Matrix3 constructor`);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
super(inMatrix);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
////// Initializers
|
|
100
|
-
identity(): this {
|
|
101
|
-
this[0 ] = 1; this[1 ] = 0; this[2 ] = 0; this[3 ] = 0
|
|
102
|
-
this[4 ] = 0; this[5 ] = 1; this[6 ] = 0; this[7 ] = 0
|
|
103
|
-
this[8 ] = 0; this[9 ] = 0; this[10] = 1; this[11] = 0
|
|
104
|
-
this[12] = 0; this[13] = 0; this[14] = 0; this[15] = 1
|
|
105
|
-
return this;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
zero(): this {
|
|
109
|
-
this[ 0] = 0; this[ 1] = 0; this[ 2] = 0; this[ 3] = 0;
|
|
110
|
-
this[ 4] = 0; this[ 5] = 0; this[ 6] = 0; this[ 7] = 0;
|
|
111
|
-
this[ 8] = 0; this[ 9] = 0; this[10] = 0; this[11] = 0;
|
|
112
|
-
this[12] = 0; this[13] = 0; this[14] = 0; this[15] = 0;
|
|
113
|
-
return this;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
perspective(fovy: number, aspect: number, nearPlane: number, farPlane: number): this {
|
|
117
|
-
let fovy2 = Math.tan(fovy * PI / 360.0) * nearPlane;
|
|
118
|
-
let fovy2aspect = fovy2 * aspect;
|
|
119
|
-
this.frustum(-fovy2aspect,fovy2aspect,-fovy2,fovy2,nearPlane,farPlane);
|
|
120
|
-
return this;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
frustum(left: number, right: number, bottom: number, top: number, nearPlane: number, farPlane: number): this {
|
|
124
|
-
let A = right - left;
|
|
125
|
-
let B = top-bottom;
|
|
126
|
-
let C = farPlane-nearPlane;
|
|
127
|
-
|
|
128
|
-
this.setRow(0, new Vec(nearPlane*2.0/A, 0.0, 0.0, 0.0));
|
|
129
|
-
this.setRow(1, new Vec(0.0, nearPlane*2.0/B, 0.0, 0.0));
|
|
130
|
-
this.setRow(2, new Vec((right+left)/A, (top+bottom)/B, -(farPlane+nearPlane)/C, -1.0));
|
|
131
|
-
this.setRow(3, new Vec(0.0, 0.0, -(farPlane*nearPlane*2.0)/C, 0.0));
|
|
132
|
-
|
|
133
|
-
return this;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
ortho(left: number, right: number, bottom: number, top: number, nearPlane: number, farPlane: number): this {
|
|
137
|
-
let m = right-left;
|
|
138
|
-
let l = top-bottom;
|
|
139
|
-
let k = farPlane-nearPlane;;
|
|
140
|
-
|
|
141
|
-
this[0] = 2/m; this[1] = 0; this[2] = 0; this[3] = 0;
|
|
142
|
-
this[4] = 0; this[5] = 2/l; this[6] = 0; this[7] = 0;
|
|
143
|
-
this[8] = 0; this[9] = 0; this[10] = -2/k; this[11]= 0;
|
|
144
|
-
this[12]=-(left+right)/m; this[13] = -(top+bottom)/l; this[14] = -(farPlane+nearPlane)/k; this[15]=1;
|
|
145
|
-
|
|
146
|
-
return this;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
lookAt(p_eye: Vec, p_center: Vec, p_up: Vec): this {
|
|
150
|
-
this.identity();
|
|
151
|
-
|
|
152
|
-
const y = new Vec(p_up);
|
|
153
|
-
const z = Vec.Sub(p_eye,p_center);
|
|
154
|
-
z.normalize();
|
|
155
|
-
const x = Vec.Cross(y,z) as Vec;
|
|
156
|
-
x.normalize();
|
|
157
|
-
y.normalize();
|
|
158
|
-
|
|
159
|
-
this.m00 = x.x;
|
|
160
|
-
this.m10 = x.y;
|
|
161
|
-
this.m20 = x.z;
|
|
162
|
-
this.m30 = -Vec.Dot(x, p_eye);
|
|
163
|
-
this.m01 = y.x;
|
|
164
|
-
this.m11 = y.y;
|
|
165
|
-
this.m21 = y.z;
|
|
166
|
-
this.m31 = -Vec.Dot(y, p_eye);
|
|
167
|
-
this.m02 = z.x;
|
|
168
|
-
this.m12 = z.y;
|
|
169
|
-
this.m22 = z.z;
|
|
170
|
-
this.m32 = -Vec.Dot(z, p_eye);
|
|
171
|
-
this.m03 = 0;
|
|
172
|
-
this.m13 = 0;
|
|
173
|
-
this.m23 = 0;
|
|
174
|
-
this.m33 = 1;
|
|
175
|
-
|
|
176
|
-
return this;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
///// Setters and getters
|
|
183
|
-
get m00(): number { return this[0]; }
|
|
184
|
-
get m01(): number { return this[1]; }
|
|
185
|
-
get m02(): number { return this[2]; }
|
|
186
|
-
get m03(): number { return this[3]; }
|
|
187
|
-
get m10(): number { return this[4]; }
|
|
188
|
-
get m11(): number { return this[5]; }
|
|
189
|
-
get m12(): number { return this[6]; }
|
|
190
|
-
get m13(): number { return this[7]; }
|
|
191
|
-
get m20(): number { return this[8]; }
|
|
192
|
-
get m21(): number { return this[9]; }
|
|
193
|
-
get m22(): number { return this[10]; }
|
|
194
|
-
get m23(): number { return this[11]; }
|
|
195
|
-
get m30(): number { return this[12]; }
|
|
196
|
-
get m31(): number { return this[13]; }
|
|
197
|
-
get m32(): number { return this[14]; }
|
|
198
|
-
get m33(): number { return this[15]; }
|
|
199
|
-
|
|
200
|
-
set m00(v: number) { this[0] = v; }
|
|
201
|
-
set m01(v: number) { this[1] = v; }
|
|
202
|
-
set m02(v: number) { this[2] = v; }
|
|
203
|
-
set m03(v: number) { this[3] = v; }
|
|
204
|
-
set m10(v: number) { this[4] = v; }
|
|
205
|
-
set m11(v: number) { this[5] = v; }
|
|
206
|
-
set m12(v: number) { this[6] = v; }
|
|
207
|
-
set m13(v: number) { this[7] = v; }
|
|
208
|
-
set m20(v: number) { this[8] = v; }
|
|
209
|
-
set m21(v: number) { this[9] = v; }
|
|
210
|
-
set m22(v: number) { this[10] = v; }
|
|
211
|
-
set m23(v: number) { this[11] = v; }
|
|
212
|
-
set m30(v: number) { this[12] = v; }
|
|
213
|
-
set m31(v: number) { this[13] = v; }
|
|
214
|
-
set m32(v: number) { this[14] = v; }
|
|
215
|
-
set m33(v: number) { this[15] = v; }
|
|
216
|
-
|
|
217
|
-
get mat3(): Mat3 {
|
|
218
|
-
return new Mat3(this[0], this[1], this[ 2],
|
|
219
|
-
this[4], this[5], this[ 6],
|
|
220
|
-
this[8], this[9], this[10]);
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
get forwardVector(): Vec {
|
|
224
|
-
return Mat4.TransformDirection(this, new Vec(0.0, 0.0, 1.0));
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
get rightVector(): Vec {
|
|
228
|
-
return Mat4.TransformDirection(this, new Vec(1.0, 0.0, 0.0));
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
get upVector(): Vec {
|
|
232
|
-
return Mat4.TransformDirection(this, new Vec(0.0, 1.0, 0.0));
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
get backwardVector(): Vec {
|
|
236
|
-
return Mat4.TransformDirection(this, new Vec(0.0, 0.0, -1.0));
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
get leftVector(): Vec {
|
|
240
|
-
return Mat4.TransformDirection(this, new Vec(-1.0, 0.0, 0.0));
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
get downVector(): Vec {
|
|
244
|
-
return Mat4.TransformDirection(this, new Vec(0.0, -1.0, 0.0));
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
get translation(): Vec {
|
|
248
|
-
return new Vec(this[12], this[13], this[14]);
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
row(i: number): Vec {
|
|
252
|
-
return new Vec(
|
|
253
|
-
this[i * 4],
|
|
254
|
-
this[i * 4 + 1],
|
|
255
|
-
this[i * 4 + 2],
|
|
256
|
-
this[i * 4 + 3]);
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
setRow(i: number, a: ArrayLike<number>): this;
|
|
260
|
-
setRow(i: number, x: number, y: number, z: number, w: number): this;
|
|
261
|
-
setRow(i: number, a: number | ArrayLike<number>, y?: number, z?: number, w?: number): this {
|
|
262
|
-
if (typeof a === 'object' && a.length >= 4) {
|
|
263
|
-
this[i * 4] = a[0];
|
|
264
|
-
this[i * 4 + 1] = a[1];
|
|
265
|
-
this[i * 4 + 2] = a[2];
|
|
266
|
-
this[i * 4 + 3] = a[3];
|
|
267
|
-
}
|
|
268
|
-
else if (typeof a === 'object' && a.length == 3) {
|
|
269
|
-
this[i * 4] = a[0];
|
|
270
|
-
this[i * 4 + 1] = a[1];
|
|
271
|
-
this[i * 4 + 2] = a[2];
|
|
272
|
-
}
|
|
273
|
-
else if (typeof(a) === "number" &&
|
|
274
|
-
typeof(y) === "number" &&
|
|
275
|
-
typeof(z) === "number" &&
|
|
276
|
-
typeof(w) === "number"
|
|
277
|
-
) {
|
|
278
|
-
this[i * 4] = a;
|
|
279
|
-
this[i * 4 + 1] = y;
|
|
280
|
-
this[i * 4 + 2] = z;
|
|
281
|
-
this[i * 4 + 3] = w;
|
|
282
|
-
}
|
|
283
|
-
else {
|
|
284
|
-
throw new Error(`Invalid parameter setting matrix row`);
|
|
285
|
-
}
|
|
286
|
-
return this;
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
col(i: number): Vec {
|
|
290
|
-
return new Vec(
|
|
291
|
-
this[i],
|
|
292
|
-
this[i + 4],
|
|
293
|
-
this[i + 4 * 2],
|
|
294
|
-
this[i + 4 * 3]
|
|
295
|
-
)
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
setCol(i: number, a: ArrayLike<number>): this;
|
|
299
|
-
setCol(i: number, x: number, y: number, z: number, w: number): this;
|
|
300
|
-
setCol(i: number, a: number | ArrayLike<number>, y?: number, z?: number, w?: number): this {
|
|
301
|
-
if (typeof a === 'object' && a.length >= 4) {
|
|
302
|
-
this[i] = a[0];
|
|
303
|
-
this[i + 4] = a[1];
|
|
304
|
-
this[i + 4 * 2] = a[2];
|
|
305
|
-
this[i + 4 * 3] = a[3];
|
|
306
|
-
}
|
|
307
|
-
else if (typeof(a) === "number" &&
|
|
308
|
-
typeof(y) === "number" &&
|
|
309
|
-
typeof(z) === "number" &&
|
|
310
|
-
typeof(w) === "number"
|
|
311
|
-
) {
|
|
312
|
-
this[i] = a;
|
|
313
|
-
this[i + 4] = y;
|
|
314
|
-
this[i + 4 * 2] = z;
|
|
315
|
-
this[i + 4 * 3] = w;
|
|
316
|
-
}
|
|
317
|
-
else {
|
|
318
|
-
throw new Error(`Invalid parameter setting matrix row`);
|
|
319
|
-
}
|
|
320
|
-
return this;
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
assign(a: ArrayLike<number>): this {
|
|
324
|
-
if (a.length==9) {
|
|
325
|
-
this[0] = a[0]; this[1] = a[1]; this[2] = a[2]; this[3] = 0;
|
|
326
|
-
this[4] = a[3]; this[5] = a[4]; this[6] = a[5]; this[7] = 0;
|
|
327
|
-
this[8] = a[6]; this[9] = a[7]; this[10] = a[8]; this[11] = 0;
|
|
328
|
-
this[12] = 0; this[13] = 0; this[14] = 0; this[15] = 1;
|
|
329
|
-
}
|
|
330
|
-
else if (a.length==16) {
|
|
331
|
-
this[0] = a[0]; this[1] = a[1]; this[2] = a[2]; this[3] = a[3];
|
|
332
|
-
this[4] = a[4]; this[5] = a[5]; this[6] = a[6]; this[7] = a[7];
|
|
333
|
-
this[8] = a[8]; this[9] = a[9]; this[10] = a[10]; this[11] = a[11];
|
|
334
|
-
this[12] = a[12]; this[13] = a[13]; this[14] = a[14]; this[15] = a[15];
|
|
335
|
-
}
|
|
336
|
-
return this;
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
translate(x: number | ArrayLike<number>, y?: number, z?: number): this {
|
|
340
|
-
if (checkArray(x, 3)) {
|
|
341
|
-
y = (x as ArrayLike<number>)[1];
|
|
342
|
-
z = (x as ArrayLike<number>)[2];
|
|
343
|
-
x = (x as ArrayLike<number>)[0];
|
|
344
|
-
}
|
|
345
|
-
this.mult(Mat4.MakeTranslation(x, y, z));
|
|
346
|
-
return this;
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
rotate(alpha: number, x: number | ArrayLike<number>, y?: number, z?: number): this {
|
|
350
|
-
if (checkArray(x, 3)) {
|
|
351
|
-
y = (x as ArrayLike<number>)[1];
|
|
352
|
-
z = (x as ArrayLike<number>)[2];
|
|
353
|
-
x = (x as ArrayLike<number>)[0];
|
|
354
|
-
}
|
|
355
|
-
this.mult(Mat4.MakeRotation(alpha, x as number, y ?? 0, z ?? 0));
|
|
356
|
-
return this;
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
scale(x: number | ArrayLike<number>, y?: number, z?: number): this {
|
|
360
|
-
if (checkArray(x, 3)) {
|
|
361
|
-
y = (x as ArrayLike<number>)[1];
|
|
362
|
-
z = (x as ArrayLike<number>)[2];
|
|
363
|
-
x = (x as ArrayLike<number>)[0];
|
|
364
|
-
}
|
|
365
|
-
this.mult(Mat4.MakeScale(x, y, z));
|
|
366
|
-
return this;
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
toString(): string {
|
|
370
|
-
return `[ ${this[ 0]}, ${this[ 1]}, ${this[ 2]}, ${this[ 3]}\n` +
|
|
371
|
-
` ${this[ 4]}, ${this[ 5]}, ${this[ 6]}, ${this[ 7]}\n` +
|
|
372
|
-
` ${this[ 8]}, ${this[ 9]}, ${this[10]}, ${this[11]}\n` +
|
|
373
|
-
` ${this[12]}, ${this[13]}, ${this[14]}, ${this[15]} ]`;
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
setScale(x: number, y: number, z: number): this {
|
|
377
|
-
const rx = new Vec(this[0], this[4], this[8]).normalize().scale(x);
|
|
378
|
-
const ry = new Vec(this[1], this[5], this[9]).normalize().scale(y);
|
|
379
|
-
const rz = new Vec(this[2], this[6], this[10]).normalize().scale(z);
|
|
380
|
-
this[0] = rx.x; this[4] = rx.y; this[8] = rx.z;
|
|
381
|
-
this[1] = ry.x; this[5] = ry.y; this[9] = ry.z;
|
|
382
|
-
this[2] = rz.x; this[6] = rz.y; this[10] = rz.z;
|
|
383
|
-
return this;
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
setPosition(pos: Vec): this;
|
|
387
|
-
setPosition(x: number, y: number, z: number): this;
|
|
388
|
-
setPosition(pos: number | Vec, y?: number, z?: number): this {
|
|
389
|
-
if (typeof(pos) === "number" && y !== undefined && z !== undefined) {
|
|
390
|
-
this[12] = pos;
|
|
391
|
-
this[13] = y;
|
|
392
|
-
this[14] = z;
|
|
393
|
-
}
|
|
394
|
-
else if (typeof pos === 'object') {
|
|
395
|
-
this[12] = pos.x;
|
|
396
|
-
this[13] = pos.y;
|
|
397
|
-
this[14] = pos.z;
|
|
398
|
-
}
|
|
399
|
-
return this;
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
setRotation(rotationMatrix: ArrayLike<number>): this {
|
|
403
|
-
if (rotationMatrix.length === 9) {
|
|
404
|
-
this[0] = rotationMatrix[0]; this[1] = rotationMatrix[1]; this[2] = rotationMatrix[2];
|
|
405
|
-
this[4] = rotationMatrix[3]; this[5] = rotationMatrix[4]; this[6] = rotationMatrix[5];
|
|
406
|
-
this[8] = rotationMatrix[6]; this[9] = rotationMatrix[7]; this[10] = rotationMatrix[8];
|
|
407
|
-
}
|
|
408
|
-
else if (rotationMatrix.length === 16) {
|
|
409
|
-
this[0] = rotationMatrix[0]; this[1] = rotationMatrix[1]; this[2] = rotationMatrix[2];
|
|
410
|
-
this[4] = rotationMatrix[4]; this[5] = rotationMatrix[5]; this[6] = rotationMatrix[6];
|
|
411
|
-
this[8] = rotationMatrix[8]; this[9] = rotationMatrix[9]; this[10] = rotationMatrix[10];
|
|
412
|
-
}
|
|
413
|
-
else {
|
|
414
|
-
throw new Error("Invalid parameter setting rotation matrix");
|
|
415
|
-
}
|
|
416
|
-
return this;
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
mult(a: number): this;
|
|
420
|
-
mult(a: Mat4): this;
|
|
421
|
-
mult(a: number | Mat4): this {
|
|
422
|
-
if (typeof(a)=='number') {
|
|
423
|
-
this[ 0] *= a; this[ 1] *= a; this[ 2] *= a; this[ 3] *= a;
|
|
424
|
-
this[ 4] *= a; this[ 5] *= a; this[ 6] *= a; this[ 7] *= a;
|
|
425
|
-
this[ 8] *= a; this[ 9] *= a; this[10] *= a; this[11] *= a;
|
|
426
|
-
this[12] *= a; this[13] *= a; this[14] *= a; this[15] *= a;
|
|
427
|
-
return this;
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
const r0 = this.row(0);
|
|
431
|
-
const r1 = this.row(1);
|
|
432
|
-
const r2 = this.row(2);
|
|
433
|
-
const r3 = this.row(3);
|
|
434
|
-
const c0 = a.col(0);
|
|
435
|
-
const c1 = a.col(1);
|
|
436
|
-
const c2 = a.col(2);
|
|
437
|
-
const c3 = a.col(3);
|
|
438
|
-
|
|
439
|
-
this[0 ] = Vec.Dot(r0, c0); this[1 ] = Vec.Dot(r0, c1); this[2 ] = Vec.Dot(r0, c2); this[3 ] = Vec.Dot(r0, c3);
|
|
440
|
-
this[4 ] = Vec.Dot(r1, c0); this[5 ] = Vec.Dot(r1, c1); this[6 ] = Vec.Dot(r1, c2); this[7 ] = Vec.Dot(r1, c3);
|
|
441
|
-
this[8 ] = Vec.Dot(r2, c0); this[9 ] = Vec.Dot(r2, c1); this[10] = Vec.Dot(r2, c2); this[11] = Vec.Dot(r2, c3);
|
|
442
|
-
this[12] = Vec.Dot(r3, c0); this[13] = Vec.Dot(r3, c1); this[14] = Vec.Dot(r3, c2); this[15] = Vec.Dot(r3, c3);
|
|
443
|
-
|
|
444
|
-
return this;
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
multVector(vec: ArrayLike<number>): Vec {
|
|
448
|
-
if (vec.length<3) {
|
|
449
|
-
throw new Error("Invalid parameter multiplying Mat4 by vector");
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
const x = vec[0];
|
|
453
|
-
const y = vec[1];
|
|
454
|
-
const z = vec[2];
|
|
455
|
-
const w = vec.length >3 ? vec[3] : 1.0;
|
|
456
|
-
|
|
457
|
-
return new Vec( this[0] * x + this[4] * y + this[ 8] * z + this[12] * w,
|
|
458
|
-
this[1] * x + this[5] * y + this[ 9] * z + this[13] * w,
|
|
459
|
-
this[2] * x + this[6] * y + this[10] * z + this[14] * w,
|
|
460
|
-
this[3] * x + this[7] * y + this[11] * z + this[15] * w);
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
invert(): this {
|
|
464
|
-
const a00 = this[0], a01 = this[1], a02 = this[2], a03 = this[3],
|
|
465
|
-
a10 = this[4], a11 = this[5], a12 = this[6], a13 = this[7],
|
|
466
|
-
a20 = this[8], a21 = this[9], a22 = this[10], a23 = this[11],
|
|
467
|
-
a30 = this[12], a31 = this[13], a32 = this[14], a33 = this[15];
|
|
468
|
-
|
|
469
|
-
const b00 = a00 * a11 - a01 * a10,
|
|
470
|
-
b01 = a00 * a12 - a02 * a10,
|
|
471
|
-
b02 = a00 * a13 - a03 * a10,
|
|
472
|
-
b03 = a01 * a12 - a02 * a11,
|
|
473
|
-
b04 = a01 * a13 - a03 * a11,
|
|
474
|
-
b05 = a02 * a13 - a03 * a12,
|
|
475
|
-
b06 = a20 * a31 - a21 * a30,
|
|
476
|
-
b07 = a20 * a32 - a22 * a30,
|
|
477
|
-
b08 = a20 * a33 - a23 * a30,
|
|
478
|
-
b09 = a21 * a32 - a22 * a31,
|
|
479
|
-
b10 = a21 * a33 - a23 * a31,
|
|
480
|
-
b11 = a22 * a33 - a23 * a32;
|
|
481
|
-
|
|
482
|
-
let det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
|
|
483
|
-
|
|
484
|
-
if (!det) {
|
|
485
|
-
this.zero();
|
|
486
|
-
}
|
|
487
|
-
else {
|
|
488
|
-
det = 1.0 / det;
|
|
489
|
-
|
|
490
|
-
this[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det;
|
|
491
|
-
this[1] = (a02 * b10 - a01 * b11 - a03 * b09) * det;
|
|
492
|
-
this[2] = (a31 * b05 - a32 * b04 + a33 * b03) * det;
|
|
493
|
-
this[3] = (a22 * b04 - a21 * b05 - a23 * b03) * det;
|
|
494
|
-
this[4] = (a12 * b08 - a10 * b11 - a13 * b07) * det;
|
|
495
|
-
this[5] = (a00 * b11 - a02 * b08 + a03 * b07) * det;
|
|
496
|
-
this[6] = (a32 * b02 - a30 * b05 - a33 * b01) * det;
|
|
497
|
-
this[7] = (a20 * b05 - a22 * b02 + a23 * b01) * det;
|
|
498
|
-
this[8] = (a10 * b10 - a11 * b08 + a13 * b06) * det;
|
|
499
|
-
this[9] = (a01 * b08 - a00 * b10 - a03 * b06) * det;
|
|
500
|
-
this[10] = (a30 * b04 - a31 * b02 + a33 * b00) * det;
|
|
501
|
-
this[11] = (a21 * b02 - a20 * b04 - a23 * b00) * det;
|
|
502
|
-
this[12] = (a11 * b07 - a10 * b09 - a12 * b06) * det;
|
|
503
|
-
this[13] = (a00 * b09 - a01 * b07 + a02 * b06) * det;
|
|
504
|
-
this[14] = (a31 * b01 - a30 * b03 - a32 * b00) * det;
|
|
505
|
-
this[15] = (a20 * b03 - a21 * b01 + a22 * b00) * det;
|
|
506
|
-
}
|
|
507
|
-
|
|
508
|
-
return this;
|
|
509
|
-
}
|
|
510
|
-
|
|
511
|
-
traspose(): this {
|
|
512
|
-
const r0 = new Vec(this[0], this[4], this[ 8], this[12]);
|
|
513
|
-
const r1 = new Vec(this[1], this[5], this[ 9], this[13]);
|
|
514
|
-
const r2 = new Vec(this[2], this[6], this[10], this[14]);
|
|
515
|
-
const r3 = new Vec(this[3], this[7], this[11], this[15]);
|
|
516
|
-
|
|
517
|
-
this.setRow(0, r0);
|
|
518
|
-
this.setRow(1, r1);
|
|
519
|
-
this.setRow(2, r2);
|
|
520
|
-
this.setRow(3, r3);
|
|
521
|
-
return this;
|
|
522
|
-
}
|
|
523
|
-
|
|
524
|
-
///////// Factory methods
|
|
525
|
-
static MakeIdentity(): Mat4 {
|
|
526
|
-
const m = new Mat4();
|
|
527
|
-
return m.identity();
|
|
528
|
-
}
|
|
529
|
-
|
|
530
|
-
static MakeZero(): Mat4 {
|
|
531
|
-
const m = new Mat4();
|
|
532
|
-
return m.zero();
|
|
533
|
-
}
|
|
534
|
-
|
|
535
|
-
static MakeWithQuaternion(q: ArrayLike<number>): Mat4 {
|
|
536
|
-
const m = Mat4.MakeIdentity();
|
|
537
|
-
m.setRotation(Mat3.MakeWithQuaternion(q));
|
|
538
|
-
return m;
|
|
539
|
-
}
|
|
540
|
-
|
|
541
|
-
static MakeTranslation(x: number | ArrayLike<number>, y?: number, z?: number): Mat4 {
|
|
542
|
-
if (checkArray(x, 3)) {
|
|
543
|
-
y = (x as ArrayLike<number>)[1];
|
|
544
|
-
z = (x as ArrayLike<number>)[2];
|
|
545
|
-
x = (x as ArrayLike<number>)[0];
|
|
546
|
-
}
|
|
547
|
-
return new Mat4(
|
|
548
|
-
1.0, 0.0, 0.0, 0.0,
|
|
549
|
-
0.0, 1.0, 0.0, 0.0,
|
|
550
|
-
0.0, 0.0, 1.0, 0.0,
|
|
551
|
-
x as number, y as number, z as number, 1.0
|
|
552
|
-
);
|
|
553
|
-
}
|
|
554
|
-
|
|
555
|
-
static MakeRotation(alpha: number, x: number, y: number, z: number): Mat4 {
|
|
556
|
-
const axis = new Vec(x,y,z);
|
|
557
|
-
axis.normalize();
|
|
558
|
-
|
|
559
|
-
var cosAlpha = Math.cos(alpha);
|
|
560
|
-
var acosAlpha = 1.0 - cosAlpha;
|
|
561
|
-
var sinAlpha = Math.sin(alpha);
|
|
562
|
-
|
|
563
|
-
return new Mat4(
|
|
564
|
-
axis.x * axis.x * acosAlpha + cosAlpha, axis.x * axis.y * acosAlpha + axis.z * sinAlpha, axis.x * axis.z * acosAlpha - axis.y * sinAlpha, 0,
|
|
565
|
-
axis.y * axis.x * acosAlpha - axis.z * sinAlpha, axis.y * axis.y * acosAlpha + cosAlpha, axis.y * axis.z * acosAlpha + axis.x * sinAlpha, 0,
|
|
566
|
-
axis.z * axis.x * acosAlpha + axis.y * sinAlpha, axis.z * axis.y * acosAlpha - axis.x * sinAlpha, axis.z * axis.z * acosAlpha + cosAlpha, 0,
|
|
567
|
-
0,0,0,1
|
|
568
|
-
);
|
|
569
|
-
}
|
|
570
|
-
|
|
571
|
-
static MakeRotationWithDirection(direction: Vec, up?: Vec): Mat4 {
|
|
572
|
-
const trx = Mat4.MakeIdentity();
|
|
573
|
-
trx.setRotation(Mat3.MakeRotationWithDirection(direction, up));
|
|
574
|
-
return trx;
|
|
575
|
-
}
|
|
576
|
-
|
|
577
|
-
static MakeScale(x: number | ArrayLike<number>, y?: number, z?: number): Mat4 {
|
|
578
|
-
if (checkArray(x, 3)) {
|
|
579
|
-
y = (x as ArrayLike<number>)[1];
|
|
580
|
-
z = (x as ArrayLike<number>)[2];
|
|
581
|
-
x = (x as ArrayLike<number>)[0];
|
|
582
|
-
}
|
|
583
|
-
return new Mat4(
|
|
584
|
-
x as number, 0, 0, 0,
|
|
585
|
-
0, y as number, 0, 0,
|
|
586
|
-
0, 0, z as number, 0,
|
|
587
|
-
0, 0, 0, 1
|
|
588
|
-
)
|
|
589
|
-
}
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
static MakePerspective(fovy: number, aspect: number, nearPlane: number, farPlane: number): Mat4 {
|
|
593
|
-
return (new Mat4()).perspective(fovy, aspect, nearPlane, farPlane);
|
|
594
|
-
}
|
|
595
|
-
|
|
596
|
-
static MakeFrustum(left: number, right: number, bottom: number, top: number, nearPlane: number, farPlane: number): Mat4 {
|
|
597
|
-
return (new Mat4()).frustum(left, right, bottom, top, nearPlane, farPlane);
|
|
598
|
-
}
|
|
599
|
-
|
|
600
|
-
static MakeOrtho(left: number, right: number, bottom: number, top: number, nearPlane: number, farPlane: number): Mat4 {
|
|
601
|
-
return (new Mat4()).ortho(left, right, bottom, top, nearPlane, farPlane);
|
|
602
|
-
}
|
|
603
|
-
|
|
604
|
-
static MakeLookAt(origin: Vec, target: Vec, up: Vec): Mat4 {
|
|
605
|
-
return (new Mat4()).lookAt(origin,target,up);
|
|
606
|
-
}
|
|
607
|
-
|
|
608
|
-
// Other static methods. This function multyplies two B x A matrices. It works opposite than the non-static mult() function:
|
|
609
|
-
// A.mult(B) is the same as Mat4.Mult(B,A)
|
|
610
|
-
static Mult(A: Mat4, B: Mat4): Mat4 {
|
|
611
|
-
const result = new Mat4(B);
|
|
612
|
-
return result.mult(A);
|
|
613
|
-
}
|
|
614
|
-
|
|
615
|
-
static Unproject(x: number, y: number, depth: number, mvMat: Mat4, pMat: Mat4, viewport: Vec): Vec {
|
|
616
|
-
let mvp = new Mat4(pMat);
|
|
617
|
-
mvp.mult(mvMat);
|
|
618
|
-
mvp.invert();
|
|
619
|
-
|
|
620
|
-
const vin = new Vec(((x - viewport.y) / viewport.width) * 2.0 - 1.0,
|
|
621
|
-
((y - viewport.x) / viewport.height) * 2.0 - 1.0,
|
|
622
|
-
depth * 2.0 - 1.0,
|
|
623
|
-
1.0);
|
|
624
|
-
|
|
625
|
-
const result = mvp.multVector(vin);
|
|
626
|
-
if (result.z==0) {
|
|
627
|
-
result.setValue(0, 0, 0, 0);
|
|
628
|
-
}
|
|
629
|
-
else {
|
|
630
|
-
result.setValue(result.x/result.w,
|
|
631
|
-
result.y/result.w,
|
|
632
|
-
result.z/result.w,
|
|
633
|
-
result.w/result.w);
|
|
634
|
-
}
|
|
635
|
-
|
|
636
|
-
return result;
|
|
637
|
-
}
|
|
638
|
-
|
|
639
|
-
static GetScale(m: ArrayLike<number>): Vec {
|
|
640
|
-
return new Vec(
|
|
641
|
-
Vec.Magnitude([m[1], m[5], m[9]]),
|
|
642
|
-
Vec.Magnitude([m[0], m[4], m[8]]),
|
|
643
|
-
Vec.Magnitude([m[2], m[6], m[10]])
|
|
644
|
-
);
|
|
645
|
-
}
|
|
646
|
-
|
|
647
|
-
static GetRotation(m: ArrayLike<number>): Mat4 {
|
|
648
|
-
const scale = Mat4.GetScale(m);
|
|
649
|
-
return new Mat4(
|
|
650
|
-
m[0] / scale.x, m[1] / scale.y, m[ 2] / scale.z, 0,
|
|
651
|
-
m[4] / scale.x, m[5] / scale.y, m[ 6] / scale.z, 0,
|
|
652
|
-
m[8] / scale.x, m[9] / scale.y, m[10] / scale.z, 0,
|
|
653
|
-
0, 0, 0, 1
|
|
654
|
-
);
|
|
655
|
-
}
|
|
656
|
-
|
|
657
|
-
static GetPosition(m: ArrayLike<number>): Vec {
|
|
658
|
-
return new Vec(m[12], m[13], m[14]);
|
|
659
|
-
}
|
|
660
|
-
|
|
661
|
-
static GetInverted(m: Mat4): Mat4 {
|
|
662
|
-
const inverted = new Mat4(m);
|
|
663
|
-
inverted.invert();
|
|
664
|
-
return inverted;
|
|
665
|
-
}
|
|
666
|
-
|
|
667
|
-
static GetNormalMatrix(m: Mat4): Mat3 {
|
|
668
|
-
return new Mat4(m)
|
|
669
|
-
.invert()
|
|
670
|
-
.traspose()
|
|
671
|
-
.mat3;
|
|
672
|
-
}
|
|
673
|
-
|
|
674
|
-
static Equals(m: ArrayLike<number>, n: ArrayLike<number>): boolean {
|
|
675
|
-
return m[ 0] == n[ 0] && m[ 1] == n[ 1] && m[ 2] == n[ 2] && m[ 3] == n[ 3] &&
|
|
676
|
-
m[ 4] == n[ 4] && m[ 5] == n[ 5] && m[ 6] == n[ 6] && m[ 7] == n[ 7] &&
|
|
677
|
-
m[ 8] == n[ 8] && m[ 9] == n[ 9] && m[10] == n[10] && m[11] == n[11] &&
|
|
678
|
-
m[12] == n[12] && m[13] == n[13] && m[14] == n[14] && m[15] == n[15];
|
|
679
|
-
}
|
|
680
|
-
|
|
681
|
-
static TransformDirection(M: Mat4, dir: Vec): Vec {
|
|
682
|
-
const direction = new Vec(dir);
|
|
683
|
-
const trx = new Mat4(M);
|
|
684
|
-
trx.setRow(3, new Vec(0, 0, 0, 1));
|
|
685
|
-
direction.assign(trx.multVector(direction).xyz);
|
|
686
|
-
direction.normalize();
|
|
687
|
-
return direction;
|
|
688
|
-
}
|
|
689
|
-
|
|
690
|
-
static IsNan(m: ArrayLike<number>): boolean {
|
|
691
|
-
return isNaN(m[ 0]) || isNaN(m[ 1]) || isNaN(m[ 2]) || isNaN(m[ 3]) ||
|
|
692
|
-
isNaN(m[ 4]) || isNaN(m[ 5]) || isNaN(m[ 6]) || isNaN(m[ 7]) ||
|
|
693
|
-
isNaN(m[ 8]) || isNaN(m[ 9]) || isNaN(m[10]) || isNaN(m[11]) ||
|
|
694
|
-
isNaN(m[12]) || isNaN(m[13]) || isNaN(m[14]) || isNaN(m[15]);
|
|
695
|
-
}
|
|
696
|
-
|
|
697
|
-
static IsZero(m: ArrayLike<number>): boolean {
|
|
698
|
-
return isZero(m[ 0]) && isZero(m[ 1]) && isZero(m[ 2]) && isZero(m[ 3]) &&
|
|
699
|
-
isZero(m[ 4]) && isZero(m[ 5]) && isZero(m[ 6]) && isZero(m[ 7]) &&
|
|
700
|
-
isZero(m[ 8]) && isZero(m[ 9]) && isZero(m[10]) && isZero(m[11]) &&
|
|
701
|
-
isZero(m[12]) && isZero(m[13]) && isZero(m[14]) && isZero(m[15]);
|
|
702
|
-
}
|
|
703
|
-
|
|
704
|
-
static IsIdentity(m: ArrayLike<number>): boolean {
|
|
705
|
-
return equals(m[ 0],1) && equals(m[ 1],0) && equals(m[ 2],0) && equals(m[ 3],0) &&
|
|
706
|
-
equals(m[ 4],0) && equals(m[ 5],1) && equals(m[ 6],0) && equals(m[ 7],0) &&
|
|
707
|
-
equals(m[ 8],0) && equals(m[ 9],0) && equals(m[10],1) && equals(m[11],0) &&
|
|
708
|
-
equals(m[12],0) && equals(m[13],0) && equals(m[14],0) && equals(m[15],1);
|
|
709
|
-
}
|
|
710
|
-
}
|
|
1
|
+
import { NumericArray, PI, checkArray } from "./constants";
|
|
2
|
+
import Vec from "./Vec";
|
|
3
|
+
import Mat3 from "./Mat3";
|
|
4
|
+
import { equals, isZero } from "./functions";
|
|
5
|
+
|
|
6
|
+
export default class Mat4 extends NumericArray {
|
|
7
|
+
constructor();
|
|
8
|
+
constructor(m: ArrayLike<number>);
|
|
9
|
+
constructor(m0: number, m1: number, m2: number, m3: number, m4: number, m5: number, m6: number, m7: number, m8: number, m9: number, m10: number, m11: number, m12: number, m13: number, m14: number, m15: number);
|
|
10
|
+
constructor(...args: any[]) {
|
|
11
|
+
const inMatrix = [
|
|
12
|
+
0, 0, 0, 0,
|
|
13
|
+
0, 0, 0, 0,
|
|
14
|
+
0, 0, 0, 0,
|
|
15
|
+
0, 0, 0, 0
|
|
16
|
+
];
|
|
17
|
+
|
|
18
|
+
// Create from matrix3
|
|
19
|
+
if (args.length === 9) {
|
|
20
|
+
inMatrix[0] = args[0];
|
|
21
|
+
inMatrix[1] = args[1];
|
|
22
|
+
inMatrix[2] = args[2];
|
|
23
|
+
|
|
24
|
+
inMatrix[4] = args[3];
|
|
25
|
+
inMatrix[5] = args[4];
|
|
26
|
+
inMatrix[6] = args[5];
|
|
27
|
+
|
|
28
|
+
inMatrix[8] = args[6];
|
|
29
|
+
inMatrix[9] = args[7];
|
|
30
|
+
inMatrix[10] = args[8];
|
|
31
|
+
|
|
32
|
+
inMatrix[15] = 1;
|
|
33
|
+
}
|
|
34
|
+
else if (args.length === 1 && args[0].length === 9) {
|
|
35
|
+
inMatrix[0] = args[0][0];
|
|
36
|
+
inMatrix[1] = args[0][1];
|
|
37
|
+
inMatrix[2] = args[0][2];
|
|
38
|
+
|
|
39
|
+
inMatrix[4] = args[0][3];
|
|
40
|
+
inMatrix[5] = args[0][4];
|
|
41
|
+
inMatrix[6] = args[0][5];
|
|
42
|
+
|
|
43
|
+
inMatrix[8] = args[0][6];
|
|
44
|
+
inMatrix[9] = args[0][7];
|
|
45
|
+
inMatrix[10] = args[0][8];
|
|
46
|
+
|
|
47
|
+
inMatrix[15] = 1;
|
|
48
|
+
}
|
|
49
|
+
// Create from matrix4
|
|
50
|
+
else if (args.length === 16) {
|
|
51
|
+
inMatrix[0 ] = args[0];
|
|
52
|
+
inMatrix[1 ] = args[1 ];
|
|
53
|
+
inMatrix[2 ] = args[2 ];
|
|
54
|
+
inMatrix[3 ] = args[3 ];
|
|
55
|
+
|
|
56
|
+
inMatrix[4 ] = args[4 ];
|
|
57
|
+
inMatrix[5 ] = args[5 ];
|
|
58
|
+
inMatrix[6 ] = args[6 ];
|
|
59
|
+
inMatrix[7 ] = args[7 ];
|
|
60
|
+
|
|
61
|
+
inMatrix[8 ] = args[8 ];
|
|
62
|
+
inMatrix[9 ] = args[9 ];
|
|
63
|
+
inMatrix[10] = args[10];
|
|
64
|
+
inMatrix[11] = args[11];
|
|
65
|
+
|
|
66
|
+
inMatrix[12] = args[12];
|
|
67
|
+
inMatrix[13] = args[13];
|
|
68
|
+
inMatrix[14] = args[14];
|
|
69
|
+
inMatrix[15] = args[15];
|
|
70
|
+
}
|
|
71
|
+
else if (args.length === 1 && args[0].length === 16) {
|
|
72
|
+
inMatrix[0 ] = args[0][0];
|
|
73
|
+
inMatrix[1 ] = args[0][1 ];
|
|
74
|
+
inMatrix[2 ] = args[0][2 ];
|
|
75
|
+
inMatrix[3 ] = args[0][3 ];
|
|
76
|
+
|
|
77
|
+
inMatrix[4 ] = args[0][4 ];
|
|
78
|
+
inMatrix[5 ] = args[0][5 ];
|
|
79
|
+
inMatrix[6 ] = args[0][6 ];
|
|
80
|
+
inMatrix[7 ] = args[0][7 ];
|
|
81
|
+
|
|
82
|
+
inMatrix[8 ] = args[0][8 ];
|
|
83
|
+
inMatrix[9 ] = args[0][9 ];
|
|
84
|
+
inMatrix[10] = args[0][10];
|
|
85
|
+
inMatrix[11] = args[0][11];
|
|
86
|
+
|
|
87
|
+
inMatrix[12] = args[0][12];
|
|
88
|
+
inMatrix[13] = args[0][13];
|
|
89
|
+
inMatrix[14] = args[0][14];
|
|
90
|
+
inMatrix[15] = args[0][15];
|
|
91
|
+
}
|
|
92
|
+
else if (args.length != 0) {
|
|
93
|
+
throw new Error(`Invalid parameter size in Matrix3 constructor`);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
super(inMatrix);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
////// Initializers
|
|
100
|
+
identity(): this {
|
|
101
|
+
this[0 ] = 1; this[1 ] = 0; this[2 ] = 0; this[3 ] = 0
|
|
102
|
+
this[4 ] = 0; this[5 ] = 1; this[6 ] = 0; this[7 ] = 0
|
|
103
|
+
this[8 ] = 0; this[9 ] = 0; this[10] = 1; this[11] = 0
|
|
104
|
+
this[12] = 0; this[13] = 0; this[14] = 0; this[15] = 1
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
zero(): this {
|
|
109
|
+
this[ 0] = 0; this[ 1] = 0; this[ 2] = 0; this[ 3] = 0;
|
|
110
|
+
this[ 4] = 0; this[ 5] = 0; this[ 6] = 0; this[ 7] = 0;
|
|
111
|
+
this[ 8] = 0; this[ 9] = 0; this[10] = 0; this[11] = 0;
|
|
112
|
+
this[12] = 0; this[13] = 0; this[14] = 0; this[15] = 0;
|
|
113
|
+
return this;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
perspective(fovy: number, aspect: number, nearPlane: number, farPlane: number): this {
|
|
117
|
+
let fovy2 = Math.tan(fovy * PI / 360.0) * nearPlane;
|
|
118
|
+
let fovy2aspect = fovy2 * aspect;
|
|
119
|
+
this.frustum(-fovy2aspect,fovy2aspect,-fovy2,fovy2,nearPlane,farPlane);
|
|
120
|
+
return this;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
frustum(left: number, right: number, bottom: number, top: number, nearPlane: number, farPlane: number): this {
|
|
124
|
+
let A = right - left;
|
|
125
|
+
let B = top-bottom;
|
|
126
|
+
let C = farPlane-nearPlane;
|
|
127
|
+
|
|
128
|
+
this.setRow(0, new Vec(nearPlane*2.0/A, 0.0, 0.0, 0.0));
|
|
129
|
+
this.setRow(1, new Vec(0.0, nearPlane*2.0/B, 0.0, 0.0));
|
|
130
|
+
this.setRow(2, new Vec((right+left)/A, (top+bottom)/B, -(farPlane+nearPlane)/C, -1.0));
|
|
131
|
+
this.setRow(3, new Vec(0.0, 0.0, -(farPlane*nearPlane*2.0)/C, 0.0));
|
|
132
|
+
|
|
133
|
+
return this;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
ortho(left: number, right: number, bottom: number, top: number, nearPlane: number, farPlane: number): this {
|
|
137
|
+
let m = right-left;
|
|
138
|
+
let l = top-bottom;
|
|
139
|
+
let k = farPlane-nearPlane;;
|
|
140
|
+
|
|
141
|
+
this[0] = 2/m; this[1] = 0; this[2] = 0; this[3] = 0;
|
|
142
|
+
this[4] = 0; this[5] = 2/l; this[6] = 0; this[7] = 0;
|
|
143
|
+
this[8] = 0; this[9] = 0; this[10] = -2/k; this[11]= 0;
|
|
144
|
+
this[12]=-(left+right)/m; this[13] = -(top+bottom)/l; this[14] = -(farPlane+nearPlane)/k; this[15]=1;
|
|
145
|
+
|
|
146
|
+
return this;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
lookAt(p_eye: Vec, p_center: Vec, p_up: Vec): this {
|
|
150
|
+
this.identity();
|
|
151
|
+
|
|
152
|
+
const y = new Vec(p_up);
|
|
153
|
+
const z = Vec.Sub(p_eye,p_center);
|
|
154
|
+
z.normalize();
|
|
155
|
+
const x = Vec.Cross(y,z) as Vec;
|
|
156
|
+
x.normalize();
|
|
157
|
+
y.normalize();
|
|
158
|
+
|
|
159
|
+
this.m00 = x.x;
|
|
160
|
+
this.m10 = x.y;
|
|
161
|
+
this.m20 = x.z;
|
|
162
|
+
this.m30 = -Vec.Dot(x, p_eye);
|
|
163
|
+
this.m01 = y.x;
|
|
164
|
+
this.m11 = y.y;
|
|
165
|
+
this.m21 = y.z;
|
|
166
|
+
this.m31 = -Vec.Dot(y, p_eye);
|
|
167
|
+
this.m02 = z.x;
|
|
168
|
+
this.m12 = z.y;
|
|
169
|
+
this.m22 = z.z;
|
|
170
|
+
this.m32 = -Vec.Dot(z, p_eye);
|
|
171
|
+
this.m03 = 0;
|
|
172
|
+
this.m13 = 0;
|
|
173
|
+
this.m23 = 0;
|
|
174
|
+
this.m33 = 1;
|
|
175
|
+
|
|
176
|
+
return this;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
///// Setters and getters
|
|
183
|
+
get m00(): number { return this[0]; }
|
|
184
|
+
get m01(): number { return this[1]; }
|
|
185
|
+
get m02(): number { return this[2]; }
|
|
186
|
+
get m03(): number { return this[3]; }
|
|
187
|
+
get m10(): number { return this[4]; }
|
|
188
|
+
get m11(): number { return this[5]; }
|
|
189
|
+
get m12(): number { return this[6]; }
|
|
190
|
+
get m13(): number { return this[7]; }
|
|
191
|
+
get m20(): number { return this[8]; }
|
|
192
|
+
get m21(): number { return this[9]; }
|
|
193
|
+
get m22(): number { return this[10]; }
|
|
194
|
+
get m23(): number { return this[11]; }
|
|
195
|
+
get m30(): number { return this[12]; }
|
|
196
|
+
get m31(): number { return this[13]; }
|
|
197
|
+
get m32(): number { return this[14]; }
|
|
198
|
+
get m33(): number { return this[15]; }
|
|
199
|
+
|
|
200
|
+
set m00(v: number) { this[0] = v; }
|
|
201
|
+
set m01(v: number) { this[1] = v; }
|
|
202
|
+
set m02(v: number) { this[2] = v; }
|
|
203
|
+
set m03(v: number) { this[3] = v; }
|
|
204
|
+
set m10(v: number) { this[4] = v; }
|
|
205
|
+
set m11(v: number) { this[5] = v; }
|
|
206
|
+
set m12(v: number) { this[6] = v; }
|
|
207
|
+
set m13(v: number) { this[7] = v; }
|
|
208
|
+
set m20(v: number) { this[8] = v; }
|
|
209
|
+
set m21(v: number) { this[9] = v; }
|
|
210
|
+
set m22(v: number) { this[10] = v; }
|
|
211
|
+
set m23(v: number) { this[11] = v; }
|
|
212
|
+
set m30(v: number) { this[12] = v; }
|
|
213
|
+
set m31(v: number) { this[13] = v; }
|
|
214
|
+
set m32(v: number) { this[14] = v; }
|
|
215
|
+
set m33(v: number) { this[15] = v; }
|
|
216
|
+
|
|
217
|
+
get mat3(): Mat3 {
|
|
218
|
+
return new Mat3(this[0], this[1], this[ 2],
|
|
219
|
+
this[4], this[5], this[ 6],
|
|
220
|
+
this[8], this[9], this[10]);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
get forwardVector(): Vec {
|
|
224
|
+
return Mat4.TransformDirection(this, new Vec(0.0, 0.0, 1.0));
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
get rightVector(): Vec {
|
|
228
|
+
return Mat4.TransformDirection(this, new Vec(1.0, 0.0, 0.0));
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
get upVector(): Vec {
|
|
232
|
+
return Mat4.TransformDirection(this, new Vec(0.0, 1.0, 0.0));
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
get backwardVector(): Vec {
|
|
236
|
+
return Mat4.TransformDirection(this, new Vec(0.0, 0.0, -1.0));
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
get leftVector(): Vec {
|
|
240
|
+
return Mat4.TransformDirection(this, new Vec(-1.0, 0.0, 0.0));
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
get downVector(): Vec {
|
|
244
|
+
return Mat4.TransformDirection(this, new Vec(0.0, -1.0, 0.0));
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
get translation(): Vec {
|
|
248
|
+
return new Vec(this[12], this[13], this[14]);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
row(i: number): Vec {
|
|
252
|
+
return new Vec(
|
|
253
|
+
this[i * 4],
|
|
254
|
+
this[i * 4 + 1],
|
|
255
|
+
this[i * 4 + 2],
|
|
256
|
+
this[i * 4 + 3]);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
setRow(i: number, a: ArrayLike<number>): this;
|
|
260
|
+
setRow(i: number, x: number, y: number, z: number, w: number): this;
|
|
261
|
+
setRow(i: number, a: number | ArrayLike<number>, y?: number, z?: number, w?: number): this {
|
|
262
|
+
if (typeof a === 'object' && a.length >= 4) {
|
|
263
|
+
this[i * 4] = a[0];
|
|
264
|
+
this[i * 4 + 1] = a[1];
|
|
265
|
+
this[i * 4 + 2] = a[2];
|
|
266
|
+
this[i * 4 + 3] = a[3];
|
|
267
|
+
}
|
|
268
|
+
else if (typeof a === 'object' && a.length == 3) {
|
|
269
|
+
this[i * 4] = a[0];
|
|
270
|
+
this[i * 4 + 1] = a[1];
|
|
271
|
+
this[i * 4 + 2] = a[2];
|
|
272
|
+
}
|
|
273
|
+
else if (typeof(a) === "number" &&
|
|
274
|
+
typeof(y) === "number" &&
|
|
275
|
+
typeof(z) === "number" &&
|
|
276
|
+
typeof(w) === "number"
|
|
277
|
+
) {
|
|
278
|
+
this[i * 4] = a;
|
|
279
|
+
this[i * 4 + 1] = y;
|
|
280
|
+
this[i * 4 + 2] = z;
|
|
281
|
+
this[i * 4 + 3] = w;
|
|
282
|
+
}
|
|
283
|
+
else {
|
|
284
|
+
throw new Error(`Invalid parameter setting matrix row`);
|
|
285
|
+
}
|
|
286
|
+
return this;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
col(i: number): Vec {
|
|
290
|
+
return new Vec(
|
|
291
|
+
this[i],
|
|
292
|
+
this[i + 4],
|
|
293
|
+
this[i + 4 * 2],
|
|
294
|
+
this[i + 4 * 3]
|
|
295
|
+
)
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
setCol(i: number, a: ArrayLike<number>): this;
|
|
299
|
+
setCol(i: number, x: number, y: number, z: number, w: number): this;
|
|
300
|
+
setCol(i: number, a: number | ArrayLike<number>, y?: number, z?: number, w?: number): this {
|
|
301
|
+
if (typeof a === 'object' && a.length >= 4) {
|
|
302
|
+
this[i] = a[0];
|
|
303
|
+
this[i + 4] = a[1];
|
|
304
|
+
this[i + 4 * 2] = a[2];
|
|
305
|
+
this[i + 4 * 3] = a[3];
|
|
306
|
+
}
|
|
307
|
+
else if (typeof(a) === "number" &&
|
|
308
|
+
typeof(y) === "number" &&
|
|
309
|
+
typeof(z) === "number" &&
|
|
310
|
+
typeof(w) === "number"
|
|
311
|
+
) {
|
|
312
|
+
this[i] = a;
|
|
313
|
+
this[i + 4] = y;
|
|
314
|
+
this[i + 4 * 2] = z;
|
|
315
|
+
this[i + 4 * 3] = w;
|
|
316
|
+
}
|
|
317
|
+
else {
|
|
318
|
+
throw new Error(`Invalid parameter setting matrix row`);
|
|
319
|
+
}
|
|
320
|
+
return this;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
assign(a: ArrayLike<number>): this {
|
|
324
|
+
if (a.length==9) {
|
|
325
|
+
this[0] = a[0]; this[1] = a[1]; this[2] = a[2]; this[3] = 0;
|
|
326
|
+
this[4] = a[3]; this[5] = a[4]; this[6] = a[5]; this[7] = 0;
|
|
327
|
+
this[8] = a[6]; this[9] = a[7]; this[10] = a[8]; this[11] = 0;
|
|
328
|
+
this[12] = 0; this[13] = 0; this[14] = 0; this[15] = 1;
|
|
329
|
+
}
|
|
330
|
+
else if (a.length==16) {
|
|
331
|
+
this[0] = a[0]; this[1] = a[1]; this[2] = a[2]; this[3] = a[3];
|
|
332
|
+
this[4] = a[4]; this[5] = a[5]; this[6] = a[6]; this[7] = a[7];
|
|
333
|
+
this[8] = a[8]; this[9] = a[9]; this[10] = a[10]; this[11] = a[11];
|
|
334
|
+
this[12] = a[12]; this[13] = a[13]; this[14] = a[14]; this[15] = a[15];
|
|
335
|
+
}
|
|
336
|
+
return this;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
translate(x: number | ArrayLike<number>, y?: number, z?: number): this {
|
|
340
|
+
if (checkArray(x, 3)) {
|
|
341
|
+
y = (x as ArrayLike<number>)[1];
|
|
342
|
+
z = (x as ArrayLike<number>)[2];
|
|
343
|
+
x = (x as ArrayLike<number>)[0];
|
|
344
|
+
}
|
|
345
|
+
this.mult(Mat4.MakeTranslation(x, y, z));
|
|
346
|
+
return this;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
rotate(alpha: number, x: number | ArrayLike<number>, y?: number, z?: number): this {
|
|
350
|
+
if (checkArray(x, 3)) {
|
|
351
|
+
y = (x as ArrayLike<number>)[1];
|
|
352
|
+
z = (x as ArrayLike<number>)[2];
|
|
353
|
+
x = (x as ArrayLike<number>)[0];
|
|
354
|
+
}
|
|
355
|
+
this.mult(Mat4.MakeRotation(alpha, x as number, y ?? 0, z ?? 0));
|
|
356
|
+
return this;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
scale(x: number | ArrayLike<number>, y?: number, z?: number): this {
|
|
360
|
+
if (checkArray(x, 3)) {
|
|
361
|
+
y = (x as ArrayLike<number>)[1];
|
|
362
|
+
z = (x as ArrayLike<number>)[2];
|
|
363
|
+
x = (x as ArrayLike<number>)[0];
|
|
364
|
+
}
|
|
365
|
+
this.mult(Mat4.MakeScale(x, y, z));
|
|
366
|
+
return this;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
toString(): string {
|
|
370
|
+
return `[ ${this[ 0]}, ${this[ 1]}, ${this[ 2]}, ${this[ 3]}\n` +
|
|
371
|
+
` ${this[ 4]}, ${this[ 5]}, ${this[ 6]}, ${this[ 7]}\n` +
|
|
372
|
+
` ${this[ 8]}, ${this[ 9]}, ${this[10]}, ${this[11]}\n` +
|
|
373
|
+
` ${this[12]}, ${this[13]}, ${this[14]}, ${this[15]} ]`;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
setScale(x: number, y: number, z: number): this {
|
|
377
|
+
const rx = new Vec(this[0], this[4], this[8]).normalize().scale(x);
|
|
378
|
+
const ry = new Vec(this[1], this[5], this[9]).normalize().scale(y);
|
|
379
|
+
const rz = new Vec(this[2], this[6], this[10]).normalize().scale(z);
|
|
380
|
+
this[0] = rx.x; this[4] = rx.y; this[8] = rx.z;
|
|
381
|
+
this[1] = ry.x; this[5] = ry.y; this[9] = ry.z;
|
|
382
|
+
this[2] = rz.x; this[6] = rz.y; this[10] = rz.z;
|
|
383
|
+
return this;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
setPosition(pos: Vec): this;
|
|
387
|
+
setPosition(x: number, y: number, z: number): this;
|
|
388
|
+
setPosition(pos: number | Vec, y?: number, z?: number): this {
|
|
389
|
+
if (typeof(pos) === "number" && y !== undefined && z !== undefined) {
|
|
390
|
+
this[12] = pos;
|
|
391
|
+
this[13] = y;
|
|
392
|
+
this[14] = z;
|
|
393
|
+
}
|
|
394
|
+
else if (typeof pos === 'object') {
|
|
395
|
+
this[12] = pos.x;
|
|
396
|
+
this[13] = pos.y;
|
|
397
|
+
this[14] = pos.z;
|
|
398
|
+
}
|
|
399
|
+
return this;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
setRotation(rotationMatrix: ArrayLike<number>): this {
|
|
403
|
+
if (rotationMatrix.length === 9) {
|
|
404
|
+
this[0] = rotationMatrix[0]; this[1] = rotationMatrix[1]; this[2] = rotationMatrix[2];
|
|
405
|
+
this[4] = rotationMatrix[3]; this[5] = rotationMatrix[4]; this[6] = rotationMatrix[5];
|
|
406
|
+
this[8] = rotationMatrix[6]; this[9] = rotationMatrix[7]; this[10] = rotationMatrix[8];
|
|
407
|
+
}
|
|
408
|
+
else if (rotationMatrix.length === 16) {
|
|
409
|
+
this[0] = rotationMatrix[0]; this[1] = rotationMatrix[1]; this[2] = rotationMatrix[2];
|
|
410
|
+
this[4] = rotationMatrix[4]; this[5] = rotationMatrix[5]; this[6] = rotationMatrix[6];
|
|
411
|
+
this[8] = rotationMatrix[8]; this[9] = rotationMatrix[9]; this[10] = rotationMatrix[10];
|
|
412
|
+
}
|
|
413
|
+
else {
|
|
414
|
+
throw new Error("Invalid parameter setting rotation matrix");
|
|
415
|
+
}
|
|
416
|
+
return this;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
mult(a: number): this;
|
|
420
|
+
mult(a: Mat4): this;
|
|
421
|
+
mult(a: number | Mat4): this {
|
|
422
|
+
if (typeof(a)=='number') {
|
|
423
|
+
this[ 0] *= a; this[ 1] *= a; this[ 2] *= a; this[ 3] *= a;
|
|
424
|
+
this[ 4] *= a; this[ 5] *= a; this[ 6] *= a; this[ 7] *= a;
|
|
425
|
+
this[ 8] *= a; this[ 9] *= a; this[10] *= a; this[11] *= a;
|
|
426
|
+
this[12] *= a; this[13] *= a; this[14] *= a; this[15] *= a;
|
|
427
|
+
return this;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
const r0 = this.row(0);
|
|
431
|
+
const r1 = this.row(1);
|
|
432
|
+
const r2 = this.row(2);
|
|
433
|
+
const r3 = this.row(3);
|
|
434
|
+
const c0 = a.col(0);
|
|
435
|
+
const c1 = a.col(1);
|
|
436
|
+
const c2 = a.col(2);
|
|
437
|
+
const c3 = a.col(3);
|
|
438
|
+
|
|
439
|
+
this[0 ] = Vec.Dot(r0, c0); this[1 ] = Vec.Dot(r0, c1); this[2 ] = Vec.Dot(r0, c2); this[3 ] = Vec.Dot(r0, c3);
|
|
440
|
+
this[4 ] = Vec.Dot(r1, c0); this[5 ] = Vec.Dot(r1, c1); this[6 ] = Vec.Dot(r1, c2); this[7 ] = Vec.Dot(r1, c3);
|
|
441
|
+
this[8 ] = Vec.Dot(r2, c0); this[9 ] = Vec.Dot(r2, c1); this[10] = Vec.Dot(r2, c2); this[11] = Vec.Dot(r2, c3);
|
|
442
|
+
this[12] = Vec.Dot(r3, c0); this[13] = Vec.Dot(r3, c1); this[14] = Vec.Dot(r3, c2); this[15] = Vec.Dot(r3, c3);
|
|
443
|
+
|
|
444
|
+
return this;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
multVector(vec: ArrayLike<number>): Vec {
|
|
448
|
+
if (vec.length<3) {
|
|
449
|
+
throw new Error("Invalid parameter multiplying Mat4 by vector");
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
const x = vec[0];
|
|
453
|
+
const y = vec[1];
|
|
454
|
+
const z = vec[2];
|
|
455
|
+
const w = vec.length >3 ? vec[3] : 1.0;
|
|
456
|
+
|
|
457
|
+
return new Vec( this[0] * x + this[4] * y + this[ 8] * z + this[12] * w,
|
|
458
|
+
this[1] * x + this[5] * y + this[ 9] * z + this[13] * w,
|
|
459
|
+
this[2] * x + this[6] * y + this[10] * z + this[14] * w,
|
|
460
|
+
this[3] * x + this[7] * y + this[11] * z + this[15] * w);
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
invert(): this {
|
|
464
|
+
const a00 = this[0], a01 = this[1], a02 = this[2], a03 = this[3],
|
|
465
|
+
a10 = this[4], a11 = this[5], a12 = this[6], a13 = this[7],
|
|
466
|
+
a20 = this[8], a21 = this[9], a22 = this[10], a23 = this[11],
|
|
467
|
+
a30 = this[12], a31 = this[13], a32 = this[14], a33 = this[15];
|
|
468
|
+
|
|
469
|
+
const b00 = a00 * a11 - a01 * a10,
|
|
470
|
+
b01 = a00 * a12 - a02 * a10,
|
|
471
|
+
b02 = a00 * a13 - a03 * a10,
|
|
472
|
+
b03 = a01 * a12 - a02 * a11,
|
|
473
|
+
b04 = a01 * a13 - a03 * a11,
|
|
474
|
+
b05 = a02 * a13 - a03 * a12,
|
|
475
|
+
b06 = a20 * a31 - a21 * a30,
|
|
476
|
+
b07 = a20 * a32 - a22 * a30,
|
|
477
|
+
b08 = a20 * a33 - a23 * a30,
|
|
478
|
+
b09 = a21 * a32 - a22 * a31,
|
|
479
|
+
b10 = a21 * a33 - a23 * a31,
|
|
480
|
+
b11 = a22 * a33 - a23 * a32;
|
|
481
|
+
|
|
482
|
+
let det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
|
|
483
|
+
|
|
484
|
+
if (!det) {
|
|
485
|
+
this.zero();
|
|
486
|
+
}
|
|
487
|
+
else {
|
|
488
|
+
det = 1.0 / det;
|
|
489
|
+
|
|
490
|
+
this[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det;
|
|
491
|
+
this[1] = (a02 * b10 - a01 * b11 - a03 * b09) * det;
|
|
492
|
+
this[2] = (a31 * b05 - a32 * b04 + a33 * b03) * det;
|
|
493
|
+
this[3] = (a22 * b04 - a21 * b05 - a23 * b03) * det;
|
|
494
|
+
this[4] = (a12 * b08 - a10 * b11 - a13 * b07) * det;
|
|
495
|
+
this[5] = (a00 * b11 - a02 * b08 + a03 * b07) * det;
|
|
496
|
+
this[6] = (a32 * b02 - a30 * b05 - a33 * b01) * det;
|
|
497
|
+
this[7] = (a20 * b05 - a22 * b02 + a23 * b01) * det;
|
|
498
|
+
this[8] = (a10 * b10 - a11 * b08 + a13 * b06) * det;
|
|
499
|
+
this[9] = (a01 * b08 - a00 * b10 - a03 * b06) * det;
|
|
500
|
+
this[10] = (a30 * b04 - a31 * b02 + a33 * b00) * det;
|
|
501
|
+
this[11] = (a21 * b02 - a20 * b04 - a23 * b00) * det;
|
|
502
|
+
this[12] = (a11 * b07 - a10 * b09 - a12 * b06) * det;
|
|
503
|
+
this[13] = (a00 * b09 - a01 * b07 + a02 * b06) * det;
|
|
504
|
+
this[14] = (a31 * b01 - a30 * b03 - a32 * b00) * det;
|
|
505
|
+
this[15] = (a20 * b03 - a21 * b01 + a22 * b00) * det;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
return this;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
traspose(): this {
|
|
512
|
+
const r0 = new Vec(this[0], this[4], this[ 8], this[12]);
|
|
513
|
+
const r1 = new Vec(this[1], this[5], this[ 9], this[13]);
|
|
514
|
+
const r2 = new Vec(this[2], this[6], this[10], this[14]);
|
|
515
|
+
const r3 = new Vec(this[3], this[7], this[11], this[15]);
|
|
516
|
+
|
|
517
|
+
this.setRow(0, r0);
|
|
518
|
+
this.setRow(1, r1);
|
|
519
|
+
this.setRow(2, r2);
|
|
520
|
+
this.setRow(3, r3);
|
|
521
|
+
return this;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
///////// Factory methods
|
|
525
|
+
static MakeIdentity(): Mat4 {
|
|
526
|
+
const m = new Mat4();
|
|
527
|
+
return m.identity();
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
static MakeZero(): Mat4 {
|
|
531
|
+
const m = new Mat4();
|
|
532
|
+
return m.zero();
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
static MakeWithQuaternion(q: ArrayLike<number>): Mat4 {
|
|
536
|
+
const m = Mat4.MakeIdentity();
|
|
537
|
+
m.setRotation(Mat3.MakeWithQuaternion(q));
|
|
538
|
+
return m;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
static MakeTranslation(x: number | ArrayLike<number>, y?: number, z?: number): Mat4 {
|
|
542
|
+
if (checkArray(x, 3)) {
|
|
543
|
+
y = (x as ArrayLike<number>)[1];
|
|
544
|
+
z = (x as ArrayLike<number>)[2];
|
|
545
|
+
x = (x as ArrayLike<number>)[0];
|
|
546
|
+
}
|
|
547
|
+
return new Mat4(
|
|
548
|
+
1.0, 0.0, 0.0, 0.0,
|
|
549
|
+
0.0, 1.0, 0.0, 0.0,
|
|
550
|
+
0.0, 0.0, 1.0, 0.0,
|
|
551
|
+
x as number, y as number, z as number, 1.0
|
|
552
|
+
);
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
static MakeRotation(alpha: number, x: number, y: number, z: number): Mat4 {
|
|
556
|
+
const axis = new Vec(x,y,z);
|
|
557
|
+
axis.normalize();
|
|
558
|
+
|
|
559
|
+
var cosAlpha = Math.cos(alpha);
|
|
560
|
+
var acosAlpha = 1.0 - cosAlpha;
|
|
561
|
+
var sinAlpha = Math.sin(alpha);
|
|
562
|
+
|
|
563
|
+
return new Mat4(
|
|
564
|
+
axis.x * axis.x * acosAlpha + cosAlpha, axis.x * axis.y * acosAlpha + axis.z * sinAlpha, axis.x * axis.z * acosAlpha - axis.y * sinAlpha, 0,
|
|
565
|
+
axis.y * axis.x * acosAlpha - axis.z * sinAlpha, axis.y * axis.y * acosAlpha + cosAlpha, axis.y * axis.z * acosAlpha + axis.x * sinAlpha, 0,
|
|
566
|
+
axis.z * axis.x * acosAlpha + axis.y * sinAlpha, axis.z * axis.y * acosAlpha - axis.x * sinAlpha, axis.z * axis.z * acosAlpha + cosAlpha, 0,
|
|
567
|
+
0,0,0,1
|
|
568
|
+
);
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
static MakeRotationWithDirection(direction: Vec, up?: Vec): Mat4 {
|
|
572
|
+
const trx = Mat4.MakeIdentity();
|
|
573
|
+
trx.setRotation(Mat3.MakeRotationWithDirection(direction, up));
|
|
574
|
+
return trx;
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
static MakeScale(x: number | ArrayLike<number>, y?: number, z?: number): Mat4 {
|
|
578
|
+
if (checkArray(x, 3)) {
|
|
579
|
+
y = (x as ArrayLike<number>)[1];
|
|
580
|
+
z = (x as ArrayLike<number>)[2];
|
|
581
|
+
x = (x as ArrayLike<number>)[0];
|
|
582
|
+
}
|
|
583
|
+
return new Mat4(
|
|
584
|
+
x as number, 0, 0, 0,
|
|
585
|
+
0, y as number, 0, 0,
|
|
586
|
+
0, 0, z as number, 0,
|
|
587
|
+
0, 0, 0, 1
|
|
588
|
+
)
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
|
|
592
|
+
static MakePerspective(fovy: number, aspect: number, nearPlane: number, farPlane: number): Mat4 {
|
|
593
|
+
return (new Mat4()).perspective(fovy, aspect, nearPlane, farPlane);
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
static MakeFrustum(left: number, right: number, bottom: number, top: number, nearPlane: number, farPlane: number): Mat4 {
|
|
597
|
+
return (new Mat4()).frustum(left, right, bottom, top, nearPlane, farPlane);
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
static MakeOrtho(left: number, right: number, bottom: number, top: number, nearPlane: number, farPlane: number): Mat4 {
|
|
601
|
+
return (new Mat4()).ortho(left, right, bottom, top, nearPlane, farPlane);
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
static MakeLookAt(origin: Vec, target: Vec, up: Vec): Mat4 {
|
|
605
|
+
return (new Mat4()).lookAt(origin,target,up);
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
// Other static methods. This function multyplies two B x A matrices. It works opposite than the non-static mult() function:
|
|
609
|
+
// A.mult(B) is the same as Mat4.Mult(B,A)
|
|
610
|
+
static Mult(A: Mat4, B: Mat4): Mat4 {
|
|
611
|
+
const result = new Mat4(B);
|
|
612
|
+
return result.mult(A);
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
static Unproject(x: number, y: number, depth: number, mvMat: Mat4, pMat: Mat4, viewport: Vec): Vec {
|
|
616
|
+
let mvp = new Mat4(pMat);
|
|
617
|
+
mvp.mult(mvMat);
|
|
618
|
+
mvp.invert();
|
|
619
|
+
|
|
620
|
+
const vin = new Vec(((x - viewport.y) / viewport.width) * 2.0 - 1.0,
|
|
621
|
+
((y - viewport.x) / viewport.height) * 2.0 - 1.0,
|
|
622
|
+
depth * 2.0 - 1.0,
|
|
623
|
+
1.0);
|
|
624
|
+
|
|
625
|
+
const result = mvp.multVector(vin);
|
|
626
|
+
if (result.z==0) {
|
|
627
|
+
result.setValue(0, 0, 0, 0);
|
|
628
|
+
}
|
|
629
|
+
else {
|
|
630
|
+
result.setValue(result.x/result.w,
|
|
631
|
+
result.y/result.w,
|
|
632
|
+
result.z/result.w,
|
|
633
|
+
result.w/result.w);
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
return result;
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
static GetScale(m: ArrayLike<number>): Vec {
|
|
640
|
+
return new Vec(
|
|
641
|
+
Vec.Magnitude([m[1], m[5], m[9]]),
|
|
642
|
+
Vec.Magnitude([m[0], m[4], m[8]]),
|
|
643
|
+
Vec.Magnitude([m[2], m[6], m[10]])
|
|
644
|
+
);
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
static GetRotation(m: ArrayLike<number>): Mat4 {
|
|
648
|
+
const scale = Mat4.GetScale(m);
|
|
649
|
+
return new Mat4(
|
|
650
|
+
m[0] / scale.x, m[1] / scale.y, m[ 2] / scale.z, 0,
|
|
651
|
+
m[4] / scale.x, m[5] / scale.y, m[ 6] / scale.z, 0,
|
|
652
|
+
m[8] / scale.x, m[9] / scale.y, m[10] / scale.z, 0,
|
|
653
|
+
0, 0, 0, 1
|
|
654
|
+
);
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
static GetPosition(m: ArrayLike<number>): Vec {
|
|
658
|
+
return new Vec(m[12], m[13], m[14]);
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
static GetInverted(m: Mat4): Mat4 {
|
|
662
|
+
const inverted = new Mat4(m);
|
|
663
|
+
inverted.invert();
|
|
664
|
+
return inverted;
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
static GetNormalMatrix(m: Mat4): Mat3 {
|
|
668
|
+
return new Mat4(m)
|
|
669
|
+
.invert()
|
|
670
|
+
.traspose()
|
|
671
|
+
.mat3;
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
static Equals(m: ArrayLike<number>, n: ArrayLike<number>): boolean {
|
|
675
|
+
return m[ 0] == n[ 0] && m[ 1] == n[ 1] && m[ 2] == n[ 2] && m[ 3] == n[ 3] &&
|
|
676
|
+
m[ 4] == n[ 4] && m[ 5] == n[ 5] && m[ 6] == n[ 6] && m[ 7] == n[ 7] &&
|
|
677
|
+
m[ 8] == n[ 8] && m[ 9] == n[ 9] && m[10] == n[10] && m[11] == n[11] &&
|
|
678
|
+
m[12] == n[12] && m[13] == n[13] && m[14] == n[14] && m[15] == n[15];
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
static TransformDirection(M: Mat4, dir: Vec): Vec {
|
|
682
|
+
const direction = new Vec(dir);
|
|
683
|
+
const trx = new Mat4(M);
|
|
684
|
+
trx.setRow(3, new Vec(0, 0, 0, 1));
|
|
685
|
+
direction.assign(trx.multVector(direction).xyz);
|
|
686
|
+
direction.normalize();
|
|
687
|
+
return direction;
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
static IsNan(m: ArrayLike<number>): boolean {
|
|
691
|
+
return isNaN(m[ 0]) || isNaN(m[ 1]) || isNaN(m[ 2]) || isNaN(m[ 3]) ||
|
|
692
|
+
isNaN(m[ 4]) || isNaN(m[ 5]) || isNaN(m[ 6]) || isNaN(m[ 7]) ||
|
|
693
|
+
isNaN(m[ 8]) || isNaN(m[ 9]) || isNaN(m[10]) || isNaN(m[11]) ||
|
|
694
|
+
isNaN(m[12]) || isNaN(m[13]) || isNaN(m[14]) || isNaN(m[15]);
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
static IsZero(m: ArrayLike<number>): boolean {
|
|
698
|
+
return isZero(m[ 0]) && isZero(m[ 1]) && isZero(m[ 2]) && isZero(m[ 3]) &&
|
|
699
|
+
isZero(m[ 4]) && isZero(m[ 5]) && isZero(m[ 6]) && isZero(m[ 7]) &&
|
|
700
|
+
isZero(m[ 8]) && isZero(m[ 9]) && isZero(m[10]) && isZero(m[11]) &&
|
|
701
|
+
isZero(m[12]) && isZero(m[13]) && isZero(m[14]) && isZero(m[15]);
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
static IsIdentity(m: ArrayLike<number>): boolean {
|
|
705
|
+
return equals(m[ 0],1) && equals(m[ 1],0) && equals(m[ 2],0) && equals(m[ 3],0) &&
|
|
706
|
+
equals(m[ 4],0) && equals(m[ 5],1) && equals(m[ 6],0) && equals(m[ 7],0) &&
|
|
707
|
+
equals(m[ 8],0) && equals(m[ 9],0) && equals(m[10],1) && equals(m[11],0) &&
|
|
708
|
+
equals(m[12],0) && equals(m[13],0) && equals(m[14],0) && equals(m[15],1);
|
|
709
|
+
}
|
|
710
|
+
}
|