@lightningjs/renderer 0.4.1 → 0.5.0

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.
Files changed (162) hide show
  1. package/README.md +13 -13
  2. package/dist/exports/utils.d.ts +1 -1
  3. package/dist/exports/utils.js +1 -1
  4. package/dist/exports/utils.js.map +1 -1
  5. package/dist/src/common/CommonTypes.d.ts +32 -12
  6. package/dist/src/core/CoreNode.d.ts +14 -18
  7. package/dist/src/core/CoreNode.js +90 -124
  8. package/dist/src/core/CoreNode.js.map +1 -1
  9. package/dist/src/core/CoreTextNode.d.ts +0 -2
  10. package/dist/src/core/CoreTextNode.js +39 -17
  11. package/dist/src/core/CoreTextNode.js.map +1 -1
  12. package/dist/src/core/Stage.js +11 -8
  13. package/dist/src/core/Stage.js.map +1 -1
  14. package/dist/src/core/lib/Matrix3d.d.ts +59 -0
  15. package/dist/src/core/lib/Matrix3d.js +253 -0
  16. package/dist/src/core/lib/Matrix3d.js.map +1 -0
  17. package/dist/src/core/lib/utils.d.ts +1 -0
  18. package/dist/src/core/lib/utils.js +3 -0
  19. package/dist/src/core/lib/utils.js.map +1 -1
  20. package/dist/src/core/renderers/CoreRenderer.d.ts +2 -4
  21. package/dist/src/core/renderers/CoreRenderer.js.map +1 -1
  22. package/dist/src/core/renderers/webgl/WebGlCoreRenderer.d.ts +1 -1
  23. package/dist/src/core/renderers/webgl/WebGlCoreRenderer.js +16 -16
  24. package/dist/src/core/renderers/webgl/WebGlCoreRenderer.js.map +1 -1
  25. package/dist/src/core/renderers/webgl/WebGlCoreShader.d.ts +1 -1
  26. package/dist/src/core/renderers/webgl/WebGlCoreShader.js +2 -2
  27. package/dist/src/core/renderers/webgl/WebGlCoreShader.js.map +1 -1
  28. package/dist/src/core/renderers/webgl/shaders/SdfShader.d.ts +2 -1
  29. package/dist/src/core/renderers/webgl/shaders/SdfShader.js +15 -6
  30. package/dist/src/core/renderers/webgl/shaders/SdfShader.js.map +1 -1
  31. package/dist/src/core/text-rendering/renderers/CanvasTextRenderer.d.ts +2 -1
  32. package/dist/src/core/text-rendering/renderers/CanvasTextRenderer.js +27 -30
  33. package/dist/src/core/text-rendering/renderers/CanvasTextRenderer.js.map +1 -1
  34. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.d.ts +2 -1
  35. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.js +7 -9
  36. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.js.map +1 -1
  37. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.js +0 -1
  38. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.js.map +1 -1
  39. package/dist/src/core/text-rendering/renderers/TextRenderer.d.ts +24 -2
  40. package/dist/src/core/text-rendering/renderers/TextRenderer.js +6 -3
  41. package/dist/src/core/text-rendering/renderers/TextRenderer.js.map +1 -1
  42. package/dist/src/core/textures/SubTexture.js +1 -1
  43. package/dist/src/core/textures/SubTexture.js.map +1 -1
  44. package/dist/src/core/textures/Texture.d.ts +13 -1
  45. package/dist/src/core/textures/Texture.js.map +1 -1
  46. package/dist/src/main-api/INode.d.ts +30 -5
  47. package/dist/src/main-api/RendererMain.js +3 -1
  48. package/dist/src/main-api/RendererMain.js.map +1 -1
  49. package/dist/src/render-drivers/main/MainOnlyNode.d.ts +6 -4
  50. package/dist/src/render-drivers/main/MainOnlyNode.js +31 -16
  51. package/dist/src/render-drivers/main/MainOnlyNode.js.map +1 -1
  52. package/dist/src/render-drivers/main/MainOnlyTextNode.d.ts +0 -2
  53. package/dist/src/render-drivers/main/MainOnlyTextNode.js +3 -10
  54. package/dist/src/render-drivers/main/MainOnlyTextNode.js.map +1 -1
  55. package/dist/src/render-drivers/threadx/NodeStruct.d.ts +6 -3
  56. package/dist/src/render-drivers/threadx/NodeStruct.js +12 -3
  57. package/dist/src/render-drivers/threadx/NodeStruct.js.map +1 -1
  58. package/dist/src/render-drivers/threadx/SharedNode.d.ts +2 -1
  59. package/dist/src/render-drivers/threadx/SharedNode.js +2 -1
  60. package/dist/src/render-drivers/threadx/SharedNode.js.map +1 -1
  61. package/dist/src/render-drivers/threadx/ThreadXMainNode.d.ts +2 -0
  62. package/dist/src/render-drivers/threadx/ThreadXMainNode.js +14 -0
  63. package/dist/src/render-drivers/threadx/ThreadXMainNode.js.map +1 -1
  64. package/dist/src/render-drivers/threadx/ThreadXRenderDriver.js +4 -2
  65. package/dist/src/render-drivers/threadx/ThreadXRenderDriver.js.map +1 -1
  66. package/dist/src/render-drivers/threadx/worker/ThreadXRendererNode.js +6 -5
  67. package/dist/src/render-drivers/threadx/worker/ThreadXRendererNode.js.map +1 -1
  68. package/dist/src/render-drivers/threadx/worker/ThreadXRendererTextNode.js +2 -7
  69. package/dist/src/render-drivers/threadx/worker/ThreadXRendererTextNode.js.map +1 -1
  70. package/dist/src/render-drivers/threadx/worker/renderer.js +2 -1
  71. package/dist/src/render-drivers/threadx/worker/renderer.js.map +1 -1
  72. package/dist/src/utils.d.ts +7 -12
  73. package/dist/src/utils.js +9 -25
  74. package/dist/src/utils.js.map +1 -1
  75. package/dist/tsconfig.dist.tsbuildinfo +1 -1
  76. package/exports/utils.ts +1 -1
  77. package/package.json +22 -19
  78. package/src/common/CommonTypes.ts +42 -16
  79. package/src/core/CoreNode.ts +120 -158
  80. package/src/core/CoreTextNode.ts +54 -24
  81. package/src/core/Stage.ts +16 -11
  82. package/src/core/lib/Matrix3d.ts +290 -0
  83. package/src/core/lib/utils.ts +4 -0
  84. package/src/core/renderers/CoreRenderer.ts +2 -4
  85. package/src/core/renderers/webgl/WebGlCoreRenderer.ts +18 -21
  86. package/src/core/renderers/webgl/WebGlCoreShader.ts +2 -2
  87. package/src/core/renderers/webgl/shaders/SdfShader.ts +17 -8
  88. package/src/core/text-rendering/renderers/CanvasTextRenderer.ts +30 -28
  89. package/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.ts +9 -20
  90. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.ts +0 -1
  91. package/src/core/text-rendering/renderers/TextRenderer.ts +37 -5
  92. package/src/core/textures/SubTexture.ts +6 -5
  93. package/src/core/textures/Texture.ts +19 -6
  94. package/src/main-api/INode.ts +32 -5
  95. package/src/main-api/RendererMain.ts +3 -1
  96. package/src/render-drivers/main/MainOnlyNode.ts +39 -22
  97. package/src/render-drivers/main/MainOnlyTextNode.ts +2 -15
  98. package/src/render-drivers/threadx/NodeStruct.ts +13 -3
  99. package/src/render-drivers/threadx/SharedNode.ts +4 -2
  100. package/src/render-drivers/threadx/ThreadXMainNode.ts +16 -0
  101. package/src/render-drivers/threadx/ThreadXRenderDriver.ts +4 -2
  102. package/src/render-drivers/threadx/worker/ThreadXRendererNode.ts +18 -8
  103. package/src/render-drivers/threadx/worker/ThreadXRendererTextNode.ts +2 -14
  104. package/src/render-drivers/threadx/worker/renderer.ts +2 -1
  105. package/src/utils.ts +10 -27
  106. package/dist/src/core/Matrix2DContext.d.ts +0 -15
  107. package/dist/src/core/Matrix2DContext.js +0 -45
  108. package/dist/src/core/Matrix2DContext.js.map +0 -1
  109. package/dist/src/core/lib/glm/common.d.ts +0 -162
  110. package/dist/src/core/lib/glm/common.js +0 -81
  111. package/dist/src/core/lib/glm/common.js.map +0 -1
  112. package/dist/src/core/lib/glm/index.d.ts +0 -11
  113. package/dist/src/core/lib/glm/index.js +0 -30
  114. package/dist/src/core/lib/glm/index.js.map +0 -1
  115. package/dist/src/core/lib/glm/mat2.d.ts +0 -219
  116. package/dist/src/core/lib/glm/mat2.js +0 -396
  117. package/dist/src/core/lib/glm/mat2.js.map +0 -1
  118. package/dist/src/core/lib/glm/mat2d.d.ts +0 -237
  119. package/dist/src/core/lib/glm/mat2d.js +0 -442
  120. package/dist/src/core/lib/glm/mat2d.js.map +0 -1
  121. package/dist/src/core/lib/glm/mat3.d.ts +0 -283
  122. package/dist/src/core/lib/glm/mat3.js +0 -680
  123. package/dist/src/core/lib/glm/mat3.js.map +0 -1
  124. package/dist/src/core/lib/glm/mat4.d.ts +0 -550
  125. package/dist/src/core/lib/glm/mat4.js +0 -1802
  126. package/dist/src/core/lib/glm/mat4.js.map +0 -1
  127. package/dist/src/core/lib/glm/quat.d.ts +0 -363
  128. package/dist/src/core/lib/glm/quat.js +0 -693
  129. package/dist/src/core/lib/glm/quat.js.map +0 -1
  130. package/dist/src/core/lib/glm/quat2.d.ts +0 -356
  131. package/dist/src/core/lib/glm/quat2.js +0 -754
  132. package/dist/src/core/lib/glm/quat2.js.map +0 -1
  133. package/dist/src/core/lib/glm/vec2.d.ts +0 -365
  134. package/dist/src/core/lib/glm/vec2.js +0 -569
  135. package/dist/src/core/lib/glm/vec2.js.map +0 -1
  136. package/dist/src/core/lib/glm/vec3.d.ts +0 -406
  137. package/dist/src/core/lib/glm/vec3.js +0 -720
  138. package/dist/src/core/lib/glm/vec3.js.map +0 -1
  139. package/dist/src/core/lib/glm/vec4.d.ts +0 -330
  140. package/dist/src/core/lib/glm/vec4.js +0 -608
  141. package/dist/src/core/lib/glm/vec4.js.map +0 -1
  142. package/dist/src/core/textures/utils.d.ts +0 -11
  143. package/dist/src/core/textures/utils.js +0 -32
  144. package/dist/src/core/textures/utils.js.map +0 -1
  145. package/dist/src/main-api/TextureRegistry.d.ts +0 -33
  146. package/dist/src/main-api/TextureRegistry.js +0 -97
  147. package/dist/src/main-api/TextureRegistry.js.map +0 -1
  148. package/dist/src/main-api/TextureUsageRegistry/TextureRegistry.d.ts +0 -33
  149. package/dist/src/main-api/TextureUsageRegistry/TextureRegistry.js +0 -97
  150. package/dist/src/main-api/TextureUsageRegistry/TextureRegistry.js.map +0 -1
  151. package/src/core/Matrix2DContext.ts +0 -52
  152. package/src/core/lib/glm/common.ts +0 -231
  153. package/src/core/lib/glm/index.ts +0 -31
  154. package/src/core/lib/glm/mat2.ts +0 -499
  155. package/src/core/lib/glm/mat2d.ts +0 -547
  156. package/src/core/lib/glm/mat3.ts +0 -849
  157. package/src/core/lib/glm/mat4.ts +0 -2169
  158. package/src/core/lib/glm/quat.ts +0 -828
  159. package/src/core/lib/glm/quat2.ts +0 -951
  160. package/src/core/lib/glm/vec2.ts +0 -671
  161. package/src/core/lib/glm/vec3.ts +0 -859
  162. package/src/core/lib/glm/vec4.ts +0 -708
