@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,2169 +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
- getMatrixArrayType,
22
- EPSILON,
23
- type FOV,
24
- type Float32ArrayLen16,
25
- type NumberArrayLen16,
26
- } from './common.js';
27
-
28
- import type { Vec3 } from './vec3.js';
29
- import type { Quat } from './quat.js';
30
- import type { Quat2 } from './quat2.js';
31
-
32
- export type Mat4 = Float32ArrayLen16 | NumberArrayLen16;
33
- /**
34
- * Creates a new identity Mat4
35
- *
36
- * @returns {Mat4} a new 4x4 matrix
37
- */
38
-
39
- export function create(): Mat4 {
40
- const out = getMatrixArrayType(16) as Mat4;
41
-
42
- if (!(out instanceof Float32Array)) {
43
- out[1] = 0;
44
- out[2] = 0;
45
- out[3] = 0;
46
- out[4] = 0;
47
- out[6] = 0;
48
- out[7] = 0;
49
- out[8] = 0;
50
- out[9] = 0;
51
- out[11] = 0;
52
- out[12] = 0;
53
- out[13] = 0;
54
- out[14] = 0;
55
- }
56
-
57
- out[0] = 1;
58
- out[5] = 1;
59
- out[10] = 1;
60
- out[15] = 1;
61
- return out;
62
- }
63
- /**
64
- * Creates a new Mat4 initialized with values from an existing matrix
65
- *
66
- * @param {Mat4} a matrix to clone
67
- * @returns {Mat4} a new 4x4 matrix
68
- */
69
-
70
- export function clone(a: Mat4): Mat4 {
71
- const out = getMatrixArrayType(16) as Mat4;
72
- out[0] = a[0];
73
- out[1] = a[1];
74
- out[2] = a[2];
75
- out[3] = a[3];
76
- out[4] = a[4];
77
- out[5] = a[5];
78
- out[6] = a[6];
79
- out[7] = a[7];
80
- out[8] = a[8];
81
- out[9] = a[9];
82
- out[10] = a[10];
83
- out[11] = a[11];
84
- out[12] = a[12];
85
- out[13] = a[13];
86
- out[14] = a[14];
87
- out[15] = a[15];
88
- return out;
89
- }
90
- /**
91
- * Copy the values from one Mat4 to another
92
- *
93
- * @param {Mat4} out the receiving matrix
94
- * @param {Mat4} a the source matrix
95
- * @returns {Mat4} out
96
- */
97
-
98
- export function copy(out: Mat4, a: Mat4): Mat4 {
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
- out[9] = a[9];
109
- out[10] = a[10];
110
- out[11] = a[11];
111
- out[12] = a[12];
112
- out[13] = a[13];
113
- out[14] = a[14];
114
- out[15] = a[15];
115
- return out;
116
- }
117
- /**
118
- * Create a new Mat4 with the given values
119
- *
120
- * @param {Number} m00 Component in column 0, row 0 position (index 0)
121
- * @param {Number} m01 Component in column 0, row 1 position (index 1)
122
- * @param {Number} m02 Component in column 0, row 2 position (index 2)
123
- * @param {Number} m03 Component in column 0, row 3 position (index 3)
124
- * @param {Number} m10 Component in column 1, row 0 position (index 4)
125
- * @param {Number} m11 Component in column 1, row 1 position (index 5)
126
- * @param {Number} m12 Component in column 1, row 2 position (index 6)
127
- * @param {Number} m13 Component in column 1, row 3 position (index 7)
128
- * @param {Number} m20 Component in column 2, row 0 position (index 8)
129
- * @param {Number} m21 Component in column 2, row 1 position (index 9)
130
- * @param {Number} m22 Component in column 2, row 2 position (index 10)
131
- * @param {Number} m23 Component in column 2, row 3 position (index 11)
132
- * @param {Number} m30 Component in column 3, row 0 position (index 12)
133
- * @param {Number} m31 Component in column 3, row 1 position (index 13)
134
- * @param {Number} m32 Component in column 3, row 2 position (index 14)
135
- * @param {Number} m33 Component in column 3, row 3 position (index 15)
136
- * @returns {Mat4} A new Mat4
137
- */
138
-
139
- export function fromValues(
140
- m00: number,
141
- m01: number,
142
- m02: number,
143
- m03: number,
144
- m10: number,
145
- m11: number,
146
- m12: number,
147
- m13: number,
148
- m20: number,
149
- m21: number,
150
- m22: number,
151
- m23: number,
152
- m30: number,
153
- m31: number,
154
- m32: number,
155
- m33: number,
156
- ): Mat4 {
157
- const out = getMatrixArrayType(16) as Mat4;
158
- out[0] = m00;
159
- out[1] = m01;
160
- out[2] = m02;
161
- out[3] = m03;
162
- out[4] = m10;
163
- out[5] = m11;
164
- out[6] = m12;
165
- out[7] = m13;
166
- out[8] = m20;
167
- out[9] = m21;
168
- out[10] = m22;
169
- out[11] = m23;
170
- out[12] = m30;
171
- out[13] = m31;
172
- out[14] = m32;
173
- out[15] = m33;
174
- return out;
175
- }
176
- /**
177
- * Set the components of a Mat4 to the given values
178
- *
179
- * @param {Mat4} out the receiving matrix
180
- * @param {Number} m00 Component in column 0, row 0 position (index 0)
181
- * @param {Number} m01 Component in column 0, row 1 position (index 1)
182
- * @param {Number} m02 Component in column 0, row 2 position (index 2)
183
- * @param {Number} m03 Component in column 0, row 3 position (index 3)
184
- * @param {Number} m10 Component in column 1, row 0 position (index 4)
185
- * @param {Number} m11 Component in column 1, row 1 position (index 5)
186
- * @param {Number} m12 Component in column 1, row 2 position (index 6)
187
- * @param {Number} m13 Component in column 1, row 3 position (index 7)
188
- * @param {Number} m20 Component in column 2, row 0 position (index 8)
189
- * @param {Number} m21 Component in column 2, row 1 position (index 9)
190
- * @param {Number} m22 Component in column 2, row 2 position (index 10)
191
- * @param {Number} m23 Component in column 2, row 3 position (index 11)
192
- * @param {Number} m30 Component in column 3, row 0 position (index 12)
193
- * @param {Number} m31 Component in column 3, row 1 position (index 13)
194
- * @param {Number} m32 Component in column 3, row 2 position (index 14)
195
- * @param {Number} m33 Component in column 3, row 3 position (index 15)
196
- * @returns {Mat4} out
197
- */
198
-
199
- export function set(
200
- out: Mat4,
201
- m00: number,
202
- m01: number,
203
- m02: number,
204
- m03: number,
205
- m10: number,
206
- m11: number,
207
- m12: number,
208
- m13: number,
209
- m20: number,
210
- m21: number,
211
- m22: number,
212
- m23: number,
213
- m30: number,
214
- m31: number,
215
- m32: number,
216
- m33: number,
217
- ): Mat4 {
218
- out[0] = m00;
219
- out[1] = m01;
220
- out[2] = m02;
221
- out[3] = m03;
222
- out[4] = m10;
223
- out[5] = m11;
224
- out[6] = m12;
225
- out[7] = m13;
226
- out[8] = m20;
227
- out[9] = m21;
228
- out[10] = m22;
229
- out[11] = m23;
230
- out[12] = m30;
231
- out[13] = m31;
232
- out[14] = m32;
233
- out[15] = m33;
234
- return out;
235
- }
236
- /**
237
- * Set a Mat4 to the identity matrix
238
- *
239
- * @param {Mat4} out the receiving matrix
240
- * @returns {Mat4} out
241
- */
242
-
243
- export function identity(out: Mat4): Mat4 {
244
- out[0] = 1;
245
- out[1] = 0;
246
- out[2] = 0;
247
- out[3] = 0;
248
- out[4] = 0;
249
- out[5] = 1;
250
- out[6] = 0;
251
- out[7] = 0;
252
- out[8] = 0;
253
- out[9] = 0;
254
- out[10] = 1;
255
- out[11] = 0;
256
- out[12] = 0;
257
- out[13] = 0;
258
- out[14] = 0;
259
- out[15] = 1;
260
- return out;
261
- }
262
- /**
263
- * Transpose the values of a Mat4
264
- *
265
- * @param {Mat4} out the receiving matrix
266
- * @param {Mat4} a the source matrix
267
- * @returns {Mat4} out
268
- */
269
-
270
- export function transpose(out: Mat4, a: Mat4): Mat4 {
271
- // If we are transposing ourselves we can skip a few steps but have to cache some values
272
- if (out === a) {
273
- const a01 = a[1],
274
- a02 = a[2],
275
- a03 = a[3];
276
- const a12 = a[6],
277
- a13 = a[7];
278
- const a23 = a[11];
279
- out[1] = a[4];
280
- out[2] = a[8];
281
- out[3] = a[12];
282
- out[4] = a01;
283
- out[6] = a[9];
284
- out[7] = a[13];
285
- out[8] = a02;
286
- out[9] = a12;
287
- out[11] = a[14];
288
- out[12] = a03;
289
- out[13] = a13;
290
- out[14] = a23;
291
- } else {
292
- out[0] = a[0];
293
- out[1] = a[4];
294
- out[2] = a[8];
295
- out[3] = a[12];
296
- out[4] = a[1];
297
- out[5] = a[5];
298
- out[6] = a[9];
299
- out[7] = a[13];
300
- out[8] = a[2];
301
- out[9] = a[6];
302
- out[10] = a[10];
303
- out[11] = a[14];
304
- out[12] = a[3];
305
- out[13] = a[7];
306
- out[14] = a[11];
307
- out[15] = a[15];
308
- }
309
-
310
- return out;
311
- }
312
- /**
313
- * Inverts a Mat4
314
- *
315
- * @param {Mat4} out the receiving matrix
316
- * @param {Mat4} a the source matrix
317
- * @returns {Mat4} out
318
- */
319
-
320
- export function invert(out: Mat4, a: Mat4): Mat4 | null {
321
- const a00 = a[0],
322
- a01 = a[1],
323
- a02 = a[2],
324
- a03 = a[3];
325
- const a10 = a[4],
326
- a11 = a[5],
327
- a12 = a[6],
328
- a13 = a[7];
329
- const a20 = a[8],
330
- a21 = a[9],
331
- a22 = a[10],
332
- a23 = a[11];
333
- const a30 = a[12],
334
- a31 = a[13],
335
- a32 = a[14],
336
- a33 = a[15];
337
- const b00 = a00 * a11 - a01 * a10;
338
- const b01 = a00 * a12 - a02 * a10;
339
- const b02 = a00 * a13 - a03 * a10;
340
- const b03 = a01 * a12 - a02 * a11;
341
- const b04 = a01 * a13 - a03 * a11;
342
- const b05 = a02 * a13 - a03 * a12;
343
- const b06 = a20 * a31 - a21 * a30;
344
- const b07 = a20 * a32 - a22 * a30;
345
- const b08 = a20 * a33 - a23 * a30;
346
- const b09 = a21 * a32 - a22 * a31;
347
- const b10 = a21 * a33 - a23 * a31;
348
- const b11 = a22 * a33 - a23 * a32; // Calculate the determinant
349
-
350
- let det =
351
- b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
352
-
353
- if (!det) {
354
- return null;
355
- }
356
-
357
- det = 1.0 / det;
358
- out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det;
359
- out[1] = (a02 * b10 - a01 * b11 - a03 * b09) * det;
360
- out[2] = (a31 * b05 - a32 * b04 + a33 * b03) * det;
361
- out[3] = (a22 * b04 - a21 * b05 - a23 * b03) * det;
362
- out[4] = (a12 * b08 - a10 * b11 - a13 * b07) * det;
363
- out[5] = (a00 * b11 - a02 * b08 + a03 * b07) * det;
364
- out[6] = (a32 * b02 - a30 * b05 - a33 * b01) * det;
365
- out[7] = (a20 * b05 - a22 * b02 + a23 * b01) * det;
366
- out[8] = (a10 * b10 - a11 * b08 + a13 * b06) * det;
367
- out[9] = (a01 * b08 - a00 * b10 - a03 * b06) * det;
368
- out[10] = (a30 * b04 - a31 * b02 + a33 * b00) * det;
369
- out[11] = (a21 * b02 - a20 * b04 - a23 * b00) * det;
370
- out[12] = (a11 * b07 - a10 * b09 - a12 * b06) * det;
371
- out[13] = (a00 * b09 - a01 * b07 + a02 * b06) * det;
372
- out[14] = (a31 * b01 - a30 * b03 - a32 * b00) * det;
373
- out[15] = (a20 * b03 - a21 * b01 + a22 * b00) * det;
374
- return out;
375
- }
376
- /**
377
- * Calculates the adjugate of a Mat4
378
- *
379
- * @param {Mat4} out the receiving matrix
380
- * @param {Mat4} a the source matrix
381
- * @returns {Mat4} out
382
- */
383
-
384
- export function adjoint(out: Mat4, a: Mat4): Mat4 {
385
- const a00 = a[0],
386
- a01 = a[1],
387
- a02 = a[2],
388
- a03 = a[3];
389
- const a10 = a[4],
390
- a11 = a[5],
391
- a12 = a[6],
392
- a13 = a[7];
393
- const a20 = a[8],
394
- a21 = a[9],
395
- a22 = a[10],
396
- a23 = a[11];
397
- const a30 = a[12],
398
- a31 = a[13],
399
- a32 = a[14],
400
- a33 = a[15];
401
- const b00 = a00 * a11 - a01 * a10;
402
- const b01 = a00 * a12 - a02 * a10;
403
- const b02 = a00 * a13 - a03 * a10;
404
- const b03 = a01 * a12 - a02 * a11;
405
- const b04 = a01 * a13 - a03 * a11;
406
- const b05 = a02 * a13 - a03 * a12;
407
- const b06 = a20 * a31 - a21 * a30;
408
- const b07 = a20 * a32 - a22 * a30;
409
- const b08 = a20 * a33 - a23 * a30;
410
- const b09 = a21 * a32 - a22 * a31;
411
- const b10 = a21 * a33 - a23 * a31;
412
- const b11 = a22 * a33 - a23 * a32;
413
- out[0] = a11 * b11 - a12 * b10 + a13 * b09;
414
- out[1] = a02 * b10 - a01 * b11 - a03 * b09;
415
- out[2] = a31 * b05 - a32 * b04 + a33 * b03;
416
- out[3] = a22 * b04 - a21 * b05 - a23 * b03;
417
- out[4] = a12 * b08 - a10 * b11 - a13 * b07;
418
- out[5] = a00 * b11 - a02 * b08 + a03 * b07;
419
- out[6] = a32 * b02 - a30 * b05 - a33 * b01;
420
- out[7] = a20 * b05 - a22 * b02 + a23 * b01;
421
- out[8] = a10 * b10 - a11 * b08 + a13 * b06;
422
- out[9] = a01 * b08 - a00 * b10 - a03 * b06;
423
- out[10] = a30 * b04 - a31 * b02 + a33 * b00;
424
- out[11] = a21 * b02 - a20 * b04 - a23 * b00;
425
- out[12] = a11 * b07 - a10 * b09 - a12 * b06;
426
- out[13] = a00 * b09 - a01 * b07 + a02 * b06;
427
- out[14] = a31 * b01 - a30 * b03 - a32 * b00;
428
- out[15] = a20 * b03 - a21 * b01 + a22 * b00;
429
- return out;
430
- }
431
- /**
432
- * Calculates the determinant of a Mat4
433
- *
434
- * @param {Mat4} a the source matrix
435
- * @returns {Number} determinant of a
436
- */
437
-
438
- export function determinant(a: Mat4): number {
439
- const a00 = a[0],
440
- a01 = a[1],
441
- a02 = a[2],
442
- a03 = a[3];
443
- const a10 = a[4],
444
- a11 = a[5],
445
- a12 = a[6],
446
- a13 = a[7];
447
- const a20 = a[8],
448
- a21 = a[9],
449
- a22 = a[10],
450
- a23 = a[11];
451
- const a30 = a[12],
452
- a31 = a[13],
453
- a32 = a[14],
454
- a33 = a[15];
455
- const b0 = a00 * a11 - a01 * a10;
456
- const b1 = a00 * a12 - a02 * a10;
457
- const b2 = a01 * a12 - a02 * a11;
458
- const b3 = a20 * a31 - a21 * a30;
459
- const b4 = a20 * a32 - a22 * a30;
460
- const b5 = a21 * a32 - a22 * a31;
461
- const b6 = a00 * b5 - a01 * b4 + a02 * b3;
462
- const b7 = a10 * b5 - a11 * b4 + a12 * b3;
463
- const b8 = a20 * b2 - a21 * b1 + a22 * b0;
464
- const b9 = a30 * b2 - a31 * b1 + a32 * b0; // Calculate the determinant
465
-
466
- return a13 * b6 - a03 * b7 + a33 * b8 - a23 * b9;
467
- }
468
- /**
469
- * Multiplies two Mat4s
470
- *
471
- * @param {Mat4} out the receiving matrix
472
- * @param {Mat4} a the first operand
473
- * @param {Mat4} b the second operand
474
- * @returns {Mat4} out
475
- */
476
-
477
- export function multiply(out: Mat4, a: Mat4, b: Mat4): Mat4 {
478
- const a00 = a[0],
479
- a01 = a[1],
480
- a02 = a[2],
481
- a03 = a[3];
482
- const a10 = a[4],
483
- a11 = a[5],
484
- a12 = a[6],
485
- a13 = a[7];
486
- const a20 = a[8],
487
- a21 = a[9],
488
- a22 = a[10],
489
- a23 = a[11];
490
- const a30 = a[12],
491
- a31 = a[13],
492
- a32 = a[14],
493
- a33 = a[15]; // Cache only the current line of the second matrix
494
-
495
- let b0 = b[0],
496
- b1 = b[1],
497
- b2 = b[2],
498
- b3 = b[3];
499
- out[0] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
500
- out[1] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
501
- out[2] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
502
- out[3] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
503
- b0 = b[4];
504
- b1 = b[5];
505
- b2 = b[6];
506
- b3 = b[7];
507
- out[4] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
508
- out[5] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
509
- out[6] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
510
- out[7] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
511
- b0 = b[8];
512
- b1 = b[9];
513
- b2 = b[10];
514
- b3 = b[11];
515
- out[8] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
516
- out[9] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
517
- out[10] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
518
- out[11] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
519
- b0 = b[12];
520
- b1 = b[13];
521
- b2 = b[14];
522
- b3 = b[15];
523
- out[12] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
524
- out[13] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
525
- out[14] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
526
- out[15] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
527
- return out;
528
- }
529
- /**
530
- * Translate a Mat4 by the given vector
531
- *
532
- * @param {Mat4} out the receiving matrix
533
- * @param {Mat4} a the matrix to translate
534
- * @param {Vec3} v vector to translate by
535
- * @returns {Mat4} out
536
- */
537
-
538
- export function translate(out: Mat4, a: Mat4, v: Vec3): Mat4 {
539
- const x = v[0],
540
- y = v[1],
541
- z = v[2];
542
- let a00, a01, a02, a03;
543
- let a10, a11, a12, a13;
544
- let a20, a21, a22, a23;
545
-
546
- if (a === out) {
547
- out[12] = a[0] * x + a[4] * y + a[8] * z + a[12];
548
- out[13] = a[1] * x + a[5] * y + a[9] * z + a[13];
549
- out[14] = a[2] * x + a[6] * y + a[10] * z + a[14];
550
- out[15] = a[3] * x + a[7] * y + a[11] * z + a[15];
551
- } else {
552
- a00 = a[0];
553
- a01 = a[1];
554
- a02 = a[2];
555
- a03 = a[3];
556
- a10 = a[4];
557
- a11 = a[5];
558
- a12 = a[6];
559
- a13 = a[7];
560
- a20 = a[8];
561
- a21 = a[9];
562
- a22 = a[10];
563
- a23 = a[11];
564
- out[0] = a00;
565
- out[1] = a01;
566
- out[2] = a02;
567
- out[3] = a03;
568
- out[4] = a10;
569
- out[5] = a11;
570
- out[6] = a12;
571
- out[7] = a13;
572
- out[8] = a20;
573
- out[9] = a21;
574
- out[10] = a22;
575
- out[11] = a23;
576
- out[12] = a00 * x + a10 * y + a20 * z + a[12];
577
- out[13] = a01 * x + a11 * y + a21 * z + a[13];
578
- out[14] = a02 * x + a12 * y + a22 * z + a[14];
579
- out[15] = a03 * x + a13 * y + a23 * z + a[15];
580
- }
581
-
582
- return out;
583
- }
584
- /**
585
- * Scales the Mat4 by the dimensions in the given Vec3 not using vectorization
586
- *
587
- * @param {Mat4} out the receiving matrix
588
- * @param {Mat4} a the matrix to scale
589
- * @param {Vec3} v the Vec3 to scale the matrix by
590
- * @returns {Mat4} out
591
- **/
592
-
593
- export function scale(out: Mat4, a: Mat4, v: Mat4): Mat4 {
594
- const x = v[0],
595
- y = v[1],
596
- z = v[2];
597
- out[0] = a[0] * x;
598
- out[1] = a[1] * x;
599
- out[2] = a[2] * x;
600
- out[3] = a[3] * x;
601
- out[4] = a[4] * y;
602
- out[5] = a[5] * y;
603
- out[6] = a[6] * y;
604
- out[7] = a[7] * y;
605
- out[8] = a[8] * z;
606
- out[9] = a[9] * z;
607
- out[10] = a[10] * z;
608
- out[11] = a[11] * z;
609
- out[12] = a[12];
610
- out[13] = a[13];
611
- out[14] = a[14];
612
- out[15] = a[15];
613
- return out;
614
- }
615
- /**
616
- * Rotates a Mat4 by the given angle around the given axis
617
- *
618
- * @param {Mat4} out the receiving matrix
619
- * @param {Mat4} a the matrix to rotate
620
- * @param {Number} rad the angle to rotate the matrix by
621
- * @param {Vec3} axis the axis to rotate around
622
- * @returns {Mat4} out
623
- */
624
-
625
- export function rotate(
626
- out: Mat4,
627
- a: Mat4,
628
- rad: number,
629
- axis: Vec3,
630
- ): Mat4 | null {
631
- let x = axis[0],
632
- y = axis[1],
633
- z = axis[2];
634
- let len = Math.hypot(x, y, z);
635
-
636
- const s = Math.sin(rad),
637
- c = Math.cos(rad),
638
- t = 1 - c,
639
- a00 = a[0],
640
- a01 = a[1],
641
- a02 = a[2],
642
- a03 = a[3],
643
- a10 = a[4],
644
- a11 = a[5],
645
- a12 = a[6],
646
- a13 = a[7],
647
- a20 = a[8],
648
- a21 = a[9],
649
- a22 = a[10],
650
- a23 = a[11]; // Construct the elements of the rotation matrix
651
-
652
- if (len < EPSILON) {
653
- return null;
654
- }
655
-
656
- len = 1 / len;
657
- x *= len;
658
- y *= len;
659
- z *= len;
660
-
661
- const b00 = x * x * t + c,
662
- b01 = y * x * t + z * s,
663
- b02 = z * x * t - y * s,
664
- b10 = x * y * t - z * s,
665
- b11 = y * y * t + c,
666
- b12 = z * y * t + x * s,
667
- b20 = x * z * t + y * s,
668
- b21 = y * z * t - x * s,
669
- b22 = z * z * t + c; // Perform rotation-specific matrix multiplication
670
-
671
- out[0] = a00 * b00 + a10 * b01 + a20 * b02;
672
- out[1] = a01 * b00 + a11 * b01 + a21 * b02;
673
- out[2] = a02 * b00 + a12 * b01 + a22 * b02;
674
- out[3] = a03 * b00 + a13 * b01 + a23 * b02;
675
- out[4] = a00 * b10 + a10 * b11 + a20 * b12;
676
- out[5] = a01 * b10 + a11 * b11 + a21 * b12;
677
- out[6] = a02 * b10 + a12 * b11 + a22 * b12;
678
- out[7] = a03 * b10 + a13 * b11 + a23 * b12;
679
- out[8] = a00 * b20 + a10 * b21 + a20 * b22;
680
- out[9] = a01 * b20 + a11 * b21 + a21 * b22;
681
- out[10] = a02 * b20 + a12 * b21 + a22 * b22;
682
- out[11] = a03 * b20 + a13 * b21 + a23 * b22;
683
-
684
- if (a !== out) {
685
- // If the source and destination differ, copy the unchanged last row
686
- out[12] = a[12];
687
- out[13] = a[13];
688
- out[14] = a[14];
689
- out[15] = a[15];
690
- }
691
-
692
- return out;
693
- }
694
- /**
695
- * Rotates a matrix by the given angle around the X axis
696
- *
697
- * @param {Mat4} out the receiving matrix
698
- * @param {Mat4} a the matrix to rotate
699
- * @param {Number} rad the angle to rotate the matrix by
700
- * @returns {Mat4} out
701
- */
702
-
703
- export function rotateX(out: Mat4, a: Mat4, rad: number): Mat4 {
704
- const s = Math.sin(rad);
705
- const c = Math.cos(rad);
706
- const a10 = a[4];
707
- const a11 = a[5];
708
- const a12 = a[6];
709
- const a13 = a[7];
710
- const a20 = a[8];
711
- const a21 = a[9];
712
- const a22 = a[10];
713
- const a23 = a[11];
714
-
715
- if (a !== out) {
716
- // If the source and destination differ, copy the unchanged rows
717
- out[0] = a[0];
718
- out[1] = a[1];
719
- out[2] = a[2];
720
- out[3] = a[3];
721
- out[12] = a[12];
722
- out[13] = a[13];
723
- out[14] = a[14];
724
- out[15] = a[15];
725
- } // Perform axis-specific matrix multiplication
726
-
727
- out[4] = a10 * c + a20 * s;
728
- out[5] = a11 * c + a21 * s;
729
- out[6] = a12 * c + a22 * s;
730
- out[7] = a13 * c + a23 * s;
731
- out[8] = a20 * c - a10 * s;
732
- out[9] = a21 * c - a11 * s;
733
- out[10] = a22 * c - a12 * s;
734
- out[11] = a23 * c - a13 * s;
735
- return out;
736
- }
737
- /**
738
- * Rotates a matrix by the given angle around the Y axis
739
- *
740
- * @param {Mat4} out the receiving matrix
741
- * @param {Mat4} a the matrix to rotate
742
- * @param {Number} rad the angle to rotate the matrix by
743
- * @returns {Mat4} out
744
- */
745
-
746
- export function rotateY(out: Mat4, a: Mat4, rad: number): Mat4 {
747
- const s = Math.sin(rad);
748
- const c = Math.cos(rad);
749
- const a00 = a[0];
750
- const a01 = a[1];
751
- const a02 = a[2];
752
- const a03 = a[3];
753
- const a20 = a[8];
754
- const a21 = a[9];
755
- const a22 = a[10];
756
- const a23 = a[11];
757
-
758
- if (a !== out) {
759
- // If the source and destination differ, copy the unchanged rows
760
- out[4] = a[4];
761
- out[5] = a[5];
762
- out[6] = a[6];
763
- out[7] = a[7];
764
- out[12] = a[12];
765
- out[13] = a[13];
766
- out[14] = a[14];
767
- out[15] = a[15];
768
- } // Perform axis-specific matrix multiplication
769
-
770
- out[0] = a00 * c - a20 * s;
771
- out[1] = a01 * c - a21 * s;
772
- out[2] = a02 * c - a22 * s;
773
- out[3] = a03 * c - a23 * s;
774
- out[8] = a00 * s + a20 * c;
775
- out[9] = a01 * s + a21 * c;
776
- out[10] = a02 * s + a22 * c;
777
- out[11] = a03 * s + a23 * c;
778
- return out;
779
- }
780
- /**
781
- * Rotates a matrix by the given angle around the Z axis
782
- *
783
- * @param {Mat4} out the receiving matrix
784
- * @param {Mat4} a the matrix to rotate
785
- * @param {Number} rad the angle to rotate the matrix by
786
- * @returns {Mat4} out
787
- */
788
-
789
- export function rotateZ(out: Mat4, a: Mat4, rad: number) {
790
- const s = Math.sin(rad);
791
- const c = Math.cos(rad);
792
- const a00 = a[0];
793
- const a01 = a[1];
794
- const a02 = a[2];
795
- const a03 = a[3];
796
- const a10 = a[4];
797
- const a11 = a[5];
798
- const a12 = a[6];
799
- const a13 = a[7];
800
-
801
- if (a !== out) {
802
- // If the source and destination differ, copy the unchanged last row
803
- out[8] = a[8];
804
- out[9] = a[9];
805
- out[10] = a[10];
806
- out[11] = a[11];
807
- out[12] = a[12];
808
- out[13] = a[13];
809
- out[14] = a[14];
810
- out[15] = a[15];
811
- } // Perform axis-specific matrix multiplication
812
-
813
- out[0] = a00 * c + a10 * s;
814
- out[1] = a01 * c + a11 * s;
815
- out[2] = a02 * c + a12 * s;
816
- out[3] = a03 * c + a13 * s;
817
- out[4] = a10 * c - a00 * s;
818
- out[5] = a11 * c - a01 * s;
819
- out[6] = a12 * c - a02 * s;
820
- out[7] = a13 * c - a03 * s;
821
- return out;
822
- }
823
- /**
824
- * Creates a matrix from a vector translation
825
- * This is equivalent to (but much faster than):
826
- *
827
- * Mat4.identity(dest);
828
- * Mat4.translate(dest, dest, vec);
829
- *
830
- * @param {Mat4} out Mat4 receiving operation result
831
- * @param {Vec3} v Translation vector
832
- * @returns {Mat4} out
833
- */
834
-
835
- export function fromTranslation(out: Mat4, v: Vec3): Mat4 {
836
- out[0] = 1;
837
- out[1] = 0;
838
- out[2] = 0;
839
- out[3] = 0;
840
- out[4] = 0;
841
- out[5] = 1;
842
- out[6] = 0;
843
- out[7] = 0;
844
- out[8] = 0;
845
- out[9] = 0;
846
- out[10] = 1;
847
- out[11] = 0;
848
- out[12] = v[0];
849
- out[13] = v[1];
850
- out[14] = v[2];
851
- out[15] = 1;
852
- return out;
853
- }
854
- /**
855
- * Creates a matrix from a vector scaling
856
- * This is equivalent to (but much faster than):
857
- *
858
- * Mat4.identity(dest);
859
- * Mat4.scale(dest, dest, vec);
860
- *
861
- * @param {Mat4} out Mat4 receiving operation result
862
- * @param {Vec3} v Scaling vector
863
- * @returns {Mat4} out
864
- */
865
-
866
- export function fromScaling(out: Mat4, v: Vec3): Mat4 {
867
- out[0] = v[0];
868
- out[1] = 0;
869
- out[2] = 0;
870
- out[3] = 0;
871
- out[4] = 0;
872
- out[5] = v[1];
873
- out[6] = 0;
874
- out[7] = 0;
875
- out[8] = 0;
876
- out[9] = 0;
877
- out[10] = v[2];
878
- out[11] = 0;
879
- out[12] = 0;
880
- out[13] = 0;
881
- out[14] = 0;
882
- out[15] = 1;
883
- return out;
884
- }
885
- /**
886
- * Creates a matrix from a given angle around a given axis
887
- * This is equivalent to (but much faster than):
888
- *
889
- * Mat4.identity(dest);
890
- * Mat4.rotate(dest, dest, rad, axis);
891
- *
892
- * @param {Mat4} out Mat4 receiving operation result
893
- * @param {Number} rad the angle to rotate the matrix by
894
- * @param {Vec3} axis the axis to rotate around
895
- * @returns {Mat4} out
896
- */
897
- export function fromRotation(out: Mat4, rad: number, axis: Vec3): Mat4 | null {
898
- let x = axis[0],
899
- y = axis[1],
900
- z = axis[2];
901
- let len = Math.hypot(x, y, z);
902
- const s = Math.sin(rad),
903
- c = Math.cos(rad),
904
- t = 1 - c; // Perform rotation-specific matrix multiplication
905
-
906
- if (len < EPSILON) {
907
- return null;
908
- }
909
-
910
- len = 1 / len;
911
- x *= len;
912
- y *= len;
913
- z *= len;
914
-
915
- out[0] = x * x * t + c;
916
- out[1] = y * x * t + z * s;
917
- out[2] = z * x * t - y * s;
918
- out[3] = 0;
919
- out[4] = x * y * t - z * s;
920
- out[5] = y * y * t + c;
921
- out[6] = z * y * t + x * s;
922
- out[7] = 0;
923
- out[8] = x * z * t + y * s;
924
- out[9] = y * z * t - x * s;
925
- out[10] = z * z * t + c;
926
- out[11] = 0;
927
- out[12] = 0;
928
- out[13] = 0;
929
- out[14] = 0;
930
- out[15] = 1;
931
- return out;
932
- }
933
- /**
934
- * Creates a matrix from the given angle around the X axis
935
- * This is equivalent to (but much faster than):
936
- *
937
- * Mat4.identity(dest);
938
- * Mat4.rotateX(dest, dest, rad);
939
- *
940
- * @param {Mat4} out Mat4 receiving operation result
941
- * @param {Number} rad the angle to rotate the matrix by
942
- * @returns {Mat4} out
943
- */
944
-
945
- export function fromXRotation(out: Mat4, rad: number): Mat4 {
946
- const s = Math.sin(rad);
947
- const c = Math.cos(rad); // Perform axis-specific matrix multiplication
948
-
949
- out[0] = 1;
950
- out[1] = 0;
951
- out[2] = 0;
952
- out[3] = 0;
953
- out[4] = 0;
954
- out[5] = c;
955
- out[6] = s;
956
- out[7] = 0;
957
- out[8] = 0;
958
- out[9] = -s;
959
- out[10] = c;
960
- out[11] = 0;
961
- out[12] = 0;
962
- out[13] = 0;
963
- out[14] = 0;
964
- out[15] = 1;
965
- return out;
966
- }
967
- /**
968
- * Creates a matrix from the given angle around the Y axis
969
- * This is equivalent to (but much faster than):
970
- *
971
- * Mat4.identity(dest);
972
- * Mat4.rotateY(dest, dest, rad);
973
- *
974
- * @param {Mat4} out Mat4 receiving operation result
975
- * @param {Number} rad the angle to rotate the matrix by
976
- * @returns {Mat4} out
977
- */
978
-
979
- export function fromYRotation(out: Mat4, rad: number): Mat4 {
980
- const s = Math.sin(rad);
981
- const c = Math.cos(rad); // Perform axis-specific matrix multiplication
982
-
983
- out[0] = c;
984
- out[1] = 0;
985
- out[2] = -s;
986
- out[3] = 0;
987
- out[4] = 0;
988
- out[5] = 1;
989
- out[6] = 0;
990
- out[7] = 0;
991
- out[8] = s;
992
- out[9] = 0;
993
- out[10] = c;
994
- out[11] = 0;
995
- out[12] = 0;
996
- out[13] = 0;
997
- out[14] = 0;
998
- out[15] = 1;
999
- return out;
1000
- }
1001
- /**
1002
- * Creates a matrix from the given angle around the Z axis
1003
- * This is equivalent to (but much faster than):
1004
- *
1005
- * Mat4.identity(dest);
1006
- * Mat4.rotateZ(dest, dest, rad);
1007
- *
1008
- * @param {Mat4} out Mat4 receiving operation result
1009
- * @param {Number} rad the angle to rotate the matrix by
1010
- * @returns {Mat4} out
1011
- */
1012
-
1013
- export function fromZRotation(out: Mat4, rad: number): Mat4 {
1014
- const s = Math.sin(rad);
1015
- const c = Math.cos(rad); // Perform axis-specific matrix multiplication
1016
-
1017
- out[0] = c;
1018
- out[1] = s;
1019
- out[2] = 0;
1020
- out[3] = 0;
1021
- out[4] = -s;
1022
- out[5] = c;
1023
- out[6] = 0;
1024
- out[7] = 0;
1025
- out[8] = 0;
1026
- out[9] = 0;
1027
- out[10] = 1;
1028
- out[11] = 0;
1029
- out[12] = 0;
1030
- out[13] = 0;
1031
- out[14] = 0;
1032
- out[15] = 1;
1033
- return out;
1034
- }
1035
- /**
1036
- * Creates a matrix from a quaternion rotation and vector translation
1037
- * This is equivalent to (but much faster than):
1038
- *
1039
- * Mat4.identity(dest);
1040
- * Mat4.translate(dest, vec);
1041
- * let quatMat = Mat4.create();
1042
- * quat4.toMat4(quat, quatMat);
1043
- * Mat4.multiply(dest, quatMat);
1044
- *
1045
- * @param {Mat4} out Mat4 receiving operation result
1046
- * @param {quat4} q Rotation quaternion
1047
- * @param {Vec3} v Translation vector
1048
- * @returns {Mat4} out
1049
- */
1050
-
1051
- export function fromRotationTranslation(
1052
- out: Mat4,
1053
- q: Quat | Quat2,
1054
- v: Vec3,
1055
- ): Mat4 {
1056
- // Quaternion math
1057
- const x = q[0],
1058
- y = q[1],
1059
- z = q[2],
1060
- w = q[3];
1061
- const x2 = x + x;
1062
- const y2 = y + y;
1063
- const z2 = z + z;
1064
- const xx = x * x2;
1065
- const xy = x * y2;
1066
- const xz = x * z2;
1067
- const yy = y * y2;
1068
- const yz = y * z2;
1069
- const zz = z * z2;
1070
- const wx = w * x2;
1071
- const wy = w * y2;
1072
- const wz = w * z2;
1073
- out[0] = 1 - (yy + zz);
1074
- out[1] = xy + wz;
1075
- out[2] = xz - wy;
1076
- out[3] = 0;
1077
- out[4] = xy - wz;
1078
- out[5] = 1 - (xx + zz);
1079
- out[6] = yz + wx;
1080
- out[7] = 0;
1081
- out[8] = xz + wy;
1082
- out[9] = yz - wx;
1083
- out[10] = 1 - (xx + yy);
1084
- out[11] = 0;
1085
- out[12] = v[0];
1086
- out[13] = v[1];
1087
- out[14] = v[2];
1088
- out[15] = 1;
1089
- return out;
1090
- }
1091
- /**
1092
- * Creates a new Mat4 from a dual quat.
1093
- *
1094
- * @param {Mat4} out Matrix
1095
- * @param {Quat2} a Dual Quaternion
1096
- * @returns {Mat4} Mat4 receiving operation result
1097
- */
1098
-
1099
- export function fromQuat2(out: Mat4, a: Quat2): Mat4 {
1100
- const translation = getMatrixArrayType(3) as Vec3;
1101
- const bx = -a[0],
1102
- by = -a[1],
1103
- bz = -a[2],
1104
- bw = a[3],
1105
- ax = a[4],
1106
- ay = a[5],
1107
- az = a[6],
1108
- aw = a[7];
1109
- const magnitude = bx * bx + by * by + bz * bz + bw * bw; //Only scale if it makes sense
1110
-
1111
- if (magnitude > 0) {
1112
- translation[0] = ((ax * bw + aw * bx + ay * bz - az * by) * 2) / magnitude;
1113
- translation[1] = ((ay * bw + aw * by + az * bx - ax * bz) * 2) / magnitude;
1114
- translation[2] = ((az * bw + aw * bz + ax * by - ay * bx) * 2) / magnitude;
1115
- } else {
1116
- translation[0] = (ax * bw + aw * bx + ay * bz - az * by) * 2;
1117
- translation[1] = (ay * bw + aw * by + az * bx - ax * bz) * 2;
1118
- translation[2] = (az * bw + aw * bz + ax * by - ay * bx) * 2;
1119
- }
1120
-
1121
- fromRotationTranslation(out, a, translation);
1122
- return out;
1123
- }
1124
- /**
1125
- * Returns the translation vector component of a transformation
1126
- * matrix. If a matrix is built with fromRotationTranslation,
1127
- * the returned vector will be the same as the translation vector
1128
- * originally supplied.
1129
- * @param {Vec3} out Vector to receive translation component
1130
- * @param {Mat4} mat Matrix to be decomposed (input)
1131
- * @return {Vec3} out
1132
- */
1133
-
1134
- export function getTranslation(out: Vec3, mat: Mat4): Vec3 {
1135
- out[0] = mat[12];
1136
- out[1] = mat[13];
1137
- out[2] = mat[14];
1138
- return out;
1139
- }
1140
- /**
1141
- * Returns the scaling factor component of a transformation
1142
- * matrix. If a matrix is built with fromRotationTranslationScale
1143
- * with a normalized Quaternion paramter, the returned vector will be
1144
- * the same as the scaling vector
1145
- * originally supplied.
1146
- * @param {Vec3} out Vector to receive scaling factor component
1147
- * @param {Mat4} mat Matrix to be decomposed (input)
1148
- * @return {Vec3} out
1149
- */
1150
-
1151
- export function getScaling(out: Vec3, mat: Mat4): Vec3 {
1152
- const m11 = mat[0];
1153
- const m12 = mat[1];
1154
- const m13 = mat[2];
1155
- const m21 = mat[4];
1156
- const m22 = mat[5];
1157
- const m23 = mat[6];
1158
- const m31 = mat[8];
1159
- const m32 = mat[9];
1160
- const m33 = mat[10];
1161
- out[0] = Math.hypot(m11, m12, m13);
1162
- out[1] = Math.hypot(m21, m22, m23);
1163
- out[2] = Math.hypot(m31, m32, m33);
1164
- return out;
1165
- }
1166
- /**
1167
- * Returns a quaternion representing the rotational component
1168
- * of a transformation matrix. If a matrix is built with
1169
- * fromRotationTranslation, the returned quaternion will be the
1170
- * same as the quaternion originally supplied.
1171
- * @param {quat} out Quaternion to receive the rotation component
1172
- * @param {Mat4} mat Matrix to be decomposed (input)
1173
- * @return {quat} out
1174
- */
1175
-
1176
- export function getRotation(out: Quat, mat: Mat4): Quat {
1177
- const scaling = getMatrixArrayType(3) as Vec3;
1178
- getScaling(scaling, mat);
1179
- const is1 = 1 / scaling[0];
1180
- const is2 = 1 / scaling[1];
1181
- const is3 = 1 / scaling[2];
1182
- const sm11 = mat[0] * is1;
1183
- const sm12 = mat[1] * is2;
1184
- const sm13 = mat[2] * is3;
1185
- const sm21 = mat[4] * is1;
1186
- const sm22 = mat[5] * is2;
1187
- const sm23 = mat[6] * is3;
1188
- const sm31 = mat[8] * is1;
1189
- const sm32 = mat[9] * is2;
1190
- const sm33 = mat[10] * is3;
1191
- const trace = sm11 + sm22 + sm33;
1192
- let S = 0;
1193
-
1194
- if (trace > 0) {
1195
- S = Math.sqrt(trace + 1.0) * 2;
1196
- out[3] = 0.25 * S;
1197
- out[0] = (sm23 - sm32) / S;
1198
- out[1] = (sm31 - sm13) / S;
1199
- out[2] = (sm12 - sm21) / S;
1200
- } else if (sm11 > sm22 && sm11 > sm33) {
1201
- S = Math.sqrt(1.0 + sm11 - sm22 - sm33) * 2;
1202
- out[3] = (sm23 - sm32) / S;
1203
- out[0] = 0.25 * S;
1204
- out[1] = (sm12 + sm21) / S;
1205
- out[2] = (sm31 + sm13) / S;
1206
- } else if (sm22 > sm33) {
1207
- S = Math.sqrt(1.0 + sm22 - sm11 - sm33) * 2;
1208
- out[3] = (sm31 - sm13) / S;
1209
- out[0] = (sm12 + sm21) / S;
1210
- out[1] = 0.25 * S;
1211
- out[2] = (sm23 + sm32) / S;
1212
- } else {
1213
- S = Math.sqrt(1.0 + sm33 - sm11 - sm22) * 2;
1214
- out[3] = (sm12 - sm21) / S;
1215
- out[0] = (sm31 + sm13) / S;
1216
- out[1] = (sm23 + sm32) / S;
1217
- out[2] = 0.25 * S;
1218
- }
1219
-
1220
- return out;
1221
- }
1222
- /**
1223
- * Decomposes a transformation matrix into its rotation, translation
1224
- * and scale components. Returns only the rotation component
1225
- * @param {quat} out_r Quaternion to receive the rotation component
1226
- * @param {Vec3} out_t Vector to receive the translation vector
1227
- * @param {Vec3} out_s Vector to receive the scaling factor
1228
- * @param {Mat4} mat Matrix to be decomposed (input)
1229
- * @returns {quat} out_r
1230
- */
1231
-
1232
- export function decompose(
1233
- out_r: Quat,
1234
- out_t: Vec3,
1235
- out_s: Vec3,
1236
- mat: Mat4,
1237
- ): Quat {
1238
- out_t[0] = mat[12];
1239
- out_t[1] = mat[13];
1240
- out_t[2] = mat[14];
1241
- const m11 = mat[0];
1242
- const m12 = mat[1];
1243
- const m13 = mat[2];
1244
- const m21 = mat[4];
1245
- const m22 = mat[5];
1246
- const m23 = mat[6];
1247
- const m31 = mat[8];
1248
- const m32 = mat[9];
1249
- const m33 = mat[10];
1250
- out_s[0] = Math.hypot(m11, m12, m13);
1251
- out_s[1] = Math.hypot(m21, m22, m23);
1252
- out_s[2] = Math.hypot(m31, m32, m33);
1253
- const is1 = 1 / out_s[0];
1254
- const is2 = 1 / out_s[1];
1255
- const is3 = 1 / out_s[2];
1256
- const sm11 = m11 * is1;
1257
- const sm12 = m12 * is2;
1258
- const sm13 = m13 * is3;
1259
- const sm21 = m21 * is1;
1260
- const sm22 = m22 * is2;
1261
- const sm23 = m23 * is3;
1262
- const sm31 = m31 * is1;
1263
- const sm32 = m32 * is2;
1264
- const sm33 = m33 * is3;
1265
- const trace = sm11 + sm22 + sm33;
1266
- let S = 0;
1267
-
1268
- if (trace > 0) {
1269
- S = Math.sqrt(trace + 1.0) * 2;
1270
- out_r[3] = 0.25 * S;
1271
- out_r[0] = (sm23 - sm32) / S;
1272
- out_r[1] = (sm31 - sm13) / S;
1273
- out_r[2] = (sm12 - sm21) / S;
1274
- } else if (sm11 > sm22 && sm11 > sm33) {
1275
- S = Math.sqrt(1.0 + sm11 - sm22 - sm33) * 2;
1276
- out_r[3] = (sm23 - sm32) / S;
1277
- out_r[0] = 0.25 * S;
1278
- out_r[1] = (sm12 + sm21) / S;
1279
- out_r[2] = (sm31 + sm13) / S;
1280
- } else if (sm22 > sm33) {
1281
- S = Math.sqrt(1.0 + sm22 - sm11 - sm33) * 2;
1282
- out_r[3] = (sm31 - sm13) / S;
1283
- out_r[0] = (sm12 + sm21) / S;
1284
- out_r[1] = 0.25 * S;
1285
- out_r[2] = (sm23 + sm32) / S;
1286
- } else {
1287
- S = Math.sqrt(1.0 + sm33 - sm11 - sm22) * 2;
1288
- out_r[3] = (sm12 - sm21) / S;
1289
- out_r[0] = (sm31 + sm13) / S;
1290
- out_r[1] = (sm23 + sm32) / S;
1291
- out_r[2] = 0.25 * S;
1292
- }
1293
-
1294
- return out_r;
1295
- }
1296
- /**
1297
- * Creates a matrix from a quaternion rotation, vector translation and vector scale
1298
- * This is equivalent to (but much faster than):
1299
- *
1300
- * Mat4.identity(dest);
1301
- * Mat4.translate(dest, vec);
1302
- * let quatMat = Mat4.create();
1303
- * quat4.toMat4(quat, quatMat);
1304
- * Mat4.multiply(dest, quatMat);
1305
- * Mat4.scale(dest, scale)
1306
- *
1307
- * @param {Mat4} out Mat4 receiving operation result
1308
- * @param {quat4} q Rotation quaternion
1309
- * @param {Vec3} v Translation vector
1310
- * @param {Vec3} s Scaling vector
1311
- * @returns {Mat4} out
1312
- */
1313
-
1314
- export function fromRotationTranslationScale(
1315
- out: Mat4,
1316
- q: Quat,
1317
- v: Vec3,
1318
- s: Vec3,
1319
- ): Mat4 {
1320
- // Quaternion math
1321
- const x = q[0],
1322
- y = q[1],
1323
- z = q[2],
1324
- w = q[3];
1325
- const x2 = x + x;
1326
- const y2 = y + y;
1327
- const z2 = z + z;
1328
- const xx = x * x2;
1329
- const xy = x * y2;
1330
- const xz = x * z2;
1331
- const yy = y * y2;
1332
- const yz = y * z2;
1333
- const zz = z * z2;
1334
- const wx = w * x2;
1335
- const wy = w * y2;
1336
- const wz = w * z2;
1337
- const sx = s[0];
1338
- const sy = s[1];
1339
- const sz = s[2];
1340
- out[0] = (1 - (yy + zz)) * sx;
1341
- out[1] = (xy + wz) * sx;
1342
- out[2] = (xz - wy) * sx;
1343
- out[3] = 0;
1344
- out[4] = (xy - wz) * sy;
1345
- out[5] = (1 - (xx + zz)) * sy;
1346
- out[6] = (yz + wx) * sy;
1347
- out[7] = 0;
1348
- out[8] = (xz + wy) * sz;
1349
- out[9] = (yz - wx) * sz;
1350
- out[10] = (1 - (xx + yy)) * sz;
1351
- out[11] = 0;
1352
- out[12] = v[0];
1353
- out[13] = v[1];
1354
- out[14] = v[2];
1355
- out[15] = 1;
1356
- return out;
1357
- }
1358
- /**
1359
- * Creates a matrix from a quaternion rotation, vector translation and vector scale, rotating and scaling around the given origin
1360
- * This is equivalent to (but much faster than):
1361
- *
1362
- * Mat4.identity(dest);
1363
- * Mat4.translate(dest, vec);
1364
- * Mat4.translate(dest, origin);
1365
- * let quatMat = Mat4.create();
1366
- * quat4.toMat4(quat, quatMat);
1367
- * Mat4.multiply(dest, quatMat);
1368
- * Mat4.scale(dest, scale)
1369
- * Mat4.translate(dest, negativeOrigin);
1370
- *
1371
- * @param {Mat4} out Mat4 receiving operation result
1372
- * @param {quat4} q Rotation quaternion
1373
- * @param {Vec3} v Translation vector
1374
- * @param {Vec3} s Scaling vector
1375
- * @param {Vec3} o The origin vector around which to scale and rotate
1376
- * @returns {Mat4} out
1377
- */
1378
-
1379
- export function fromRotationTranslationScaleOrigin(
1380
- out: Mat4,
1381
- q: Quat,
1382
- v: Vec3,
1383
- s: Vec3,
1384
- o: Vec3,
1385
- ): Mat4 {
1386
- // Quaternion math
1387
- const x = q[0],
1388
- y = q[1],
1389
- z = q[2],
1390
- w = q[3];
1391
- const x2 = x + x;
1392
- const y2 = y + y;
1393
- const z2 = z + z;
1394
- const xx = x * x2;
1395
- const xy = x * y2;
1396
- const xz = x * z2;
1397
- const yy = y * y2;
1398
- const yz = y * z2;
1399
- const zz = z * z2;
1400
- const wx = w * x2;
1401
- const wy = w * y2;
1402
- const wz = w * z2;
1403
- const sx = s[0];
1404
- const sy = s[1];
1405
- const sz = s[2];
1406
- const ox = o[0];
1407
- const oy = o[1];
1408
- const oz = o[2];
1409
- const out0 = (1 - (yy + zz)) * sx;
1410
- const out1 = (xy + wz) * sx;
1411
- const out2 = (xz - wy) * sx;
1412
- const out4 = (xy - wz) * sy;
1413
- const out5 = (1 - (xx + zz)) * sy;
1414
- const out6 = (yz + wx) * sy;
1415
- const out8 = (xz + wy) * sz;
1416
- const out9 = (yz - wx) * sz;
1417
- const out10 = (1 - (xx + yy)) * sz;
1418
- out[0] = out0;
1419
- out[1] = out1;
1420
- out[2] = out2;
1421
- out[3] = 0;
1422
- out[4] = out4;
1423
- out[5] = out5;
1424
- out[6] = out6;
1425
- out[7] = 0;
1426
- out[8] = out8;
1427
- out[9] = out9;
1428
- out[10] = out10;
1429
- out[11] = 0;
1430
- out[12] = v[0] + ox - (out0 * ox + out4 * oy + out8 * oz);
1431
- out[13] = v[1] + oy - (out1 * ox + out5 * oy + out9 * oz);
1432
- out[14] = v[2] + oz - (out2 * ox + out6 * oy + out10 * oz);
1433
- out[15] = 1;
1434
- return out;
1435
- }
1436
- /**
1437
- * Calculates a 4x4 matrix from the given quaternion
1438
- *
1439
- * @param {Mat4} out Mat4 receiving operation result
1440
- * @param {Quat} q Quaternion to create matrix from
1441
- *
1442
- * @returns {Mat4} out
1443
- */
1444
-
1445
- export function fromQuat(out: Mat4, q: Quat): Mat4 {
1446
- const x = q[0],
1447
- y = q[1],
1448
- z = q[2],
1449
- w = q[3];
1450
- const x2 = x + x;
1451
- const y2 = y + y;
1452
- const z2 = z + z;
1453
- const xx = x * x2;
1454
- const yx = y * x2;
1455
- const yy = y * y2;
1456
- const zx = z * x2;
1457
- const zy = z * y2;
1458
- const zz = z * z2;
1459
- const wx = w * x2;
1460
- const wy = w * y2;
1461
- const wz = w * z2;
1462
- out[0] = 1 - yy - zz;
1463
- out[1] = yx + wz;
1464
- out[2] = zx - wy;
1465
- out[3] = 0;
1466
- out[4] = yx - wz;
1467
- out[5] = 1 - xx - zz;
1468
- out[6] = zy + wx;
1469
- out[7] = 0;
1470
- out[8] = zx + wy;
1471
- out[9] = zy - wx;
1472
- out[10] = 1 - xx - yy;
1473
- out[11] = 0;
1474
- out[12] = 0;
1475
- out[13] = 0;
1476
- out[14] = 0;
1477
- out[15] = 1;
1478
- return out;
1479
- }
1480
- /**
1481
- * Generates a frustum matrix with the given bounds
1482
- *
1483
- * @param {Mat4} out Mat4 frustum matrix will be written into
1484
- * @param {Number} left Left bound of the frustum
1485
- * @param {Number} right Right bound of the frustum
1486
- * @param {Number} bottom Bottom bound of the frustum
1487
- * @param {Number} top Top bound of the frustum
1488
- * @param {Number} near Near bound of the frustum
1489
- * @param {Number} far Far bound of the frustum
1490
- * @returns {Mat4} out
1491
- */
1492
-
1493
- export function frustum(
1494
- out: Mat4,
1495
- left: number,
1496
- right: number,
1497
- bottom: number,
1498
- top: number,
1499
- near: number,
1500
- far: number,
1501
- ): Mat4 {
1502
- const rl = 1 / (right - left);
1503
- const tb = 1 / (top - bottom);
1504
- const nf = 1 / (near - far);
1505
- out[0] = near * 2 * rl;
1506
- out[1] = 0;
1507
- out[2] = 0;
1508
- out[3] = 0;
1509
- out[4] = 0;
1510
- out[5] = near * 2 * tb;
1511
- out[6] = 0;
1512
- out[7] = 0;
1513
- out[8] = (right + left) * rl;
1514
- out[9] = (top + bottom) * tb;
1515
- out[10] = (far + near) * nf;
1516
- out[11] = -1;
1517
- out[12] = 0;
1518
- out[13] = 0;
1519
- out[14] = far * near * 2 * nf;
1520
- out[15] = 0;
1521
- return out;
1522
- }
1523
- /**
1524
- * Generates a perspective projection matrix with the given bounds.
1525
- * The near/far clip planes correspond to a normalized device coordinate Z range of [-1, 1],
1526
- * which matches WebGL/OpenGL's clip volume.
1527
- * Passing null/undefined/no value for far will generate infinite projection matrix.
1528
- *
1529
- * @param {Mat4} out Mat4 frustum matrix will be written into
1530
- * @param {number} fovy Vertical field of view in radians
1531
- * @param {number} aspect Aspect ratio. typically viewport width/height
1532
- * @param {number} near Near bound of the frustum
1533
- * @param {number} far Far bound of the frustum, can be null or Infinity
1534
- * @returns {Mat4} out
1535
- */
1536
-
1537
- export function perspectiveNO(
1538
- out: Mat4,
1539
- fovy: number,
1540
- aspect: number,
1541
- near: number,
1542
- far: number,
1543
- ): Mat4 {
1544
- const f = 1.0 / Math.tan(fovy / 2);
1545
- out[0] = f / aspect;
1546
- out[1] = 0;
1547
- out[2] = 0;
1548
- out[3] = 0;
1549
- out[4] = 0;
1550
- out[5] = f;
1551
- out[6] = 0;
1552
- out[7] = 0;
1553
- out[8] = 0;
1554
- out[9] = 0;
1555
- out[11] = -1;
1556
- out[12] = 0;
1557
- out[13] = 0;
1558
- out[15] = 0;
1559
-
1560
- if (far != null && far !== Infinity) {
1561
- const nf = 1 / (near - far);
1562
- out[10] = (far + near) * nf;
1563
- out[14] = 2 * far * near * nf;
1564
- } else {
1565
- out[10] = -1;
1566
- out[14] = -2 * near;
1567
- }
1568
-
1569
- return out;
1570
- }
1571
- /**
1572
- * Alias for {@link perspectiveNO}
1573
- * @function
1574
- */
1575
-
1576
- export const perspective = perspectiveNO;
1577
- /**
1578
- * Generates a perspective projection matrix suitable for WebGPU with the given bounds.
1579
- * The near/far clip planes correspond to a normalized device coordinate Z range of [0, 1],
1580
- * which matches WebGPU/Vulkan/DirectX/Metal's clip volume.
1581
- * Passing null/undefined/no value for far will generate infinite projection matrix.
1582
- *
1583
- * @param {Mat4} out Mat4 frustum matrix will be written into
1584
- * @param {number} fovy Vertical field of view in radians
1585
- * @param {number} aspect Aspect ratio. typically viewport width/height
1586
- * @param {number} near Near bound of the frustum
1587
- * @param {number} far Far bound of the frustum, can be null or Infinity
1588
- * @returns {Mat4} out
1589
- */
1590
-
1591
- export function perspectiveZO(
1592
- out: Mat4,
1593
- fovy: number,
1594
- aspect: number,
1595
- near: number,
1596
- far: number,
1597
- ): Mat4 {
1598
- const f = 1.0 / Math.tan(fovy / 2);
1599
- out[0] = f / aspect;
1600
- out[1] = 0;
1601
- out[2] = 0;
1602
- out[3] = 0;
1603
- out[4] = 0;
1604
- out[5] = f;
1605
- out[6] = 0;
1606
- out[7] = 0;
1607
- out[8] = 0;
1608
- out[9] = 0;
1609
- out[11] = -1;
1610
- out[12] = 0;
1611
- out[13] = 0;
1612
- out[15] = 0;
1613
-
1614
- if (far != null && far !== Infinity) {
1615
- const nf = 1 / (near - far);
1616
- out[10] = far * nf;
1617
- out[14] = far * near * nf;
1618
- } else {
1619
- out[10] = -1;
1620
- out[14] = -near;
1621
- }
1622
-
1623
- return out;
1624
- }
1625
- /**
1626
- * Generates a perspective projection matrix with the given field of view.
1627
- * This is primarily useful for generating projection matrices to be used
1628
- * with the still experiemental WebVR API.
1629
- *
1630
- * @param {Mat4} out Mat4 frustum matrix will be written into
1631
- * @param {Object} fov Object containing the following values: upDegrees, downDegrees, leftDegrees, rightDegrees
1632
- * @param {number} near Near bound of the frustum
1633
- * @param {number} far Far bound of the frustum
1634
- * @returns {Mat4} out
1635
- */
1636
-
1637
- export function perspectiveFromFieldOfView(
1638
- out: Mat4,
1639
- fov: FOV,
1640
- near: number,
1641
- far: number,
1642
- ): Mat4 {
1643
- const upTan = Math.tan((fov.upDegrees * Math.PI) / 180.0);
1644
- const downTan = Math.tan((fov.downDegrees * Math.PI) / 180.0);
1645
- const leftTan = Math.tan((fov.leftDegrees * Math.PI) / 180.0);
1646
- const rightTan = Math.tan((fov.rightDegrees * Math.PI) / 180.0);
1647
- const xScale = 2.0 / (leftTan + rightTan);
1648
- const yScale = 2.0 / (upTan + downTan);
1649
- out[0] = xScale;
1650
- out[1] = 0.0;
1651
- out[2] = 0.0;
1652
- out[3] = 0.0;
1653
- out[4] = 0.0;
1654
- out[5] = yScale;
1655
- out[6] = 0.0;
1656
- out[7] = 0.0;
1657
- out[8] = -((leftTan - rightTan) * xScale * 0.5);
1658
- out[9] = (upTan - downTan) * yScale * 0.5;
1659
- out[10] = far / (near - far);
1660
- out[11] = -1.0;
1661
- out[12] = 0.0;
1662
- out[13] = 0.0;
1663
- out[14] = (far * near) / (near - far);
1664
- out[15] = 0.0;
1665
- return out;
1666
- }
1667
- /**
1668
- * Generates a orthogonal projection matrix with the given bounds.
1669
- * The near/far clip planes correspond to a normalized device coordinate Z range of [-1, 1],
1670
- * which matches WebGL/OpenGL's clip volume.
1671
- *
1672
- * @param {Mat4} out Mat4 frustum matrix will be written into
1673
- * @param {number} left Left bound of the frustum
1674
- * @param {number} right Right bound of the frustum
1675
- * @param {number} bottom Bottom bound of the frustum
1676
- * @param {number} top Top bound of the frustum
1677
- * @param {number} near Near bound of the frustum
1678
- * @param {number} far Far bound of the frustum
1679
- * @returns {Mat4} out
1680
- */
1681
-
1682
- export function orthoNO(
1683
- out: Mat4,
1684
- left: number,
1685
- right: number,
1686
- bottom: number,
1687
- top: number,
1688
- near: number,
1689
- far: number,
1690
- ): Mat4 {
1691
- const lr = 1 / (left - right);
1692
- const bt = 1 / (bottom - top);
1693
- const nf = 1 / (near - far);
1694
- out[0] = -2 * lr;
1695
- out[1] = 0;
1696
- out[2] = 0;
1697
- out[3] = 0;
1698
- out[4] = 0;
1699
- out[5] = -2 * bt;
1700
- out[6] = 0;
1701
- out[7] = 0;
1702
- out[8] = 0;
1703
- out[9] = 0;
1704
- out[10] = 2 * nf;
1705
- out[11] = 0;
1706
- out[12] = (left + right) * lr;
1707
- out[13] = (top + bottom) * bt;
1708
- out[14] = (far + near) * nf;
1709
- out[15] = 1;
1710
- return out;
1711
- }
1712
- /**
1713
- * Alias for {@link orthoNO}
1714
- * @function
1715
- */
1716
-
1717
- export const ortho = orthoNO;
1718
- /**
1719
- * Generates a orthogonal projection matrix with the given bounds.
1720
- * The near/far clip planes correspond to a normalized device coordinate Z range of [0, 1],
1721
- * which matches WebGPU/Vulkan/DirectX/Metal's clip volume.
1722
- *
1723
- * @param {Mat4} out Mat4 frustum matrix will be written into
1724
- * @param {number} left Left bound of the frustum
1725
- * @param {number} right Right bound of the frustum
1726
- * @param {number} bottom Bottom bound of the frustum
1727
- * @param {number} top Top bound of the frustum
1728
- * @param {number} near Near bound of the frustum
1729
- * @param {number} far Far bound of the frustum
1730
- * @returns {Mat4} out
1731
- */
1732
-
1733
- export function orthoZO(
1734
- out: Mat4,
1735
- left: number,
1736
- right: number,
1737
- bottom: number,
1738
- top: number,
1739
- near: number,
1740
- far: number,
1741
- ): Mat4 {
1742
- const lr = 1 / (left - right);
1743
- const bt = 1 / (bottom - top);
1744
- const nf = 1 / (near - far);
1745
- out[0] = -2 * lr;
1746
- out[1] = 0;
1747
- out[2] = 0;
1748
- out[3] = 0;
1749
- out[4] = 0;
1750
- out[5] = -2 * bt;
1751
- out[6] = 0;
1752
- out[7] = 0;
1753
- out[8] = 0;
1754
- out[9] = 0;
1755
- out[10] = nf;
1756
- out[11] = 0;
1757
- out[12] = (left + right) * lr;
1758
- out[13] = (top + bottom) * bt;
1759
- out[14] = near * nf;
1760
- out[15] = 1;
1761
- return out;
1762
- }
1763
- /**
1764
- * Generates a look-at matrix with the given eye position, focal point, and up axis.
1765
- * If you want a matrix that actually makes an object look at another object, you should use targetTo instead.
1766
- *
1767
- * @param {Mat4} out Mat4 frustum matrix will be written into
1768
- * @param {Vec3} eye Position of the viewer
1769
- * @param {Vec3} center Point the viewer is looking at
1770
- * @param {Vec3} up Vec3 pointing up
1771
- * @returns {Mat4} out
1772
- */
1773
-
1774
- export function lookAt(out: Mat4, eye: Vec3, center: Vec3, up: Vec3): Mat4 {
1775
- let x0, x1, x2, y0, y1, y2, z0, z1, z2, len;
1776
- const eyex = eye[0];
1777
- const eyey = eye[1];
1778
- const eyez = eye[2];
1779
- const upx = up[0];
1780
- const upy = up[1];
1781
- const upz = up[2];
1782
- const centerx = center[0];
1783
- const centery = center[1];
1784
- const centerz = center[2];
1785
-
1786
- if (
1787
- Math.abs(eyex - centerx) < EPSILON &&
1788
- Math.abs(eyey - centery) < EPSILON &&
1789
- Math.abs(eyez - centerz) < EPSILON
1790
- ) {
1791
- return identity(out);
1792
- }
1793
-
1794
- z0 = eyex - centerx;
1795
- z1 = eyey - centery;
1796
- z2 = eyez - centerz;
1797
- len = 1 / Math.hypot(z0, z1, z2);
1798
- z0 *= len;
1799
- z1 *= len;
1800
- z2 *= len;
1801
- x0 = upy * z2 - upz * z1;
1802
- x1 = upz * z0 - upx * z2;
1803
- x2 = upx * z1 - upy * z0;
1804
- len = Math.hypot(x0, x1, x2);
1805
-
1806
- if (!len) {
1807
- x0 = 0;
1808
- x1 = 0;
1809
- x2 = 0;
1810
- } else {
1811
- len = 1 / len;
1812
- x0 *= len;
1813
- x1 *= len;
1814
- x2 *= len;
1815
- }
1816
-
1817
- y0 = z1 * x2 - z2 * x1;
1818
- y1 = z2 * x0 - z0 * x2;
1819
- y2 = z0 * x1 - z1 * x0;
1820
- len = Math.hypot(y0, y1, y2);
1821
-
1822
- if (!len) {
1823
- y0 = 0;
1824
- y1 = 0;
1825
- y2 = 0;
1826
- } else {
1827
- len = 1 / len;
1828
- y0 *= len;
1829
- y1 *= len;
1830
- y2 *= len;
1831
- }
1832
-
1833
- out[0] = x0;
1834
- out[1] = y0;
1835
- out[2] = z0;
1836
- out[3] = 0;
1837
- out[4] = x1;
1838
- out[5] = y1;
1839
- out[6] = z1;
1840
- out[7] = 0;
1841
- out[8] = x2;
1842
- out[9] = y2;
1843
- out[10] = z2;
1844
- out[11] = 0;
1845
- out[12] = -(x0 * eyex + x1 * eyey + x2 * eyez);
1846
- out[13] = -(y0 * eyex + y1 * eyey + y2 * eyez);
1847
- out[14] = -(z0 * eyex + z1 * eyey + z2 * eyez);
1848
- out[15] = 1;
1849
- return out;
1850
- }
1851
- /**
1852
- * Generates a matrix that makes something look at something else.
1853
- *
1854
- * @param {Mat4} out Mat4 frustum matrix will be written into
1855
- * @param {Vec3} eye Position of the viewer
1856
- * @param {Vec3} center Point the viewer is looking at
1857
- * @param {Vec3} up Vec3 pointing up
1858
- * @returns {Mat4} out
1859
- */
1860
-
1861
- export function targetTo(out: Mat4, eye: Vec3, target: Vec3, up: Vec3): Mat4 {
1862
- const eyex = eye[0],
1863
- eyey = eye[1],
1864
- eyez = eye[2],
1865
- upx = up[0],
1866
- upy = up[1],
1867
- upz = up[2];
1868
- let z0 = eyex - target[0],
1869
- z1 = eyey - target[1],
1870
- z2 = eyez - target[2];
1871
- let len = z0 * z0 + z1 * z1 + z2 * z2;
1872
-
1873
- if (len > 0) {
1874
- len = 1 / Math.sqrt(len);
1875
- z0 *= len;
1876
- z1 *= len;
1877
- z2 *= len;
1878
- }
1879
-
1880
- let x0 = upy * z2 - upz * z1,
1881
- x1 = upz * z0 - upx * z2,
1882
- x2 = upx * z1 - upy * z0;
1883
- len = x0 * x0 + x1 * x1 + x2 * x2;
1884
-
1885
- if (len > 0) {
1886
- len = 1 / Math.sqrt(len);
1887
- x0 *= len;
1888
- x1 *= len;
1889
- x2 *= len;
1890
- }
1891
-
1892
- out[0] = x0;
1893
- out[1] = x1;
1894
- out[2] = x2;
1895
- out[3] = 0;
1896
- out[4] = z1 * x2 - z2 * x1;
1897
- out[5] = z2 * x0 - z0 * x2;
1898
- out[6] = z0 * x1 - z1 * x0;
1899
- out[7] = 0;
1900
- out[8] = z0;
1901
- out[9] = z1;
1902
- out[10] = z2;
1903
- out[11] = 0;
1904
- out[12] = eyex;
1905
- out[13] = eyey;
1906
- out[14] = eyez;
1907
- out[15] = 1;
1908
- return out;
1909
- }
1910
- /**
1911
- * Returns a string representation of a Mat4
1912
- *
1913
- * @param {Mat4} a matrix to represent as a string
1914
- * @returns {String} string representation of the matrix
1915
- */
1916
-
1917
- export function str(a: Mat4): string {
1918
- return `Mat4(${a[0]}, ${a[1]}, ${a[2]}, ${a[3]}, ${a[4]}, ${a[5]}, ${a[6]}, ${a[7]}, ${a[8]}, ${a[9]}, ${a[10]}, ${a[11]}, ${a[12]}, ${a[13]}, ${a[14]}, ${a[15]})`;
1919
- }
1920
- /**
1921
- * Returns Frobenius norm of a Mat4
1922
- *
1923
- * @param {Mat4} a the matrix to calculate Frobenius norm of
1924
- * @returns {Number} Frobenius norm
1925
- */
1926
-
1927
- export function frob(a: Mat4): number {
1928
- return Math.hypot(
1929
- a[0],
1930
- a[1],
1931
- a[2],
1932
- a[3],
1933
- a[4],
1934
- a[5],
1935
- a[6],
1936
- a[7],
1937
- a[8],
1938
- a[9],
1939
- a[10],
1940
- a[11],
1941
- a[12],
1942
- a[13],
1943
- a[14],
1944
- a[15],
1945
- );
1946
- }
1947
- /**
1948
- * Adds two Mat4's
1949
- *
1950
- * @param {Mat4} out the receiving matrix
1951
- * @param {Mat4} a the first operand
1952
- * @param {Mat4} b the second operand
1953
- * @returns {Mat4} out
1954
- */
1955
-
1956
- export function add(out: Mat4, a: Mat4, b: Mat4): Mat4 {
1957
- out[0] = a[0] + b[0];
1958
- out[1] = a[1] + b[1];
1959
- out[2] = a[2] + b[2];
1960
- out[3] = a[3] + b[3];
1961
- out[4] = a[4] + b[4];
1962
- out[5] = a[5] + b[5];
1963
- out[6] = a[6] + b[6];
1964
- out[7] = a[7] + b[7];
1965
- out[8] = a[8] + b[8];
1966
- out[9] = a[9] + b[9];
1967
- out[10] = a[10] + b[10];
1968
- out[11] = a[11] + b[11];
1969
- out[12] = a[12] + b[12];
1970
- out[13] = a[13] + b[13];
1971
- out[14] = a[14] + b[14];
1972
- out[15] = a[15] + b[15];
1973
- return out;
1974
- }
1975
- /**
1976
- * Subtracts matrix b from matrix a
1977
- *
1978
- * @param {Mat4} out the receiving matrix
1979
- * @param {Mat4} a the first operand
1980
- * @param {Mat4} b the second operand
1981
- * @returns {Mat4} out
1982
- */
1983
-
1984
- export function subtract(out: Mat4, a: Mat4, b: Mat4): Mat4 {
1985
- out[0] = a[0] - b[0];
1986
- out[1] = a[1] - b[1];
1987
- out[2] = a[2] - b[2];
1988
- out[3] = a[3] - b[3];
1989
- out[4] = a[4] - b[4];
1990
- out[5] = a[5] - b[5];
1991
- out[6] = a[6] - b[6];
1992
- out[7] = a[7] - b[7];
1993
- out[8] = a[8] - b[8];
1994
- out[9] = a[9] - b[9];
1995
- out[10] = a[10] - b[10];
1996
- out[11] = a[11] - b[11];
1997
- out[12] = a[12] - b[12];
1998
- out[13] = a[13] - b[13];
1999
- out[14] = a[14] - b[14];
2000
- out[15] = a[15] - b[15];
2001
- return out;
2002
- }
2003
- /**
2004
- * Multiply each element of the matrix by a scalar.
2005
- *
2006
- * @param {Mat4} out the receiving matrix
2007
- * @param {Mat4} a the matrix to scale
2008
- * @param {Number} b amount to scale the matrix's elements by
2009
- * @returns {Mat4} out
2010
- */
2011
-
2012
- export function multiplyScalar(out: Mat4, a: Mat4, b: number): Mat4 {
2013
- out[0] = a[0] * b;
2014
- out[1] = a[1] * b;
2015
- out[2] = a[2] * b;
2016
- out[3] = a[3] * b;
2017
- out[4] = a[4] * b;
2018
- out[5] = a[5] * b;
2019
- out[6] = a[6] * b;
2020
- out[7] = a[7] * b;
2021
- out[8] = a[8] * b;
2022
- out[9] = a[9] * b;
2023
- out[10] = a[10] * b;
2024
- out[11] = a[11] * b;
2025
- out[12] = a[12] * b;
2026
- out[13] = a[13] * b;
2027
- out[14] = a[14] * b;
2028
- out[15] = a[15] * b;
2029
- return out;
2030
- }
2031
- /**
2032
- * Adds two Mat4's after multiplying each element of the second operand by a scalar value.
2033
- *
2034
- * @param {Mat4} out the receiving vector
2035
- * @param {Mat4} a the first operand
2036
- * @param {Mat4} b the second operand
2037
- * @param {Number} scale the amount to scale b's elements by before adding
2038
- * @returns {Mat4} out
2039
- */
2040
-
2041
- export function multiplyScalarAndAdd(
2042
- out: Mat4,
2043
- a: Mat4,
2044
- b: Mat4,
2045
- scale: number,
2046
- ): Mat4 {
2047
- out[0] = a[0] + b[0] * scale;
2048
- out[1] = a[1] + b[1] * scale;
2049
- out[2] = a[2] + b[2] * scale;
2050
- out[3] = a[3] + b[3] * scale;
2051
- out[4] = a[4] + b[4] * scale;
2052
- out[5] = a[5] + b[5] * scale;
2053
- out[6] = a[6] + b[6] * scale;
2054
- out[7] = a[7] + b[7] * scale;
2055
- out[8] = a[8] + b[8] * scale;
2056
- out[9] = a[9] + b[9] * scale;
2057
- out[10] = a[10] + b[10] * scale;
2058
- out[11] = a[11] + b[11] * scale;
2059
- out[12] = a[12] + b[12] * scale;
2060
- out[13] = a[13] + b[13] * scale;
2061
- out[14] = a[14] + b[14] * scale;
2062
- out[15] = a[15] + b[15] * scale;
2063
- return out;
2064
- }
2065
- /**
2066
- * Returns whether or not the matrices have exactly the same elements in the same position (when compared with ===)
2067
- *
2068
- * @param {Mat4} a The first matrix.
2069
- * @param {Mat4} b The second matrix.
2070
- * @returns {Boolean} True if the matrices are equal, false otherwise.
2071
- */
2072
-
2073
- export function exactEquals(a: Mat4, b: Mat4): boolean {
2074
- return (
2075
- a[0] === b[0] &&
2076
- a[1] === b[1] &&
2077
- a[2] === b[2] &&
2078
- a[3] === b[3] &&
2079
- a[4] === b[4] &&
2080
- a[5] === b[5] &&
2081
- a[6] === b[6] &&
2082
- a[7] === b[7] &&
2083
- a[8] === b[8] &&
2084
- a[9] === b[9] &&
2085
- a[10] === b[10] &&
2086
- a[11] === b[11] &&
2087
- a[12] === b[12] &&
2088
- a[13] === b[13] &&
2089
- a[14] === b[14] &&
2090
- a[15] === b[15]
2091
- );
2092
- }
2093
- /**
2094
- * Returns whether or not the matrices have approximately the same elements in the same position.
2095
- *
2096
- * @param {Mat4} a The first matrix.
2097
- * @param {Mat4} b The second matrix.
2098
- * @returns {Boolean} True if the matrices are equal, false otherwise.
2099
- */
2100
-
2101
- export function equals(a: Mat4, b: Mat4): boolean {
2102
- const a0 = a[0],
2103
- a1 = a[1],
2104
- a2 = a[2],
2105
- a3 = a[3];
2106
- const a4 = a[4],
2107
- a5 = a[5],
2108
- a6 = a[6],
2109
- a7 = a[7];
2110
- const a8 = a[8],
2111
- a9 = a[9],
2112
- a10 = a[10],
2113
- a11 = a[11];
2114
- const a12 = a[12],
2115
- a13 = a[13],
2116
- a14 = a[14],
2117
- a15 = a[15];
2118
- const b0 = b[0],
2119
- b1 = b[1],
2120
- b2 = b[2],
2121
- b3 = b[3];
2122
- const b4 = b[4],
2123
- b5 = b[5],
2124
- b6 = b[6],
2125
- b7 = b[7];
2126
- const b8 = b[8],
2127
- b9 = b[9],
2128
- b10 = b[10],
2129
- b11 = b[11];
2130
- const b12 = b[12],
2131
- b13 = b[13],
2132
- b14 = b[14],
2133
- b15 = b[15];
2134
- return (
2135
- Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) &&
2136
- Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) &&
2137
- Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) &&
2138
- Math.abs(a3 - b3) <= EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)) &&
2139
- Math.abs(a4 - b4) <= EPSILON * Math.max(1.0, Math.abs(a4), Math.abs(b4)) &&
2140
- Math.abs(a5 - b5) <= EPSILON * Math.max(1.0, Math.abs(a5), Math.abs(b5)) &&
2141
- Math.abs(a6 - b6) <= EPSILON * Math.max(1.0, Math.abs(a6), Math.abs(b6)) &&
2142
- Math.abs(a7 - b7) <= EPSILON * Math.max(1.0, Math.abs(a7), Math.abs(b7)) &&
2143
- Math.abs(a8 - b8) <= EPSILON * Math.max(1.0, Math.abs(a8), Math.abs(b8)) &&
2144
- Math.abs(a9 - b9) <= EPSILON * Math.max(1.0, Math.abs(a9), Math.abs(b9)) &&
2145
- Math.abs(a10 - b10) <=
2146
- EPSILON * Math.max(1.0, Math.abs(a10), Math.abs(b10)) &&
2147
- Math.abs(a11 - b11) <=
2148
- EPSILON * Math.max(1.0, Math.abs(a11), Math.abs(b11)) &&
2149
- Math.abs(a12 - b12) <=
2150
- EPSILON * Math.max(1.0, Math.abs(a12), Math.abs(b12)) &&
2151
- Math.abs(a13 - b13) <=
2152
- EPSILON * Math.max(1.0, Math.abs(a13), Math.abs(b13)) &&
2153
- Math.abs(a14 - b14) <=
2154
- EPSILON * Math.max(1.0, Math.abs(a14), Math.abs(b14)) &&
2155
- Math.abs(a15 - b15) <= EPSILON * Math.max(1.0, Math.abs(a15), Math.abs(b15))
2156
- );
2157
- }
2158
- /**
2159
- * Alias for {@link multiply}
2160
- * @function
2161
- */
2162
-
2163
- export const mul = multiply;
2164
- /**
2165
- * Alias for {@link subtract}
2166
- * @function
2167
- */
2168
-
2169
- export const sub = subtract;