@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,828 +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
- ANGLE_ORDER,
22
- EPSILON,
23
- getMatrixArrayType,
24
- RANDOM,
25
- type ConversionOrder,
26
- type Float32ArrayLen4,
27
- type NumberArrayLen4,
28
- } from './common.js';
29
- import type { Mat3 } from './mat3.js';
30
- import type { Vec3 } from './vec3.js';
31
- import type { Quat2 } from './quat2.js';
32
-
33
- import * as mat3 from './mat3.js';
34
- import * as vec3 from './vec3.js';
35
- import * as vec4 from './vec4.js';
36
-
37
- export type Quat = Float32ArrayLen4 | NumberArrayLen4;
38
-
39
- /**
40
- * Quaternion in the format XYZW
41
- * @module quat
42
- */
43
-
44
- /**
45
- * Creates a new identity quat
46
- *
47
- * @returns {quat} a new quaternion
48
- */
49
-
50
- export function create(): Quat {
51
- const out = getMatrixArrayType(4) as Quat;
52
-
53
- if (!(out instanceof Float32Array)) {
54
- out[0] = 0;
55
- out[1] = 0;
56
- out[2] = 0;
57
- }
58
-
59
- out[3] = 1;
60
- return out;
61
- }
62
- /**
63
- * Set a quat to the identity quaternion
64
- *
65
- * @param {quat} out the receiving quaternion
66
- * @returns {quat} out
67
- */
68
-
69
- export function identity(out: Quat): Quat {
70
- out[0] = 0;
71
- out[1] = 0;
72
- out[2] = 0;
73
- out[3] = 1;
74
- return out;
75
- }
76
- /**
77
- * Sets a quat from the given angle and rotation axis,
78
- * then returns it.
79
- *
80
- * @param {quat} out the receiving quaternion
81
- * @param {Vec3} axis the axis around which to rotate
82
- * @param {Number} rad the angle in radians
83
- * @returns {quat} out
84
- **/
85
-
86
- export function setAxisAngle(out: Quat, axis: Vec3, rad: number): Quat {
87
- rad = rad * 0.5;
88
- const s = Math.sin(rad);
89
- out[0] = s * axis[0];
90
- out[1] = s * axis[1];
91
- out[2] = s * axis[2];
92
- out[3] = Math.cos(rad);
93
- return out;
94
- }
95
- /**
96
- * Gets the rotation axis and angle for a given
97
- * quaternion. If a quaternion is created with
98
- * setAxisAngle, this method will return the same
99
- * values as providied in the original parameter list
100
- * OR functionally equivalent values.
101
- * Example: The quaternion formed by axis [0, 0, 1] and
102
- * angle -90 is the same as the quaternion formed by
103
- * [0, 0, 1] and 270. This method favors the latter.
104
- * @param {Vec3} out_axis Vector receiving the axis of rotation
105
- * @param {Quat} q Quaternion to be decomposed
106
- * @return {Number} Angle, in radians, of the rotation
107
- */
108
-
109
- export function getAxisAngle(out_axis: Vec3, q: Quat): number {
110
- const rad = Math.acos(q[3]) * 2.0;
111
- const s = Math.sin(rad / 2.0);
112
-
113
- if (s > EPSILON) {
114
- out_axis[0] = q[0] / s;
115
- out_axis[1] = q[1] / s;
116
- out_axis[2] = q[2] / s;
117
- } else {
118
- // If s is zero, return any axis (no rotation - axis does not matter)
119
- out_axis[0] = 1;
120
- out_axis[1] = 0;
121
- out_axis[2] = 0;
122
- }
123
-
124
- return rad;
125
- }
126
- /**
127
- * Gets the angular distance between two unit quaternions
128
- *
129
- * @param {Quat} a Origin unit quaternion
130
- * @param {Quat} b Destination unit quaternion
131
- * @return {Number} Angle, in radians, between the two quaternions
132
- */
133
-
134
- export function getAngle(a: Quat, b: Quat): number {
135
- const dotproduct = dot(a, b);
136
- return Math.acos(2 * dotproduct * dotproduct - 1);
137
- }
138
- /**
139
- * Multiplies two quat's
140
- *
141
- * @param {quat} out the receiving quaternion
142
- * @param {Quat} a the first operand
143
- * @param {Quat} b the second operand
144
- * @returns {quat} out
145
- */
146
-
147
- export function multiply(out: Quat, a: Quat, b: Quat): Quat {
148
- const ax = a[0],
149
- ay = a[1],
150
- az = a[2],
151
- aw = a[3];
152
- const bx = b[0],
153
- by = b[1],
154
- bz = b[2],
155
- bw = b[3];
156
- out[0] = ax * bw + aw * bx + ay * bz - az * by;
157
- out[1] = ay * bw + aw * by + az * bx - ax * bz;
158
- out[2] = az * bw + aw * bz + ax * by - ay * bx;
159
- out[3] = aw * bw - ax * bx - ay * by - az * bz;
160
- return out;
161
- }
162
- /**
163
- * Rotates a quaternion by the given angle about the X axis
164
- *
165
- * @param {quat} out quat receiving operation result
166
- * @param {Quat} a quat to rotate
167
- * @param {number} rad angle (in radians) to rotate
168
- * @returns {quat} out
169
- */
170
-
171
- export function rotateX(
172
- out: Quat | Quat2,
173
- a: Quat | Quat2,
174
- rad: number,
175
- ): Quat | Quat2 {
176
- rad *= 0.5;
177
- const ax = a[0],
178
- ay = a[1],
179
- az = a[2],
180
- aw = a[3];
181
- const bx = Math.sin(rad),
182
- bw = Math.cos(rad);
183
- out[0] = ax * bw + aw * bx;
184
- out[1] = ay * bw + az * bx;
185
- out[2] = az * bw - ay * bx;
186
- out[3] = aw * bw - ax * bx;
187
- return out;
188
- }
189
- /**
190
- * Rotates a quaternion by the given angle about the Y axis
191
- *
192
- * @param {quat} out quat receiving operation result
193
- * @param {Quat} a quat to rotate
194
- * @param {number} rad angle (in radians) to rotate
195
- * @returns {quat} out
196
- */
197
-
198
- export function rotateY(
199
- out: Quat | Quat2,
200
- a: Quat | Quat2,
201
- rad: number,
202
- ): Quat | Quat2 {
203
- rad *= 0.5;
204
- const ax = a[0],
205
- ay = a[1],
206
- az = a[2],
207
- aw = a[3];
208
- const by = Math.sin(rad),
209
- bw = Math.cos(rad);
210
- out[0] = ax * bw - az * by;
211
- out[1] = ay * bw + aw * by;
212
- out[2] = az * bw + ax * by;
213
- out[3] = aw * bw - ay * by;
214
- return out;
215
- }
216
- /**
217
- * Rotates a quaternion by the given angle about the Z axis
218
- *
219
- * @param {quat} out quat receiving operation result
220
- * @param {Quat} a quat to rotate
221
- * @param {number} rad angle (in radians) to rotate
222
- * @returns {quat} out
223
- */
224
-
225
- export function rotateZ(
226
- out: Quat | Quat2,
227
- a: Quat | Quat2,
228
- rad: number,
229
- ): Quat | Quat2 {
230
- rad *= 0.5;
231
- const ax = a[0],
232
- ay = a[1],
233
- az = a[2],
234
- aw = a[3];
235
- const bz = Math.sin(rad),
236
- bw = Math.cos(rad);
237
- out[0] = ax * bw + ay * bz;
238
- out[1] = ay * bw - ax * bz;
239
- out[2] = az * bw + aw * bz;
240
- out[3] = aw * bw - az * bz;
241
- return out;
242
- }
243
- /**
244
- * Calculates the W component of a quat from the X, Y, and Z components.
245
- * Assumes that quaternion is 1 unit in length.
246
- * Any existing W component will be ignored.
247
- *
248
- * @param {quat} out the receiving quaternion
249
- * @param {Quat} a quat to calculate W component of
250
- * @returns {quat} out
251
- */
252
-
253
- export function calculateW(out: Quat, a: Quat): Quat {
254
- const x = a[0],
255
- y = a[1],
256
- z = a[2];
257
- out[0] = x;
258
- out[1] = y;
259
- out[2] = z;
260
- out[3] = Math.sqrt(Math.abs(1.0 - x * x - y * y - z * z));
261
- return out;
262
- }
263
- /**
264
- * Calculate the exponential of a unit quaternion.
265
- *
266
- * @param {quat} out the receiving quaternion
267
- * @param {Quat} a quat to calculate the exponential of
268
- * @returns {quat} out
269
- */
270
-
271
- export function exp(out: Quat, a: Quat): Quat {
272
- const x = a[0],
273
- y = a[1],
274
- z = a[2],
275
- w = a[3];
276
- const r = Math.sqrt(x * x + y * y + z * z);
277
- const et = Math.exp(w);
278
- const s = r > 0 ? (et * Math.sin(r)) / r : 0;
279
- out[0] = x * s;
280
- out[1] = y * s;
281
- out[2] = z * s;
282
- out[3] = et * Math.cos(r);
283
- return out;
284
- }
285
- /**
286
- * Calculate the natural logarithm of a unit quaternion.
287
- *
288
- * @param {quat} out the receiving quaternion
289
- * @param {Quat} a quat to calculate the exponential of
290
- * @returns {quat} out
291
- */
292
-
293
- export function ln(out: Quat, a: Quat): Quat {
294
- const x = a[0],
295
- y = a[1],
296
- z = a[2],
297
- w = a[3];
298
- const r = Math.sqrt(x * x + y * y + z * z);
299
- const t = r > 0 ? Math.atan2(r, w) / r : 0;
300
- out[0] = x * t;
301
- out[1] = y * t;
302
- out[2] = z * t;
303
- out[3] = 0.5 * Math.log(x * x + y * y + z * z + w * w);
304
- return out;
305
- }
306
- /**
307
- * Calculate the scalar power of a unit quaternion.
308
- *
309
- * @param {quat} out the receiving quaternion
310
- * @param {Quat} a quat to calculate the exponential of
311
- * @param {Number} b amount to scale the quaternion by
312
- * @returns {quat} out
313
- */
314
-
315
- export function pow(out: Quat, a: Quat, b: number): Quat {
316
- ln(out, a);
317
- scale(out, out, b);
318
- exp(out, out);
319
- return out;
320
- }
321
- /**
322
- * Performs a spherical linear interpolation between two quat
323
- *
324
- * @param {quat} out the receiving quaternion
325
- * @param {Quat} a the first operand
326
- * @param {Quat} b the second operand
327
- * @param {Number} t interpolation amount, in the range [0-1], between the two inputs
328
- * @returns {quat} out
329
- */
330
-
331
- export function slerp(out: Quat, a: Quat, b: Quat, t: number): Quat {
332
- // benchmarks:
333
- // http://jsperf.com/quaternion-slerp-implementations
334
- const ax = a[0],
335
- ay = a[1],
336
- az = a[2],
337
- aw = a[3];
338
- let bx = b[0],
339
- by = b[1],
340
- bz = b[2],
341
- bw = b[3];
342
- let omega, cosom, sinom, scale0, scale1; // calc cosine
343
-
344
- cosom = ax * bx + ay * by + az * bz + aw * bw; // adjust signs (if necessary)
345
-
346
- if (cosom < 0.0) {
347
- cosom = -cosom;
348
- bx = -bx;
349
- by = -by;
350
- bz = -bz;
351
- bw = -bw;
352
- } // calculate coefficients
353
-
354
- if (1.0 - cosom > EPSILON) {
355
- // standard case (slerp)
356
- omega = Math.acos(cosom);
357
- sinom = Math.sin(omega);
358
- scale0 = Math.sin((1.0 - t) * omega) / sinom;
359
- scale1 = Math.sin(t * omega) / sinom;
360
- } else {
361
- // "from" and "to" quaternions are very close
362
- // ... so we can do a linear interpolation
363
- scale0 = 1.0 - t;
364
- scale1 = t;
365
- } // calculate final values
366
-
367
- out[0] = scale0 * ax + scale1 * bx;
368
- out[1] = scale0 * ay + scale1 * by;
369
- out[2] = scale0 * az + scale1 * bz;
370
- out[3] = scale0 * aw + scale1 * bw;
371
- return out;
372
- }
373
- /**
374
- * Generates a random unit quaternion
375
- *
376
- * @param {quat} out the receiving quaternion
377
- * @returns {quat} out
378
- */
379
-
380
- export function random(out: Quat): Quat {
381
- // Implementation of http://planning.cs.uiuc.edu/node198.html
382
- // TODO: Calling random 3 times is probably not the fastest solution
383
- const u1 = RANDOM();
384
- const u2 = RANDOM();
385
- const u3 = RANDOM();
386
- const sqrt1MinusU1 = Math.sqrt(1 - u1);
387
- const sqrtU1 = Math.sqrt(u1);
388
- out[0] = sqrt1MinusU1 * Math.sin(2.0 * Math.PI * u2);
389
- out[1] = sqrt1MinusU1 * Math.cos(2.0 * Math.PI * u2);
390
- out[2] = sqrtU1 * Math.sin(2.0 * Math.PI * u3);
391
- out[3] = sqrtU1 * Math.cos(2.0 * Math.PI * u3);
392
- return out;
393
- }
394
- /**
395
- * Calculates the inverse of a quat
396
- *
397
- * @param {quat} out the receiving quaternion
398
- * @param {Quat} a quat to calculate inverse of
399
- * @returns {quat} out
400
- */
401
-
402
- export function invert(out: Quat, a: Quat): Quat {
403
- const a0 = a[0],
404
- a1 = a[1],
405
- a2 = a[2],
406
- a3 = a[3];
407
- const dot = a0 * a0 + a1 * a1 + a2 * a2 + a3 * a3;
408
- const invDot = dot ? 1.0 / dot : 0; // TODO: Would be faster to return [0,0,0,0] immediately if dot == 0
409
-
410
- out[0] = -a0 * invDot;
411
- out[1] = -a1 * invDot;
412
- out[2] = -a2 * invDot;
413
- out[3] = a3 * invDot;
414
- return out;
415
- }
416
- /**
417
- * Calculates the conjugate of a quat
418
- * If the quaternion is normalized, this function is faster than quat.inverse and produces the same result.
419
- *
420
- * @param {quat} out the receiving quaternion
421
- * @param {Quat} a quat to calculate conjugate of
422
- * @returns {quat} out
423
- */
424
-
425
- export function conjugate(out: Quat, a: Quat): Quat {
426
- out[0] = -a[0];
427
- out[1] = -a[1];
428
- out[2] = -a[2];
429
- out[3] = a[3];
430
- return out;
431
- }
432
- /**
433
- * Creates a quaternion from the given 3x3 rotation matrix.
434
- *
435
- * NOTE: The resultant quaternion is not normalized, so you should be sure
436
- * to renormalize the quaternion yourself where necessary.
437
- *
438
- * @param {quat} out the receiving quaternion
439
- * @param {Mat3} m rotation matrix
440
- * @returns {quat} out
441
- * @function
442
- */
443
-
444
- export function fromMat3(out: Quat, m: Mat3) {
445
- // Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes
446
- // article "Quaternion Calculus and Fast Animation".
447
- const fTrace = m[0] + m[4] + m[8];
448
- let fRoot: number;
449
-
450
- if (fTrace > 0.0) {
451
- // |w| > 1/2, may as well choose w > 1/2
452
- fRoot = Math.sqrt(fTrace + 1.0); // 2w
453
-
454
- out[3] = 0.5 * fRoot;
455
- fRoot = 0.5 / fRoot; // 1/(4w)
456
-
457
- out[0] = (m[5] - m[7]) * fRoot;
458
- out[1] = (m[6] - m[2]) * fRoot;
459
- out[2] = (m[1] - m[3]) * fRoot;
460
- } else {
461
- // |w| <= 1/2
462
- // Since we're doing math to calculate tuple indices we extract these
463
- // types so we can easily assert to them below when needed.
464
- type Mat3Index = Extract<keyof Mat3, number>;
465
- type QuatIndex = Extract<keyof Quat, number>;
466
- let i = 0;
467
- if (m[4] > m[0]) i = 1;
468
- if (m[8] > m[(i * 3 + i) as Mat3Index]) i = 2;
469
- const j = ((i + 1) % 3) as QuatIndex;
470
- const k = ((i + 2) % 3) as QuatIndex;
471
- fRoot = Math.sqrt(
472
- m[(i * 3 + i) as Mat3Index] -
473
- m[(j * 3 + j) as Mat3Index] -
474
- m[(k * 3 + k) as Mat3Index] +
475
- 1.0,
476
- );
477
- out[i as QuatIndex] = 0.5 * fRoot;
478
- fRoot = 0.5 / fRoot;
479
- out[3] =
480
- (m[(j * 3 + k) as Mat3Index] - m[(k * 3 + j) as Mat3Index]) * fRoot;
481
- out[j] =
482
- (m[(j * 3 + i) as Mat3Index] + m[(i * 3 + j) as Mat3Index]) * fRoot;
483
- out[k] =
484
- (m[(k * 3 + i) as Mat3Index] + m[(i * 3 + k) as Mat3Index]) * fRoot;
485
- }
486
-
487
- return out;
488
- }
489
- /**
490
- * Creates a quaternion from the given euler angle x, y, z using the provided intrinsic order for the conversion.
491
- *
492
- * @param {quat} out the receiving quaternion
493
- * @param {x} x Angle to rotate around X axis in degrees.
494
- * @param {y} y Angle to rotate around Y axis in degrees.
495
- * @param {z} z Angle to rotate around Z axis in degrees.
496
- * @param {'zyx'|'xyz'|'yxz'|'yzx'|'zxy'|'zyx'} order Intrinsic order for conversion, default is zyx.
497
- * @returns {quat} out
498
- * @function
499
- */
500
-
501
- export function fromEuler(
502
- out: Quat,
503
- x: number,
504
- y: number,
505
- z: number,
506
- order?: ConversionOrder,
507
- ): Quat {
508
- order = order ?? ANGLE_ORDER;
509
- const halfToRad = Math.PI / 360;
510
- x *= halfToRad;
511
- z *= halfToRad;
512
- y *= halfToRad;
513
- const sx = Math.sin(x);
514
- const cx = Math.cos(x);
515
- const sy = Math.sin(y);
516
- const cy = Math.cos(y);
517
- const sz = Math.sin(z);
518
- const cz = Math.cos(z);
519
-
520
- switch (order) {
521
- case 'xyz':
522
- out[0] = sx * cy * cz + cx * sy * sz;
523
- out[1] = cx * sy * cz - sx * cy * sz;
524
- out[2] = cx * cy * sz + sx * sy * cz;
525
- out[3] = cx * cy * cz - sx * sy * sz;
526
- break;
527
-
528
- case 'xzy':
529
- out[0] = sx * cy * cz - cx * sy * sz;
530
- out[1] = cx * sy * cz - sx * cy * sz;
531
- out[2] = cx * cy * sz + sx * sy * cz;
532
- out[3] = cx * cy * cz + sx * sy * sz;
533
- break;
534
-
535
- case 'yxz':
536
- out[0] = sx * cy * cz + cx * sy * sz;
537
- out[1] = cx * sy * cz - sx * cy * sz;
538
- out[2] = cx * cy * sz - sx * sy * cz;
539
- out[3] = cx * cy * cz + sx * sy * sz;
540
- break;
541
-
542
- case 'yzx':
543
- out[0] = sx * cy * cz + cx * sy * sz;
544
- out[1] = cx * sy * cz + sx * cy * sz;
545
- out[2] = cx * cy * sz - sx * sy * cz;
546
- out[3] = cx * cy * cz - sx * sy * sz;
547
- break;
548
-
549
- case 'zxy':
550
- out[0] = sx * cy * cz - cx * sy * sz;
551
- out[1] = cx * sy * cz + sx * cy * sz;
552
- out[2] = cx * cy * sz + sx * sy * cz;
553
- out[3] = cx * cy * cz - sx * sy * sz;
554
- break;
555
-
556
- case 'zyx':
557
- out[0] = sx * cy * cz - cx * sy * sz;
558
- out[1] = cx * sy * cz + sx * cy * sz;
559
- out[2] = cx * cy * sz - sx * sy * cz;
560
- out[3] = cx * cy * cz + sx * sy * sz;
561
- break;
562
-
563
- default:
564
- throw new Error('Unknown angle order ' + (order as string));
565
- }
566
-
567
- return out;
568
- }
569
- /**
570
- * Returns a string representation of a quaternion
571
- *
572
- * @param {Quat} a vector to represent as a string
573
- * @returns {String} string representation of the vector
574
- */
575
-
576
- export function str(a: Quat): string {
577
- return `quat(${a[0]}, ${a[1]}, ${a[2]}, ${a[3]})`;
578
- }
579
- /**
580
- * Creates a new quat initialized with values from an existing quaternion
581
- *
582
- * @param {Quat} a quaternion to clone
583
- * @returns {quat} a new quaternion
584
- * @function
585
- */
586
-
587
- export const clone = vec4.clone;
588
- /**
589
- * Creates a new quat initialized with the given values
590
- *
591
- * @param {Number} x X component
592
- * @param {Number} y Y component
593
- * @param {Number} z Z component
594
- * @param {Number} w W component
595
- * @returns {quat} a new quaternion
596
- * @function
597
- */
598
-
599
- export const fromValues = vec4.fromValues;
600
- /**
601
- * Copy the values from one quat to another
602
- *
603
- * @param {quat} out the receiving quaternion
604
- * @param {Quat} a the source quaternion
605
- * @returns {quat} out
606
- * @function
607
- */
608
-
609
- export const copy = vec4.copy;
610
- /**
611
- * Set the components of a quat to the given values
612
- *
613
- * @param {quat} out the receiving quaternion
614
- * @param {Number} x X component
615
- * @param {Number} y Y component
616
- * @param {Number} z Z component
617
- * @param {Number} w W component
618
- * @returns {quat} out
619
- * @function
620
- */
621
-
622
- export const set = vec4.set;
623
- /**
624
- * Adds two quat's
625
- *
626
- * @param {quat} out the receiving quaternion
627
- * @param {Quat} a the first operand
628
- * @param {Quat} b the second operand
629
- * @returns {quat} out
630
- * @function
631
- */
632
-
633
- export const add = vec4.add;
634
- /**
635
- * Alias for {@link quat.multiply}
636
- * @function
637
- */
638
-
639
- export const mul = multiply;
640
- /**
641
- * Scales a quat by a scalar number
642
- *
643
- * @param {quat} out the receiving vector
644
- * @param {Quat} a the vector to scale
645
- * @param {Number} b amount to scale the vector by
646
- * @returns {quat} out
647
- * @function
648
- */
649
-
650
- export const scale = vec4.scale;
651
- /**
652
- * Calculates the dot product of two quat's
653
- *
654
- * @param {Quat} a the first operand
655
- * @param {Quat} b the second operand
656
- * @returns {Number} dot product of a and b
657
- * @function
658
- */
659
-
660
- export const dot = vec4.dot;
661
- /**
662
- * Performs a linear interpolation between two quat's
663
- *
664
- * @param {quat} out the receiving quaternion
665
- * @param {Quat} a the first operand
666
- * @param {Quat} b the second operand
667
- * @param {Number} t interpolation amount, in the range [0-1], between the two inputs
668
- * @returns {quat} out
669
- * @function
670
- */
671
-
672
- export const lerp = vec4.lerp;
673
- /**
674
- * Calculates the length of a quat
675
- *
676
- * @param {Quat} a vector to calculate length of
677
- * @returns {Number} length of a
678
- */
679
-
680
- export const length = vec4.length;
681
- /**
682
- * Alias for {@link length}
683
- * @function
684
- */
685
-
686
- export const len = length;
687
- /**
688
- * Calculates the squared length of a quat
689
- *
690
- * @param {Quat} a vector to calculate squared length of
691
- * @returns {Number} squared length of a
692
- * @function
693
- */
694
-
695
- export const squaredLength = vec4.squaredLength;
696
- /**
697
- * Alias for {@link squaredLength}
698
- * @function
699
- */
700
-
701
- export const sqrLen = squaredLength;
702
- /**
703
- * Normalize a quat
704
- *
705
- * @param {quat} out the receiving quaternion
706
- * @param {Quat} a quaternion to normalize
707
- * @returns {quat} out
708
- * @function
709
- */
710
- export const normalize = vec4.normalize;
711
- /**
712
- * Returns whether or not the quaternions have exactly the same elements in the same position (when compared with ===)
713
- *
714
- * @param {Quat} a The first quaternion.
715
- * @param {Quat} b The second quaternion.
716
- * @returns {Boolean} True if the vectors are equal, false otherwise.
717
- */
718
-
719
- export const exactEquals = vec4.exactEquals;
720
- /**
721
- * Returns whether or not the quaternions point approximately to the same direction.
722
- *
723
- * Both quaternions are assumed to be unit length.
724
- *
725
- * @param {Quat} a The first unit quaternion.
726
- * @param {Quat} b The second unit quaternion.
727
- * @returns {Boolean} True if the quaternions are equal, false otherwise.
728
- */
729
-
730
- export function equals(a: Quat, b: Quat): boolean {
731
- return Math.abs(vec4.dot(a, b)) >= 1 - EPSILON;
732
- }
733
- /**
734
- * Sets a quaternion to represent the shortest rotation from one
735
- * vector to another.
736
- *
737
- * Both vectors are assumed to be unit length.
738
- *
739
- * @param {quat} out the receiving quaternion.
740
- * @param {Vec3} a the initial vector
741
- * @param {Vec3} b the destination vector
742
- * @returns {quat} out
743
- */
744
-
745
- export const rotationTo = (function () {
746
- const tmpVec3 = vec3.create();
747
- const xUnitVec3 = vec3.fromValues(1, 0, 0);
748
- const yUnitVec3 = vec3.fromValues(0, 1, 0);
749
- return function (out: Quat, a: Vec3, b: Vec3) {
750
- const dot = vec3.dot(a, b);
751
-
752
- if (dot < -0.999999) {
753
- vec3.cross(tmpVec3, xUnitVec3, a);
754
- if (vec3.len(tmpVec3) < 0.000001) vec3.cross(tmpVec3, yUnitVec3, a);
755
- vec3.normalize(tmpVec3, tmpVec3);
756
- setAxisAngle(out, tmpVec3, Math.PI);
757
- return out;
758
- } else if (dot > 0.999999) {
759
- out[0] = 0;
760
- out[1] = 0;
761
- out[2] = 0;
762
- out[3] = 1;
763
- return out;
764
- } else {
765
- vec3.cross(tmpVec3, a, b);
766
- out[0] = tmpVec3[0];
767
- out[1] = tmpVec3[1];
768
- out[2] = tmpVec3[2];
769
- out[3] = 1 + dot;
770
- return normalize(out, out);
771
- }
772
- };
773
- })();
774
- /**
775
- * Performs a spherical linear interpolation with two control points
776
- *
777
- * @param {quat} out the receiving quaternion
778
- * @param {Quat} a the first operand
779
- * @param {Quat} b the second operand
780
- * @param {Quat} c the third operand
781
- * @param {Quat} d the fourth operand
782
- * @param {Number} t interpolation amount, in the range [0-1], between the two inputs
783
- * @returns {quat} out
784
- */
785
-
786
- export const sqlerp = (function () {
787
- const temp1 = create();
788
- const temp2 = create();
789
- return function (
790
- out: Quat,
791
- a: Quat,
792
- b: Quat,
793
- c: Quat,
794
- d: Quat,
795
- t: number,
796
- ): Quat {
797
- slerp(temp1, a, d, t);
798
- slerp(temp2, b, c, t);
799
- slerp(out, temp1, temp2, 2 * t * (1 - t));
800
- return out;
801
- };
802
- })();
803
- /**
804
- * Sets the specified quaternion with values corresponding to the given
805
- * axes. Each axis is a Vec3 and is expected to be unit length and
806
- * perpendicular to all other specified axes.
807
- *
808
- * @param {Vec3} view the vector representing the viewing direction
809
- * @param {Vec3} right the vector representing the local "right" direction
810
- * @param {Vec3} up the vector representing the local "up" direction
811
- * @returns {quat} out
812
- */
813
-
814
- export const setAxes = (function () {
815
- const matr = mat3.create();
816
- return function (out: Quat, view: Vec3, right: Vec3, up: Vec3) {
817
- matr[0] = right[0];
818
- matr[3] = right[1];
819
- matr[6] = right[2];
820
- matr[1] = up[0];
821
- matr[4] = up[1];
822
- matr[7] = up[2];
823
- matr[2] = -view[0];
824
- matr[5] = -view[1];
825
- matr[8] = -view[2];
826
- return normalize(out, fromMat3(out, matr));
827
- };
828
- })();