@@ -1,849 +0,0 @@
1
- /*
2
- * If not stated otherwise in this file or this component's LICENSE file the
3
- * following copyright and licenses apply:
4
- *
5
- * Copyright 2023 Comcast Cable Communications Management, LLC.
6
- *
7
- * Licensed under the Apache License, Version 2.0 (the License);
8
- * you may not use this file except in compliance with the License.
9
- * You may obtain a copy of the License at
10
- *
11
- * http://www.apache.org/licenses/LICENSE-2.0
12
- *
13
- * Unless required by applicable law or agreed to in writing, software
14
- * distributed under the License is distributed on an "AS IS" BASIS,
15
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
- * See the License for the specific language governing permissions and
17
- * limitations under the License.
18
- */
19
-
20
- import {
21
- EPSILON,
22
- getMatrixArrayType,
23
- type Float32ArrayLen9,
24
- type NumberArrayLen9,
25
- } from './common.js';
26
- import type { Vec2 } from './vec2.js';
27
- import type { Mat2d } from './mat2d.js';
28
- import type { Mat4 } from './mat4.js';
29
- import type { Quat } from './quat.js';
30
-
31
- export type Mat3 = Float32ArrayLen9 | NumberArrayLen9;
32
-
33
- export function create(): Mat3 {
34
- const out = getMatrixArrayType(9) as Mat3;
35
-
36
- if (!(out instanceof Float32Array)) {
37
- out[1] = 0;
38
- out[2] = 0;
39
- out[3] = 0;
40
- out[5] = 0;
41
- out[6] = 0;
42
- out[7] = 0;
43
- }
44
-
45
- out[0] = 1;
46
- out[4] = 1;
47
- out[8] = 1;
48
- return out;
49
- }
50
- /**
51
- * Copies the upper-left 3x3 values into the given mat3.
52
- *
53
- * @param {mat3} out the receiving 3x3 matrix
54
- * @param {Mat3} a the source 4x4 matrix
55
- * @returns {mat3} out
56
- */
57
-
58
- export function fromMat4(out: Mat3, a: Mat4): Mat3 {
59
- out[0] = a[0];
60
- out[1] = a[1];
61
- out[2] = a[2];
62
- out[3] = a[4];
63
- out[4] = a[5];
64
- out[5] = a[6];
65
- out[6] = a[8];
66
- out[7] = a[9];
67
- out[8] = a[10];
68
- return out;
69
- }
70
- /**
71
- * Creates a new mat3 initialized with values from an existing matrix
72
- *
73
- * @param {Mat3} a matrix to clone
74
- * @returns {mat3} a new 3x3 matrix
75
- */
76
-
77
- export function clone(a: Mat3): Mat3 {
78
- const out = getMatrixArrayType(9) as Mat3;
79
- out[0] = a[0];
80
- out[1] = a[1];
81
- out[2] = a[2];
82
- out[3] = a[3];
83
- out[4] = a[4];
84
- out[5] = a[5];
85
- out[6] = a[6];
86
- out[7] = a[7];
87
- out[8] = a[8];
88
- return out;
89
- }
90
- /**
91
- * Copy the values from one mat3 to another
92
- *
93
- * @param {mat3} out the receiving matrix
94
- * @param {Mat3} a the source matrix
95
- * @returns {mat3} out
96
- */
97
-
98
- export function copy(out: Mat3, a: Mat3): Mat3 {
99
- out[0] = a[0];
100
- out[1] = a[1];
101
- out[2] = a[2];
102
- out[3] = a[3];
103
- out[4] = a[4];
104
- out[5] = a[5];
105
- out[6] = a[6];
106
- out[7] = a[7];
107
- out[8] = a[8];
108
- return out;
109
- }
110
- /**
111
- * Create a new mat3 with the given values
112
- *
113
- * @param {Number} m00 Component in column 0, row 0 position (index 0)
114
- * @param {Number} m01 Component in column 0, row 1 position (index 1)
115
- * @param {Number} m02 Component in column 0, row 2 position (index 2)
116
- * @param {Number} m10 Component in column 1, row 0 position (index 3)
117
- * @param {Number} m11 Component in column 1, row 1 position (index 4)
118
- * @param {Number} m12 Component in column 1, row 2 position (index 5)
119
- * @param {Number} m20 Component in column 2, row 0 position (index 6)
120
- * @param {Number} m21 Component in column 2, row 1 position (index 7)
121
- * @param {Number} m22 Component in column 2, row 2 position (index 8)
122
- * @returns {mat3} A new mat3
123
- */
124
-
125
- export function fromValues(
126
- m00: number,
127
- m01: number,
128
- m02: number,
129
- m10: number,
130
- m11: number,
131
- m12: number,
132
- m20: number,
133
- m21: number,
134
- m22: number,
135
- ): Mat3 {
136
- const out = getMatrixArrayType(9) as Mat3;
137
- out[0] = m00;
138
- out[1] = m01;
139
- out[2] = m02;
140
- out[3] = m10;
141
- out[4] = m11;
142
- out[5] = m12;
143
- out[6] = m20;
144
- out[7] = m21;
145
- out[8] = m22;
146
- return out;
147
- }
148
- /**
149
- * Set the components of a mat3 to the given values
150
- *
151
- * @param {mat3} out the receiving matrix
152
- * @param {Number} m00 Component in column 0, row 0 position (index 0)
153
- * @param {Number} m01 Component in column 0, row 1 position (index 1)
154
- * @param {Number} m02 Component in column 0, row 2 position (index 2)
155
- * @param {Number} m10 Component in column 1, row 0 position (index 3)
156
- * @param {Number} m11 Component in column 1, row 1 position (index 4)
157
- * @param {Number} m12 Component in column 1, row 2 position (index 5)
158
- * @param {Number} m20 Component in column 2, row 0 position (index 6)
159
- * @param {Number} m21 Component in column 2, row 1 position (index 7)
160
- * @param {Number} m22 Component in column 2, row 2 position (index 8)
161
- * @returns {mat3} out
162
- */
163
-
164
- export function set(
165
- out: Mat3,
166
- m00: number,
167
- m01: number,
168
- m02: number,
169
- m10: number,
170
- m11: number,
171
- m12: number,
172
- m20: number,
173
- m21: number,
174
- m22: number,
175
- ): Mat3 {
176
- out[0] = m00;
177
- out[1] = m01;
178
- out[2] = m02;
179
- out[3] = m10;
180
- out[4] = m11;
181
- out[5] = m12;
182
- out[6] = m20;
183
- out[7] = m21;
184
- out[8] = m22;
185
- return out;
186
- }
187
- /**
188
- * Set a mat3 to the identity matrix
189
- *
190
- * @param {mat3} out the receiving matrix
191
- * @returns {mat3} out
192
- */
193
-
194
- export function identity(out: Mat3): Mat3 {
195
- out[0] = 1;
196
- out[1] = 0;
197
- out[2] = 0;
198
- out[3] = 0;
199
- out[4] = 1;
200
- out[5] = 0;
201
- out[6] = 0;
202
- out[7] = 0;
203
- out[8] = 1;
204
- return out;
205
- }
206
- /**
207
- * Transpose the values of a mat3
208
- *
209
- * @param {mat3} out the receiving matrix
210
- * @param {Mat3} a the source matrix
211
- * @returns {mat3} out
212
- */
213
-
214
- export function transpose(out: Mat3, a: Mat3): Mat3 {
215
- // If we are transposing ourselves we can skip a few steps but have to cache some values
216
- if (out === a) {
217
- const a01 = a[1],
218
- a02 = a[2],
219
- a12 = a[5];
220
- out[1] = a[3];
221
- out[2] = a[6];
222
- out[3] = a01;
223
- out[5] = a[7];
224
- out[6] = a02;
225
- out[7] = a12;
226
- } else {
227
- out[0] = a[0];
228
- out[1] = a[3];
229
- out[2] = a[6];
230
- out[3] = a[1];
231
- out[4] = a[4];
232
- out[5] = a[7];
233
- out[6] = a[2];
234
- out[7] = a[5];
235
- out[8] = a[8];
236
- }
237
-
238
- return out;
239
- }
240
- /**
241
- * Inverts a mat3
242
- *
243
- * @param {mat3} out the receiving matrix
244
- * @param {Mat3} a the source matrix
245
- * @returns {mat3} out
246
- */
247
-
248
- export function invert(out: Mat3, a: Mat3): Mat3 | null {
249
- const a00 = a[0],
250
- a01 = a[1],
251
- a02 = a[2];
252
- const a10 = a[3],
253
- a11 = a[4],
254
- a12 = a[5];
255
- const a20 = a[6],
256
- a21 = a[7],
257
- a22 = a[8];
258
- const b01 = a22 * a11 - a12 * a21;
259
- const b11 = -a22 * a10 + a12 * a20;
260
- const b21 = a21 * a10 - a11 * a20; // Calculate the determinant
261
-
262
- let det = a00 * b01 + a01 * b11 + a02 * b21;
263
-
264
- if (!det) {
265
- return null;
266
- }
267
-
268
- det = 1.0 / det;
269
- out[0] = b01 * det;
270
- out[1] = (-a22 * a01 + a02 * a21) * det;
271
- out[2] = (a12 * a01 - a02 * a11) * det;
272
- out[3] = b11 * det;
273
- out[4] = (a22 * a00 - a02 * a20) * det;
274
- out[5] = (-a12 * a00 + a02 * a10) * det;
275
- out[6] = b21 * det;
276
- out[7] = (-a21 * a00 + a01 * a20) * det;
277
- out[8] = (a11 * a00 - a01 * a10) * det;
278
- return out;
279
- }
280
- /**
281
- * Calculates the adjugate of a mat3
282
- *
283
- * @param {mat3} out the receiving matrix
284
- * @param {Mat3} a the source matrix
285
- * @returns {mat3} out
286
- */
287
-
288
- export function adjoint(out: Mat3, a: Mat3): Mat3 {
289
- const a00 = a[0],
290
- a01 = a[1],
291
- a02 = a[2];
292
- const a10 = a[3],
293
- a11 = a[4],
294
- a12 = a[5];
295
- const a20 = a[6],
296
- a21 = a[7],
297
- a22 = a[8];
298
- out[0] = a11 * a22 - a12 * a21;
299
- out[1] = a02 * a21 - a01 * a22;
300
- out[2] = a01 * a12 - a02 * a11;
301
- out[3] = a12 * a20 - a10 * a22;
302
- out[4] = a00 * a22 - a02 * a20;
303
- out[5] = a02 * a10 - a00 * a12;
304
- out[6] = a10 * a21 - a11 * a20;
305
- out[7] = a01 * a20 - a00 * a21;
306
- out[8] = a00 * a11 - a01 * a10;
307
- return out;
308
- }
309
- /**
310
- * Calculates the determinant of a mat3
311
- *
312
- * @param {Mat3} a the source matrix
313
- * @returns {Number} determinant of a
314
- */
315
-
316
- export function determinant(a: Mat3): number {
317
- const a00 = a[0],
318
- a01 = a[1],
319
- a02 = a[2];
320
- const a10 = a[3],
321
- a11 = a[4],
322
- a12 = a[5];
323
- const a20 = a[6],
324
- a21 = a[7],
325
- a22 = a[8];
326
- return (
327
- a00 * (a22 * a11 - a12 * a21) +
328
- a01 * (-a22 * a10 + a12 * a20) +
329
- a02 * (a21 * a10 - a11 * a20)
330
- );
331
- }
332
- /**
333
- * Multiplies two mat3's
334
- *
335
- * @param {mat3} out the receiving matrix
336
- * @param {Mat3} a the first operand
337
- * @param {Mat3} b the second operand
338
- * @returns {mat3} out
339
- */
340
-
341
- export function multiply(out: Mat3, a: Mat3, b: Mat3): Mat3 {
342
- const a00 = a[0],
343
- a01 = a[1],
344
- a02 = a[2];
345
- const a10 = a[3],
346
- a11 = a[4],
347
- a12 = a[5];
348
- const a20 = a[6],
349
- a21 = a[7],
350
- a22 = a[8];
351
- const b00 = b[0],
352
- b01 = b[1],
353
- b02 = b[2];
354
- const b10 = b[3],
355
- b11 = b[4],
356
- b12 = b[5];
357
- const b20 = b[6],
358
- b21 = b[7],
359
- b22 = b[8];
360
- out[0] = b00 * a00 + b01 * a10 + b02 * a20;
361
- out[1] = b00 * a01 + b01 * a11 + b02 * a21;
362
- out[2] = b00 * a02 + b01 * a12 + b02 * a22;
363
- out[3] = b10 * a00 + b11 * a10 + b12 * a20;
364
- out[4] = b10 * a01 + b11 * a11 + b12 * a21;
365
- out[5] = b10 * a02 + b11 * a12 + b12 * a22;
366
- out[6] = b20 * a00 + b21 * a10 + b22 * a20;
367
- out[7] = b20 * a01 + b21 * a11 + b22 * a21;
368
- out[8] = b20 * a02 + b21 * a12 + b22 * a22;
369
- return out;
370
- }
371
- /**
372
- * Translate a mat3 by the given vector
373
- *
374
- * @param {mat3} out the receiving matrix
375
- * @param {Mat3} a the matrix to translate
376
- * @param {Vec2} v vector to translate by
377
- * @returns {mat3} out
378
- */
379
-
380
- export function translate(out: Mat3, a: Mat3, v: Mat3): Mat3 {
381
- const a00 = a[0],
382
- a01 = a[1],
383
- a02 = a[2],
384
- a10 = a[3],
385
- a11 = a[4],
386
- a12 = a[5],
387
- a20 = a[6],
388
- a21 = a[7],
389
- a22 = a[8],
390
- x = v[0],
391
- y = v[1];
392
- out[0] = a00;
393
- out[1] = a01;
394
- out[2] = a02;
395
- out[3] = a10;
396
- out[4] = a11;
397
- out[5] = a12;
398
- out[6] = x * a00 + y * a10 + a20;
399
- out[7] = x * a01 + y * a11 + a21;
400
- out[8] = x * a02 + y * a12 + a22;
401
- return out;
402
- }
403
- /**
404
- * Rotates a mat3 by the given angle
405
- *
406
- * @param {mat3} out the receiving matrix
407
- * @param {Mat3} a the matrix to rotate
408
- * @param {Number} rad the angle to rotate the matrix by
409
- * @returns {mat3} out
410
- */
411
-
412
- export function rotate(out: Mat3, a: Mat3, rad: number): Mat3 {
413
- const a00 = a[0],
414
- a01 = a[1],
415
- a02 = a[2],
416
- a10 = a[3],
417
- a11 = a[4],
418
- a12 = a[5],
419
- a20 = a[6],
420
- a21 = a[7],
421
- a22 = a[8],
422
- s = Math.sin(rad),
423
- c = Math.cos(rad);
424
- out[0] = c * a00 + s * a10;
425
- out[1] = c * a01 + s * a11;
426
- out[2] = c * a02 + s * a12;
427
- out[3] = c * a10 - s * a00;
428
- out[4] = c * a11 - s * a01;
429
- out[5] = c * a12 - s * a02;
430
- out[6] = a20;
431
- out[7] = a21;
432
- out[8] = a22;
433
- return out;
434
- }
435
- /**
436
- * Scales the mat3 by the dimensions in the given vec2
437
- *
438
- * @param {mat3} out the receiving matrix
439
- * @param {Mat3} a the matrix to rotate
440
- * @param {Vec2} v the vec2 to scale the matrix by
441
- * @returns {mat3} out
442
- **/
443
-
444
- export function scale(out: Mat3, a: Mat3, v: Mat3): Mat3 {
445
- const x = v[0],
446
- y = v[1];
447
- out[0] = x * a[0];
448
- out[1] = x * a[1];
449
- out[2] = x * a[2];
450
- out[3] = y * a[3];
451
- out[4] = y * a[4];
452
- out[5] = y * a[5];
453
- out[6] = a[6];
454
- out[7] = a[7];
455
- out[8] = a[8];
456
- return out;
457
- }
458
- /**
459
- * Creates a matrix from a vector translation
460
- * This is equivalent to (but much faster than):
461
- *
462
- * mat3.identity(dest);
463
- * mat3.translate(dest, dest, vec);
464
- *
465
- * @param {mat3} out mat3 receiving operation result
466
- * @param {Vec2} v Translation vector
467
- * @returns {mat3} out
468
- */
469
-
470
- export function fromTranslation(out: Mat3, v: Vec2): Mat3 {
471
- out[0] = 1;
472
- out[1] = 0;
473
- out[2] = 0;
474
- out[3] = 0;
475
- out[4] = 1;
476
- out[5] = 0;
477
- out[6] = v[0];
478
- out[7] = v[1];
479
- out[8] = 1;
480
- return out;
481
- }
482
- /**
483
- * Creates a matrix from a given angle
484
- * This is equivalent to (but much faster than):
485
- *
486
- * mat3.identity(dest);
487
- * mat3.rotate(dest, dest, rad);
488
- *
489
- * @param {mat3} out mat3 receiving operation result
490
- * @param {Number} rad the angle to rotate the matrix by
491
- * @returns {mat3} out
492
- */
493
-
494
- export function fromRotation(out: Mat3, rad: number): Mat3 {
495
- const s = Math.sin(rad),
496
- c = Math.cos(rad);
497
- out[0] = c;
498
- out[1] = s;
499
- out[2] = 0;
500
- out[3] = -s;
501
- out[4] = c;
502
- out[5] = 0;
503
- out[6] = 0;
504
- out[7] = 0;
505
- out[8] = 1;
506
- return out;
507
- }
508
- /**
509
- * Creates a matrix from a vector scaling
510
- * This is equivalent to (but much faster than):
511
- *
512
- * mat3.identity(dest);
513
- * mat3.scale(dest, dest, vec);
514
- *
515
- * @param {mat3} out mat3 receiving operation result
516
- * @param {Vec2} v Scaling vector
517
- * @returns {mat3} out
518
- */
519
-
520
- export function fromScaling(out: Mat3, v: Vec2): Mat3 {
521
- out[0] = v[0];
522
- out[1] = 0;
523
- out[2] = 0;
524
- out[3] = 0;
525
- out[4] = v[1];
526
- out[5] = 0;
527
- out[6] = 0;
528
- out[7] = 0;
529
- out[8] = 1;
530
- return out;
531
- }
532
- /**
533
- * Copies the values from a mat2d into a mat3
534
- *
535
- * @param {mat3} out the receiving matrix
536
- * @param {Mat2d} a the matrix to copy
537
- * @returns {mat3} out
538
- **/
539
-
540
- export function fromMat2d(out: Mat3, a: Mat2d): Mat3 {
541
- out[0] = a[0];
542
- out[1] = a[1];
543
- out[2] = 0;
544
- out[3] = a[2];
545
- out[4] = a[3];
546
- out[5] = 0;
547
- out[6] = a[4];
548
- out[7] = a[5];
549
- out[8] = 1;
550
- return out;
551
- }
552
- /**
553
- * Calculates a 3x3 matrix from the given quaternion
554
- *
555
- * @param {mat3} out mat3 receiving operation result
556
- * @param {Quat} q Quaternion to create matrix from
557
- *
558
- * @returns {mat3} out
559
- */
560
-
561
- export function fromQuat(out: Mat3, q: Quat): Mat3 {
562
- const x = q[0],
563
- y = q[1],
564
- z = q[2],
565
- w = q[3];
566
- const x2 = x + x;
567
- const y2 = y + y;
568
- const z2 = z + z;
569
- const xx = x * x2;
570
- const yx = y * x2;
571
- const yy = y * y2;
572
- const zx = z * x2;
573
- const zy = z * y2;
574
- const zz = z * z2;
575
- const wx = w * x2;
576
- const wy = w * y2;
577
- const wz = w * z2;
578
- out[0] = 1 - yy - zz;
579
- out[3] = yx - wz;
580
- out[6] = zx + wy;
581
- out[1] = yx + wz;
582
- out[4] = 1 - xx - zz;
583
- out[7] = zy - wx;
584
- out[2] = zx - wy;
585
- out[5] = zy + wx;
586
- out[8] = 1 - xx - yy;
587
- return out;
588
- }
589
- /**
590
- * Calculates a 3x3 normal matrix (transpose inverse) from the 4x4 matrix
591
- *
592
- * @param {mat3} out mat3 receiving operation result
593
- * @param {Mat4} a Mat4 to derive the normal matrix from
594
- *
595
- * @returns {mat3} out
596
- */
597
-
598
- export function normalFromMat4(out: Mat3, a: Mat4): Mat3 | null {
599
- const a00 = a[0],
600
- a01 = a[1],
601
- a02 = a[2],
602
- a03 = a[3];
603
- const a10 = a[4],
604
- a11 = a[5],
605
- a12 = a[6],
606
- a13 = a[7];
607
- const a20 = a[8],
608
- a21 = a[9],
609
- a22 = a[10],
610
- a23 = a[11];
611
- const a30 = a[12],
612
- a31 = a[13],
613
- a32 = a[14],
614
- a33 = a[15];
615
- const b00 = a00 * a11 - a01 * a10;
616
- const b01 = a00 * a12 - a02 * a10;
617
- const b02 = a00 * a13 - a03 * a10;
618
- const b03 = a01 * a12 - a02 * a11;
619
- const b04 = a01 * a13 - a03 * a11;
620
- const b05 = a02 * a13 - a03 * a12;
621
- const b06 = a20 * a31 - a21 * a30;
622
- const b07 = a20 * a32 - a22 * a30;
623
- const b08 = a20 * a33 - a23 * a30;
624
- const b09 = a21 * a32 - a22 * a31;
625
- const b10 = a21 * a33 - a23 * a31;
626
- const b11 = a22 * a33 - a23 * a32; // Calculate the determinant
627
-
628
- let det =
629
- b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
630
-
631
- if (!det) {
632
- return null;
633
- }
634
-
635
- det = 1.0 / det;
636
- out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det;
637
- out[1] = (a12 * b08 - a10 * b11 - a13 * b07) * det;
638
- out[2] = (a10 * b10 - a11 * b08 + a13 * b06) * det;
639
- out[3] = (a02 * b10 - a01 * b11 - a03 * b09) * det;
640
- out[4] = (a00 * b11 - a02 * b08 + a03 * b07) * det;
641
- out[5] = (a01 * b08 - a00 * b10 - a03 * b06) * det;
642
- out[6] = (a31 * b05 - a32 * b04 + a33 * b03) * det;
643
- out[7] = (a32 * b02 - a30 * b05 - a33 * b01) * det;
644
- out[8] = (a30 * b04 - a31 * b02 + a33 * b00) * det;
645
- return out;
646
- }
647
- /**
648
- * Generates a 2D projection matrix with the given bounds
649
- *
650
- * @param {mat3} out mat3 frustum matrix will be written into
651
- * @param {number} width Width of your gl context
652
- * @param {number} height Height of gl context
653
- * @returns {mat3} out
654
- */
655
-
656
- export function projection(out: Mat3, width: number, height: number): Mat3 {
657
- out[0] = 2 / width;
658
- out[1] = 0;
659
- out[2] = 0;
660
- out[3] = 0;
661
- out[4] = -2 / height;
662
- out[5] = 0;
663
- out[6] = -1;
664
- out[7] = 1;
665
- out[8] = 1;
666
- return out;
667
- }
668
- /**
669
- * Returns a string representation of a mat3
670
- *
671
- * @param {Mat3} a matrix to represent as a string
672
- * @returns {String} string representation of the matrix
673
- */
674
-
675
- export function str(a: Mat3): string {
676
- return `mat3(${a[0]}, ${a[1]}, ${a[2]}, ${a[3]}, ${a[4]}, ${a[5]}, ${a[6]}, ${a[7]}, ${a[8]})`;
677
- }
678
- /**
679
- * Returns Frobenius norm of a mat3
680
- *
681
- * @param {Mat3} a the matrix to calculate Frobenius norm of
682
- * @returns {Number} Frobenius norm
683
- */
684
-
685
- export function frob(a: Mat3): number {
686
- return Math.hypot(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8]);
687
- }
688
- /**
689
- * Adds two mat3's
690
- *
691
- * @param {mat3} out the receiving matrix
692
- * @param {Mat3} a the first operand
693
- * @param {Mat3} b the second operand
694
- * @returns {mat3} out
695
- */
696
-
697
- export function add(out: Mat3, a: Mat3, b: Mat3): Mat3 {
698
- out[0] = a[0] + b[0];
699
- out[1] = a[1] + b[1];
700
- out[2] = a[2] + b[2];
701
- out[3] = a[3] + b[3];
702
- out[4] = a[4] + b[4];
703
- out[5] = a[5] + b[5];
704
- out[6] = a[6] + b[6];
705
- out[7] = a[7] + b[7];
706
- out[8] = a[8] + b[8];
707
- return out;
708
- }
709
- /**
710
- * Subtracts matrix b from matrix a
711
- *
712
- * @param {mat3} out the receiving matrix
713
- * @param {Mat3} a the first operand
714
- * @param {Mat3} b the second operand
715
- * @returns {mat3} out
716
- */
717
-
718
- export function subtract(out: Mat3, a: Mat3, b: Mat3): Mat3 {
719
- out[0] = a[0] - b[0];
720
- out[1] = a[1] - b[1];
721
- out[2] = a[2] - b[2];
722
- out[3] = a[3] - b[3];
723
- out[4] = a[4] - b[4];
724
- out[5] = a[5] - b[5];
725
- out[6] = a[6] - b[6];
726
- out[7] = a[7] - b[7];
727
- out[8] = a[8] - b[8];
728
- return out;
729
- }
730
- /**
731
- * Multiply each element of the matrix by a scalar.
732
- *
733
- * @param {mat3} out the receiving matrix
734
- * @param {Mat3} a the matrix to scale
735
- * @param {Number} b amount to scale the matrix's elements by
736
- * @returns {mat3} out
737
- */
738
-
739
- export function multiplyScalar(out: Mat3, a: Mat3, b: number): Mat3 {
740
- out[0] = a[0] * b;
741
- out[1] = a[1] * b;
742
- out[2] = a[2] * b;
743
- out[3] = a[3] * b;
744
- out[4] = a[4] * b;
745
- out[5] = a[5] * b;
746
- out[6] = a[6] * b;
747
- out[7] = a[7] * b;
748
- out[8] = a[8] * b;
749
- return out;
750
- }
751
- /**
752
- * Adds two mat3's after multiplying each element of the second operand by a scalar value.
753
- *
754
- * @param {mat3} out the receiving vector
755
- * @param {Mat3} a the first operand
756
- * @param {Mat3} b the second operand
757
- * @param {Number} scale the amount to scale b's elements by before adding
758
- * @returns {mat3} out
759
- */
760
-
761
- export function multiplyScalarAndAdd(
762
- out: Mat3,
763
- a: Mat3,
764
- b: Mat3,
765
- scale: number,
766
- ): Mat3 {
767
- out[0] = a[0] + b[0] * scale;
768
- out[1] = a[1] + b[1] * scale;
769
- out[2] = a[2] + b[2] * scale;
770
- out[3] = a[3] + b[3] * scale;
771
- out[4] = a[4] + b[4] * scale;
772
- out[5] = a[5] + b[5] * scale;
773
- out[6] = a[6] + b[6] * scale;
774
- out[7] = a[7] + b[7] * scale;
775
- out[8] = a[8] + b[8] * scale;
776
- return out;
777
- }
778
- /**
779
- * Returns whether or not the matrices have exactly the same elements in the same position (when compared with ===)
780
- *
781
- * @param {Mat3} a The first matrix.
782
- * @param {Mat3} b The second matrix.
783
- * @returns {Boolean} True if the matrices are equal, false otherwise.
784
- */
785
-
786
- export function exactEquals(a: Mat3, b: Mat3): boolean {
787
- return (
788
- a[0] === b[0] &&
789
- a[1] === b[1] &&
790
- a[2] === b[2] &&
791
- a[3] === b[3] &&
792
- a[4] === b[4] &&
793
- a[5] === b[5] &&
794
- a[6] === b[6] &&
795
- a[7] === b[7] &&
796
- a[8] === b[8]
797
- );
798
- }
799
- /**
800
- * Returns whether or not the matrices have approximately the same elements in the same position.
801
- *
802
- * @param {Mat3} a The first matrix.
803
- * @param {Mat3} b The second matrix.
804
- * @returns {Boolean} True if the matrices are equal, false otherwise.
805
- */
806
-
807
- export function equals(a: Mat3, b: Mat3): boolean {
808
- const a0 = a[0],
809
- a1 = a[1],
810
- a2 = a[2],
811
- a3 = a[3],
812
- a4 = a[4],
813
- a5 = a[5],
814
- a6 = a[6],
815
- a7 = a[7],
816
- a8 = a[8];
817
- const b0 = b[0],
818
- b1 = b[1],
819
- b2 = b[2],
820
- b3 = b[3],
821
- b4 = b[4],
822
- b5 = b[5],
823
- b6 = b[6],
824
- b7 = b[7],
825
- b8 = b[8];
826
- return (
827
- Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) &&
828
- Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) &&
829
- Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) &&
830
- Math.abs(a3 - b3) <= EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)) &&
831
- Math.abs(a4 - b4) <= EPSILON * Math.max(1.0, Math.abs(a4), Math.abs(b4)) &&
832
- Math.abs(a5 - b5) <= EPSILON * Math.max(1.0, Math.abs(a5), Math.abs(b5)) &&
833
- Math.abs(a6 - b6) <= EPSILON * Math.max(1.0, Math.abs(a6), Math.abs(b6)) &&
834
- Math.abs(a7 - b7) <= EPSILON * Math.max(1.0, Math.abs(a7), Math.abs(b7)) &&
835
- Math.abs(a8 - b8) <= EPSILON * Math.max(1.0, Math.abs(a8), Math.abs(b8))
836
- );
837
- }
838
- /**
839
- * Alias for {@link multiply}
840
- * @function
841
- */
842
-
843
- export const mul = multiply;
844
- /**
845
- * Alias for {@link subtract}
846
- * @function
847
- */
848
-
849
- export const sub = subtract;