@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,859 +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
- RANDOM,
24
- type Float32ArrayLen3,
25
- type NumberArrayLen3,
26
- } from './common.js';
27
- import type { Mat3 } from './mat3.js';
28
- import type { Mat4 } from './mat4.js';
29
- import type { Quat } from './quat.js';
30
-
31
- export type Vec3 = Float32ArrayLen3 | NumberArrayLen3;
32
- /**
33
- * Creates a new, empty Vec3
34
- *
35
- * @returns {Vec3} a new 3D vector
36
- */
37
-
38
- export function create(): Vec3 {
39
- const out = getMatrixArrayType(3) as Vec3;
40
-
41
- if (!(out instanceof Float32Array)) {
42
- out[0] = 0;
43
- out[1] = 0;
44
- out[2] = 0;
45
- }
46
-
47
- return out;
48
- }
49
- /**
50
- * Creates a new Vec3 initialized with values from an existing vector
51
- *
52
- * @param {Vec3} a vector to clone
53
- * @returns {Vec3} a new 3D vector
54
- */
55
-
56
- export function clone(a: Vec3): Vec3 {
57
- const out = getMatrixArrayType(3) as Vec3;
58
- out[0] = a[0];
59
- out[1] = a[1];
60
- out[2] = a[2];
61
- return out;
62
- }
63
- /**
64
- * Calculates the length of a Vec3
65
- *
66
- * @param {Vec3} a vector to calculate length of
67
- * @returns {Number} length of a
68
- */
69
-
70
- export function length(a: Vec3): number {
71
- const x = a[0];
72
- const y = a[1];
73
- const z = a[2];
74
- return Math.hypot(x, y, z);
75
- }
76
- /**
77
- * Creates a new Vec3 initialized with the given values
78
- *
79
- * @param {Number} x X component
80
- * @param {Number} y Y component
81
- * @param {Number} z Z component
82
- * @returns {Vec3} a new 3D vector
83
- */
84
-
85
- export function fromValues(x: number, y: number, z: number) {
86
- const out = getMatrixArrayType(3) as Vec3;
87
- out[0] = x;
88
- out[1] = y;
89
- out[2] = z;
90
- return out;
91
- }
92
-
93
- /**
94
- * Copy the values from one Vec3 to another
95
- *
96
- * @param {Vec3} out the receiving vector
97
- * @param {Vec3} a the source vector
98
- * @returns {Vec3} out
99
- */
100
-
101
- export function copy(out: Vec3, a: Vec3): Vec3 {
102
- out[0] = a[0];
103
- out[1] = a[1];
104
- out[2] = a[2];
105
- return out;
106
- }
107
- /**
108
- * Set the components of a Vec3 to the given values
109
- *
110
- * @param {Vec3} out the receiving vector
111
- * @param {Number} x X component
112
- * @param {Number} y Y component
113
- * @param {Number} z Z component
114
- * @returns {Vec3} out
115
- */
116
-
117
- export function set(out: Vec3, x: number, y: number, z: number): Vec3 {
118
- out[0] = x;
119
- out[1] = y;
120
- out[2] = z;
121
- return out;
122
- }
123
- /**
124
- * Adds two Vec3's
125
- *
126
- * @param {Vec3} out the receiving vector
127
- * @param {Vec3} a the first operand
128
- * @param {Vec3} b the second operand
129
- * @returns {Vec3} out
130
- */
131
-
132
- export function add(out: Vec3, a: Vec3, b: Vec3): Vec3 {
133
- out[0] = a[0] + b[0];
134
- out[1] = a[1] + b[1];
135
- out[2] = a[2] + b[2];
136
- return out;
137
- }
138
- /**
139
- * Subtracts vector b from vector a
140
- *
141
- * @param {Vec3} out the receiving vector
142
- * @param {Vec3} a the first operand
143
- * @param {Vec3} b the second operand
144
- * @returns {Vec3} out
145
- */
146
-
147
- export function subtract(out: Vec3, a: Vec3, b: Vec3): Vec3 {
148
- out[0] = a[0] - b[0];
149
- out[1] = a[1] - b[1];
150
- out[2] = a[2] - b[2];
151
- return out;
152
- }
153
- /**
154
- * Multiplies two Vec3's
155
- *
156
- * @param {Vec3} out the receiving vector
157
- * @param {Vec3} a the first operand
158
- * @param {Vec3} b the second operand
159
- * @returns {Vec3} out
160
- */
161
-
162
- export function multiply(out: Vec3, a: Vec3, b: Vec3): Vec3 {
163
- out[0] = a[0] * b[0];
164
- out[1] = a[1] * b[1];
165
- out[2] = a[2] * b[2];
166
- return out;
167
- }
168
- /**
169
- * Divides two Vec3's
170
- *
171
- * @param {Vec3} out the receiving vector
172
- * @param {Vec3} a the first operand
173
- * @param {Vec3} b the second operand
174
- * @returns {Vec3} out
175
- */
176
-
177
- export function divide(out: Vec3, a: Vec3, b: Vec3): Vec3 {
178
- out[0] = a[0] / b[0];
179
- out[1] = a[1] / b[1];
180
- out[2] = a[2] / b[2];
181
- return out;
182
- }
183
- /**
184
- * Math.ceil the components of a Vec3
185
- *
186
- * @param {Vec3} out the receiving vector
187
- * @param {Vec3} a vector to ceil
188
- * @returns {Vec3} out
189
- */
190
-
191
- export function ceil(out: Vec3, a: Vec3): Vec3 {
192
- out[0] = Math.ceil(a[0]);
193
- out[1] = Math.ceil(a[1]);
194
- out[2] = Math.ceil(a[2]);
195
- return out;
196
- }
197
- /**
198
- * Math.floor the components of a Vec3
199
- *
200
- * @param {Vec3} out the receiving vector
201
- * @param {Vec3} a vector to floor
202
- * @returns {Vec3} out
203
- */
204
-
205
- export function floor(out: Vec3, a: Vec3): Vec3 {
206
- out[0] = Math.floor(a[0]);
207
- out[1] = Math.floor(a[1]);
208
- out[2] = Math.floor(a[2]);
209
- return out;
210
- }
211
- /**
212
- * Returns the minimum of two Vec3's
213
- *
214
- * @param {Vec3} out the receiving vector
215
- * @param {Vec3} a the first operand
216
- * @param {Vec3} b the second operand
217
- * @returns {Vec3} out
218
- */
219
-
220
- export function min(out: Vec3, a: Vec3, b: Vec3): Vec3 {
221
- out[0] = Math.min(a[0], b[0]);
222
- out[1] = Math.min(a[1], b[1]);
223
- out[2] = Math.min(a[2], b[2]);
224
- return out;
225
- }
226
- /**
227
- * Returns the maximum of two Vec3's
228
- *
229
- * @param {Vec3} out the receiving vector
230
- * @param {Vec3} a the first operand
231
- * @param {Vec3} b the second operand
232
- * @returns {Vec3} out
233
- */
234
-
235
- export function max(out: Vec3, a: Vec3, b: Vec3): Vec3 {
236
- out[0] = Math.max(a[0], b[0]);
237
- out[1] = Math.max(a[1], b[1]);
238
- out[2] = Math.max(a[2], b[2]);
239
- return out;
240
- }
241
- /**
242
- * Math.round the components of a Vec3
243
- *
244
- * @param {Vec3} out the receiving vector
245
- * @param {Vec3} a vector to round
246
- * @returns {Vec3} out
247
- */
248
-
249
- export function round(out: Vec3, a: Vec3): Vec3 {
250
- out[0] = Math.round(a[0]);
251
- out[1] = Math.round(a[1]);
252
- out[2] = Math.round(a[2]);
253
- return out;
254
- }
255
- /**
256
- * Scales a Vec3 by a scalar number
257
- *
258
- * @param {Vec3} out the receiving vector
259
- * @param {Vec3} a the vector to scale
260
- * @param {Number} b amount to scale the vector by
261
- * @returns {Vec3} out
262
- */
263
-
264
- export function scale(out: Vec3, a: Vec3, b: number): Vec3 {
265
- out[0] = a[0] * b;
266
- out[1] = a[1] * b;
267
- out[2] = a[2] * b;
268
- return out;
269
- }
270
- /**
271
- * Adds two Vec3's after scaling the second operand by a scalar value
272
- *
273
- * @param {Vec3} out the receiving vector
274
- * @param {Vec3} a the first operand
275
- * @param {Vec3} b the second operand
276
- * @param {Number} scale the amount to scale b by before adding
277
- * @returns {Vec3} out
278
- */
279
-
280
- export function scaleAndAdd(out: Vec3, a: Vec3, b: Vec3, scale: number): Vec3 {
281
- out[0] = a[0] + b[0] * scale;
282
- out[1] = a[1] + b[1] * scale;
283
- out[2] = a[2] + b[2] * scale;
284
- return out;
285
- }
286
- /**
287
- * Calculates the euclidian distance between two Vec3's
288
- *
289
- * @param {Vec3} a the first operand
290
- * @param {Vec3} b the second operand
291
- * @returns {Number} distance between a and b
292
- */
293
-
294
- export function distance(a: Vec3, b: Vec3): number {
295
- const x = b[0] - a[0];
296
- const y = b[1] - a[1];
297
- const z = b[2] - a[2];
298
- return Math.hypot(x, y, z);
299
- }
300
- /**
301
- * Calculates the squared euclidian distance between two Vec3's
302
- *
303
- * @param {Vec3} a the first operand
304
- * @param {Vec3} b the second operand
305
- * @returns {Number} squared distance between a and b
306
- */
307
-
308
- export function squaredDistance(a: Vec3, b: Vec3): number {
309
- const x = b[0] - a[0];
310
- const y = b[1] - a[1];
311
- const z = b[2] - a[2];
312
- return x * x + y * y + z * z;
313
- }
314
- /**
315
- * Calculates the squared length of a Vec3
316
- *
317
- * @param {Vec3} a vector to calculate squared length of
318
- * @returns {Number} squared length of a
319
- */
320
-
321
- export function squaredLength(a: Vec3): number {
322
- const x = a[0];
323
- const y = a[1];
324
- const z = a[2];
325
- return x * x + y * y + z * z;
326
- }
327
- /**
328
- * Negates the components of a Vec3
329
- *
330
- * @param {Vec3} out the receiving vector
331
- * @param {Vec3} a vector to negate
332
- * @returns {Vec3} out
333
- */
334
-
335
- export function negate(out: Vec3, a: Vec3): Vec3 {
336
- out[0] = -a[0];
337
- out[1] = -a[1];
338
- out[2] = -a[2];
339
- return out;
340
- }
341
- /**
342
- * Returns the inverse of the components of a Vec3
343
- *
344
- * @param {Vec3} out the receiving vector
345
- * @param {Vec3} a vector to invert
346
- * @returns {Vec3} out
347
- */
348
-
349
- export function inverse(out: Vec3, a: Vec3): Vec3 {
350
- out[0] = 1.0 / a[0];
351
- out[1] = 1.0 / a[1];
352
- out[2] = 1.0 / a[2];
353
- return out;
354
- }
355
- /**
356
- * Normalize a Vec3
357
- *
358
- * @param {Vec3} out the receiving vector
359
- * @param {Vec3} a vector to normalize
360
- * @returns {Vec3} out
361
- */
362
-
363
- export function normalize(out: Vec3, a: Vec3): Vec3 {
364
- const x = a[0];
365
- const y = a[1];
366
- const z = a[2];
367
- let len = x * x + y * y + z * z;
368
-
369
- if (len > 0) {
370
- //TODO: evaluate use of glm_invsqrt here?
371
- len = 1 / Math.sqrt(len);
372
- }
373
-
374
- out[0] = a[0] * len;
375
- out[1] = a[1] * len;
376
- out[2] = a[2] * len;
377
- return out;
378
- }
379
- /**
380
- * Calculates the dot product of two Vec3's
381
- *
382
- * @param {Vec3} a the first operand
383
- * @param {Vec3} b the second operand
384
- * @returns {Number} dot product of a and b
385
- */
386
-
387
- export function dot(a: Vec3, b: Vec3): number {
388
- return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
389
- }
390
- /**
391
- * Computes the cross product of two Vec3's
392
- *
393
- * @param {Vec3} out the receiving vector
394
- * @param {Vec3} a the first operand
395
- * @param {Vec3} b the second operand
396
- * @returns {Vec3} out
397
- */
398
-
399
- export function cross(out: Vec3, a: Vec3, b: Vec3): Vec3 {
400
- const ax = a[0],
401
- ay = a[1],
402
- az = a[2];
403
- const bx = b[0],
404
- by = b[1],
405
- bz = b[2];
406
- out[0] = ay * bz - az * by;
407
- out[1] = az * bx - ax * bz;
408
- out[2] = ax * by - ay * bx;
409
- return out;
410
- }
411
- /**
412
- * Performs a linear interpolation between two Vec3's
413
- *
414
- * @param {Vec3} out the receiving vector
415
- * @param {Vec3} a the first operand
416
- * @param {Vec3} b the second operand
417
- * @param {Number} t interpolation amount, in the range [0-1], between the two inputs
418
- * @returns {Vec3} out
419
- */
420
-
421
- export function lerp(out: Vec3, a: Vec3, b: Vec3, t: number): Vec3 {
422
- const ax = a[0];
423
- const ay = a[1];
424
- const az = a[2];
425
- out[0] = ax + t * (b[0] - ax);
426
- out[1] = ay + t * (b[1] - ay);
427
- out[2] = az + t * (b[2] - az);
428
- return out;
429
- }
430
- /**
431
- * Performs a spherical linear interpolation between two Vec3's
432
- *
433
- * @param {Vec3} out the receiving vector
434
- * @param {Vec3} a the first operand
435
- * @param {Vec3} b the second operand
436
- * @param {Number} t interpolation amount, in the range [0-1], between the two inputs
437
- * @returns {Vec3} out
438
- */
439
-
440
- export function slerp(out: Vec3, a: Vec3, b: Vec3, t: number): Vec3 {
441
- const angle = Math.acos(Math.min(Math.max(dot(a, b), -1), 1));
442
- const sinTotal = Math.sin(angle);
443
- const ratioA = Math.sin((1 - t) * angle) / sinTotal;
444
- const ratioB = Math.sin(t * angle) / sinTotal;
445
- out[0] = ratioA * a[0] + ratioB * b[0];
446
- out[1] = ratioA * a[1] + ratioB * b[1];
447
- out[2] = ratioA * a[2] + ratioB * b[2];
448
- return out;
449
- }
450
- /**
451
- * Performs a hermite interpolation with two control points
452
- *
453
- * @param {Vec3} out the receiving vector
454
- * @param {Vec3} a the first operand
455
- * @param {Vec3} b the second operand
456
- * @param {Vec3} c the third operand
457
- * @param {Vec3} d the fourth operand
458
- * @param {Number} t interpolation amount, in the range [0-1], between the two inputs
459
- * @returns {Vec3} out
460
- */
461
-
462
- export function hermite(
463
- out: Vec3,
464
- a: Vec3,
465
- b: Vec3,
466
- c: Vec3,
467
- d: Vec3,
468
- t: number,
469
- ): Vec3 {
470
- const factorTimes2 = t * t;
471
- const factor1 = factorTimes2 * (2 * t - 3) + 1;
472
- const factor2 = factorTimes2 * (t - 2) + t;
473
- const factor3 = factorTimes2 * (t - 1);
474
- const factor4 = factorTimes2 * (3 - 2 * t);
475
- out[0] = a[0] * factor1 + b[0] * factor2 + c[0] * factor3 + d[0] * factor4;
476
- out[1] = a[1] * factor1 + b[1] * factor2 + c[1] * factor3 + d[1] * factor4;
477
- out[2] = a[2] * factor1 + b[2] * factor2 + c[2] * factor3 + d[2] * factor4;
478
- return out;
479
- }
480
- /**
481
- * Performs a bezier interpolation with two control points
482
- *
483
- * @param {Vec3} out the receiving vector
484
- * @param {Vec3} a the first operand
485
- * @param {Vec3} b the second operand
486
- * @param {Vec3} c the third operand
487
- * @param {Vec3} d the fourth operand
488
- * @param {Number} t interpolation amount, in the range [0-1], between the two inputs
489
- * @returns {Vec3} out
490
- */
491
-
492
- export function bezier(
493
- out: Vec3,
494
- a: Vec3,
495
- b: Vec3,
496
- c: Vec3,
497
- d: Vec3,
498
- t: number,
499
- ): Vec3 {
500
- const inverseFactor = 1 - t;
501
- const inverseFactorTimesTwo = inverseFactor * inverseFactor;
502
- const factorTimes2 = t * t;
503
- const factor1 = inverseFactorTimesTwo * inverseFactor;
504
- const factor2 = 3 * t * inverseFactorTimesTwo;
505
- const factor3 = 3 * factorTimes2 * inverseFactor;
506
- const factor4 = factorTimes2 * t;
507
- out[0] = a[0] * factor1 + b[0] * factor2 + c[0] * factor3 + d[0] * factor4;
508
- out[1] = a[1] * factor1 + b[1] * factor2 + c[1] * factor3 + d[1] * factor4;
509
- out[2] = a[2] * factor1 + b[2] * factor2 + c[2] * factor3 + d[2] * factor4;
510
- return out;
511
- }
512
- /**
513
- * Generates a random vector with the given scale
514
- *
515
- * @param {Vec3} out the receiving vector
516
- * @param {Number} [scale] Length of the resulting vector. If omitted, a unit vector will be returned
517
- * @returns {Vec3} out
518
- */
519
-
520
- export function random(out: Vec3, scale?: number): Vec3 {
521
- scale = scale ?? 1.0;
522
- const r = RANDOM() * 2.0 * Math.PI;
523
- const z = RANDOM() * 2.0 - 1.0;
524
- const zScale = Math.sqrt(1.0 - z * z) * scale;
525
- out[0] = Math.cos(r) * zScale;
526
- out[1] = Math.sin(r) * zScale;
527
- out[2] = z * scale;
528
- return out;
529
- }
530
- /**
531
- * Transforms the Vec3 with a mat4.
532
- * 4th vector component is implicitly '1'
533
- *
534
- * @param {Vec3} out the receiving vector
535
- * @param {Vec3} a the vector to transform
536
- * @param {Mat4} m matrix to transform with
537
- * @returns {Vec3} out
538
- */
539
-
540
- export function transformMat4(out: Vec3, a: Vec3, m: Mat4): Vec3 {
541
- const x = a[0],
542
- y = a[1],
543
- z = a[2];
544
- let w = m[3] * x + m[7] * y + m[11] * z + m[15];
545
- w = w || 1.0;
546
- out[0] = (m[0] * x + m[4] * y + m[8] * z + m[12]) / w;
547
- out[1] = (m[1] * x + m[5] * y + m[9] * z + m[13]) / w;
548
- out[2] = (m[2] * x + m[6] * y + m[10] * z + m[14]) / w;
549
- return out;
550
- }
551
- /**
552
- * Transforms the Vec3 with a mat3.
553
- *
554
- * @param {Vec3} out the receiving vector
555
- * @param {Vec3} a the vector to transform
556
- * @param {Mat3} m the 3x3 matrix to transform with
557
- * @returns {Vec3} out
558
- */
559
-
560
- export function transformMat3(out: Vec3, a: Vec3, m: Mat3): Vec3 {
561
- const x = a[0],
562
- y = a[1],
563
- z = a[2];
564
- out[0] = x * m[0] + y * m[3] + z * m[6];
565
- out[1] = x * m[1] + y * m[4] + z * m[7];
566
- out[2] = x * m[2] + y * m[5] + z * m[8];
567
- return out;
568
- }
569
- /**
570
- * Transforms the Vec3 with a quat
571
- * Can also be used for dual quaternions. (Multiply it with the real part)
572
- *
573
- * @param {Vec3} out the receiving vector
574
- * @param {Vec3} a the vector to transform
575
- * @param {Quat} q quaternion to transform with
576
- * @returns {Vec3} out
577
- */
578
-
579
- export function transformQuat(out: Vec3, a: Vec3, q: Quat): Vec3 {
580
- // benchmarks: https://jsperf.com/quaternion-transform-Vec3-implementations-fixed
581
- const qx = q[0],
582
- qy = q[1],
583
- qz = q[2],
584
- qw = q[3];
585
- const x = a[0],
586
- y = a[1],
587
- z = a[2]; // var qvec = [qx, qy, qz];
588
-
589
- let uvx = qy * z - qz * y,
590
- uvy = qz * x - qx * z,
591
- uvz = qx * y - qy * x; // var uuv = Vec3.cross([], qvec, uv);
592
-
593
- let uuvx = qy * uvz - qz * uvy,
594
- uuvy = qz * uvx - qx * uvz,
595
- uuvz = qx * uvy - qy * uvx; // Vec3.scale(uv, uv, 2 * w);
596
-
597
- const w2 = qw * 2;
598
- uvx *= w2;
599
- uvy *= w2;
600
- uvz *= w2; // Vec3.scale(uuv, uuv, 2);
601
-
602
- uuvx *= 2;
603
- uuvy *= 2;
604
- uuvz *= 2; // return Vec3.add(out, a, Vec3.add(out, uv, uuv));
605
-
606
- out[0] = x + uvx + uuvx;
607
- out[1] = y + uvy + uuvy;
608
- out[2] = z + uvz + uuvz;
609
- return out;
610
- }
611
- /**
612
- * Rotate a 3D vector around the x-axis
613
- * @param {Vec3} out The receiving Vec3
614
- * @param {Vec3} a The Vec3 point to rotate
615
- * @param {Vec3} b The origin of the rotation
616
- * @param {Number} rad The angle of rotation in radians
617
- * @returns {Vec3} out
618
- */
619
-
620
- export function rotateX(out: Vec3, a: Vec3, b: Vec3, rad: number): Vec3 {
621
- const p = [],
622
- r = []; //Translate point to the origin
623
-
624
- p[0] = a[0] - b[0];
625
- p[1] = a[1] - b[1];
626
- p[2] = a[2] - b[2]; //perform rotation
627
-
628
- r[0] = p[0];
629
- r[1] = p[1] * Math.cos(rad) - p[2] * Math.sin(rad);
630
- r[2] = p[1] * Math.sin(rad) + p[2] * Math.cos(rad); //translate to correct position
631
-
632
- out[0] = r[0] + b[0];
633
- out[1] = r[1] + b[1];
634
- out[2] = r[2] + b[2];
635
- return out;
636
- }
637
- /**
638
- * Rotate a 3D vector around the y-axis
639
- * @param {Vec3} out The receiving Vec3
640
- * @param {Vec3} a The Vec3 point to rotate
641
- * @param {Vec3} b The origin of the rotation
642
- * @param {Number} rad The angle of rotation in radians
643
- * @returns {Vec3} out
644
- */
645
-
646
- export function rotateY(out: Vec3, a: Vec3, b: Vec3, rad: number): Vec3 {
647
- const p = [],
648
- r = []; //Translate point to the origin
649
-
650
- p[0] = a[0] - b[0];
651
- p[1] = a[1] - b[1];
652
- p[2] = a[2] - b[2]; //perform rotation
653
-
654
- r[0] = p[2] * Math.sin(rad) + p[0] * Math.cos(rad);
655
- r[1] = p[1];
656
- r[2] = p[2] * Math.cos(rad) - p[0] * Math.sin(rad); //translate to correct position
657
-
658
- out[0] = r[0] + b[0];
659
- out[1] = r[1] + b[1];
660
- out[2] = r[2] + b[2];
661
- return out;
662
- }
663
- /**
664
- * Rotate a 3D vector around the z-axis
665
- * @param {Vec3} out The receiving Vec3
666
- * @param {Vec3} a The Vec3 point to rotate
667
- * @param {Vec3} b The origin of the rotation
668
- * @param {Number} rad The angle of rotation in radians
669
- * @returns {Vec3} out
670
- */
671
-
672
- export function rotateZ(out: Vec3, a: Vec3, b: Vec3, rad: number): Vec3 {
673
- const p = [],
674
- r = []; //Translate point to the origin
675
-
676
- p[0] = a[0] - b[0];
677
- p[1] = a[1] - b[1];
678
- p[2] = a[2] - b[2]; //perform rotation
679
-
680
- r[0] = p[0] * Math.cos(rad) - p[1] * Math.sin(rad);
681
- r[1] = p[0] * Math.sin(rad) + p[1] * Math.cos(rad);
682
- r[2] = p[2]; //translate to correct position
683
-
684
- out[0] = r[0] + b[0];
685
- out[1] = r[1] + b[1];
686
- out[2] = r[2] + b[2];
687
- return out;
688
- }
689
- /**
690
- * Get the angle between two 3D vectors
691
- * @param {Vec3} a The first operand
692
- * @param {Vec3} b The second operand
693
- * @returns {Number} The angle in radians
694
- */
695
-
696
- export function angle(a: Vec3, b: Vec3): number {
697
- const ax = a[0],
698
- ay = a[1],
699
- az = a[2],
700
- bx = b[0],
701
- by = b[1],
702
- bz = b[2],
703
- mag = Math.sqrt(
704
- (ax * ax + ay * ay + az * az) * (bx * bx + by * by + bz * bz),
705
- ),
706
- cosine = mag && dot(a, b) / mag;
707
- return Math.acos(Math.min(Math.max(cosine, -1), 1));
708
- }
709
- /**
710
- * Set the components of a Vec3 to zero
711
- *
712
- * @param {Vec3} out the receiving vector
713
- * @returns {Vec3} out
714
- */
715
-
716
- export function zero(out: Vec3): Vec3 {
717
- out[0] = 0.0;
718
- out[1] = 0.0;
719
- out[2] = 0.0;
720
- return out;
721
- }
722
- /**
723
- * Returns a string representation of a vector
724
- *
725
- * @param {Vec3} a vector to represent as a string
726
- * @returns {String} string representation of the vector
727
- */
728
-
729
- export function str(a: Vec3): string {
730
- return `Vec3(${a[0]}, ${a[1]}, ${a[2]})`;
731
- }
732
- /**
733
- * Returns whether or not the vectors have exactly the same elements in the same position (when compared with ===)
734
- *
735
- * @param {Vec3} a The first vector.
736
- * @param {Vec3} b The second vector.
737
- * @returns {Boolean} True if the vectors are equal, false otherwise.
738
- */
739
-
740
- export function exactEquals(a: Vec3, b: Vec3): boolean {
741
- return a[0] === b[0] && a[1] === b[1] && a[2] === b[2];
742
- }
743
- /**
744
- * Returns whether or not the vectors have approximately the same elements in the same position.
745
- *
746
- * @param {Vec3} a The first vector.
747
- * @param {Vec3} b The second vector.
748
- * @returns {Boolean} True if the vectors are equal, false otherwise.
749
- */
750
-
751
- export function equals(a: Vec3, b: Vec3): boolean {
752
- const a0 = a[0],
753
- a1 = a[1],
754
- a2 = a[2];
755
- const b0 = b[0],
756
- b1 = b[1],
757
- b2 = b[2];
758
- return (
759
- Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) &&
760
- Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) &&
761
- Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2))
762
- );
763
- }
764
- /**
765
- * Alias for {@link subtract}
766
- * @function
767
- */
768
-
769
- export const sub = subtract;
770
- /**
771
- * Alias for {@link multiply}
772
- * @function
773
- */
774
-
775
- export const mul = multiply;
776
- /**
777
- * Alias for {@link divide}
778
- * @function
779
- */
780
-
781
- export const div = divide;
782
- /**
783
- * Alias for {@link distance}
784
- * @function
785
- */
786
-
787
- export const dist = distance;
788
- /**
789
- * Alias for {@link squaredDistance}
790
- * @function
791
- */
792
-
793
- export const sqrDist = squaredDistance;
794
- /**
795
- * Alias for {@link length}
796
- * @function
797
- */
798
-
799
- export const len = length;
800
- /**
801
- * Alias for {@link squaredLength}
802
- * @function
803
- */
804
-
805
- export const sqrLen = squaredLength;
806
- /**
807
- * Perform some operation over an array of Vec3s.
808
- *
809
- * @param {Array} a the array of vectors to iterate over
810
- * @param {Number} stride Number of elements between the start of each Vec3. If 0 assumes tightly packed
811
- * @param {Number} offset Number of elements to skip at the beginning of the array
812
- * @param {Number} count Number of Vec3s to iterate over. If 0 iterates over entire array
813
- * @param {Function} fn Function to call for each vector in the array
814
- * @param {Object} [arg] additional argument to pass to fn
815
- * @returns {Array} a
816
- * @function
817
- */
818
-
819
- export const forEach = (function () {
820
- const vec = [] as Vec3[];
821
- return function (
822
- a: Vec3[],
823
- stride: number,
824
- offset: number,
825
- count: number,
826
- fn: (a: Vec3[], b: Vec3[], arg: number) => void,
827
- arg: number,
828
- ): Vec3[] {
829
- let i: number, l: number;
830
-
831
- if (!stride) {
832
- stride = 3;
833
- }
834
-
835
- if (!offset) {
836
- offset = 0;
837
- }
838
-
839
- if (count) {
840
- l = Math.min(count * stride + offset, a.length);
841
- } else {
842
- l = a.length;
843
- }
844
-
845
- for (i = offset; i < l; i += stride) {
846
- /* eslint-disable @typescript-eslint/no-non-null-assertion */
847
- vec[0] = a[i]!;
848
- vec[1] = a[i + 1]!;
849
- vec[2] = a[i + 2]!;
850
- /* eslint-enable @typescript-eslint/no-non-null-assertion */
851
- fn(vec, vec, arg);
852
- a[i] = vec[0];
853
- a[i + 1] = vec[1];
854
- a[i + 2] = vec[2];
855
- }
856
-
857
- return a;
858
- };
859
- })();