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/Mat3.ts
CHANGED
|
@@ -1,259 +1,259 @@
|
|
|
1
|
-
import { NumericArray } from "./constants";
|
|
2
|
-
import Vec from "./Vec";
|
|
3
|
-
import { isZero, equals } from "./functions";
|
|
4
|
-
|
|
5
|
-
export default class Mat3 extends NumericArray {
|
|
6
|
-
constructor();
|
|
7
|
-
constructor(m: ArrayLike<number>);
|
|
8
|
-
constructor(m0: number, m1: number, m2: number, m3: number, m4: number, m5: number, m6: number, m7: number, m8: number);
|
|
9
|
-
constructor(...args: any[]) {
|
|
10
|
-
if (args.length === 9) {
|
|
11
|
-
super(args);
|
|
12
|
-
}
|
|
13
|
-
else if (args.length === 1 && args[0].length === 9) {
|
|
14
|
-
super(args[0]);
|
|
15
|
-
}
|
|
16
|
-
else if (args.length === 0) {
|
|
17
|
-
super([0,0,0,0,0,0,0,0,0]);
|
|
18
|
-
}
|
|
19
|
-
else {
|
|
20
|
-
throw new Error(`Invalid parameter size in Mat3 constructor`);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
identity(): this {
|
|
25
|
-
this[0] = 1; this[1] = 0; this[2] = 0;
|
|
26
|
-
this[3] = 0; this[4] = 1; this[5] = 0;
|
|
27
|
-
this[6] = 0; this[7] = 0; this[8] = 1;
|
|
28
|
-
return this;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
zero(): this {
|
|
32
|
-
this[0] = 0; this[1] = 0; this[2] = 0;
|
|
33
|
-
this[3] = 0; this[4] = 0; this[5] = 0;
|
|
34
|
-
this[6] = 0; this[7] = 0; this[8] = 0;
|
|
35
|
-
return this;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
row(i: number): Vec {
|
|
39
|
-
return new Vec(
|
|
40
|
-
this[i * 3],
|
|
41
|
-
this[i * 3 + 1],
|
|
42
|
-
this[i * 3 + 2]);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
setRow(i: number, a: ArrayLike<number>): this;
|
|
46
|
-
setRow(i: number, x: number, y: number, z: number): this;
|
|
47
|
-
setRow(i: number, a: number | ArrayLike<number>, y?: number, z?: number): this {
|
|
48
|
-
if (typeof a === 'object' && a.length >= 3) {
|
|
49
|
-
this[i * 3] = a[0];
|
|
50
|
-
this[i * 3 + 1] = a[1];
|
|
51
|
-
this[i * 3 + 2] = a[2];
|
|
52
|
-
}
|
|
53
|
-
else if (typeof(a) === "number" &&
|
|
54
|
-
typeof(y) === "number" &&
|
|
55
|
-
typeof(z) === "number"
|
|
56
|
-
) {
|
|
57
|
-
this[i * 3] = a;
|
|
58
|
-
this[i * 3 + 1] = y;
|
|
59
|
-
this[i * 3 + 2] = z;
|
|
60
|
-
}
|
|
61
|
-
else {
|
|
62
|
-
throw new Error(`Invalid parameter setting matrix row`);
|
|
63
|
-
}
|
|
64
|
-
return this;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
col(i: number): Vec {
|
|
68
|
-
return new Vec(
|
|
69
|
-
this[i],
|
|
70
|
-
this[i + 3],
|
|
71
|
-
this[i + 3 * 2]
|
|
72
|
-
)
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
setCol(i: number, a: ArrayLike<number>): this;
|
|
76
|
-
setCol(i: number, x: number, y: number, z: number): this;
|
|
77
|
-
setCol(i: number, a: number | ArrayLike<number>, y: number | null = null, z: number | null = null): this {
|
|
78
|
-
if (typeof a === 'object' && a.length >= 3) {
|
|
79
|
-
this[i] = a[0];
|
|
80
|
-
this[i + 3] = a[1];
|
|
81
|
-
this[i + 3 * 2] = a[2];
|
|
82
|
-
}
|
|
83
|
-
else if (typeof(a) === "number" &&
|
|
84
|
-
typeof(y) === "number" &&
|
|
85
|
-
typeof(z) === "number"
|
|
86
|
-
) {
|
|
87
|
-
this[i] = a;
|
|
88
|
-
this[i + 3] = y;
|
|
89
|
-
this[i + 3 * 2] = z;
|
|
90
|
-
}
|
|
91
|
-
else {
|
|
92
|
-
throw new Error(`Invalid parameter setting matrix row`);
|
|
93
|
-
}
|
|
94
|
-
return this;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
assign(m: ArrayLike<number>): this {
|
|
98
|
-
if (m.length === 9) {
|
|
99
|
-
this[0] = m[0]; this[1] = m[1]; this[2] = m[2];
|
|
100
|
-
this[3] = m[3]; this[4] = m[4]; this[5] = m[5];
|
|
101
|
-
this[6] = m[6]; this[7] = m[7]; this[8] = m[8];
|
|
102
|
-
}
|
|
103
|
-
else if (m.length === 16) {
|
|
104
|
-
this[0] = m[0]; this[1] = m[1]; this[2] = m[2];
|
|
105
|
-
this[3] = m[4]; this[4] = m[5]; this[5] = m[6];
|
|
106
|
-
this[6] = m[8]; this[7] = m[9]; this[8] = m[10];
|
|
107
|
-
}
|
|
108
|
-
else {
|
|
109
|
-
throw new Error(`Invalid plarameter setting matrix data`);
|
|
110
|
-
}
|
|
111
|
-
return this;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
setScale(x: number, y: number, z: number): this {
|
|
115
|
-
const rx = (new Vec(this[0], this[3], this[6])).normalize().scale(x);
|
|
116
|
-
const ry = (new Vec(this[1], this[4], this[7])).normalize().scale(y);
|
|
117
|
-
const rz = (new Vec(this[2], this[5], this[8])).normalize().scale(z);
|
|
118
|
-
this[0] = rx.x; this[3] = rx.y; this[6] = rx.z;
|
|
119
|
-
this[1] = ry.x; this[4] = ry.y; this[7] = ry.z;
|
|
120
|
-
this[2] = rz.x; this[5] = rz.y; this[8] = rz.z;
|
|
121
|
-
return this;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
traspose(): this {
|
|
125
|
-
const m3 = this[3]; // 0, 1, 2
|
|
126
|
-
const m7 = this[7]; // 3, 4, 5
|
|
127
|
-
const m6 = this[6]; // 6, 7, 8
|
|
128
|
-
this[3] = this[1];
|
|
129
|
-
this[6] = this[2];
|
|
130
|
-
this[7] = this[5];
|
|
131
|
-
this[1] = m3;
|
|
132
|
-
this[2] = m6;
|
|
133
|
-
this[5] = m7;
|
|
134
|
-
return this;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
mult(a: number): this;
|
|
138
|
-
mult(a: Mat3): this;
|
|
139
|
-
mult(a: number | Mat3): this {
|
|
140
|
-
if (typeof(a) === "number") {
|
|
141
|
-
this[0] *= a; this[1] *= a; this[2] *= a;
|
|
142
|
-
this[3] *= a; this[4] *= a; this[5] *= a;
|
|
143
|
-
this[6] *= a; this[7] *= a; this[8] *= a;
|
|
144
|
-
}
|
|
145
|
-
else if (a instanceof NumericArray && a.length === 9) {
|
|
146
|
-
const r0 = this.row(0);
|
|
147
|
-
const r1 = this.row(1);
|
|
148
|
-
const r2 = this.row(2);
|
|
149
|
-
const c0 = (a as Mat3).col(0);
|
|
150
|
-
const c1 = (a as Mat3).col(1);
|
|
151
|
-
const c2 = (a as Mat3).col(2);
|
|
152
|
-
|
|
153
|
-
this[0] = Vec.Dot(r0,c0); this[1] = Vec.Dot(r0,c1); this[2] = Vec.Dot(r0,c2);
|
|
154
|
-
this[3] = Vec.Dot(r1,c0); this[4] = Vec.Dot(r1,c1); this[5] = Vec.Dot(r1,c2);
|
|
155
|
-
this[6] = Vec.Dot(r2,c0); this[7] = Vec.Dot(r2,c1); this[8] = Vec.Dot(r2,c2);
|
|
156
|
-
}
|
|
157
|
-
else {
|
|
158
|
-
throw new Error(`Invalid parameter in Mat3.mult()`);
|
|
159
|
-
}
|
|
160
|
-
return this;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
multVector(v: ArrayLike<number>): Vec {
|
|
164
|
-
if (v.length === 2 || v.length === 3) {
|
|
165
|
-
const x = v[0];
|
|
166
|
-
const y = v[1];
|
|
167
|
-
const z = v.length === 2 ? 1 : v[2];
|
|
168
|
-
|
|
169
|
-
return new Vec( this[0] * x + this[3] * y + this[6] * z,
|
|
170
|
-
this[1] * x + this[4] * y + this[7] * z,
|
|
171
|
-
this[2] * x + this[5] * y + this[8] * z);
|
|
172
|
-
}
|
|
173
|
-
else {
|
|
174
|
-
throw new Error(`Invalid parameter in Mat3.multVector()`);
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
toString(): string {
|
|
179
|
-
return `[ ${this[0]}, ${this[1]}, ${this[2]}\n` +
|
|
180
|
-
` ${this[3]}, ${this[4]}, ${this[5]}\n` +
|
|
181
|
-
` ${this[6]}, ${this[7]}, ${this[8]} ]`;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
static MakeIdentity(): Mat3 {
|
|
185
|
-
const m = new Mat3();
|
|
186
|
-
return m.identity();
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
static MakeZero(): Mat3 {
|
|
190
|
-
const m = new Mat3();
|
|
191
|
-
return m.zero();
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
static MakeWithQuaternion(q: ArrayLike<number>): Mat3 {
|
|
195
|
-
const m = Mat3.MakeIdentity();
|
|
196
|
-
|
|
197
|
-
m.setRow(0, new Vec( 1 - 2 * q[1] * q[1] - 2 * q[2] * q[2], 2 * q[0] * q[1] - 2 * q[2] * q[3], 2 * q[0] * q[2] + 2 * q[1] * q[3]));
|
|
198
|
-
m.setRow(1, new Vec( 2 * q[0] * q[1] + 2 * q[2] * q[3], 1 - 2.0 * q[0] * q[0] - 2 * q[2] * q[2], 2 * q[1] * q[2] - 2 * q[0] * q[3]));
|
|
199
|
-
m.setRow(2, new Vec( 2 * q[0] * q[2] - 2 * q[1] * q[3], 2 * q[1] * q[2] + 2 * q[0] * q[3] , 1 - 2 * q[0] * q[0] - 2 * q[1] * q[1]));
|
|
200
|
-
|
|
201
|
-
return m;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
// https://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_and_angle
|
|
205
|
-
// Make a rotation matrix from an axis and an angle
|
|
206
|
-
static MakeRotationWithDirection(direction: Vec, up?: Vec): Mat3 {
|
|
207
|
-
const m = Mat3.MakeIdentity();
|
|
208
|
-
const upVec = up || new Vec(0,1,0);
|
|
209
|
-
const z = direction.normalize();
|
|
210
|
-
// Since vectors are 3-dimensional, the cross product will always return a Vecz.
|
|
211
|
-
const x = (Vec.Cross(upVec, z) as Vec).normalize() as Vec;
|
|
212
|
-
const y = (Vec.Cross(z, x) as Vec).normalize() as Vec;
|
|
213
|
-
|
|
214
|
-
m.setRow(0, x);
|
|
215
|
-
m.setRow(1, y);
|
|
216
|
-
m.setRow(2, z);
|
|
217
|
-
|
|
218
|
-
return m;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
static IsZero(m: ArrayLike<number>): boolean {
|
|
222
|
-
return isZero(m[0]) && isZero(m[1]) && isZero(m[2]) &&
|
|
223
|
-
isZero(m[3]) && isZero(m[4]) && isZero(m[5]) &&
|
|
224
|
-
isZero(m[6]) && isZero(m[7]) && isZero(m[8]);
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
static IsIdentity(m: ArrayLike<number>): boolean {
|
|
228
|
-
return equals(m[0], 1) && isZero(m[1]) && isZero(m[2]) &&
|
|
229
|
-
isZero(m[3]) && equals(m[4], 1) && isZero(m[5]) &&
|
|
230
|
-
isZero(m[6]) && isZero(m[7]) && equals(m[8], 1);
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
static GetScale(m: ArrayLike<number>): Vec {
|
|
234
|
-
return new Vec(
|
|
235
|
-
Vec.Magnitude(new Vec(m[0], m[3], m[6])),
|
|
236
|
-
Vec.Magnitude(new Vec(m[1], m[4], m[7])),
|
|
237
|
-
Vec.Magnitude(new Vec(m[2], m[5], m[8]))
|
|
238
|
-
);
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
static Equals(a: ArrayLike<number>, b: ArrayLike<number>): boolean {
|
|
242
|
-
return equals(a[0], b[0]) && equals(a[1], b[1]) && equals(a[2], b[2]) &&
|
|
243
|
-
equals(a[3], b[3]) && equals(a[4], b[4]) && equals(a[5], b[5]) &&
|
|
244
|
-
equals(a[6], b[6]) && equals(a[7], b[7]) && equals(a[8], b[8]);
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
static IsNaN(m: ArrayLike<number>): boolean {
|
|
248
|
-
return isNaN(m[0]) || isNaN(m[1]) || isNaN(m[2]) &&
|
|
249
|
-
isNaN(m[3]) || isNaN(m[4]) || isNaN(m[5]) &&
|
|
250
|
-
isNaN(m[6]) || isNaN(m[7]) || isNaN(m[8]);
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
// This function multyplies two B x A matrices. It works opposite than the non-static mult() function:
|
|
254
|
-
// A.mult(B) is the same as Mat4.Mult(B,A)
|
|
255
|
-
static Mult(A: Mat3, B: Mat3): Mat3 {
|
|
256
|
-
const result = new Mat3(B);
|
|
257
|
-
return result.mult(A);
|
|
258
|
-
}
|
|
259
|
-
};
|
|
1
|
+
import { NumericArray } from "./constants";
|
|
2
|
+
import Vec from "./Vec";
|
|
3
|
+
import { isZero, equals } from "./functions";
|
|
4
|
+
|
|
5
|
+
export default class Mat3 extends NumericArray {
|
|
6
|
+
constructor();
|
|
7
|
+
constructor(m: ArrayLike<number>);
|
|
8
|
+
constructor(m0: number, m1: number, m2: number, m3: number, m4: number, m5: number, m6: number, m7: number, m8: number);
|
|
9
|
+
constructor(...args: any[]) {
|
|
10
|
+
if (args.length === 9) {
|
|
11
|
+
super(args);
|
|
12
|
+
}
|
|
13
|
+
else if (args.length === 1 && args[0].length === 9) {
|
|
14
|
+
super(args[0]);
|
|
15
|
+
}
|
|
16
|
+
else if (args.length === 0) {
|
|
17
|
+
super([0,0,0,0,0,0,0,0,0]);
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
throw new Error(`Invalid parameter size in Mat3 constructor`);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
identity(): this {
|
|
25
|
+
this[0] = 1; this[1] = 0; this[2] = 0;
|
|
26
|
+
this[3] = 0; this[4] = 1; this[5] = 0;
|
|
27
|
+
this[6] = 0; this[7] = 0; this[8] = 1;
|
|
28
|
+
return this;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
zero(): this {
|
|
32
|
+
this[0] = 0; this[1] = 0; this[2] = 0;
|
|
33
|
+
this[3] = 0; this[4] = 0; this[5] = 0;
|
|
34
|
+
this[6] = 0; this[7] = 0; this[8] = 0;
|
|
35
|
+
return this;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
row(i: number): Vec {
|
|
39
|
+
return new Vec(
|
|
40
|
+
this[i * 3],
|
|
41
|
+
this[i * 3 + 1],
|
|
42
|
+
this[i * 3 + 2]);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
setRow(i: number, a: ArrayLike<number>): this;
|
|
46
|
+
setRow(i: number, x: number, y: number, z: number): this;
|
|
47
|
+
setRow(i: number, a: number | ArrayLike<number>, y?: number, z?: number): this {
|
|
48
|
+
if (typeof a === 'object' && a.length >= 3) {
|
|
49
|
+
this[i * 3] = a[0];
|
|
50
|
+
this[i * 3 + 1] = a[1];
|
|
51
|
+
this[i * 3 + 2] = a[2];
|
|
52
|
+
}
|
|
53
|
+
else if (typeof(a) === "number" &&
|
|
54
|
+
typeof(y) === "number" &&
|
|
55
|
+
typeof(z) === "number"
|
|
56
|
+
) {
|
|
57
|
+
this[i * 3] = a;
|
|
58
|
+
this[i * 3 + 1] = y;
|
|
59
|
+
this[i * 3 + 2] = z;
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
throw new Error(`Invalid parameter setting matrix row`);
|
|
63
|
+
}
|
|
64
|
+
return this;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
col(i: number): Vec {
|
|
68
|
+
return new Vec(
|
|
69
|
+
this[i],
|
|
70
|
+
this[i + 3],
|
|
71
|
+
this[i + 3 * 2]
|
|
72
|
+
)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
setCol(i: number, a: ArrayLike<number>): this;
|
|
76
|
+
setCol(i: number, x: number, y: number, z: number): this;
|
|
77
|
+
setCol(i: number, a: number | ArrayLike<number>, y: number | null = null, z: number | null = null): this {
|
|
78
|
+
if (typeof a === 'object' && a.length >= 3) {
|
|
79
|
+
this[i] = a[0];
|
|
80
|
+
this[i + 3] = a[1];
|
|
81
|
+
this[i + 3 * 2] = a[2];
|
|
82
|
+
}
|
|
83
|
+
else if (typeof(a) === "number" &&
|
|
84
|
+
typeof(y) === "number" &&
|
|
85
|
+
typeof(z) === "number"
|
|
86
|
+
) {
|
|
87
|
+
this[i] = a;
|
|
88
|
+
this[i + 3] = y;
|
|
89
|
+
this[i + 3 * 2] = z;
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
throw new Error(`Invalid parameter setting matrix row`);
|
|
93
|
+
}
|
|
94
|
+
return this;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
assign(m: ArrayLike<number>): this {
|
|
98
|
+
if (m.length === 9) {
|
|
99
|
+
this[0] = m[0]; this[1] = m[1]; this[2] = m[2];
|
|
100
|
+
this[3] = m[3]; this[4] = m[4]; this[5] = m[5];
|
|
101
|
+
this[6] = m[6]; this[7] = m[7]; this[8] = m[8];
|
|
102
|
+
}
|
|
103
|
+
else if (m.length === 16) {
|
|
104
|
+
this[0] = m[0]; this[1] = m[1]; this[2] = m[2];
|
|
105
|
+
this[3] = m[4]; this[4] = m[5]; this[5] = m[6];
|
|
106
|
+
this[6] = m[8]; this[7] = m[9]; this[8] = m[10];
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
throw new Error(`Invalid plarameter setting matrix data`);
|
|
110
|
+
}
|
|
111
|
+
return this;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
setScale(x: number, y: number, z: number): this {
|
|
115
|
+
const rx = (new Vec(this[0], this[3], this[6])).normalize().scale(x);
|
|
116
|
+
const ry = (new Vec(this[1], this[4], this[7])).normalize().scale(y);
|
|
117
|
+
const rz = (new Vec(this[2], this[5], this[8])).normalize().scale(z);
|
|
118
|
+
this[0] = rx.x; this[3] = rx.y; this[6] = rx.z;
|
|
119
|
+
this[1] = ry.x; this[4] = ry.y; this[7] = ry.z;
|
|
120
|
+
this[2] = rz.x; this[5] = rz.y; this[8] = rz.z;
|
|
121
|
+
return this;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
traspose(): this {
|
|
125
|
+
const m3 = this[3]; // 0, 1, 2
|
|
126
|
+
const m7 = this[7]; // 3, 4, 5
|
|
127
|
+
const m6 = this[6]; // 6, 7, 8
|
|
128
|
+
this[3] = this[1];
|
|
129
|
+
this[6] = this[2];
|
|
130
|
+
this[7] = this[5];
|
|
131
|
+
this[1] = m3;
|
|
132
|
+
this[2] = m6;
|
|
133
|
+
this[5] = m7;
|
|
134
|
+
return this;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
mult(a: number): this;
|
|
138
|
+
mult(a: Mat3): this;
|
|
139
|
+
mult(a: number | Mat3): this {
|
|
140
|
+
if (typeof(a) === "number") {
|
|
141
|
+
this[0] *= a; this[1] *= a; this[2] *= a;
|
|
142
|
+
this[3] *= a; this[4] *= a; this[5] *= a;
|
|
143
|
+
this[6] *= a; this[7] *= a; this[8] *= a;
|
|
144
|
+
}
|
|
145
|
+
else if (a instanceof NumericArray && a.length === 9) {
|
|
146
|
+
const r0 = this.row(0);
|
|
147
|
+
const r1 = this.row(1);
|
|
148
|
+
const r2 = this.row(2);
|
|
149
|
+
const c0 = (a as Mat3).col(0);
|
|
150
|
+
const c1 = (a as Mat3).col(1);
|
|
151
|
+
const c2 = (a as Mat3).col(2);
|
|
152
|
+
|
|
153
|
+
this[0] = Vec.Dot(r0,c0); this[1] = Vec.Dot(r0,c1); this[2] = Vec.Dot(r0,c2);
|
|
154
|
+
this[3] = Vec.Dot(r1,c0); this[4] = Vec.Dot(r1,c1); this[5] = Vec.Dot(r1,c2);
|
|
155
|
+
this[6] = Vec.Dot(r2,c0); this[7] = Vec.Dot(r2,c1); this[8] = Vec.Dot(r2,c2);
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
throw new Error(`Invalid parameter in Mat3.mult()`);
|
|
159
|
+
}
|
|
160
|
+
return this;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
multVector(v: ArrayLike<number>): Vec {
|
|
164
|
+
if (v.length === 2 || v.length === 3) {
|
|
165
|
+
const x = v[0];
|
|
166
|
+
const y = v[1];
|
|
167
|
+
const z = v.length === 2 ? 1 : v[2];
|
|
168
|
+
|
|
169
|
+
return new Vec( this[0] * x + this[3] * y + this[6] * z,
|
|
170
|
+
this[1] * x + this[4] * y + this[7] * z,
|
|
171
|
+
this[2] * x + this[5] * y + this[8] * z);
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
throw new Error(`Invalid parameter in Mat3.multVector()`);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
toString(): string {
|
|
179
|
+
return `[ ${this[0]}, ${this[1]}, ${this[2]}\n` +
|
|
180
|
+
` ${this[3]}, ${this[4]}, ${this[5]}\n` +
|
|
181
|
+
` ${this[6]}, ${this[7]}, ${this[8]} ]`;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
static MakeIdentity(): Mat3 {
|
|
185
|
+
const m = new Mat3();
|
|
186
|
+
return m.identity();
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
static MakeZero(): Mat3 {
|
|
190
|
+
const m = new Mat3();
|
|
191
|
+
return m.zero();
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
static MakeWithQuaternion(q: ArrayLike<number>): Mat3 {
|
|
195
|
+
const m = Mat3.MakeIdentity();
|
|
196
|
+
|
|
197
|
+
m.setRow(0, new Vec( 1 - 2 * q[1] * q[1] - 2 * q[2] * q[2], 2 * q[0] * q[1] - 2 * q[2] * q[3], 2 * q[0] * q[2] + 2 * q[1] * q[3]));
|
|
198
|
+
m.setRow(1, new Vec( 2 * q[0] * q[1] + 2 * q[2] * q[3], 1 - 2.0 * q[0] * q[0] - 2 * q[2] * q[2], 2 * q[1] * q[2] - 2 * q[0] * q[3]));
|
|
199
|
+
m.setRow(2, new Vec( 2 * q[0] * q[2] - 2 * q[1] * q[3], 2 * q[1] * q[2] + 2 * q[0] * q[3] , 1 - 2 * q[0] * q[0] - 2 * q[1] * q[1]));
|
|
200
|
+
|
|
201
|
+
return m;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// https://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_and_angle
|
|
205
|
+
// Make a rotation matrix from an axis and an angle
|
|
206
|
+
static MakeRotationWithDirection(direction: Vec, up?: Vec): Mat3 {
|
|
207
|
+
const m = Mat3.MakeIdentity();
|
|
208
|
+
const upVec = up || new Vec(0,1,0);
|
|
209
|
+
const z = direction.normalize();
|
|
210
|
+
// Since vectors are 3-dimensional, the cross product will always return a Vecz.
|
|
211
|
+
const x = (Vec.Cross(upVec, z) as Vec).normalize() as Vec;
|
|
212
|
+
const y = (Vec.Cross(z, x) as Vec).normalize() as Vec;
|
|
213
|
+
|
|
214
|
+
m.setRow(0, x);
|
|
215
|
+
m.setRow(1, y);
|
|
216
|
+
m.setRow(2, z);
|
|
217
|
+
|
|
218
|
+
return m;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
static IsZero(m: ArrayLike<number>): boolean {
|
|
222
|
+
return isZero(m[0]) && isZero(m[1]) && isZero(m[2]) &&
|
|
223
|
+
isZero(m[3]) && isZero(m[4]) && isZero(m[5]) &&
|
|
224
|
+
isZero(m[6]) && isZero(m[7]) && isZero(m[8]);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
static IsIdentity(m: ArrayLike<number>): boolean {
|
|
228
|
+
return equals(m[0], 1) && isZero(m[1]) && isZero(m[2]) &&
|
|
229
|
+
isZero(m[3]) && equals(m[4], 1) && isZero(m[5]) &&
|
|
230
|
+
isZero(m[6]) && isZero(m[7]) && equals(m[8], 1);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
static GetScale(m: ArrayLike<number>): Vec {
|
|
234
|
+
return new Vec(
|
|
235
|
+
Vec.Magnitude(new Vec(m[0], m[3], m[6])),
|
|
236
|
+
Vec.Magnitude(new Vec(m[1], m[4], m[7])),
|
|
237
|
+
Vec.Magnitude(new Vec(m[2], m[5], m[8]))
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
static Equals(a: ArrayLike<number>, b: ArrayLike<number>): boolean {
|
|
242
|
+
return equals(a[0], b[0]) && equals(a[1], b[1]) && equals(a[2], b[2]) &&
|
|
243
|
+
equals(a[3], b[3]) && equals(a[4], b[4]) && equals(a[5], b[5]) &&
|
|
244
|
+
equals(a[6], b[6]) && equals(a[7], b[7]) && equals(a[8], b[8]);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
static IsNaN(m: ArrayLike<number>): boolean {
|
|
248
|
+
return isNaN(m[0]) || isNaN(m[1]) || isNaN(m[2]) &&
|
|
249
|
+
isNaN(m[3]) || isNaN(m[4]) || isNaN(m[5]) &&
|
|
250
|
+
isNaN(m[6]) || isNaN(m[7]) || isNaN(m[8]);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// This function multyplies two B x A matrices. It works opposite than the non-static mult() function:
|
|
254
|
+
// A.mult(B) is the same as Mat4.Mult(B,A)
|
|
255
|
+
static Mult(A: Mat3, B: Mat3): Mat3 {
|
|
256
|
+
const result = new Mat3(B);
|
|
257
|
+
return result.mult(A);
|
|
258
|
+
}
|
|
259
|
+
};
|