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