@carbonenginejs/runtime-utils 0.1.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.
- package/LICENSE +21 -0
- package/NOTICE +23 -0
- package/README.md +56 -0
- package/THIRD-PARTY-NOTICES.md +38 -0
- package/docs/README.md +81 -0
- package/docs/architecture.md +101 -0
- package/docs/concepts/foundation-consolidation.md +86 -0
- package/docs/const-kb.md +92 -0
- package/docs/core-types/DECORATOR-TODOS.md +25 -0
- package/docs/core-types/README.md +203 -0
- package/docs/reference/api.md +70 -0
- package/docs/reference/classes/README.md +115 -0
- package/package.json +132 -0
- package/src/arrays.js +5 -0
- package/src/audio/audioFormats.js +34 -0
- package/src/audio/index.js +1 -0
- package/src/box3.js +1385 -0
- package/src/bytes.js +56 -0
- package/src/compression.js +56 -0
- package/src/constants/index.js +6 -0
- package/src/constants.js +15 -0
- package/src/curve.js +419 -0
- package/src/d3d/dxgiFormats.js +46 -0
- package/src/d3d/index.js +2 -0
- package/src/d3d/primitiveTopology.js +11 -0
- package/src/document/CjsCarbonDocument.js +212 -0
- package/src/document/CjsClassRegistry.js +373 -0
- package/src/document/CjsDocumentDehydrator.js +142 -0
- package/src/document/CjsDocumentHydrator.js +156 -0
- package/src/document/CjsStructRegistry.js +348 -0
- package/src/document/hydrationAdapter.js +129 -0
- package/src/document/index.js +6 -0
- package/src/geometry/box.js +137 -0
- package/src/geometry/cylinder.js +244 -0
- package/src/geometry/helpers/LICENSE +15 -0
- package/src/geometry/helpers/earcut.js +766 -0
- package/src/geometry/helpers/misc.js +103 -0
- package/src/geometry/index.js +8 -0
- package/src/geometry/json.js +165 -0
- package/src/geometry/lathe.js +172 -0
- package/src/geometry/octahedron.js +0 -0
- package/src/geometry/plane.js +81 -0
- package/src/geometry/shape.js +95 -0
- package/src/geometry/sphere.js +123 -0
- package/src/geometry/torus.js +96 -0
- package/src/graphics/colorSpaces.js +22 -0
- package/src/graphics/index.js +4 -0
- package/src/graphics/pixelFormats.js +158 -0
- package/src/graphics/textureDimensions.js +22 -0
- package/src/graphics/trinityEnums.js +87 -0
- package/src/index.js +58 -0
- package/src/is.js +524 -0
- package/src/json.js +23 -0
- package/src/lifecycle/CjsLifecycleState.js +77 -0
- package/src/lifecycle/index.js +1 -0
- package/src/lne3.js +497 -0
- package/src/lookup.js +48 -0
- package/src/mat3.js +131 -0
- package/src/mat4.js +699 -0
- package/src/math/index.js +25 -0
- package/src/math/scalar.js +63 -0
- package/src/media/index.js +1 -0
- package/src/media/mediaTypes.js +50 -0
- package/src/mesh.js +394 -0
- package/src/model/CjsEventEmitter.js +333 -0
- package/src/model/CjsModel.js +1544 -0
- package/src/model/CjsModelState.js +72 -0
- package/src/model/index.js +4 -0
- package/src/model/sourceRecordUtils.js +54 -0
- package/src/noise.js +310 -0
- package/src/num.js +827 -0
- package/src/path.js +21 -0
- package/src/pln.js +762 -0
- package/src/pool.js +160 -0
- package/src/quat.js +144 -0
- package/src/ray3.js +1085 -0
- package/src/renderContext/formats.js +145 -0
- package/src/renderContext/index.js +5 -0
- package/src/renderContext/presentation.js +125 -0
- package/src/renderContext/resources.js +27 -0
- package/src/renderContext/upscaling.js +22 -0
- package/src/renderContext/window.js +20 -0
- package/src/runtime/CjsRuntimeState.js +50 -0
- package/src/schema/CjsSchema.js +1009 -0
- package/src/schema/index.js +17 -0
- package/src/shader/index.js +1 -0
- package/src/shader/shaderStages.js +37 -0
- package/src/sph3.js +754 -0
- package/src/tangent.js +288 -0
- package/src/text.js +40 -0
- package/src/tri3.js +650 -0
- package/src/types/carbonTypes.js +635 -0
- package/src/types/index.js +2 -0
- package/src/utils.js +58 -0
- package/src/validation.js +46 -0
- package/src/vec2.js +229 -0
- package/src/vec3.js +1172 -0
- package/src/vec4.js +347 -0
- package/src/vertex.js +108 -0
- package/src/webgpu/index.js +1 -0
- package/src/webgpu/textureFormats.js +121 -0
package/src/num.js
ADDED
|
@@ -0,0 +1,827 @@
|
|
|
1
|
+
export const num = {};
|
|
2
|
+
|
|
3
|
+
num.EPSILON = 0.000001;
|
|
4
|
+
num.RAD2DEG = 180 / Math.PI;
|
|
5
|
+
num.DEG2RAD = Math.PI / 180;
|
|
6
|
+
num.TWO_PI = Math.PI * 2;
|
|
7
|
+
num.PI = Math.PI;
|
|
8
|
+
num.INV_TWO_PI = 1 / num.TWO_PI;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* biCumulative
|
|
12
|
+
*
|
|
13
|
+
* @param {number} t
|
|
14
|
+
* @param {number} order
|
|
15
|
+
* @returns {number}
|
|
16
|
+
*/
|
|
17
|
+
num.biCumulative = function (t, order)
|
|
18
|
+
{
|
|
19
|
+
if (order === 1)
|
|
20
|
+
{
|
|
21
|
+
const some = (1.0 - t);
|
|
22
|
+
return 1.0 - some * some * some;
|
|
23
|
+
}
|
|
24
|
+
else if (order === 2)
|
|
25
|
+
{
|
|
26
|
+
return 3.0 * t * t - 2.0 * t * t * t;
|
|
27
|
+
}
|
|
28
|
+
else
|
|
29
|
+
{
|
|
30
|
+
return t * t * t;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @alias Math.ceil
|
|
36
|
+
*/
|
|
37
|
+
num.ceil = Math.ceil;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Clamps a number
|
|
41
|
+
*
|
|
42
|
+
* @param {number} a
|
|
43
|
+
* @param {number} min
|
|
44
|
+
* @param {number} max
|
|
45
|
+
* @returns {number}
|
|
46
|
+
*/
|
|
47
|
+
num.clamp = function (a, min, max)
|
|
48
|
+
{
|
|
49
|
+
return Math.max(min, Math.min(max, a));
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Returns how many decimal places a number has
|
|
54
|
+
*
|
|
55
|
+
* @param {number} a
|
|
56
|
+
* @returns {number}
|
|
57
|
+
*/
|
|
58
|
+
num.decimalPlaces = function (a)
|
|
59
|
+
{
|
|
60
|
+
let match = ("" + a).match(/(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/);
|
|
61
|
+
return match ? Math.max(0, (match[1] ? match[1].length : 0) - (match[2] ? +match[2] : 0)) : 0;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Converts from radians to degrees
|
|
66
|
+
*
|
|
67
|
+
* @param {number} a
|
|
68
|
+
* @returns {number}
|
|
69
|
+
*/
|
|
70
|
+
num.degrees = function (a)
|
|
71
|
+
{
|
|
72
|
+
return a * num.RAD2DEG;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Converts from radians to unwrapped degrees
|
|
77
|
+
*
|
|
78
|
+
* @param {number} a
|
|
79
|
+
* @returns {number}
|
|
80
|
+
*/
|
|
81
|
+
num.degreesUnwrapped = function (a)
|
|
82
|
+
{
|
|
83
|
+
return num.unwrapDegrees(a * num.RAD2DEG);
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Converts a Dword to Float
|
|
88
|
+
* @param value
|
|
89
|
+
* @return {Number}
|
|
90
|
+
*/
|
|
91
|
+
num.dwordToFloat = (function ()
|
|
92
|
+
{
|
|
93
|
+
const
|
|
94
|
+
words = new Uint32Array(1),
|
|
95
|
+
floats = new Float32Array(words.buffer);
|
|
96
|
+
|
|
97
|
+
return function (value)
|
|
98
|
+
{
|
|
99
|
+
words[0] = value >>> 0;
|
|
100
|
+
return floats[0];
|
|
101
|
+
};
|
|
102
|
+
}());
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Checks if a number equals another
|
|
106
|
+
*
|
|
107
|
+
* @param a
|
|
108
|
+
* @param b
|
|
109
|
+
* @returns {boolean}
|
|
110
|
+
*/
|
|
111
|
+
num.equals = function (a, b)
|
|
112
|
+
{
|
|
113
|
+
return Math.abs(a - b) <= num.EPSILON * Math.max(1.0, Math.abs(a), Math.abs(b));
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Checks if a number exactly equals another
|
|
118
|
+
* - included for library consistency
|
|
119
|
+
*
|
|
120
|
+
* @param {number} a
|
|
121
|
+
* @param {number} b
|
|
122
|
+
* @returns {boolean}
|
|
123
|
+
*/
|
|
124
|
+
num.exactEquals = function (a, b)
|
|
125
|
+
{
|
|
126
|
+
return a === b;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Exponential decay
|
|
131
|
+
*
|
|
132
|
+
* @param {number} omega0
|
|
133
|
+
* @param {number} torque
|
|
134
|
+
* @param {number} I - inertia
|
|
135
|
+
* @param {number} d - drag
|
|
136
|
+
* @param {number} time - time
|
|
137
|
+
* @returns {number}
|
|
138
|
+
*/
|
|
139
|
+
num.exponentialDecay = function (omega0, torque, I, d, time)
|
|
140
|
+
{
|
|
141
|
+
return torque * time / d + I * (omega0 * d - torque) / (d * d) * (1.0 - Math.pow(Math.E, -d * time / I));
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Gets the fractional components of a number
|
|
146
|
+
*
|
|
147
|
+
* @param {number} a
|
|
148
|
+
* @returns {number}
|
|
149
|
+
*/
|
|
150
|
+
num.fract = function (a)
|
|
151
|
+
{
|
|
152
|
+
return a - Math.floor(a);
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Gets a value from a half float
|
|
157
|
+
* @author Babylon
|
|
158
|
+
* @param {number} a
|
|
159
|
+
* @returns {number}
|
|
160
|
+
*/
|
|
161
|
+
num.fromHalfFloat = function (a)
|
|
162
|
+
{
|
|
163
|
+
const
|
|
164
|
+
s = (a & 0x8000) >> 15,
|
|
165
|
+
e = (a & 0x7C00) >> 10,
|
|
166
|
+
f = a & 0x03FF;
|
|
167
|
+
|
|
168
|
+
if (e === 0)
|
|
169
|
+
{
|
|
170
|
+
return (s ? -1 : 1) * Math.pow(2, -14) * (f / Math.pow(2, 10));
|
|
171
|
+
}
|
|
172
|
+
else if (e === 0x1F)
|
|
173
|
+
{
|
|
174
|
+
return f ? NaN : ((s ? -1 : 1) * Infinity);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return (s ? -1 : 1) * Math.pow(2, e - 15) * (1 + (f / Math.pow(2, 10)));
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* @alias Math.floor
|
|
182
|
+
*/
|
|
183
|
+
num.floor = Math.floor;
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Gets long word order
|
|
187
|
+
* @author Babylon
|
|
188
|
+
* @param {number} a
|
|
189
|
+
* @returns {number}
|
|
190
|
+
*/
|
|
191
|
+
num.getLongWordOrder = function (a)
|
|
192
|
+
{
|
|
193
|
+
let value = a >>> 0,
|
|
194
|
+
order = 0;
|
|
195
|
+
|
|
196
|
+
while (order < 3 && value !== 0 && (value & 0xff) === 0)
|
|
197
|
+
{
|
|
198
|
+
value >>>= 8;
|
|
199
|
+
order++;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
return order;
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
*
|
|
207
|
+
*
|
|
208
|
+
* @param {number} a
|
|
209
|
+
* @param {number} b
|
|
210
|
+
* @returns {number}
|
|
211
|
+
*/
|
|
212
|
+
num.greaterThan = function (a, b)
|
|
213
|
+
{
|
|
214
|
+
return a > b ? 1 : 0;
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
*
|
|
219
|
+
*
|
|
220
|
+
* @param {number} a
|
|
221
|
+
* @param {number} b
|
|
222
|
+
* @returns {number}
|
|
223
|
+
*/
|
|
224
|
+
num.greaterThanEqual = function (a, b)
|
|
225
|
+
{
|
|
226
|
+
return a === b || num.equals(a, b) || a > b ? 1 : 0;
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
*
|
|
231
|
+
* - included for library consistency
|
|
232
|
+
*
|
|
233
|
+
* @param {number} a
|
|
234
|
+
* @param {number} b
|
|
235
|
+
* @returns {number}
|
|
236
|
+
*/
|
|
237
|
+
num.greaterThanExactEqual = function (a, b)
|
|
238
|
+
{
|
|
239
|
+
return a >= b ? 1 : 0;
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Checks if a number is even
|
|
245
|
+
*
|
|
246
|
+
* @param {number} a
|
|
247
|
+
* @returns {boolean}
|
|
248
|
+
*/
|
|
249
|
+
num.isEven = function (a)
|
|
250
|
+
{
|
|
251
|
+
return Math.abs(a) % 2 === 0;
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Checks if a number is a float
|
|
256
|
+
*
|
|
257
|
+
* @param {number} a
|
|
258
|
+
* @returns {boolean}
|
|
259
|
+
*/
|
|
260
|
+
num.isFloat = function (a)
|
|
261
|
+
{
|
|
262
|
+
return a % 1 !== 0;
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* @alias Number.isFinite
|
|
267
|
+
*/
|
|
268
|
+
num.isFinite = Number.isFinite;
|
|
269
|
+
// return (typeof v === "number" && !isNaN(v) && v !== Infinity && v !== -Infinity);
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Checks if a number is an integer
|
|
273
|
+
*
|
|
274
|
+
* @param {number} a
|
|
275
|
+
* @returns {boolean}
|
|
276
|
+
*/
|
|
277
|
+
num.isInt = function (a)
|
|
278
|
+
{
|
|
279
|
+
return a % 1 === 0;
|
|
280
|
+
};
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* @alias Number.isNaN
|
|
284
|
+
*/
|
|
285
|
+
num.isNaN = Number.isNaN;
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Checks if a number is odd
|
|
289
|
+
*
|
|
290
|
+
* @param {number} a
|
|
291
|
+
* @returns {boolean}
|
|
292
|
+
*/
|
|
293
|
+
num.isOdd = function (a)
|
|
294
|
+
{
|
|
295
|
+
return Math.abs(a) % 2 === 1;
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Checks if a number is to the power of two
|
|
300
|
+
*
|
|
301
|
+
* @param {number} a
|
|
302
|
+
* @returns {boolean}
|
|
303
|
+
*/
|
|
304
|
+
num.isPowerOfTwo = function (a)
|
|
305
|
+
{
|
|
306
|
+
return Number.isSafeInteger(a) && a > 0 && Number.isInteger(Math.log2(a));
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
*
|
|
311
|
+
*
|
|
312
|
+
* @param {number} a
|
|
313
|
+
* @param {number} b
|
|
314
|
+
* @returns {number}
|
|
315
|
+
*/
|
|
316
|
+
num.lessThan = function (a, b)
|
|
317
|
+
{
|
|
318
|
+
return a < b ? 1 : 0;
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
*
|
|
323
|
+
*
|
|
324
|
+
* @param {number} a
|
|
325
|
+
* @param {number} b
|
|
326
|
+
* @returns {number}
|
|
327
|
+
*/
|
|
328
|
+
num.lessThanEqual = function (a, b)
|
|
329
|
+
{
|
|
330
|
+
return a === b || num.equals(a, b) || a < b ? 1 : 0;
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
*
|
|
335
|
+
* - included for library consistency
|
|
336
|
+
*
|
|
337
|
+
* @param {number} a
|
|
338
|
+
* @param {number} b
|
|
339
|
+
* @returns {number}
|
|
340
|
+
*/
|
|
341
|
+
num.lessThanExactEqual = function (a, b)
|
|
342
|
+
{
|
|
343
|
+
return a <= b ? 1 : 0;
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Gets the log2 of a number
|
|
348
|
+
* @param {number} a
|
|
349
|
+
* @returns {number}
|
|
350
|
+
*/
|
|
351
|
+
num.log2 = function (a)
|
|
352
|
+
{
|
|
353
|
+
return Math.log(a) * Math.LOG2E;
|
|
354
|
+
};
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* @alias Math.max
|
|
358
|
+
*/
|
|
359
|
+
num.max = Math.max;
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* @alias Math.min
|
|
363
|
+
*/
|
|
364
|
+
num.min = Math.min;
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* Gets the nearest power of two value to a number
|
|
369
|
+
*
|
|
370
|
+
* @param {number} a
|
|
371
|
+
* @returns {number}
|
|
372
|
+
*/
|
|
373
|
+
num.nearestPowerOfTwo = function (a)
|
|
374
|
+
{
|
|
375
|
+
return Math.pow(2, Math.round(Math.log(a) / Math.LN2));
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
*
|
|
380
|
+
*
|
|
381
|
+
* @param {number} value
|
|
382
|
+
* @param {number} start
|
|
383
|
+
* @param {number} end
|
|
384
|
+
* @param {number} precision
|
|
385
|
+
* @returns {number}
|
|
386
|
+
*/
|
|
387
|
+
num.normalizeInt = function (value, start, end, precision)
|
|
388
|
+
{
|
|
389
|
+
let width = end - start;
|
|
390
|
+
let offsetValue = value - start;
|
|
391
|
+
let result = (offsetValue - (Math.floor(offsetValue / width) * width)) + start;
|
|
392
|
+
return precision === undefined ? result : Number(result.toFixed(precision));
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
*
|
|
397
|
+
*
|
|
398
|
+
* @param {number} value
|
|
399
|
+
* @param {number} start
|
|
400
|
+
* @param {number} end
|
|
401
|
+
* @param {number} precision
|
|
402
|
+
* @returns {number}
|
|
403
|
+
*/
|
|
404
|
+
num.normalizeFloat = function (value, start, end, precision)
|
|
405
|
+
{
|
|
406
|
+
let width = end - start;
|
|
407
|
+
let offsetValue = value - start;
|
|
408
|
+
let result = (offsetValue - (Math.floor(offsetValue / width) * width)) + start;
|
|
409
|
+
return precision === undefined ? result : Number(result.toFixed(precision));
|
|
410
|
+
};
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* Converts from degrees to radians
|
|
415
|
+
*
|
|
416
|
+
* @param {number} a
|
|
417
|
+
* @returns {number}
|
|
418
|
+
*/
|
|
419
|
+
num.radians = function (a)
|
|
420
|
+
{
|
|
421
|
+
return a * num.DEG2RAD;
|
|
422
|
+
};
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* Converts from degrees to unwrapped radians
|
|
426
|
+
*
|
|
427
|
+
* @param {number} a
|
|
428
|
+
* @returns {number}
|
|
429
|
+
*/
|
|
430
|
+
num.radiansUnwrapped = function (a)
|
|
431
|
+
{
|
|
432
|
+
return num.unwrapRadians(a *= num.DEG2RAD);
|
|
433
|
+
};
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* Creates a random integer
|
|
437
|
+
*
|
|
438
|
+
* @param {number} low
|
|
439
|
+
* @param {number} high
|
|
440
|
+
* @returns {number}
|
|
441
|
+
*/
|
|
442
|
+
num.randomInt = function (low, high)
|
|
443
|
+
{
|
|
444
|
+
return low + Math.floor(Math.random() * (high - low + 1));
|
|
445
|
+
};
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* Creates a random float
|
|
449
|
+
*
|
|
450
|
+
* @param {number} low
|
|
451
|
+
* @param {number} high
|
|
452
|
+
* @returns {number}
|
|
453
|
+
*/
|
|
454
|
+
num.randomFloat = function (low, high)
|
|
455
|
+
{
|
|
456
|
+
return low + Math.random() * (high - low);
|
|
457
|
+
};
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* @alias for Math.round
|
|
461
|
+
*/
|
|
462
|
+
num.round = Math.round;
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* Rounds a number to the closest zero
|
|
466
|
+
*
|
|
467
|
+
* @param {number} a
|
|
468
|
+
* @returns {number}
|
|
469
|
+
*/
|
|
470
|
+
num.roundToZero = function (a)
|
|
471
|
+
{
|
|
472
|
+
return a < 0 ? Math.ceil(a) : Math.floor(a);
|
|
473
|
+
};
|
|
474
|
+
|
|
475
|
+
/**
|
|
476
|
+
* @alias for num.greaterThan
|
|
477
|
+
*/
|
|
478
|
+
num.step = num.greaterThan;
|
|
479
|
+
|
|
480
|
+
/**
|
|
481
|
+
* Force positive number (excluding 0)
|
|
482
|
+
* @param {Number} s
|
|
483
|
+
* @returns {Number}
|
|
484
|
+
*/
|
|
485
|
+
num.strictPositive = function (s)
|
|
486
|
+
{
|
|
487
|
+
return Math.max(num.EPSILON, Math.abs(s));
|
|
488
|
+
};
|
|
489
|
+
|
|
490
|
+
/**
|
|
491
|
+
* Force negative number (excluding 0)
|
|
492
|
+
* @param {Number} s
|
|
493
|
+
* @returns {Number}
|
|
494
|
+
*/
|
|
495
|
+
num.strictNegative = function (s)
|
|
496
|
+
{
|
|
497
|
+
return -Math.max(num.EPSILON, Math.abs(s));
|
|
498
|
+
};
|
|
499
|
+
|
|
500
|
+
/**
|
|
501
|
+
* Evaluates cubic Hermite interpolation using Carbon argument order.
|
|
502
|
+
*
|
|
503
|
+
* @param {number} startValue
|
|
504
|
+
* @param {number} startTangent
|
|
505
|
+
* @param {number} endValue
|
|
506
|
+
* @param {number} endTangent
|
|
507
|
+
* @param {number} amount
|
|
508
|
+
* @returns {number}
|
|
509
|
+
*/
|
|
510
|
+
// USE THIS: Carbon order is start value, start tangent, end value, end tangent.
|
|
511
|
+
num.cubicHermite = function (startValue, startTangent, endValue, endTangent, amount)
|
|
512
|
+
{
|
|
513
|
+
const
|
|
514
|
+
amountSquared = amount * amount,
|
|
515
|
+
amountCubed = amountSquared * amount,
|
|
516
|
+
startFactor = 2 * amountCubed - 3 * amountSquared + 1,
|
|
517
|
+
startTangentFactor = amountCubed - 2 * amountSquared + amount,
|
|
518
|
+
endFactor = -2 * amountCubed + 3 * amountSquared,
|
|
519
|
+
endTangentFactor = amountCubed - amountSquared;
|
|
520
|
+
|
|
521
|
+
return startValue * startFactor
|
|
522
|
+
+ startTangent * startTangentFactor
|
|
523
|
+
+ endValue * endFactor
|
|
524
|
+
+ endTangent * endTangentFactor;
|
|
525
|
+
};
|
|
526
|
+
|
|
527
|
+
/**
|
|
528
|
+
* Evaluates the derivative of cubic Hermite interpolation using Carbon
|
|
529
|
+
* argument order.
|
|
530
|
+
*
|
|
531
|
+
* @param {number} startValue
|
|
532
|
+
* @param {number} startTangent
|
|
533
|
+
* @param {number} endValue
|
|
534
|
+
* @param {number} endTangent
|
|
535
|
+
* @param {number} amount
|
|
536
|
+
* @returns {number}
|
|
537
|
+
*/
|
|
538
|
+
// USE THIS: Carbon order is start value, start tangent, end value, end tangent.
|
|
539
|
+
num.cubicHermiteDerivative = function (startValue, startTangent, endValue, endTangent, amount)
|
|
540
|
+
{
|
|
541
|
+
const
|
|
542
|
+
amountSquared = amount * amount,
|
|
543
|
+
startFactor = 6 * amountSquared - 6 * amount,
|
|
544
|
+
startTangentFactor = 3 * amountSquared - 4 * amount + 1,
|
|
545
|
+
endFactor = -startFactor,
|
|
546
|
+
endTangentFactor = 3 * amountSquared - 2 * amount;
|
|
547
|
+
|
|
548
|
+
return startValue * startFactor
|
|
549
|
+
+ startTangent * startTangentFactor
|
|
550
|
+
+ endValue * endFactor
|
|
551
|
+
+ endTangent * endTangentFactor;
|
|
552
|
+
};
|
|
553
|
+
|
|
554
|
+
/**
|
|
555
|
+
*
|
|
556
|
+
* @param a
|
|
557
|
+
* @param min
|
|
558
|
+
* @param max
|
|
559
|
+
* @returns {number}
|
|
560
|
+
*/
|
|
561
|
+
num.smoothStep = function (a, min, max)
|
|
562
|
+
{
|
|
563
|
+
if (a <= min) return 0;
|
|
564
|
+
if (a >= max) return 1;
|
|
565
|
+
a = (a - min) / (max - min);
|
|
566
|
+
return a * a * (3 - 2 * a);
|
|
567
|
+
};
|
|
568
|
+
|
|
569
|
+
/**
|
|
570
|
+
*
|
|
571
|
+
* @param a
|
|
572
|
+
* @param min
|
|
573
|
+
* @param max
|
|
574
|
+
* @returns {number}
|
|
575
|
+
*/
|
|
576
|
+
num.smootherStep = function (a, min, max)
|
|
577
|
+
{
|
|
578
|
+
if (a <= min) return 0;
|
|
579
|
+
if (a >= max) return 1;
|
|
580
|
+
a = (a - min) / (max - min);
|
|
581
|
+
return a * a * a * (a * (a * 6 - 15) + 10);
|
|
582
|
+
};
|
|
583
|
+
|
|
584
|
+
/**
|
|
585
|
+
* Converts a number to a half float
|
|
586
|
+
* @author http://stackoverflow.com/questions/32633585/how-do-you-convert-to-half-floats-in-javascript
|
|
587
|
+
* @param {number} a
|
|
588
|
+
* @returns {number}
|
|
589
|
+
*/
|
|
590
|
+
num.toHalfFloat = (function ()
|
|
591
|
+
{
|
|
592
|
+
const
|
|
593
|
+
floats = new Float32Array(1),
|
|
594
|
+
words = new Uint32Array(floats.buffer);
|
|
595
|
+
|
|
596
|
+
return function (a)
|
|
597
|
+
{
|
|
598
|
+
floats[0] = a;
|
|
599
|
+
|
|
600
|
+
const
|
|
601
|
+
word = words[0],
|
|
602
|
+
sign = (word >>> 16) & 0x8000,
|
|
603
|
+
exponent = (word >>> 23) & 0xff;
|
|
604
|
+
|
|
605
|
+
let mantissa = word & 0x7fffff;
|
|
606
|
+
|
|
607
|
+
if (exponent === 0xff)
|
|
608
|
+
{
|
|
609
|
+
return sign | 0x7c00 | (mantissa ? 0x0200 : 0);
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
let halfExponent = exponent - 127 + 15;
|
|
613
|
+
if (halfExponent >= 0x1f)
|
|
614
|
+
{
|
|
615
|
+
return sign | 0x7c00;
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
if (halfExponent <= 0)
|
|
619
|
+
{
|
|
620
|
+
if (halfExponent < -10) return sign;
|
|
621
|
+
|
|
622
|
+
mantissa |= 0x800000;
|
|
623
|
+
const
|
|
624
|
+
shift = 14 - halfExponent,
|
|
625
|
+
halfway = 1 << (shift - 1),
|
|
626
|
+
remainder = mantissa & ((1 << shift) - 1);
|
|
627
|
+
|
|
628
|
+
let halfMantissa = mantissa >>> shift;
|
|
629
|
+
if (remainder > halfway || (remainder === halfway && (halfMantissa & 1)))
|
|
630
|
+
{
|
|
631
|
+
halfMantissa++;
|
|
632
|
+
}
|
|
633
|
+
return sign | halfMantissa;
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
let halfMantissa = mantissa >>> 13;
|
|
637
|
+
const remainder = mantissa & 0x1fff;
|
|
638
|
+
if (remainder > 0x1000 || (remainder === 0x1000 && (halfMantissa & 1)))
|
|
639
|
+
{
|
|
640
|
+
halfMantissa++;
|
|
641
|
+
if (halfMantissa === 0x400)
|
|
642
|
+
{
|
|
643
|
+
halfMantissa = 0;
|
|
644
|
+
halfExponent++;
|
|
645
|
+
if (halfExponent >= 0x1f) return sign | 0x7c00;
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
return sign | (halfExponent << 10) | halfMantissa;
|
|
650
|
+
};
|
|
651
|
+
}());
|
|
652
|
+
|
|
653
|
+
/**
|
|
654
|
+
* Converts linear color to rgba/rgb color
|
|
655
|
+
* @param {Number} a
|
|
656
|
+
* @returns {Number}
|
|
657
|
+
*/
|
|
658
|
+
num.colorFromLinear = function (a)
|
|
659
|
+
{
|
|
660
|
+
return Math.max(0, Math.min(Math.floor(a * 255), 255));
|
|
661
|
+
};
|
|
662
|
+
|
|
663
|
+
/**
|
|
664
|
+
* Converts linear color to rgba/rgb color
|
|
665
|
+
* @param {Number} a
|
|
666
|
+
* @returns {Number}
|
|
667
|
+
*/
|
|
668
|
+
num.linearFromColor = function (a)
|
|
669
|
+
{
|
|
670
|
+
return a / 255;
|
|
671
|
+
};
|
|
672
|
+
|
|
673
|
+
/**
|
|
674
|
+
* Converts linear color to hex string
|
|
675
|
+
* @param {Number} a
|
|
676
|
+
* @returns {String}
|
|
677
|
+
*/
|
|
678
|
+
num.hexFromLinear = function (a)
|
|
679
|
+
{
|
|
680
|
+
return num.hexFromColor(num.colorFromLinear(a));
|
|
681
|
+
};
|
|
682
|
+
|
|
683
|
+
/**
|
|
684
|
+
* Converts rgb/rgba color to hex string
|
|
685
|
+
* @param {Number} a
|
|
686
|
+
* @returns {String}
|
|
687
|
+
*/
|
|
688
|
+
num.hexFromColor = function (a)
|
|
689
|
+
{
|
|
690
|
+
return (a | 1 << 8).toString(16).slice(1);
|
|
691
|
+
};
|
|
692
|
+
|
|
693
|
+
/**
|
|
694
|
+
* Unwraps degrees
|
|
695
|
+
*
|
|
696
|
+
* @param {number} d
|
|
697
|
+
* @returns {number}
|
|
698
|
+
*/
|
|
699
|
+
num.unwrapDegrees = function (d)
|
|
700
|
+
{
|
|
701
|
+
d = d % 360;
|
|
702
|
+
if (d > 180) d -= 360;
|
|
703
|
+
if (d < -180) d += 360;
|
|
704
|
+
return d;
|
|
705
|
+
};
|
|
706
|
+
|
|
707
|
+
/**
|
|
708
|
+
* Unwraps radians
|
|
709
|
+
*
|
|
710
|
+
* @param {number} r
|
|
711
|
+
* @returns {number}
|
|
712
|
+
*/
|
|
713
|
+
num.unwrapRadians = function (r)
|
|
714
|
+
{
|
|
715
|
+
r = r % num.TWO_PI;
|
|
716
|
+
if (r > num.PI) r -= num.TWO_PI;
|
|
717
|
+
if (r < -num.PI) r += num.TWO_PI;
|
|
718
|
+
return r;
|
|
719
|
+
};
|
|
720
|
+
|
|
721
|
+
/**
|
|
722
|
+
* Converts srgb to linear colour
|
|
723
|
+
* @param {Number} a
|
|
724
|
+
* @returns {Number}
|
|
725
|
+
*/
|
|
726
|
+
num.linearFromSRGB = function (a)
|
|
727
|
+
{
|
|
728
|
+
return (a < 0.04045) ? a * 0.0773993808 : Math.pow(a * 0.9478672986 + 0.0521327014, 2.4);
|
|
729
|
+
};
|
|
730
|
+
|
|
731
|
+
/**
|
|
732
|
+
* Converts from linear color space to Carbon gamma 2.2 color space
|
|
733
|
+
* @param {Number} a
|
|
734
|
+
* @returns {Number}
|
|
735
|
+
*/
|
|
736
|
+
num.linearToGamma = function (a)
|
|
737
|
+
{
|
|
738
|
+
return Math.pow(a, 0.454545);
|
|
739
|
+
};
|
|
740
|
+
|
|
741
|
+
/**
|
|
742
|
+
* Converts from Carbon gamma 2.2 color space to linear color space
|
|
743
|
+
* @param {Number} a
|
|
744
|
+
* @returns {Number}
|
|
745
|
+
*/
|
|
746
|
+
num.gammaToLinear = function (a)
|
|
747
|
+
{
|
|
748
|
+
return Math.pow(a, 2.2);
|
|
749
|
+
};
|
|
750
|
+
|
|
751
|
+
/**
|
|
752
|
+
* Converts linear colour to srgb
|
|
753
|
+
* @param {Number} a
|
|
754
|
+
* @returns {Number}
|
|
755
|
+
*/
|
|
756
|
+
num.srgbFromLinear = function (a)
|
|
757
|
+
{
|
|
758
|
+
return (a < 0.0031308)
|
|
759
|
+
? a * 12.92
|
|
760
|
+
: 1.055 * Math.pow(a, 1.0 / 2.4) - 0.055;
|
|
761
|
+
};
|
|
762
|
+
|
|
763
|
+
export const {
|
|
764
|
+
EPSILON,
|
|
765
|
+
RAD2DEG,
|
|
766
|
+
DEG2RAD,
|
|
767
|
+
TWO_PI,
|
|
768
|
+
PI,
|
|
769
|
+
INV_TWO_PI,
|
|
770
|
+
biCumulative,
|
|
771
|
+
ceil,
|
|
772
|
+
clamp,
|
|
773
|
+
decimalPlaces,
|
|
774
|
+
degrees,
|
|
775
|
+
degreesUnwrapped,
|
|
776
|
+
dwordToFloat,
|
|
777
|
+
equals,
|
|
778
|
+
exactEquals,
|
|
779
|
+
exponentialDecay,
|
|
780
|
+
fract,
|
|
781
|
+
fromHalfFloat,
|
|
782
|
+
floor,
|
|
783
|
+
getLongWordOrder,
|
|
784
|
+
greaterThan,
|
|
785
|
+
greaterThanEqual,
|
|
786
|
+
greaterThanExactEqual,
|
|
787
|
+
isEven,
|
|
788
|
+
isFloat,
|
|
789
|
+
isFinite,
|
|
790
|
+
isInt,
|
|
791
|
+
isNaN,
|
|
792
|
+
isOdd,
|
|
793
|
+
isPowerOfTwo,
|
|
794
|
+
lessThan,
|
|
795
|
+
lessThanEqual,
|
|
796
|
+
lessThanExactEqual,
|
|
797
|
+
log2,
|
|
798
|
+
max,
|
|
799
|
+
min,
|
|
800
|
+
nearestPowerOfTwo,
|
|
801
|
+
normalizeInt,
|
|
802
|
+
normalizeFloat,
|
|
803
|
+
radians,
|
|
804
|
+
radiansUnwrapped,
|
|
805
|
+
randomInt,
|
|
806
|
+
randomFloat,
|
|
807
|
+
round,
|
|
808
|
+
roundToZero,
|
|
809
|
+
step,
|
|
810
|
+
strictPositive,
|
|
811
|
+
strictNegative,
|
|
812
|
+
cubicHermite,
|
|
813
|
+
cubicHermiteDerivative,
|
|
814
|
+
smoothStep,
|
|
815
|
+
smootherStep,
|
|
816
|
+
toHalfFloat,
|
|
817
|
+
colorFromLinear,
|
|
818
|
+
linearFromColor,
|
|
819
|
+
hexFromLinear,
|
|
820
|
+
hexFromColor,
|
|
821
|
+
unwrapDegrees,
|
|
822
|
+
unwrapRadians,
|
|
823
|
+
linearFromSRGB,
|
|
824
|
+
linearToGamma,
|
|
825
|
+
gammaToLinear,
|
|
826
|
+
srgbFromLinear
|
|
827
|
+
} = num;
|