@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,708 +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 Float32ArrayLen4,
25
- type NumberArrayLen4,
26
- } from './common.js';
27
- import type { Mat4 } from './mat4.js';
28
- import type { Quat } from './quat.js';
29
- import type { Quat2 } from './quat2.js';
30
-
31
- export type Vec4 = Float32ArrayLen4 | NumberArrayLen4;
32
- /**
33
- * Creates a new, empty Vec4
34
- *
35
- * @returns {Vec4} a new 4D vector
36
- */
37
-
38
- export function create(): Vec4 {
39
- const out = getMatrixArrayType(4) as Vec4;
40
-
41
- if (!(out instanceof Float32Array)) {
42
- out[0] = 0;
43
- out[1] = 0;
44
- out[2] = 0;
45
- out[3] = 0;
46
- }
47
-
48
- return out;
49
- }
50
- /**
51
- * Creates a new Vec4 initialized with values from an existing vector
52
- *
53
- * @param {Vec4} a vector to clone
54
- * @returns {Vec4} a new 4D vector
55
- */
56
-
57
- export function clone(a: Vec4): Vec4 {
58
- const out = getMatrixArrayType(4) as Vec4;
59
- out[0] = a[0];
60
- out[1] = a[1];
61
- out[2] = a[2];
62
- out[3] = a[3];
63
- return out;
64
- }
65
- /**
66
- * Creates a new Vec4 initialized with the given values
67
- *
68
- * @param {Number} x X component
69
- * @param {Number} y Y component
70
- * @param {Number} z Z component
71
- * @param {Number} w W component
72
- * @returns {Vec4} a new 4D vector
73
- */
74
-
75
- export function fromValues(x: number, y: number, z: number, w: number): Vec4 {
76
- const out = getMatrixArrayType(4) as Vec4;
77
- out[0] = x;
78
- out[1] = y;
79
- out[2] = z;
80
- out[3] = w;
81
- return out;
82
- }
83
- /**
84
- * Copy the values from one Vec4 to another
85
- *
86
- * @param {Vec4} out the receiving vector
87
- * @param {Vec4} a the source vector
88
- * @returns {Vec4} out
89
- */
90
-
91
- export function copy(out: Vec4, a: Vec4): Vec4 {
92
- out[0] = a[0];
93
- out[1] = a[1];
94
- out[2] = a[2];
95
- out[3] = a[3];
96
- return out;
97
- }
98
- /**
99
- * Set the components of a Vec4 to the given values
100
- *
101
- * @param {Vec4} out the receiving vector
102
- * @param {Number} x X component
103
- * @param {Number} y Y component
104
- * @param {Number} z Z component
105
- * @param {Number} w W component
106
- * @returns {Vec4} out
107
- */
108
-
109
- export function set(
110
- out: Vec4,
111
- x: number,
112
- y: number,
113
- z: number,
114
- w: number,
115
- ): Vec4 {
116
- out[0] = x;
117
- out[1] = y;
118
- out[2] = z;
119
- out[3] = w;
120
- return out;
121
- }
122
- /**
123
- * Adds two Vec4's
124
- *
125
- * @param {Vec4} out the receiving vector
126
- * @param {Vec4} a the first operand
127
- * @param {Vec4} b the second operand
128
- * @returns {Vec4} out
129
- */
130
-
131
- export function add(out: Vec4, a: Vec4, b: Vec4): Vec4 {
132
- out[0] = a[0] + b[0];
133
- out[1] = a[1] + b[1];
134
- out[2] = a[2] + b[2];
135
- out[3] = a[3] + b[3];
136
- return out;
137
- }
138
- /**
139
- * Subtracts vector b from vector a
140
- *
141
- * @param {Vec4} out the receiving vector
142
- * @param {Vec4} a the first operand
143
- * @param {Vec4} b the second operand
144
- * @returns {Vec4} out
145
- */
146
-
147
- export function subtract(out: Vec4, a: Vec4, b: Vec4): Vec4 {
148
- out[0] = a[0] - b[0];
149
- out[1] = a[1] - b[1];
150
- out[2] = a[2] - b[2];
151
- out[3] = a[3] - b[3];
152
- return out;
153
- }
154
- /**
155
- * Multiplies two Vec4's
156
- *
157
- * @param {Vec4} out the receiving vector
158
- * @param {Vec4} a the first operand
159
- * @param {Vec4} b the second operand
160
- * @returns {Vec4} out
161
- */
162
-
163
- export function multiply(out: Vec4, a: Vec4, b: Vec4): Vec4 {
164
- out[0] = a[0] * b[0];
165
- out[1] = a[1] * b[1];
166
- out[2] = a[2] * b[2];
167
- out[3] = a[3] * b[3];
168
- return out;
169
- }
170
- /**
171
- * Divides two Vec4's
172
- *
173
- * @param {Vec4} out the receiving vector
174
- * @param {Vec4} a the first operand
175
- * @param {Vec4} b the second operand
176
- * @returns {Vec4} out
177
- */
178
-
179
- export function divide(out: Vec4, a: Vec4, b: Vec4): Vec4 {
180
- out[0] = a[0] / b[0];
181
- out[1] = a[1] / b[1];
182
- out[2] = a[2] / b[2];
183
- out[3] = a[3] / b[3];
184
- return out;
185
- }
186
- /**
187
- * Math.ceil the components of a Vec4
188
- *
189
- * @param {Vec4} out the receiving vector
190
- * @param {Vec4} a vector to ceil
191
- * @returns {Vec4} out
192
- */
193
-
194
- export function ceil(out: Vec4, a: Vec4): Vec4 {
195
- out[0] = Math.ceil(a[0]);
196
- out[1] = Math.ceil(a[1]);
197
- out[2] = Math.ceil(a[2]);
198
- out[3] = Math.ceil(a[3]);
199
- return out;
200
- }
201
- /**
202
- * Math.floor the components of a Vec4
203
- *
204
- * @param {Vec4} out the receiving vector
205
- * @param {Vec4} a vector to floor
206
- * @returns {Vec4} out
207
- */
208
-
209
- export function floor(out: Vec4, a: Vec4): Vec4 {
210
- out[0] = Math.floor(a[0]);
211
- out[1] = Math.floor(a[1]);
212
- out[2] = Math.floor(a[2]);
213
- out[3] = Math.floor(a[3]);
214
- return out;
215
- }
216
- /**
217
- * Returns the minimum of two Vec4's
218
- *
219
- * @param {Vec4} out the receiving vector
220
- * @param {Vec4} a the first operand
221
- * @param {Vec4} b the second operand
222
- * @returns {Vec4} out
223
- */
224
-
225
- export function min(out: Vec4, a: Vec4, b: Vec4): Vec4 {
226
- out[0] = Math.min(a[0], b[0]);
227
- out[1] = Math.min(a[1], b[1]);
228
- out[2] = Math.min(a[2], b[2]);
229
- out[3] = Math.min(a[3], b[3]);
230
- return out;
231
- }
232
- /**
233
- * Returns the maximum of two Vec4's
234
- *
235
- * @param {Vec4} out the receiving vector
236
- * @param {Vec4} a the first operand
237
- * @param {Vec4} b the second operand
238
- * @returns {Vec4} out
239
- */
240
-
241
- export function max(out: Vec4, a: Vec4, b: Vec4): Vec4 {
242
- out[0] = Math.max(a[0], b[0]);
243
- out[1] = Math.max(a[1], b[1]);
244
- out[2] = Math.max(a[2], b[2]);
245
- out[3] = Math.max(a[3], b[3]);
246
- return out;
247
- }
248
- /**
249
- * Math.round the components of a Vec4
250
- *
251
- * @param {Vec4} out the receiving vector
252
- * @param {Vec4} a vector to round
253
- * @returns {Vec4} out
254
- */
255
-
256
- export function round(out: Vec4, a: Vec4): Vec4 {
257
- out[0] = Math.round(a[0]);
258
- out[1] = Math.round(a[1]);
259
- out[2] = Math.round(a[2]);
260
- out[3] = Math.round(a[3]);
261
- return out;
262
- }
263
- /**
264
- * Scales a Vec4 by a scalar number
265
- *
266
- * @param {Vec4} out the receiving vector
267
- * @param {Vec4} a the vector to scale
268
- * @param {Number} b amount to scale the vector by
269
- * @returns {Vec4} out
270
- */
271
-
272
- export function scale(out: Vec4, a: Vec4, b: number): Vec4 {
273
- out[0] = a[0] * b;
274
- out[1] = a[1] * b;
275
- out[2] = a[2] * b;
276
- out[3] = a[3] * b;
277
- return out;
278
- }
279
- /**
280
- * Adds two Vec4's after scaling the second operand by a scalar value
281
- *
282
- * @param {Vec4} out the receiving vector
283
- * @param {Vec4} a the first operand
284
- * @param {Vec4} b the second operand
285
- * @param {Number} scale the amount to scale b by before adding
286
- * @returns {Vec4} out
287
- */
288
-
289
- export function scaleAndAdd(out: Vec4, a: Vec4, b: Vec4, scale: number): Vec4 {
290
- out[0] = a[0] + b[0] * scale;
291
- out[1] = a[1] + b[1] * scale;
292
- out[2] = a[2] + b[2] * scale;
293
- out[3] = a[3] + b[3] * scale;
294
- return out;
295
- }
296
- /**
297
- * Calculates the euclidian distance between two Vec4's
298
- *
299
- * @param {Vec4} a the first operand
300
- * @param {Vec4} b the second operand
301
- * @returns {Number} distance between a and b
302
- */
303
-
304
- export function distance(a: Vec4, b: Vec4): number {
305
- const x = b[0] - a[0];
306
- const y = b[1] - a[1];
307
- const z = b[2] - a[2];
308
- const w = b[3] - a[3];
309
- return Math.hypot(x, y, z, w);
310
- }
311
- /**
312
- * Calculates the squared euclidian distance between two Vec4's
313
- *
314
- * @param {Vec4} a the first operand
315
- * @param {Vec4} b the second operand
316
- * @returns {Number} squared distance between a and b
317
- */
318
-
319
- export function squaredDistance(a: Vec4, b: Vec4): number {
320
- const x = b[0] - a[0];
321
- const y = b[1] - a[1];
322
- const z = b[2] - a[2];
323
- const w = b[3] - a[3];
324
- return x * x + y * y + z * z + w * w;
325
- }
326
- /**
327
- * Calculates the length of a Vec4
328
- *
329
- * @param {Vec4} a vector to calculate length of
330
- * @returns {Number} length of a
331
- */
332
-
333
- export function length(a: Vec4): number {
334
- const x = a[0];
335
- const y = a[1];
336
- const z = a[2];
337
- const w = a[3];
338
- return Math.hypot(x, y, z, w);
339
- }
340
- /**
341
- * Calculates the squared length of a Vec4
342
- *
343
- * @param {Vec4} a vector to calculate squared length of
344
- * @returns {Number} squared length of a
345
- */
346
-
347
- export function squaredLength(a: Vec4 | Quat2): number {
348
- const x = a[0];
349
- const y = a[1];
350
- const z = a[2];
351
- const w = a[3];
352
- return x * x + y * y + z * z + w * w;
353
- }
354
- /**
355
- * Negates the components of a Vec4
356
- *
357
- * @param {Vec4} out the receiving vector
358
- * @param {Vec4} a vector to negate
359
- * @returns {Vec4} out
360
- */
361
-
362
- export function negate(out: Vec4, a: Vec4): Vec4 {
363
- out[0] = -a[0];
364
- out[1] = -a[1];
365
- out[2] = -a[2];
366
- out[3] = -a[3];
367
- return out;
368
- }
369
- /**
370
- * Returns the inverse of the components of a Vec4
371
- *
372
- * @param {Vec4} out the receiving vector
373
- * @param {Vec4} a vector to invert
374
- * @returns {Vec4} out
375
- */
376
-
377
- export function inverse(out: Vec4, a: Vec4): Vec4 {
378
- out[0] = 1.0 / a[0];
379
- out[1] = 1.0 / a[1];
380
- out[2] = 1.0 / a[2];
381
- out[3] = 1.0 / a[3];
382
- return out;
383
- }
384
- /**
385
- * Normalize a Vec4
386
- *
387
- * @param {Vec4} out the receiving vector
388
- * @param {Vec4} a vector to normalize
389
- * @returns {Vec4} out
390
- */
391
-
392
- export function normalize(out: Vec4, a: Vec4): Vec4 {
393
- const x = a[0];
394
- const y = a[1];
395
- const z = a[2];
396
- const w = a[3];
397
- let len = x * x + y * y + z * z + w * w;
398
-
399
- if (len > 0) {
400
- len = 1 / Math.sqrt(len);
401
- }
402
-
403
- out[0] = x * len;
404
- out[1] = y * len;
405
- out[2] = z * len;
406
- out[3] = w * len;
407
- return out;
408
- }
409
- /**
410
- * Calculates the dot product of two Vec4's
411
- *
412
- * @param {Vec4} a the first operand
413
- * @param {Vec4} b the second operand
414
- * @returns {Number} dot product of a and b
415
- */
416
-
417
- export function dot(a: Vec4 | Quat2, b: Vec4 | Quat2): number {
418
- return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
419
- }
420
- /**
421
- * Returns the cross-product of three vectors in a 4-dimensional space
422
- *
423
- * @param {Vec4} out the receiving vector
424
- * @param {Vec4} u the first vector
425
- * @param {Vec4} v the second vector
426
- * @param {Vec4} w the third vector
427
- * @returns {Vec4} result
428
- */
429
-
430
- export function cross(out: Vec4, u: Vec4, v: Vec4, w: Vec4): Vec4 {
431
- const A = v[0] * w[1] - v[1] * w[0],
432
- B = v[0] * w[2] - v[2] * w[0],
433
- C = v[0] * w[3] - v[3] * w[0],
434
- D = v[1] * w[2] - v[2] * w[1],
435
- E = v[1] * w[3] - v[3] * w[1],
436
- F = v[2] * w[3] - v[3] * w[2];
437
- const G = u[0];
438
- const H = u[1];
439
- const I = u[2];
440
- const J = u[3];
441
- out[0] = H * F - I * E + J * D;
442
- out[1] = -(G * F) + I * C - J * B;
443
- out[2] = G * E - H * C + J * A;
444
- out[3] = -(G * D) + H * B - I * A;
445
- return out;
446
- }
447
- /**
448
- * Performs a linear interpolation between two Vec4's
449
- *
450
- * @param {Vec4} out the receiving vector
451
- * @param {Vec4} a the first operand
452
- * @param {Vec4} b the second operand
453
- * @param {Number} t interpolation amount, in the range [0-1], between the two inputs
454
- * @returns {Vec4} out
455
- */
456
-
457
- export function lerp(out: Vec4, a: Vec4, b: Vec4, t: number): Vec4 {
458
- const ax = a[0];
459
- const ay = a[1];
460
- const az = a[2];
461
- const aw = a[3];
462
- out[0] = ax + t * (b[0] - ax);
463
- out[1] = ay + t * (b[1] - ay);
464
- out[2] = az + t * (b[2] - az);
465
- out[3] = aw + t * (b[3] - aw);
466
- return out;
467
- }
468
- /**
469
- * Generates a random vector with the given scale
470
- *
471
- * @param {Vec4} out the receiving vector
472
- * @param {Number} [scale] Length of the resulting vector. If omitted, a unit vector will be returned
473
- * @returns {Vec4} out
474
- */
475
-
476
- export function random(out: Vec4, scale?: number): Vec4 {
477
- scale = scale ?? 1.0; // Marsaglia, George. Choosing a Point from the Surface of a
478
- // Sphere. Ann. Math. Statist. 43 (1972), no. 2, 645--646.
479
- // http://projecteuclid.org/euclid.aoms/1177692644;
480
-
481
- let v1, v2, v3, v4;
482
- let s1, s2;
483
-
484
- do {
485
- v1 = RANDOM() * 2 - 1;
486
- v2 = RANDOM() * 2 - 1;
487
- s1 = v1 * v1 + v2 * v2;
488
- } while (s1 >= 1);
489
-
490
- do {
491
- v3 = RANDOM() * 2 - 1;
492
- v4 = RANDOM() * 2 - 1;
493
- s2 = v3 * v3 + v4 * v4;
494
- } while (s2 >= 1);
495
-
496
- const d = Math.sqrt((1 - s1) / s2);
497
- out[0] = scale * v1;
498
- out[1] = scale * v2;
499
- out[2] = scale * v3 * d;
500
- out[3] = scale * v4 * d;
501
- return out;
502
- }
503
- /**
504
- * Transforms the Vec4 with a Mat4.
505
- *
506
- * @param {Vec4} out the receiving vector
507
- * @param {Vec4} a the vector to transform
508
- * @param {ReadonlyMat4} m matrix to transform with
509
- * @returns {Vec4} out
510
- */
511
-
512
- export function transformMat4(out: Vec4, a: Vec4, m: Mat4): Vec4 {
513
- const x = a[0],
514
- y = a[1],
515
- z = a[2],
516
- w = a[3];
517
- out[0] = m[0] * x + m[4] * y + m[8] * z + m[12] * w;
518
- out[1] = m[1] * x + m[5] * y + m[9] * z + m[13] * w;
519
- out[2] = m[2] * x + m[6] * y + m[10] * z + m[14] * w;
520
- out[3] = m[3] * x + m[7] * y + m[11] * z + m[15] * w;
521
- return out;
522
- }
523
- /**
524
- * Transforms the Vec4 with a quat
525
- *
526
- * @param {Vec4} out the receiving vector
527
- * @param {Vec4} a the vector to transform
528
- * @param {Quat} q quaternion to transform with
529
- * @returns {Vec4} out
530
- */
531
-
532
- export function transformQuat(out: Vec4, a: Vec4, q: Quat) {
533
- const x = a[0],
534
- y = a[1],
535
- z = a[2];
536
- const qx = q[0],
537
- qy = q[1],
538
- qz = q[2],
539
- qw = q[3]; // calculate quat * vec
540
-
541
- const ix = qw * x + qy * z - qz * y;
542
- const iy = qw * y + qz * x - qx * z;
543
- const iz = qw * z + qx * y - qy * x;
544
- const iw = -qx * x - qy * y - qz * z; // calculate result * inverse quat
545
-
546
- out[0] = ix * qw + iw * -qx + iy * -qz - iz * -qy;
547
- out[1] = iy * qw + iw * -qy + iz * -qx - ix * -qz;
548
- out[2] = iz * qw + iw * -qz + ix * -qy - iy * -qx;
549
- out[3] = a[3];
550
- return out;
551
- }
552
- /**
553
- * Set the components of a Vec4 to zero
554
- *
555
- * @param {Vec4} out the receiving vector
556
- * @returns {Vec4} out
557
- */
558
-
559
- export function zero(out: Vec4) {
560
- out[0] = 0.0;
561
- out[1] = 0.0;
562
- out[2] = 0.0;
563
- out[3] = 0.0;
564
- return out;
565
- }
566
- /**
567
- * Returns a string representation of a vector
568
- *
569
- * @param {Vec4} a vector to represent as a string
570
- * @returns {String} string representation of the vector
571
- */
572
-
573
- export function str(a: Vec4): string {
574
- return `Vec4(${a[0]}, ${a[1]}, ${a[2]}, ${a[3]})`;
575
- }
576
- /**
577
- * Returns whether or not the vectors have exactly the same elements in the same position (when compared with ===)
578
- *
579
- * @param {Vec4} a The first vector.
580
- * @param {Vec4} b The second vector.
581
- * @returns {Boolean} True if the vectors are equal, false otherwise.
582
- */
583
-
584
- export function exactEquals(a: Vec4, b: Vec4): boolean {
585
- return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3];
586
- }
587
- /**
588
- * Returns whether or not the vectors have approximately the same elements in the same position.
589
- *
590
- * @param {Vec4} a The first vector.
591
- * @param {Vec4} b The second vector.
592
- * @returns {Boolean} True if the vectors are equal, false otherwise.
593
- */
594
-
595
- export function equals(a: Vec4, b: Vec4): boolean {
596
- const a0 = a[0],
597
- a1 = a[1],
598
- a2 = a[2],
599
- a3 = a[3];
600
- const b0 = b[0],
601
- b1 = b[1],
602
- b2 = b[2],
603
- b3 = b[3];
604
- return (
605
- Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) &&
606
- Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) &&
607
- Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) &&
608
- Math.abs(a3 - b3) <= EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3))
609
- );
610
- }
611
- /**
612
- * Alias for {@link Vec4.subtract}
613
- * @function
614
- */
615
-
616
- export const sub = subtract;
617
- /**
618
- * Alias for {@link Vec4.multiply}
619
- * @function
620
- */
621
-
622
- export const mul = multiply;
623
- /**
624
- * Alias for {@link Vec4.divide}
625
- * @function
626
- */
627
-
628
- export const div = divide;
629
- /**
630
- * Alias for {@link Vec4.distance}
631
- * @function
632
- */
633
-
634
- export const dist = distance;
635
- /**
636
- * Alias for {@link Vec4.squaredDistance}
637
- * @function
638
- */
639
-
640
- export const sqrDist = squaredDistance;
641
- /**
642
- * Alias for {@link Vec4.length}
643
- * @function
644
- */
645
-
646
- export const len = length;
647
- /**
648
- * Alias for {@link Vec4.squaredLength}
649
- * @function
650
- */
651
-
652
- export const sqrLen = squaredLength;
653
- /**
654
- * Perform some operation over an array of Vec4s.
655
- *
656
- * @param {Array} a the array of vectors to iterate over
657
- * @param {Number} stride Number of elements between the start of each Vec4. If 0 assumes tightly packed
658
- * @param {Number} offset Number of elements to skip at the beginning of the array
659
- * @param {Number} count Number of Vec4s to iterate over. If 0 iterates over entire array
660
- * @param {Function} fn Function to call for each vector in the array
661
- * @param {Object} [arg] additional argument to pass to fn
662
- * @returns {Array} a
663
- * @function
664
- */
665
-
666
- export const forEach = (function () {
667
- const vec = [] as Vec4[];
668
- return function (
669
- a: Vec4[],
670
- stride: number,
671
- offset: number,
672
- count: number,
673
- fn: (a: Vec4[], b: Vec4[], arg: object) => void,
674
- arg: object,
675
- ): Vec4[] {
676
- let i: number, l: number;
677
-
678
- if (!stride) {
679
- stride = 4;
680
- }
681
-
682
- if (!offset) {
683
- offset = 0;
684
- }
685
-
686
- if (count) {
687
- l = Math.min(count * stride + offset, a.length);
688
- } else {
689
- l = a.length;
690
- }
691
-
692
- for (i = offset; i < l; i += stride) {
693
- /* eslint-disable @typescript-eslint/no-non-null-assertion */
694
- vec[0] = a[i]!;
695
- vec[1] = a[i + 1]!;
696
- vec[2] = a[i + 2]!;
697
- vec[3] = a[i + 3]!;
698
- /* eslint-enable @typescript-eslint/no-non-null-assertion */
699
- fn(vec, vec, arg);
700
- a[i] = vec[0];
701
- a[i + 1] = vec[1];
702
- a[i + 2] = vec[2];
703
- a[i + 3] = vec[3];
704
- }
705
-
706
- return a;
707
- };
708
- })();