@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,951 +0,0 @@
1
- /*
2
- * If not stated otherwise in this file or this component's LICENSE file the
3
- * following copyright and licenses apply:
4
- *
5
- * Copyright 2023 Comcast Cable Communications Management, LLC.
6
- *
7
- * Licensed under the Apache License, Version 2.0 (the License);
8
- * you may not use this file except in compliance with the License.
9
- * You may obtain a copy of the License at
10
- *
11
- * http://www.apache.org/licenses/LICENSE-2.0
12
- *
13
- * Unless required by applicable law or agreed to in writing, software
14
- * distributed under the License is distributed on an "AS IS" BASIS,
15
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
- * See the License for the specific language governing permissions and
17
- * limitations under the License.
18
- */
19
-
20
- import {
21
- EPSILON,
22
- getMatrixArrayType,
23
- type Float32ArrayLen8,
24
- type NumberArrayLen8,
25
- } from './common.js';
26
- import type { Mat4 } from './mat4.js';
27
- import type { Vec3 } from './vec3.js';
28
- import type { Quat } from './quat.js';
29
-
30
- import * as quat from './quat.js';
31
- import * as mat4 from './mat4.js';
32
-
33
- export type Quat2 = Float32ArrayLen8 | NumberArrayLen8;
34
- /**
35
- * Dual Quaternion<br>
36
- * Format: [real, dual]<br>
37
- * Quaternion format: XYZW<br>
38
- * Make sure to have normalized dual quaternions, otherwise the functions may not work as intended.<br>
39
- * @module quat2
40
- */
41
-
42
- /**
43
- * Creates a new identity dual quat
44
- *
45
- * @returns {quat2} a new dual quaternion [real -> rotation, dual -> translation]
46
- */
47
-
48
- export function create(): Quat2 {
49
- const dq = getMatrixArrayType(8) as Quat2;
50
-
51
- if (!(dq instanceof Float32Array)) {
52
- dq[0] = 0;
53
- dq[1] = 0;
54
- dq[2] = 0;
55
- dq[4] = 0;
56
- dq[5] = 0;
57
- dq[6] = 0;
58
- dq[7] = 0;
59
- }
60
-
61
- dq[3] = 1;
62
- return dq;
63
- }
64
- /**
65
- * Creates a new quat initialized with values from an existing quaternion
66
- *
67
- * @param {Quat2} a dual quaternion to clone
68
- * @returns {quat2} new dual quaternion
69
- * @function
70
- */
71
-
72
- export function clone(a: Quat2): Quat2 {
73
- const dq = getMatrixArrayType(8) as Quat2;
74
- dq[0] = a[0];
75
- dq[1] = a[1];
76
- dq[2] = a[2];
77
- dq[3] = a[3];
78
- dq[4] = a[4];
79
- dq[5] = a[5];
80
- dq[6] = a[6];
81
- dq[7] = a[7];
82
- return dq;
83
- }
84
- /**
85
- * Creates a new dual quat initialized with the given values
86
- *
87
- * @param {Number} x1 X component
88
- * @param {Number} y1 Y component
89
- * @param {Number} z1 Z component
90
- * @param {Number} w1 W component
91
- * @param {Number} x2 X component
92
- * @param {Number} y2 Y component
93
- * @param {Number} z2 Z component
94
- * @param {Number} w2 W component
95
- * @returns {quat2} new dual quaternion
96
- * @function
97
- */
98
-
99
- export function fromValues(
100
- x1: number,
101
- y1: number,
102
- z1: number,
103
- w1: number,
104
- x2: number,
105
- y2: number,
106
- z2: number,
107
- w2: number,
108
- ): Quat2 {
109
- const dq = getMatrixArrayType(8) as Quat2;
110
- dq[0] = x1;
111
- dq[1] = y1;
112
- dq[2] = z1;
113
- dq[3] = w1;
114
- dq[4] = x2;
115
- dq[5] = y2;
116
- dq[6] = z2;
117
- dq[7] = w2;
118
- return dq;
119
- }
120
- /**
121
- * Creates a new dual quat from the given values (quat and translation)
122
- *
123
- * @param {Number} x1 X component
124
- * @param {Number} y1 Y component
125
- * @param {Number} z1 Z component
126
- * @param {Number} w1 W component
127
- * @param {Number} x2 X component (translation)
128
- * @param {Number} y2 Y component (translation)
129
- * @param {Number} z2 Z component (translation)
130
- * @returns {quat2} new dual quaternion
131
- * @function
132
- */
133
-
134
- export function fromRotationTranslationValues(
135
- x1: number,
136
- y1: number,
137
- z1: number,
138
- w1: number,
139
- x2: number,
140
- y2: number,
141
- z2: number,
142
- ): Quat2 {
143
- const dq = getMatrixArrayType(8) as Quat2;
144
- dq[0] = x1;
145
- dq[1] = y1;
146
- dq[2] = z1;
147
- dq[3] = w1;
148
- const ax = x2 * 0.5,
149
- ay = y2 * 0.5,
150
- az = z2 * 0.5;
151
- dq[4] = ax * w1 + ay * z1 - az * y1;
152
- dq[5] = ay * w1 + az * x1 - ax * z1;
153
- dq[6] = az * w1 + ax * y1 - ay * x1;
154
- dq[7] = -ax * x1 - ay * y1 - az * z1;
155
- return dq;
156
- }
157
- /**
158
- * Creates a dual quat from a quaternion and a translation
159
- *
160
- * @param {Quat2} out quaternion receiving operation result
161
- * @param {Quat} q a normalized quaternion
162
- * @param {Vec3} t translation vector
163
- * @returns {quat2} dual quaternion receiving operation result
164
- * @function
165
- */
166
-
167
- export function fromRotationTranslation(
168
- out: Quat2,
169
- q: Quat | Quat2,
170
- t: Vec3,
171
- ): Quat2 {
172
- const ax = t[0] * 0.5,
173
- ay = t[1] * 0.5,
174
- az = t[2] * 0.5,
175
- bx = q[0],
176
- by = q[1],
177
- bz = q[2],
178
- bw = q[3];
179
- out[0] = bx;
180
- out[1] = by;
181
- out[2] = bz;
182
- out[3] = bw;
183
- out[4] = ax * bw + ay * bz - az * by;
184
- out[5] = ay * bw + az * bx - ax * bz;
185
- out[6] = az * bw + ax * by - ay * bx;
186
- out[7] = -ax * bx - ay * by - az * bz;
187
- return out;
188
- }
189
- /**
190
- * Creates a dual quat from a translation
191
- *
192
- * @param {Quat2} dual quaternion receiving operation result
193
- * @param {Vec3} t translation vector
194
- * @returns {quat2} dual quaternion receiving operation result
195
- * @function
196
- */
197
-
198
- export function fromTranslation(out: Quat2, t: Vec3): Quat2 {
199
- out[0] = 0;
200
- out[1] = 0;
201
- out[2] = 0;
202
- out[3] = 1;
203
- out[4] = t[0] * 0.5;
204
- out[5] = t[1] * 0.5;
205
- out[6] = t[2] * 0.5;
206
- out[7] = 0;
207
- return out;
208
- }
209
- /**
210
- * Creates a dual quat from a quaternion
211
- *
212
- * @param {Quat2} dual quaternion receiving operation result
213
- * @param {Quat} q the quaternion
214
- * @returns {quat2} dual quaternion receiving operation result
215
- * @function
216
- */
217
-
218
- export function fromRotation(out: Quat2, q: Quat): Quat2 {
219
- out[0] = q[0];
220
- out[1] = q[1];
221
- out[2] = q[2];
222
- out[3] = q[3];
223
- out[4] = 0;
224
- out[5] = 0;
225
- out[6] = 0;
226
- out[7] = 0;
227
- return out;
228
- }
229
- /**
230
- * Creates a new dual quat from a matrix (4x4)
231
- *
232
- * @param {quat2} out the dual quaternion
233
- * @param {Mat4} a the matrix
234
- * @returns {quat2} dual quat receiving operation result
235
- * @function
236
- */
237
-
238
- export function fromMat4(out: Quat2, a: Mat4): Quat2 {
239
- //TODO Optimize this
240
- const outer = quat.create();
241
- mat4.getRotation(outer, a);
242
- const t = getMatrixArrayType(3) as Vec3;
243
- mat4.getTranslation(t, a);
244
- fromRotationTranslation(out, outer, t);
245
- return out;
246
- }
247
- /**
248
- * Copy the values from one dual quat to another
249
- *
250
- * @param {quat2} out the receiving dual quaternion
251
- * @param {Quat2} a the source dual quaternion
252
- * @returns {quat2} out
253
- * @function
254
- */
255
-
256
- export function copy(out: Quat2, a: Quat2): Quat2 {
257
- out[0] = a[0];
258
- out[1] = a[1];
259
- out[2] = a[2];
260
- out[3] = a[3];
261
- out[4] = a[4];
262
- out[5] = a[5];
263
- out[6] = a[6];
264
- out[7] = a[7];
265
- return out;
266
- }
267
- /**
268
- * Set a dual quat to the identity dual quaternion
269
- *
270
- * @param {quat2} out the receiving quaternion
271
- * @returns {quat2} out
272
- */
273
-
274
- export function identity(out: Quat2): Quat2 {
275
- out[0] = 0;
276
- out[1] = 0;
277
- out[2] = 0;
278
- out[3] = 1;
279
- out[4] = 0;
280
- out[5] = 0;
281
- out[6] = 0;
282
- out[7] = 0;
283
- return out;
284
- }
285
- /**
286
- * Set the components of a dual quat to the given values
287
- *
288
- * @param {quat2} out the receiving quaternion
289
- * @param {Number} x1 X component
290
- * @param {Number} y1 Y component
291
- * @param {Number} z1 Z component
292
- * @param {Number} w1 W component
293
- * @param {Number} x2 X component
294
- * @param {Number} y2 Y component
295
- * @param {Number} z2 Z component
296
- * @param {Number} w2 W component
297
- * @returns {quat2} out
298
- * @function
299
- */
300
-
301
- export function set(
302
- out: Quat2,
303
- x1: number,
304
- y1: number,
305
- z1: number,
306
- w1: number,
307
- x2: number,
308
- y2: number,
309
- z2: number,
310
- w2: number,
311
- ): Quat2 {
312
- out[0] = x1;
313
- out[1] = y1;
314
- out[2] = z1;
315
- out[3] = w1;
316
- out[4] = x2;
317
- out[5] = y2;
318
- out[6] = z2;
319
- out[7] = w2;
320
- return out;
321
- }
322
- /**
323
- * Gets the real part of a dual quat
324
- * @param {quat} out real part
325
- * @param {Quat2} a Dual Quaternion
326
- * @return {quat} real part
327
- */
328
-
329
- export const getReal = quat.copy;
330
- /**
331
- * Gets the dual part of a dual quat
332
- * @param {quat} out dual part
333
- * @param {Quat2} a Dual Quaternion
334
- * @return {quat} dual part
335
- */
336
-
337
- export function getDual(out: Quat, a: Quat2): Quat {
338
- out[0] = a[4];
339
- out[1] = a[5];
340
- out[2] = a[6];
341
- out[3] = a[7];
342
- return out;
343
- }
344
- /**
345
- * Set the real component of a dual quat to the given quaternion
346
- *
347
- * @param {quat2} out the receiving quaternion
348
- * @param {Quat} q a quaternion representing the real part
349
- * @returns {quat2} out
350
- * @function
351
- */
352
-
353
- export const setReal = quat.copy;
354
- /**
355
- * Set the dual component of a dual quat to the given quaternion
356
- *
357
- * @param {quat2} out the receiving quaternion
358
- * @param {Quat} q a quaternion representing the dual part
359
- * @returns {quat2} out
360
- * @function
361
- */
362
-
363
- export function setDual(out: Quat2, q: Quat): Quat2 {
364
- out[4] = q[0];
365
- out[5] = q[1];
366
- out[6] = q[2];
367
- out[7] = q[3];
368
- return out;
369
- }
370
- /**
371
- * Gets the translation of a normalized dual quat
372
- * @param {Vec3} out translation
373
- * @param {Quat2} a Dual Quaternion to be decomposed
374
- * @return {Vec3} translation
375
- */
376
-
377
- export function getTranslation(out: Vec3, a: Quat2): Vec3 {
378
- const ax = a[4],
379
- ay = a[5],
380
- az = a[6],
381
- aw = a[7],
382
- bx = -a[0],
383
- by = -a[1],
384
- bz = -a[2],
385
- bw = a[3];
386
- out[0] = (ax * bw + aw * bx + ay * bz - az * by) * 2;
387
- out[1] = (ay * bw + aw * by + az * bx - ax * bz) * 2;
388
- out[2] = (az * bw + aw * bz + ax * by - ay * bx) * 2;
389
- return out;
390
- }
391
- /**
392
- * Translates a dual quat by the given vector
393
- *
394
- * @param {quat2} out the receiving dual quaternion
395
- * @param {Quat2} a the dual quaternion to translate
396
- * @param {Vec3} v vector to translate by
397
- * @returns {quat2} out
398
- */
399
-
400
- export function translate(out: Quat2, a: Quat2, v: Vec3): Quat2 {
401
- const ax1 = a[0],
402
- ay1 = a[1],
403
- az1 = a[2],
404
- aw1 = a[3],
405
- bx1 = v[0] * 0.5,
406
- by1 = v[1] * 0.5,
407
- bz1 = v[2] * 0.5,
408
- ax2 = a[4],
409
- ay2 = a[5],
410
- az2 = a[6],
411
- aw2 = a[7];
412
- out[0] = ax1;
413
- out[1] = ay1;
414
- out[2] = az1;
415
- out[3] = aw1;
416
- out[4] = aw1 * bx1 + ay1 * bz1 - az1 * by1 + ax2;
417
- out[5] = aw1 * by1 + az1 * bx1 - ax1 * bz1 + ay2;
418
- out[6] = aw1 * bz1 + ax1 * by1 - ay1 * bx1 + az2;
419
- out[7] = -ax1 * bx1 - ay1 * by1 - az1 * bz1 + aw2;
420
- return out;
421
- }
422
- /**
423
- * Rotates a dual quat around the X axis
424
- *
425
- * @param {quat2} out the receiving dual quaternion
426
- * @param {Quat2} a the dual quaternion to rotate
427
- * @param {number} rad how far should the rotation be
428
- * @returns {quat2} out
429
- */
430
-
431
- export function rotateX(out: Quat2, a: Quat2, rad: number): Quat2 {
432
- let bx = -a[0],
433
- by = -a[1],
434
- bz = -a[2],
435
- bw = a[3];
436
- const ax = a[4],
437
- ay = a[5],
438
- az = a[6],
439
- aw = a[7],
440
- ax1 = ax * bw + aw * bx + ay * bz - az * by,
441
- ay1 = ay * bw + aw * by + az * bx - ax * bz,
442
- az1 = az * bw + aw * bz + ax * by - ay * bx,
443
- aw1 = aw * bw - ax * bx - ay * by - az * bz;
444
- quat.rotateX(out, a, rad);
445
- bx = out[0];
446
- by = out[1];
447
- bz = out[2];
448
- bw = out[3];
449
- out[4] = ax1 * bw + aw1 * bx + ay1 * bz - az1 * by;
450
- out[5] = ay1 * bw + aw1 * by + az1 * bx - ax1 * bz;
451
- out[6] = az1 * bw + aw1 * bz + ax1 * by - ay1 * bx;
452
- out[7] = aw1 * bw - ax1 * bx - ay1 * by - az1 * bz;
453
- return out;
454
- }
455
- /**
456
- * Rotates a dual quat around the Y axis
457
- *
458
- * @param {quat2} out the receiving dual quaternion
459
- * @param {Quat2} a the dual quaternion to rotate
460
- * @param {number} rad how far should the rotation be
461
- * @returns {quat2} out
462
- */
463
-
464
- export function rotateY(out: Quat2, a: Quat2, rad: number): Quat2 {
465
- let bx = -a[0],
466
- by = -a[1],
467
- bz = -a[2],
468
- bw = a[3];
469
- const ax = a[4],
470
- ay = a[5],
471
- az = a[6],
472
- aw = a[7],
473
- ax1 = ax * bw + aw * bx + ay * bz - az * by,
474
- ay1 = ay * bw + aw * by + az * bx - ax * bz,
475
- az1 = az * bw + aw * bz + ax * by - ay * bx,
476
- aw1 = aw * bw - ax * bx - ay * by - az * bz;
477
- quat.rotateY(out, a, rad);
478
- bx = out[0];
479
- by = out[1];
480
- bz = out[2];
481
- bw = out[3];
482
- out[4] = ax1 * bw + aw1 * bx + ay1 * bz - az1 * by;
483
- out[5] = ay1 * bw + aw1 * by + az1 * bx - ax1 * bz;
484
- out[6] = az1 * bw + aw1 * bz + ax1 * by - ay1 * bx;
485
- out[7] = aw1 * bw - ax1 * bx - ay1 * by - az1 * bz;
486
- return out;
487
- }
488
- /**
489
- * Rotates a dual quat around the Z axis
490
- *
491
- * @param {quat2} out the receiving dual quaternion
492
- * @param {Quat2} a the dual quaternion to rotate
493
- * @param {number} rad how far should the rotation be
494
- * @returns {quat2} out
495
- */
496
-
497
- export function rotateZ(out: Quat2, a: Quat2, rad: number): Quat2 {
498
- let bx = -a[0],
499
- by = -a[1],
500
- bz = -a[2],
501
- bw = a[3];
502
- const ax = a[4],
503
- ay = a[5],
504
- az = a[6],
505
- aw = a[7],
506
- ax1 = ax * bw + aw * bx + ay * bz - az * by,
507
- ay1 = ay * bw + aw * by + az * bx - ax * bz,
508
- az1 = az * bw + aw * bz + ax * by - ay * bx,
509
- aw1 = aw * bw - ax * bx - ay * by - az * bz;
510
- quat.rotateZ(out, a, rad);
511
- bx = out[0];
512
- by = out[1];
513
- bz = out[2];
514
- bw = out[3];
515
- out[4] = ax1 * bw + aw1 * bx + ay1 * bz - az1 * by;
516
- out[5] = ay1 * bw + aw1 * by + az1 * bx - ax1 * bz;
517
- out[6] = az1 * bw + aw1 * bz + ax1 * by - ay1 * bx;
518
- out[7] = aw1 * bw - ax1 * bx - ay1 * by - az1 * bz;
519
- return out;
520
- }
521
- /**
522
- * Rotates a dual quat by a given quaternion (a * q)
523
- *
524
- * @param {quat2} out the receiving dual quaternion
525
- * @param {Quat2} a the dual quaternion to rotate
526
- * @param {Quat} q quaternion to rotate by
527
- * @returns {quat2} out
528
- */
529
-
530
- export function rotateByQuatAppend(out: Quat2, a: Quat2, q: Quat): Quat2 {
531
- const qx = q[0],
532
- qy = q[1],
533
- qz = q[2],
534
- qw = q[3];
535
- let ax = a[0],
536
- ay = a[1],
537
- az = a[2],
538
- aw = a[3];
539
- out[0] = ax * qw + aw * qx + ay * qz - az * qy;
540
- out[1] = ay * qw + aw * qy + az * qx - ax * qz;
541
- out[2] = az * qw + aw * qz + ax * qy - ay * qx;
542
- out[3] = aw * qw - ax * qx - ay * qy - az * qz;
543
- ax = a[4];
544
- ay = a[5];
545
- az = a[6];
546
- aw = a[7];
547
- out[4] = ax * qw + aw * qx + ay * qz - az * qy;
548
- out[5] = ay * qw + aw * qy + az * qx - ax * qz;
549
- out[6] = az * qw + aw * qz + ax * qy - ay * qx;
550
- out[7] = aw * qw - ax * qx - ay * qy - az * qz;
551
- return out;
552
- }
553
- /**
554
- * Rotates a dual quat by a given quaternion (q * a)
555
- *
556
- * @param {quat2} out the receiving dual quaternion
557
- * @param {Quat} q quaternion to rotate by
558
- * @param {Quat2} a the dual quaternion to rotate
559
- * @returns {quat2} out
560
- */
561
-
562
- export function rotateByQuatPrepend(out: Quat2, q: Quat, a: Quat2): Quat2 {
563
- const qx = q[0],
564
- qy = q[1],
565
- qz = q[2],
566
- qw = q[3];
567
- let bx = a[0],
568
- by = a[1],
569
- bz = a[2],
570
- bw = a[3];
571
- out[0] = qx * bw + qw * bx + qy * bz - qz * by;
572
- out[1] = qy * bw + qw * by + qz * bx - qx * bz;
573
- out[2] = qz * bw + qw * bz + qx * by - qy * bx;
574
- out[3] = qw * bw - qx * bx - qy * by - qz * bz;
575
- bx = a[4];
576
- by = a[5];
577
- bz = a[6];
578
- bw = a[7];
579
- out[4] = qx * bw + qw * bx + qy * bz - qz * by;
580
- out[5] = qy * bw + qw * by + qz * bx - qx * bz;
581
- out[6] = qz * bw + qw * bz + qx * by - qy * bx;
582
- out[7] = qw * bw - qx * bx - qy * by - qz * bz;
583
- return out;
584
- }
585
- /**
586
- * Rotates a dual quat around a given axis. Does the normalisation automatically
587
- *
588
- * @param {quat2} out the receiving dual quaternion
589
- * @param {Quat2} a the dual quaternion to rotate
590
- * @param {Vec3} axis the axis to rotate around
591
- * @param {Number} rad how far the rotation should be
592
- * @returns {quat2} out
593
- */
594
-
595
- export function rotateAroundAxis(
596
- out: Quat2,
597
- a: Quat2,
598
- axis: Vec3,
599
- rad: number,
600
- ): Quat2 {
601
- //Special case for rad = 0
602
- if (Math.abs(rad) < EPSILON) {
603
- return copy(out, a);
604
- }
605
-
606
- const axisLength = Math.hypot(axis[0], axis[1], axis[2]);
607
- rad = rad * 0.5;
608
- const s = Math.sin(rad);
609
- const bx = (s * axis[0]) / axisLength;
610
- const by = (s * axis[1]) / axisLength;
611
- const bz = (s * axis[2]) / axisLength;
612
- const bw = Math.cos(rad);
613
- const ax1 = a[0],
614
- ay1 = a[1],
615
- az1 = a[2],
616
- aw1 = a[3];
617
- out[0] = ax1 * bw + aw1 * bx + ay1 * bz - az1 * by;
618
- out[1] = ay1 * bw + aw1 * by + az1 * bx - ax1 * bz;
619
- out[2] = az1 * bw + aw1 * bz + ax1 * by - ay1 * bx;
620
- out[3] = aw1 * bw - ax1 * bx - ay1 * by - az1 * bz;
621
- const ax = a[4],
622
- ay = a[5],
623
- az = a[6],
624
- aw = a[7];
625
- out[4] = ax * bw + aw * bx + ay * bz - az * by;
626
- out[5] = ay * bw + aw * by + az * bx - ax * bz;
627
- out[6] = az * bw + aw * bz + ax * by - ay * bx;
628
- out[7] = aw * bw - ax * bx - ay * by - az * bz;
629
- return out;
630
- }
631
- /**
632
- * Adds two dual quat's
633
- *
634
- * @param {quat2} out the receiving dual quaternion
635
- * @param {Quat2} a the first operand
636
- * @param {Quat2} b the second operand
637
- * @returns {quat2} out
638
- * @function
639
- */
640
-
641
- export function add(out: Quat2, a: Quat2, b: Quat2): Quat2 {
642
- out[0] = a[0] + b[0];
643
- out[1] = a[1] + b[1];
644
- out[2] = a[2] + b[2];
645
- out[3] = a[3] + b[3];
646
- out[4] = a[4] + b[4];
647
- out[5] = a[5] + b[5];
648
- out[6] = a[6] + b[6];
649
- out[7] = a[7] + b[7];
650
- return out;
651
- }
652
- /**
653
- * Multiplies two dual quat's
654
- *
655
- * @param {quat2} out the receiving dual quaternion
656
- * @param {Quat2} a the first operand
657
- * @param {Quat2} b the second operand
658
- * @returns {quat2} out
659
- */
660
-
661
- export function multiply(out: Quat2, a: Quat2, b: Quat2): Quat2 {
662
- const ax0 = a[0],
663
- ay0 = a[1],
664
- az0 = a[2],
665
- aw0 = a[3],
666
- bx1 = b[4],
667
- by1 = b[5],
668
- bz1 = b[6],
669
- bw1 = b[7],
670
- ax1 = a[4],
671
- ay1 = a[5],
672
- az1 = a[6],
673
- aw1 = a[7],
674
- bx0 = b[0],
675
- by0 = b[1],
676
- bz0 = b[2],
677
- bw0 = b[3];
678
- out[0] = ax0 * bw0 + aw0 * bx0 + ay0 * bz0 - az0 * by0;
679
- out[1] = ay0 * bw0 + aw0 * by0 + az0 * bx0 - ax0 * bz0;
680
- out[2] = az0 * bw0 + aw0 * bz0 + ax0 * by0 - ay0 * bx0;
681
- out[3] = aw0 * bw0 - ax0 * bx0 - ay0 * by0 - az0 * bz0;
682
- out[4] =
683
- ax0 * bw1 +
684
- aw0 * bx1 +
685
- ay0 * bz1 -
686
- az0 * by1 +
687
- ax1 * bw0 +
688
- aw1 * bx0 +
689
- ay1 * bz0 -
690
- az1 * by0;
691
- out[5] =
692
- ay0 * bw1 +
693
- aw0 * by1 +
694
- az0 * bx1 -
695
- ax0 * bz1 +
696
- ay1 * bw0 +
697
- aw1 * by0 +
698
- az1 * bx0 -
699
- ax1 * bz0;
700
- out[6] =
701
- az0 * bw1 +
702
- aw0 * bz1 +
703
- ax0 * by1 -
704
- ay0 * bx1 +
705
- az1 * bw0 +
706
- aw1 * bz0 +
707
- ax1 * by0 -
708
- ay1 * bx0;
709
- out[7] =
710
- aw0 * bw1 -
711
- ax0 * bx1 -
712
- ay0 * by1 -
713
- az0 * bz1 +
714
- aw1 * bw0 -
715
- ax1 * bx0 -
716
- ay1 * by0 -
717
- az1 * bz0;
718
- return out;
719
- }
720
- /**
721
- * Alias for {@link multiply}
722
- * @function
723
- */
724
-
725
- export const mul = multiply;
726
- /**
727
- * Scales a dual quat by a scalar number
728
- *
729
- * @param {quat2} out the receiving dual quat
730
- * @param {Quat2} a the dual quat to scale
731
- * @param {Number} b amount to scale the dual quat by
732
- * @returns {quat2} out
733
- * @function
734
- */
735
-
736
- export function scale(out: Quat2, a: Quat2, b: number): Quat2 {
737
- out[0] = a[0] * b;
738
- out[1] = a[1] * b;
739
- out[2] = a[2] * b;
740
- out[3] = a[3] * b;
741
- out[4] = a[4] * b;
742
- out[5] = a[5] * b;
743
- out[6] = a[6] * b;
744
- out[7] = a[7] * b;
745
- return out;
746
- }
747
- /**
748
- * Calculates the dot product of two dual quat's (The dot product of the real parts)
749
- *
750
- * @param {Quat2} a the first operand
751
- * @param {Quat2} b the second operand
752
- * @returns {Number} dot product of a and b
753
- * @function
754
- */
755
-
756
- export const dot = quat.dot;
757
- /**
758
- * Performs a linear interpolation between two dual quats's
759
- * NOTE: The resulting dual quaternions won't always be normalized (The error is most noticeable when t = 0.5)
760
- *
761
- * @param {quat2} out the receiving dual quat
762
- * @param {Quat2} a the first operand
763
- * @param {Quat2} b the second operand
764
- * @param {Number} t interpolation amount, in the range [0-1], between the two inputs
765
- * @returns {quat2} out
766
- */
767
-
768
- export function lerp(out: Quat2, a: Quat2, b: Quat2, t: number): Quat2 {
769
- const mt = 1 - t;
770
- if (dot(a, b) < 0) t = -t;
771
- out[0] = a[0] * mt + b[0] * t;
772
- out[1] = a[1] * mt + b[1] * t;
773
- out[2] = a[2] * mt + b[2] * t;
774
- out[3] = a[3] * mt + b[3] * t;
775
- out[4] = a[4] * mt + b[4] * t;
776
- out[5] = a[5] * mt + b[5] * t;
777
- out[6] = a[6] * mt + b[6] * t;
778
- out[7] = a[7] * mt + b[7] * t;
779
- return out;
780
- }
781
- /**
782
- * Calculates the inverse of a dual quat. If they are normalized, conjugate is cheaper
783
- *
784
- * @param {quat2} out the receiving dual quaternion
785
- * @param {Quat2} a dual quat to calculate inverse of
786
- * @returns {quat2} out
787
- */
788
-
789
- export function invert(out: Quat2, a: Quat2): Quat2 {
790
- const sqlen = squaredLength(a);
791
- out[0] = -a[0] / sqlen;
792
- out[1] = -a[1] / sqlen;
793
- out[2] = -a[2] / sqlen;
794
- out[3] = a[3] / sqlen;
795
- out[4] = -a[4] / sqlen;
796
- out[5] = -a[5] / sqlen;
797
- out[6] = -a[6] / sqlen;
798
- out[7] = a[7] / sqlen;
799
- return out;
800
- }
801
- /**
802
- * Calculates the conjugate of a dual quat
803
- * If the dual quaternion is normalized, this function is faster than quat2.inverse and produces the same result.
804
- *
805
- * @param {quat2} out the receiving quaternion
806
- * @param {Quat2} a quat to calculate conjugate of
807
- * @returns {quat2} out
808
- */
809
-
810
- export function conjugate(out: Quat2, a: Quat2): Quat2 {
811
- out[0] = -a[0];
812
- out[1] = -a[1];
813
- out[2] = -a[2];
814
- out[3] = a[3];
815
- out[4] = -a[4];
816
- out[5] = -a[5];
817
- out[6] = -a[6];
818
- out[7] = a[7];
819
- return out;
820
- }
821
- /**
822
- * Calculates the length of a dual quat
823
- *
824
- * @param {Quat2} a dual quat to calculate length of
825
- * @returns {Number} length of a
826
- * @function
827
- */
828
-
829
- export const length = quat.length;
830
- /**
831
- * Alias for {@link length}
832
- * @function
833
- */
834
-
835
- export const len = length;
836
- /**
837
- * Calculates the squared length of a dual quat
838
- *
839
- * @param {Quat2} a dual quat to calculate squared length of
840
- * @returns {Number} squared length of a
841
- * @function
842
- */
843
-
844
- export const squaredLength = quat.squaredLength;
845
- /**
846
- * Alias for {@link quat2.squaredLength}
847
- * @function
848
- */
849
-
850
- export const sqrLen = squaredLength;
851
- /**
852
- * Normalize a dual quat
853
- *
854
- * @param {quat2} out the receiving dual quaternion
855
- * @param {Quat2} a dual quaternion to normalize
856
- * @returns {quat2} out
857
- * @function
858
- */
859
-
860
- export function normalize(out: Quat2, a: Quat2): Quat2 {
861
- let magnitude = squaredLength(a);
862
-
863
- if (magnitude > 0) {
864
- magnitude = Math.sqrt(magnitude);
865
- const a0 = a[0] / magnitude;
866
- const a1 = a[1] / magnitude;
867
- const a2 = a[2] / magnitude;
868
- const a3 = a[3] / magnitude;
869
- const b0 = a[4];
870
- const b1 = a[5];
871
- const b2 = a[6];
872
- const b3 = a[7];
873
- const a_dot_b = a0 * b0 + a1 * b1 + a2 * b2 + a3 * b3;
874
- out[0] = a0;
875
- out[1] = a1;
876
- out[2] = a2;
877
- out[3] = a3;
878
- out[4] = (b0 - a0 * a_dot_b) / magnitude;
879
- out[5] = (b1 - a1 * a_dot_b) / magnitude;
880
- out[6] = (b2 - a2 * a_dot_b) / magnitude;
881
- out[7] = (b3 - a3 * a_dot_b) / magnitude;
882
- }
883
-
884
- return out;
885
- }
886
- /**
887
- * Returns a string representation of a dual quaternion
888
- *
889
- * @param {Quat2} a dual quaternion to represent as a string
890
- * @returns {String} string representation of the dual quat
891
- */
892
-
893
- export function str(a: Quat2): string {
894
- return `quat2(${a[0]}, ${a[1]}, ${a[2]}, ${a[3]}, ${a[4]}, ${a[5]}, ${a[6]}, ${a[7]})`;
895
- }
896
- /**
897
- * Returns whether or not the dual quaternions have exactly the same elements in the same position (when compared with ===)
898
- *
899
- * @param {Quat2} a the first dual quaternion.
900
- * @param {Quat2} b the second dual quaternion.
901
- * @returns {Boolean} true if the dual quaternions are equal, false otherwise.
902
- */
903
-
904
- export function exactEquals(a: Quat2, b: Quat2): boolean {
905
- return (
906
- a[0] === b[0] &&
907
- a[1] === b[1] &&
908
- a[2] === b[2] &&
909
- a[3] === b[3] &&
910
- a[4] === b[4] &&
911
- a[5] === b[5] &&
912
- a[6] === b[6] &&
913
- a[7] === b[7]
914
- );
915
- }
916
- /**
917
- * Returns whether or not the dual quaternions have approximately the same elements in the same position.
918
- *
919
- * @param {Quat2} a the first dual quat.
920
- * @param {Quat2} b the second dual quat.
921
- * @returns {Boolean} true if the dual quats are equal, false otherwise.
922
- */
923
-
924
- export function equals(a: Quat2, b: Quat2): boolean {
925
- const a0 = a[0],
926
- a1 = a[1],
927
- a2 = a[2],
928
- a3 = a[3],
929
- a4 = a[4],
930
- a5 = a[5],
931
- a6 = a[6],
932
- a7 = a[7];
933
- const b0 = b[0],
934
- b1 = b[1],
935
- b2 = b[2],
936
- b3 = b[3],
937
- b4 = b[4],
938
- b5 = b[5],
939
- b6 = b[6],
940
- b7 = b[7];
941
- return (
942
- Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) &&
943
- Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) &&
944
- Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) &&
945
- Math.abs(a3 - b3) <= EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)) &&
946
- Math.abs(a4 - b4) <= EPSILON * Math.max(1.0, Math.abs(a4), Math.abs(b4)) &&
947
- Math.abs(a5 - b5) <= EPSILON * Math.max(1.0, Math.abs(a5), Math.abs(b5)) &&
948
- Math.abs(a6 - b6) <= EPSILON * Math.max(1.0, Math.abs(a6), Math.abs(b6)) &&
949
- Math.abs(a7 - b7) <= EPSILON * Math.max(1.0, Math.abs(a7), Math.abs(b7))
950
- );
951
- }