@spiffcommerce/preview 3.6.2-rc.8 → 4.0.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 (45) hide show
  1. package/dist/index.esm.js +1576 -38
  2. package/dist/index.umd.js +1 -0
  3. package/package.json +4 -6
  4. package/dist/_tslib.esm.js +0 -33
  5. package/dist/animation.esm.js +0 -1364
  6. package/dist/assetCache.esm.js +0 -6
  7. package/dist/assetCache.esm2.js +0 -825
  8. package/dist/blurPostProcess.esm.js +0 -327
  9. package/dist/bumpVertex.esm.js +0 -497
  10. package/dist/compatibilityOptions.esm.js +0 -68
  11. package/dist/configuration.esm.js +0 -121
  12. package/dist/core.esm.js +0 -8135
  13. package/dist/dynamicTexture.esm.js +0 -105
  14. package/dist/dynamicTexture.esm2.js +0 -238
  15. package/dist/easing.esm.js +0 -130
  16. package/dist/effectFallbacks.esm.js +0 -378
  17. package/dist/engine.esm.js +0 -25504
  18. package/dist/glbLoaderExtensions.esm.js +0 -690
  19. package/dist/glowLayer.esm.js +0 -1621
  20. package/dist/glowLayerManager.esm.js +0 -50
  21. package/dist/guid.esm.js +0 -21
  22. package/dist/hdrFilteringFunctions.esm.js +0 -816
  23. package/dist/helperFunctions.esm.js +0 -5145
  24. package/dist/material.esm.js +0 -115
  25. package/dist/material.esm2.js +0 -5245
  26. package/dist/math.axis.esm.js +0 -35
  27. package/dist/math.color.esm.js +0 -1661
  28. package/dist/math.path.esm.js +0 -15
  29. package/dist/math.size.esm.js +0 -137
  30. package/dist/mesh.esm.js +0 -11170
  31. package/dist/modelContainer.esm.js +0 -1895
  32. package/dist/node.esm.js +0 -795
  33. package/dist/pbrBRDFFunctions.esm.js +0 -124
  34. package/dist/pbrMaterial.esm.js +8 -8739
  35. package/dist/productAnimations.esm.js +0 -182
  36. package/dist/productCamera.esm.js +0 -14
  37. package/dist/productCamera.esm2.js +0 -3870
  38. package/dist/renderConstants.esm.js +0 -116
  39. package/dist/renderingPipeline.esm.js +0 -18
  40. package/dist/renderingPipeline.esm2.js +1 -3594
  41. package/dist/sceneLoaderFlags.esm.js +0 -51
  42. package/dist/types.esm.js +0 -30
  43. package/dist/variants.esm.js +0 -16
  44. package/dist/variants.esm2.js +0 -3097
  45. package/dist/webRequest.esm.js +0 -7777
@@ -1,1661 +0,0 @@
1
- /**
2
- * Scalar computation library
3
- */
4
- class Scalar {
5
- /**
6
- * Boolean : true if the absolute difference between a and b is lower than epsilon (default = 1.401298E-45)
7
- * @param a number
8
- * @param b number
9
- * @param epsilon (default = 1.401298E-45)
10
- * @returns true if the absolute difference between a and b is lower than epsilon (default = 1.401298E-45)
11
- */
12
- static WithinEpsilon(a, b, epsilon = 1.401298e-45) {
13
- return Math.abs(a - b) <= epsilon;
14
- }
15
- /**
16
- * Returns a string : the upper case translation of the number i to hexadecimal.
17
- * @param i number
18
- * @returns the upper case translation of the number i to hexadecimal.
19
- */
20
- static ToHex(i) {
21
- const str = i.toString(16);
22
- if (i <= 15) {
23
- return ("0" + str).toUpperCase();
24
- }
25
- return str.toUpperCase();
26
- }
27
- /**
28
- * Returns -1 if value is negative and +1 is value is positive.
29
- * @param value the value
30
- * @returns the value itself if it's equal to zero.
31
- */
32
- static Sign(value) {
33
- value = +value; // convert to a number
34
- if (value === 0 || isNaN(value)) {
35
- return value;
36
- }
37
- return value > 0 ? 1 : -1;
38
- }
39
- /**
40
- * Returns the value itself if it's between min and max.
41
- * Returns min if the value is lower than min.
42
- * Returns max if the value is greater than max.
43
- * @param value the value to clmap
44
- * @param min the min value to clamp to (default: 0)
45
- * @param max the max value to clamp to (default: 1)
46
- * @returns the clamped value
47
- */
48
- static Clamp(value, min = 0, max = 1) {
49
- return Math.min(max, Math.max(min, value));
50
- }
51
- /**
52
- * the log2 of value.
53
- * @param value the value to compute log2 of
54
- * @returns the log2 of value.
55
- */
56
- static Log2(value) {
57
- return Math.log(value) * Math.LOG2E;
58
- }
59
- /**
60
- * the floor part of a log2 value.
61
- * @param value the value to compute log2 of
62
- * @returns the log2 of value.
63
- */
64
- static ILog2(value) {
65
- if (Math.log2) {
66
- return Math.floor(Math.log2(value));
67
- }
68
- if (value < 0) {
69
- return NaN;
70
- }
71
- else if (value === 0) {
72
- return -Infinity;
73
- }
74
- let n = 0;
75
- if (value < 1) {
76
- while (value < 1) {
77
- n++;
78
- value = value * 2;
79
- }
80
- n = -n;
81
- }
82
- else if (value > 1) {
83
- while (value > 1) {
84
- n++;
85
- value = Math.floor(value / 2);
86
- }
87
- }
88
- return n;
89
- }
90
- /**
91
- * Loops the value, so that it is never larger than length and never smaller than 0.
92
- *
93
- * This is similar to the modulo operator but it works with floating point numbers.
94
- * For example, using 3.0 for t and 2.5 for length, the result would be 0.5.
95
- * With t = 5 and length = 2.5, the result would be 0.0.
96
- * Note, however, that the behaviour is not defined for negative numbers as it is for the modulo operator
97
- * @param value the value
98
- * @param length the length
99
- * @returns the looped value
100
- */
101
- static Repeat(value, length) {
102
- return value - Math.floor(value / length) * length;
103
- }
104
- /**
105
- * Normalize the value between 0.0 and 1.0 using min and max values
106
- * @param value value to normalize
107
- * @param min max to normalize between
108
- * @param max min to normalize between
109
- * @returns the normalized value
110
- */
111
- static Normalize(value, min, max) {
112
- return (value - min) / (max - min);
113
- }
114
- /**
115
- * Denormalize the value from 0.0 and 1.0 using min and max values
116
- * @param normalized value to denormalize
117
- * @param min max to denormalize between
118
- * @param max min to denormalize between
119
- * @returns the denormalized value
120
- */
121
- static Denormalize(normalized, min, max) {
122
- return normalized * (max - min) + min;
123
- }
124
- /**
125
- * Calculates the shortest difference between two given angles given in degrees.
126
- * @param current current angle in degrees
127
- * @param target target angle in degrees
128
- * @returns the delta
129
- */
130
- static DeltaAngle(current, target) {
131
- let num = Scalar.Repeat(target - current, 360.0);
132
- if (num > 180.0) {
133
- num -= 360.0;
134
- }
135
- return num;
136
- }
137
- /**
138
- * PingPongs the value t, so that it is never larger than length and never smaller than 0.
139
- * @param tx value
140
- * @param length length
141
- * @returns The returned value will move back and forth between 0 and length
142
- */
143
- static PingPong(tx, length) {
144
- const t = Scalar.Repeat(tx, length * 2.0);
145
- return length - Math.abs(t - length);
146
- }
147
- /**
148
- * Interpolates between min and max with smoothing at the limits.
149
- *
150
- * This function interpolates between min and max in a similar way to Lerp. However, the interpolation will gradually speed up
151
- * from the start and slow down toward the end. This is useful for creating natural-looking animation, fading and other transitions.
152
- * @param from from
153
- * @param to to
154
- * @param tx value
155
- * @returns the smooth stepped value
156
- */
157
- static SmoothStep(from, to, tx) {
158
- let t = Scalar.Clamp(tx);
159
- t = -2.0 * t * t * t + 3.0 * t * t;
160
- return to * t + from * (1.0 - t);
161
- }
162
- /**
163
- * Moves a value current towards target.
164
- *
165
- * This is essentially the same as Mathf.Lerp but instead the function will ensure that the speed never exceeds maxDelta.
166
- * Negative values of maxDelta pushes the value away from target.
167
- * @param current current value
168
- * @param target target value
169
- * @param maxDelta max distance to move
170
- * @returns resulting value
171
- */
172
- static MoveTowards(current, target, maxDelta) {
173
- let result = 0;
174
- if (Math.abs(target - current) <= maxDelta) {
175
- result = target;
176
- }
177
- else {
178
- result = current + Scalar.Sign(target - current) * maxDelta;
179
- }
180
- return result;
181
- }
182
- /**
183
- * Same as MoveTowards but makes sure the values interpolate correctly when they wrap around 360 degrees.
184
- *
185
- * Variables current and target are assumed to be in degrees. For optimization reasons, negative values of maxDelta
186
- * are not supported and may cause oscillation. To push current away from a target angle, add 180 to that angle instead.
187
- * @param current current value
188
- * @param target target value
189
- * @param maxDelta max distance to move
190
- * @returns resulting angle
191
- */
192
- static MoveTowardsAngle(current, target, maxDelta) {
193
- const num = Scalar.DeltaAngle(current, target);
194
- let result = 0;
195
- if (-maxDelta < num && num < maxDelta) {
196
- result = target;
197
- }
198
- else {
199
- target = current + num;
200
- result = Scalar.MoveTowards(current, target, maxDelta);
201
- }
202
- return result;
203
- }
204
- /**
205
- * Creates a new scalar with values linearly interpolated of "amount" between the start scalar and the end scalar.
206
- * @param start start value
207
- * @param end target value
208
- * @param amount amount to lerp between
209
- * @returns the lerped value
210
- */
211
- static Lerp(start, end, amount) {
212
- return start + (end - start) * amount;
213
- }
214
- /**
215
- * Same as Lerp but makes sure the values interpolate correctly when they wrap around 360 degrees.
216
- * The parameter t is clamped to the range [0, 1]. Variables a and b are assumed to be in degrees.
217
- * @param start start value
218
- * @param end target value
219
- * @param amount amount to lerp between
220
- * @returns the lerped value
221
- */
222
- static LerpAngle(start, end, amount) {
223
- let num = Scalar.Repeat(end - start, 360.0);
224
- if (num > 180.0) {
225
- num -= 360.0;
226
- }
227
- return start + num * Scalar.Clamp(amount);
228
- }
229
- /**
230
- * Calculates the linear parameter t that produces the interpolant value within the range [a, b].
231
- * @param a start value
232
- * @param b target value
233
- * @param value value between a and b
234
- * @returns the inverseLerp value
235
- */
236
- static InverseLerp(a, b, value) {
237
- let result = 0;
238
- if (a != b) {
239
- result = Scalar.Clamp((value - a) / (b - a));
240
- }
241
- else {
242
- result = 0.0;
243
- }
244
- return result;
245
- }
246
- /**
247
- * Returns a new scalar located for "amount" (float) on the Hermite spline defined by the scalars "value1", "value3", "tangent1", "tangent2".
248
- * @see http://mathworld.wolfram.com/HermitePolynomial.html
249
- * @param value1 defines the first control point
250
- * @param tangent1 defines the first tangent
251
- * @param value2 defines the second control point
252
- * @param tangent2 defines the second tangent
253
- * @param amount defines the amount on the interpolation spline (between 0 and 1)
254
- * @returns hermite result
255
- */
256
- static Hermite(value1, tangent1, value2, tangent2, amount) {
257
- const squared = amount * amount;
258
- const cubed = amount * squared;
259
- const part1 = 2.0 * cubed - 3.0 * squared + 1.0;
260
- const part2 = -2.0 * cubed + 3.0 * squared;
261
- const part3 = cubed - 2.0 * squared + amount;
262
- const part4 = cubed - squared;
263
- return value1 * part1 + value2 * part2 + tangent1 * part3 + tangent2 * part4;
264
- }
265
- /**
266
- * Returns a new scalar which is the 1st derivative of the Hermite spline defined by the scalars "value1", "value2", "tangent1", "tangent2".
267
- * @param value1 defines the first control point
268
- * @param tangent1 defines the first tangent
269
- * @param value2 defines the second control point
270
- * @param tangent2 defines the second tangent
271
- * @param time define where the derivative must be done
272
- * @returns 1st derivative
273
- */
274
- static Hermite1stDerivative(value1, tangent1, value2, tangent2, time) {
275
- const t2 = time * time;
276
- return (t2 - time) * 6 * value1 + (3 * t2 - 4 * time + 1) * tangent1 + (-t2 + time) * 6 * value2 + (3 * t2 - 2 * time) * tangent2;
277
- }
278
- /**
279
- * Returns a random float number between and min and max values
280
- * @param min min value of random
281
- * @param max max value of random
282
- * @returns random value
283
- */
284
- static RandomRange(min, max) {
285
- if (min === max) {
286
- return min;
287
- }
288
- return Math.random() * (max - min) + min;
289
- }
290
- /**
291
- * This function returns percentage of a number in a given range.
292
- *
293
- * RangeToPercent(40,20,60) will return 0.5 (50%)
294
- * RangeToPercent(34,0,100) will return 0.34 (34%)
295
- * @param number to convert to percentage
296
- * @param min min range
297
- * @param max max range
298
- * @returns the percentage
299
- */
300
- static RangeToPercent(number, min, max) {
301
- return (number - min) / (max - min);
302
- }
303
- /**
304
- * This function returns number that corresponds to the percentage in a given range.
305
- *
306
- * PercentToRange(0.34,0,100) will return 34.
307
- * @param percent to convert to number
308
- * @param min min range
309
- * @param max max range
310
- * @returns the number
311
- */
312
- static PercentToRange(percent, min, max) {
313
- return (max - min) * percent + min;
314
- }
315
- /**
316
- * Returns the angle converted to equivalent value between -Math.PI and Math.PI radians.
317
- * @param angle The angle to normalize in radian.
318
- * @returns The converted angle.
319
- */
320
- static NormalizeRadians(angle) {
321
- // More precise but slower version kept for reference.
322
- // angle = angle % Tools.TwoPi;
323
- // angle = (angle + Tools.TwoPi) % Tools.TwoPi;
324
- //if (angle > Math.PI) {
325
- // angle -= Tools.TwoPi;
326
- //}
327
- angle -= Scalar.TwoPi * Math.floor((angle + Math.PI) / Scalar.TwoPi);
328
- return angle;
329
- }
330
- /**
331
- * Returns the highest common factor of two integers.
332
- * @param a first parameter
333
- * @param b second parameter
334
- * @returns HCF of a and b
335
- */
336
- static HCF(a, b) {
337
- const r = a % b;
338
- if (r === 0) {
339
- return b;
340
- }
341
- return Scalar.HCF(b, r);
342
- }
343
- }
344
- /**
345
- * Two pi constants convenient for computation.
346
- */
347
- Scalar.TwoPi = Math.PI * 2;
348
-
349
- /**
350
- * Constant used to convert a value to gamma space
351
- * @ignorenaming
352
- */
353
- // eslint-disable-next-line @typescript-eslint/naming-convention
354
- const ToGammaSpace = 1 / 2.2;
355
- /**
356
- * Constant used to convert a value to linear space
357
- * @ignorenaming
358
- */
359
- // eslint-disable-next-line @typescript-eslint/naming-convention
360
- const ToLinearSpace = 2.2;
361
- /**
362
- * Constant used to define the minimal number value in Babylon.js
363
- * @ignorenaming
364
- */
365
- // eslint-disable-next-line @typescript-eslint/naming-convention
366
- const Epsilon = 0.001;
367
-
368
- /* eslint-disable @typescript-eslint/naming-convention */
369
- /**
370
- * Class containing a set of static utilities functions for arrays.
371
- */
372
- class ArrayTools {
373
- /**
374
- * Returns an array of the given size filled with elements built from the given constructor and the parameters.
375
- * @param size the number of element to construct and put in the array.
376
- * @param itemBuilder a callback responsible for creating new instance of item. Called once per array entry.
377
- * @returns a new array filled with new objects.
378
- */
379
- static BuildArray(size, itemBuilder) {
380
- const a = [];
381
- for (let i = 0; i < size; ++i) {
382
- a.push(itemBuilder());
383
- }
384
- return a;
385
- }
386
- /**
387
- * Returns a tuple of the given size filled with elements built from the given constructor and the parameters.
388
- * @param size he number of element to construct and put in the tuple.
389
- * @param itemBuilder a callback responsible for creating new instance of item. Called once per tuple entry.
390
- * @returns a new tuple filled with new objects.
391
- */
392
- static BuildTuple(size, itemBuilder) {
393
- return ArrayTools.BuildArray(size, itemBuilder);
394
- }
395
- }
396
- /**
397
- * Observes a function and calls the given callback when it is called.
398
- * @param object Defines the object the function to observe belongs to.
399
- * @param functionName Defines the name of the function to observe.
400
- * @param callback Defines the callback to call when the function is called.
401
- * @returns A function to call to stop observing
402
- */
403
- function _observeArrayfunction(object, functionName, callback) {
404
- // Finds the function to observe
405
- const oldFunction = object[functionName];
406
- if (typeof oldFunction !== "function") {
407
- return null;
408
- }
409
- // Creates a new function that calls the callback and the old function
410
- const newFunction = function () {
411
- const previousLength = object.length;
412
- const returnValue = newFunction.previous.apply(object, arguments);
413
- callback(functionName, previousLength);
414
- return returnValue;
415
- };
416
- // Doublishly links the new function and the old function
417
- oldFunction.next = newFunction;
418
- newFunction.previous = oldFunction;
419
- // Replaces the old function with the new function
420
- object[functionName] = newFunction;
421
- // Returns a function to disable the hook
422
- return () => {
423
- // Only unhook if the function is still hooked
424
- const previous = newFunction.previous;
425
- if (!previous) {
426
- return;
427
- }
428
- // Finds the ref to the next function in the chain
429
- const next = newFunction.next;
430
- // If in the middle of the chain, link the previous and next functions
431
- if (next) {
432
- previous.next = next;
433
- next.previous = previous;
434
- }
435
- // If at the end of the chain, remove the reference to the previous function
436
- // and restore the previous function
437
- else {
438
- previous.next = undefined;
439
- object[functionName] = previous;
440
- }
441
- // Lose reference to the previous and next functions
442
- newFunction.next = undefined;
443
- newFunction.previous = undefined;
444
- };
445
- }
446
- /**
447
- * Defines the list of functions to proxy when observing an array.
448
- * The scope is currently reduced to the common functions used in the render target render list and the scene cameras.
449
- */
450
- const observedArrayFunctions = ["push", "splice", "pop", "shift", "unshift"];
451
- /**
452
- * Observes an array and notifies the given observer when the array is modified.
453
- * @param array Defines the array to observe
454
- * @param callback Defines the function to call when the array is modified (in the limit of the observed array functions)
455
- * @returns A function to call to stop observing the array
456
- * @internal
457
- */
458
- function _ObserveArray(array, callback) {
459
- // Observes all the required array functions and stores the unhook functions
460
- const unObserveFunctions = observedArrayFunctions.map((name) => {
461
- return _observeArrayfunction(array, name, callback);
462
- });
463
- // Returns a function that unhook all the observed functions
464
- return () => {
465
- unObserveFunctions.forEach((unObserveFunction) => {
466
- unObserveFunction === null || unObserveFunction === void 0 ? void 0 : unObserveFunction();
467
- });
468
- };
469
- }
470
-
471
- /** @internal */
472
- // eslint-disable-next-line @typescript-eslint/naming-convention
473
- const _RegisteredTypes = {};
474
- /**
475
- * @internal
476
- */
477
- function RegisterClass(className, type) {
478
- _RegisteredTypes[className] = type;
479
- }
480
- /**
481
- * @internal
482
- */
483
- function GetClass(fqdn) {
484
- return _RegisteredTypes[fqdn];
485
- }
486
-
487
- function colorChannelToLinearSpace(color) {
488
- return Math.pow(color, ToLinearSpace);
489
- }
490
- function colorChannelToLinearSpaceExact(color) {
491
- if (color <= 0.04045) {
492
- return 0.0773993808 * color;
493
- }
494
- return Math.pow(0.947867299 * (color + 0.055), 2.4);
495
- }
496
- function colorChannelToGammaSpace(color) {
497
- return Math.pow(color, ToGammaSpace);
498
- }
499
- function colorChannelToGammaSpaceExact(color) {
500
- if (color <= 0.0031308) {
501
- return 12.92 * color;
502
- }
503
- return 1.055 * Math.pow(color, 0.41666) - 0.055;
504
- }
505
- /**
506
- * Class used to hold a RGB color
507
- */
508
- class Color3 {
509
- /**
510
- * Creates a new Color3 object from red, green, blue values, all between 0 and 1
511
- * @param r defines the red component (between 0 and 1, default is 0)
512
- * @param g defines the green component (between 0 and 1, default is 0)
513
- * @param b defines the blue component (between 0 and 1, default is 0)
514
- */
515
- constructor(
516
- /**
517
- * Defines the red component (between 0 and 1, default is 0)
518
- */
519
- r = 0,
520
- /**
521
- * Defines the green component (between 0 and 1, default is 0)
522
- */
523
- g = 0,
524
- /**
525
- * Defines the blue component (between 0 and 1, default is 0)
526
- */
527
- b = 0) {
528
- this.r = r;
529
- this.g = g;
530
- this.b = b;
531
- }
532
- /**
533
- * Creates a string with the Color3 current values
534
- * @returns the string representation of the Color3 object
535
- */
536
- toString() {
537
- return "{R: " + this.r + " G:" + this.g + " B:" + this.b + "}";
538
- }
539
- /**
540
- * Returns the string "Color3"
541
- * @returns "Color3"
542
- */
543
- getClassName() {
544
- return "Color3";
545
- }
546
- /**
547
- * Compute the Color3 hash code
548
- * @returns an unique number that can be used to hash Color3 objects
549
- */
550
- getHashCode() {
551
- let hash = (this.r * 255) | 0;
552
- hash = (hash * 397) ^ ((this.g * 255) | 0);
553
- hash = (hash * 397) ^ ((this.b * 255) | 0);
554
- return hash;
555
- }
556
- // Operators
557
- /**
558
- * Stores in the given array from the given starting index the red, green, blue values as successive elements
559
- * @param array defines the array where to store the r,g,b components
560
- * @param index defines an optional index in the target array to define where to start storing values
561
- * @returns the current Color3 object
562
- */
563
- toArray(array, index = 0) {
564
- array[index] = this.r;
565
- array[index + 1] = this.g;
566
- array[index + 2] = this.b;
567
- return this;
568
- }
569
- /**
570
- * Update the current color with values stored in an array from the starting index of the given array
571
- * @param array defines the source array
572
- * @param offset defines an offset in the source array
573
- * @returns the current Color3 object
574
- */
575
- fromArray(array, offset = 0) {
576
- Color3.FromArrayToRef(array, offset, this);
577
- return this;
578
- }
579
- /**
580
- * Returns a new Color4 object from the current Color3 and the given alpha
581
- * @param alpha defines the alpha component on the new Color4 object (default is 1)
582
- * @returns a new Color4 object
583
- */
584
- toColor4(alpha = 1) {
585
- return new Color4(this.r, this.g, this.b, alpha);
586
- }
587
- /**
588
- * Returns a new array populated with 3 numeric elements : red, green and blue values
589
- * @returns the new array
590
- */
591
- asArray() {
592
- return [this.r, this.g, this.b];
593
- }
594
- /**
595
- * Returns the luminance value
596
- * @returns a float value
597
- */
598
- toLuminance() {
599
- return this.r * 0.3 + this.g * 0.59 + this.b * 0.11;
600
- }
601
- /**
602
- * Multiply each Color3 rgb values by the given Color3 rgb values in a new Color3 object
603
- * @param otherColor defines the second operand
604
- * @returns the new Color3 object
605
- */
606
- multiply(otherColor) {
607
- return new Color3(this.r * otherColor.r, this.g * otherColor.g, this.b * otherColor.b);
608
- }
609
- /**
610
- * Multiply the rgb values of the Color3 and the given Color3 and stores the result in the object "result"
611
- * @param otherColor defines the second operand
612
- * @param result defines the Color3 object where to store the result
613
- * @returns the current Color3
614
- */
615
- multiplyToRef(otherColor, result) {
616
- result.r = this.r * otherColor.r;
617
- result.g = this.g * otherColor.g;
618
- result.b = this.b * otherColor.b;
619
- return this;
620
- }
621
- /**
622
- * Determines equality between Color3 objects
623
- * @param otherColor defines the second operand
624
- * @returns true if the rgb values are equal to the given ones
625
- */
626
- equals(otherColor) {
627
- return otherColor && this.r === otherColor.r && this.g === otherColor.g && this.b === otherColor.b;
628
- }
629
- /**
630
- * Determines equality between the current Color3 object and a set of r,b,g values
631
- * @param r defines the red component to check
632
- * @param g defines the green component to check
633
- * @param b defines the blue component to check
634
- * @returns true if the rgb values are equal to the given ones
635
- */
636
- equalsFloats(r, g, b) {
637
- return this.r === r && this.g === g && this.b === b;
638
- }
639
- /**
640
- * Creates a new Color3 with the current Color3 values multiplied by scale
641
- * @param scale defines the scaling factor to apply
642
- * @returns a new Color3 object
643
- */
644
- scale(scale) {
645
- return new Color3(this.r * scale, this.g * scale, this.b * scale);
646
- }
647
- /**
648
- * Multiplies the Color3 values by the float "scale"
649
- * @param scale defines the scaling factor to apply
650
- * @returns the current updated Color3
651
- */
652
- scaleInPlace(scale) {
653
- this.r *= scale;
654
- this.g *= scale;
655
- this.b *= scale;
656
- return this;
657
- }
658
- /**
659
- * Multiplies the rgb values by scale and stores the result into "result"
660
- * @param scale defines the scaling factor
661
- * @param result defines the Color3 object where to store the result
662
- * @returns the unmodified current Color3
663
- */
664
- scaleToRef(scale, result) {
665
- result.r = this.r * scale;
666
- result.g = this.g * scale;
667
- result.b = this.b * scale;
668
- return this;
669
- }
670
- /**
671
- * Scale the current Color3 values by a factor and add the result to a given Color3
672
- * @param scale defines the scale factor
673
- * @param result defines color to store the result into
674
- * @returns the unmodified current Color3
675
- */
676
- scaleAndAddToRef(scale, result) {
677
- result.r += this.r * scale;
678
- result.g += this.g * scale;
679
- result.b += this.b * scale;
680
- return this;
681
- }
682
- /**
683
- * Clamps the rgb values by the min and max values and stores the result into "result"
684
- * @param min defines minimum clamping value (default is 0)
685
- * @param max defines maximum clamping value (default is 1)
686
- * @param result defines color to store the result into
687
- * @returns the original Color3
688
- */
689
- clampToRef(min = 0, max = 1, result) {
690
- result.r = Scalar.Clamp(this.r, min, max);
691
- result.g = Scalar.Clamp(this.g, min, max);
692
- result.b = Scalar.Clamp(this.b, min, max);
693
- return this;
694
- }
695
- /**
696
- * Creates a new Color3 set with the added values of the current Color3 and of the given one
697
- * @param otherColor defines the second operand
698
- * @returns the new Color3
699
- */
700
- add(otherColor) {
701
- return new Color3(this.r + otherColor.r, this.g + otherColor.g, this.b + otherColor.b);
702
- }
703
- /**
704
- * Stores the result of the addition of the current Color3 and given one rgb values into "result"
705
- * @param otherColor defines the second operand
706
- * @param result defines Color3 object to store the result into
707
- * @returns the unmodified current Color3
708
- */
709
- addToRef(otherColor, result) {
710
- result.r = this.r + otherColor.r;
711
- result.g = this.g + otherColor.g;
712
- result.b = this.b + otherColor.b;
713
- return this;
714
- }
715
- /**
716
- * Returns a new Color3 set with the subtracted values of the given one from the current Color3
717
- * @param otherColor defines the second operand
718
- * @returns the new Color3
719
- */
720
- subtract(otherColor) {
721
- return new Color3(this.r - otherColor.r, this.g - otherColor.g, this.b - otherColor.b);
722
- }
723
- /**
724
- * Stores the result of the subtraction of given one from the current Color3 rgb values into "result"
725
- * @param otherColor defines the second operand
726
- * @param result defines Color3 object to store the result into
727
- * @returns the unmodified current Color3
728
- */
729
- subtractToRef(otherColor, result) {
730
- result.r = this.r - otherColor.r;
731
- result.g = this.g - otherColor.g;
732
- result.b = this.b - otherColor.b;
733
- return this;
734
- }
735
- /**
736
- * Copy the current object
737
- * @returns a new Color3 copied the current one
738
- */
739
- clone() {
740
- return new Color3(this.r, this.g, this.b);
741
- }
742
- /**
743
- * Copies the rgb values from the source in the current Color3
744
- * @param source defines the source Color3 object
745
- * @returns the updated Color3 object
746
- */
747
- copyFrom(source) {
748
- this.r = source.r;
749
- this.g = source.g;
750
- this.b = source.b;
751
- return this;
752
- }
753
- /**
754
- * Updates the Color3 rgb values from the given floats
755
- * @param r defines the red component to read from
756
- * @param g defines the green component to read from
757
- * @param b defines the blue component to read from
758
- * @returns the current Color3 object
759
- */
760
- copyFromFloats(r, g, b) {
761
- this.r = r;
762
- this.g = g;
763
- this.b = b;
764
- return this;
765
- }
766
- /**
767
- * Updates the Color3 rgb values from the given floats
768
- * @param r defines the red component to read from
769
- * @param g defines the green component to read from
770
- * @param b defines the blue component to read from
771
- * @returns the current Color3 object
772
- */
773
- set(r, g, b) {
774
- return this.copyFromFloats(r, g, b);
775
- }
776
- /**
777
- * Compute the Color3 hexadecimal code as a string
778
- * @returns a string containing the hexadecimal representation of the Color3 object
779
- */
780
- toHexString() {
781
- const intR = Math.round(this.r * 255);
782
- const intG = Math.round(this.g * 255);
783
- const intB = Math.round(this.b * 255);
784
- return "#" + Scalar.ToHex(intR) + Scalar.ToHex(intG) + Scalar.ToHex(intB);
785
- }
786
- /**
787
- * Converts current color in rgb space to HSV values
788
- * @returns a new color3 representing the HSV values
789
- */
790
- toHSV() {
791
- const result = new Color3();
792
- this.toHSVToRef(result);
793
- return result;
794
- }
795
- /**
796
- * Converts current color in rgb space to HSV values
797
- * @param result defines the Color3 where to store the HSV values
798
- */
799
- toHSVToRef(result) {
800
- const r = this.r;
801
- const g = this.g;
802
- const b = this.b;
803
- const max = Math.max(r, g, b);
804
- const min = Math.min(r, g, b);
805
- let h = 0;
806
- let s = 0;
807
- const v = max;
808
- const dm = max - min;
809
- if (max !== 0) {
810
- s = dm / max;
811
- }
812
- if (max != min) {
813
- if (max == r) {
814
- h = (g - b) / dm;
815
- if (g < b) {
816
- h += 6;
817
- }
818
- }
819
- else if (max == g) {
820
- h = (b - r) / dm + 2;
821
- }
822
- else if (max == b) {
823
- h = (r - g) / dm + 4;
824
- }
825
- h *= 60;
826
- }
827
- result.r = h;
828
- result.g = s;
829
- result.b = v;
830
- }
831
- /**
832
- * Computes a new Color3 converted from the current one to linear space
833
- * @param exact defines if the conversion will be done in an exact way which is slower but more accurate (default is false)
834
- * @returns a new Color3 object
835
- */
836
- toLinearSpace(exact = false) {
837
- const convertedColor = new Color3();
838
- this.toLinearSpaceToRef(convertedColor, exact);
839
- return convertedColor;
840
- }
841
- /**
842
- * Converts the Color3 values to linear space and stores the result in "convertedColor"
843
- * @param convertedColor defines the Color3 object where to store the linear space version
844
- * @param exact defines if the conversion will be done in an exact way which is slower but more accurate (default is false)
845
- * @returns the unmodified Color3
846
- */
847
- toLinearSpaceToRef(convertedColor, exact = false) {
848
- if (exact) {
849
- convertedColor.r = colorChannelToLinearSpaceExact(this.r);
850
- convertedColor.g = colorChannelToLinearSpaceExact(this.g);
851
- convertedColor.b = colorChannelToLinearSpaceExact(this.b);
852
- }
853
- else {
854
- convertedColor.r = colorChannelToLinearSpace(this.r);
855
- convertedColor.g = colorChannelToLinearSpace(this.g);
856
- convertedColor.b = colorChannelToLinearSpace(this.b);
857
- }
858
- return this;
859
- }
860
- /**
861
- * Computes a new Color3 converted from the current one to gamma space
862
- * @param exact defines if the conversion will be done in an exact way which is slower but more accurate (default is false)
863
- * @returns a new Color3 object
864
- */
865
- toGammaSpace(exact = false) {
866
- const convertedColor = new Color3();
867
- this.toGammaSpaceToRef(convertedColor, exact);
868
- return convertedColor;
869
- }
870
- /**
871
- * Converts the Color3 values to gamma space and stores the result in "convertedColor"
872
- * @param convertedColor defines the Color3 object where to store the gamma space version
873
- * @param exact defines if the conversion will be done in an exact way which is slower but more accurate (default is false)
874
- * @returns the unmodified Color3
875
- */
876
- toGammaSpaceToRef(convertedColor, exact = false) {
877
- if (exact) {
878
- convertedColor.r = colorChannelToGammaSpaceExact(this.r);
879
- convertedColor.g = colorChannelToGammaSpaceExact(this.g);
880
- convertedColor.b = colorChannelToGammaSpaceExact(this.b);
881
- }
882
- else {
883
- convertedColor.r = colorChannelToGammaSpace(this.r);
884
- convertedColor.g = colorChannelToGammaSpace(this.g);
885
- convertedColor.b = colorChannelToGammaSpace(this.b);
886
- }
887
- return this;
888
- }
889
- /**
890
- * Converts Hue, saturation and value to a Color3 (RGB)
891
- * @param hue defines the hue
892
- * @param saturation defines the saturation
893
- * @param value defines the value
894
- * @param result defines the Color3 where to store the RGB values
895
- */
896
- static HSVtoRGBToRef(hue, saturation, value, result) {
897
- const chroma = value * saturation;
898
- const h = hue / 60;
899
- const x = chroma * (1 - Math.abs((h % 2) - 1));
900
- let r = 0;
901
- let g = 0;
902
- let b = 0;
903
- if (h >= 0 && h <= 1) {
904
- r = chroma;
905
- g = x;
906
- }
907
- else if (h >= 1 && h <= 2) {
908
- r = x;
909
- g = chroma;
910
- }
911
- else if (h >= 2 && h <= 3) {
912
- g = chroma;
913
- b = x;
914
- }
915
- else if (h >= 3 && h <= 4) {
916
- g = x;
917
- b = chroma;
918
- }
919
- else if (h >= 4 && h <= 5) {
920
- r = x;
921
- b = chroma;
922
- }
923
- else if (h >= 5 && h <= 6) {
924
- r = chroma;
925
- b = x;
926
- }
927
- const m = value - chroma;
928
- result.set(r + m, g + m, b + m);
929
- }
930
- /**
931
- * Converts Hue, saturation and value to a new Color3 (RGB)
932
- * @param hue defines the hue (value between 0 and 360)
933
- * @param saturation defines the saturation (value between 0 and 1)
934
- * @param value defines the value (value between 0 and 1)
935
- * @returns a new Color3 object
936
- */
937
- static FromHSV(hue, saturation, value) {
938
- const result = new Color3(0, 0, 0);
939
- Color3.HSVtoRGBToRef(hue, saturation, value, result);
940
- return result;
941
- }
942
- /**
943
- * Creates a new Color3 from the string containing valid hexadecimal values
944
- * @param hex defines a string containing valid hexadecimal values
945
- * @returns a new Color3 object
946
- */
947
- static FromHexString(hex) {
948
- if (hex.substring(0, 1) !== "#" || hex.length !== 7) {
949
- return new Color3(0, 0, 0);
950
- }
951
- const r = parseInt(hex.substring(1, 3), 16);
952
- const g = parseInt(hex.substring(3, 5), 16);
953
- const b = parseInt(hex.substring(5, 7), 16);
954
- return Color3.FromInts(r, g, b);
955
- }
956
- /**
957
- * Creates a new Color3 from the starting index of the given array
958
- * @param array defines the source array
959
- * @param offset defines an offset in the source array
960
- * @returns a new Color3 object
961
- */
962
- static FromArray(array, offset = 0) {
963
- return new Color3(array[offset], array[offset + 1], array[offset + 2]);
964
- }
965
- /**
966
- * Creates a new Color3 from the starting index element of the given array
967
- * @param array defines the source array to read from
968
- * @param offset defines the offset in the source array
969
- * @param result defines the target Color3 object
970
- */
971
- static FromArrayToRef(array, offset = 0, result) {
972
- result.r = array[offset];
973
- result.g = array[offset + 1];
974
- result.b = array[offset + 2];
975
- }
976
- /**
977
- * Creates a new Color3 from integer values (< 256)
978
- * @param r defines the red component to read from (value between 0 and 255)
979
- * @param g defines the green component to read from (value between 0 and 255)
980
- * @param b defines the blue component to read from (value between 0 and 255)
981
- * @returns a new Color3 object
982
- */
983
- static FromInts(r, g, b) {
984
- return new Color3(r / 255.0, g / 255.0, b / 255.0);
985
- }
986
- /**
987
- * Creates a new Color3 with values linearly interpolated of "amount" between the start Color3 and the end Color3
988
- * @param start defines the start Color3 value
989
- * @param end defines the end Color3 value
990
- * @param amount defines the gradient value between start and end
991
- * @returns a new Color3 object
992
- */
993
- static Lerp(start, end, amount) {
994
- const result = new Color3(0.0, 0.0, 0.0);
995
- Color3.LerpToRef(start, end, amount, result);
996
- return result;
997
- }
998
- /**
999
- * Creates a new Color3 with values linearly interpolated of "amount" between the start Color3 and the end Color3
1000
- * @param left defines the start value
1001
- * @param right defines the end value
1002
- * @param amount defines the gradient factor
1003
- * @param result defines the Color3 object where to store the result
1004
- */
1005
- static LerpToRef(left, right, amount, result) {
1006
- result.r = left.r + (right.r - left.r) * amount;
1007
- result.g = left.g + (right.g - left.g) * amount;
1008
- result.b = left.b + (right.b - left.b) * amount;
1009
- }
1010
- /**
1011
- * Returns a new Color3 located for "amount" (float) on the Hermite interpolation spline defined by the vectors "value1", "tangent1", "value2", "tangent2"
1012
- * @param value1 defines the first control point
1013
- * @param tangent1 defines the first tangent Color3
1014
- * @param value2 defines the second control point
1015
- * @param tangent2 defines the second tangent Color3
1016
- * @param amount defines the amount on the interpolation spline (between 0 and 1)
1017
- * @returns the new Color3
1018
- */
1019
- static Hermite(value1, tangent1, value2, tangent2, amount) {
1020
- const squared = amount * amount;
1021
- const cubed = amount * squared;
1022
- const part1 = 2.0 * cubed - 3.0 * squared + 1.0;
1023
- const part2 = -2.0 * cubed + 3.0 * squared;
1024
- const part3 = cubed - 2.0 * squared + amount;
1025
- const part4 = cubed - squared;
1026
- const r = value1.r * part1 + value2.r * part2 + tangent1.r * part3 + tangent2.r * part4;
1027
- const g = value1.g * part1 + value2.g * part2 + tangent1.g * part3 + tangent2.g * part4;
1028
- const b = value1.b * part1 + value2.b * part2 + tangent1.b * part3 + tangent2.b * part4;
1029
- return new Color3(r, g, b);
1030
- }
1031
- /**
1032
- * Returns a new Color3 which is the 1st derivative of the Hermite spline defined by the colors "value1", "value2", "tangent1", "tangent2".
1033
- * @param value1 defines the first control point
1034
- * @param tangent1 defines the first tangent
1035
- * @param value2 defines the second control point
1036
- * @param tangent2 defines the second tangent
1037
- * @param time define where the derivative must be done
1038
- * @returns 1st derivative
1039
- */
1040
- static Hermite1stDerivative(value1, tangent1, value2, tangent2, time) {
1041
- const result = Color3.Black();
1042
- this.Hermite1stDerivativeToRef(value1, tangent1, value2, tangent2, time, result);
1043
- return result;
1044
- }
1045
- /**
1046
- * Returns a new Color3 which is the 1st derivative of the Hermite spline defined by the colors "value1", "value2", "tangent1", "tangent2".
1047
- * @param value1 defines the first control point
1048
- * @param tangent1 defines the first tangent
1049
- * @param value2 defines the second control point
1050
- * @param tangent2 defines the second tangent
1051
- * @param time define where the derivative must be done
1052
- * @param result define where to store the derivative
1053
- */
1054
- static Hermite1stDerivativeToRef(value1, tangent1, value2, tangent2, time, result) {
1055
- const t2 = time * time;
1056
- result.r = (t2 - time) * 6 * value1.r + (3 * t2 - 4 * time + 1) * tangent1.r + (-t2 + time) * 6 * value2.r + (3 * t2 - 2 * time) * tangent2.r;
1057
- result.g = (t2 - time) * 6 * value1.g + (3 * t2 - 4 * time + 1) * tangent1.g + (-t2 + time) * 6 * value2.g + (3 * t2 - 2 * time) * tangent2.g;
1058
- result.b = (t2 - time) * 6 * value1.b + (3 * t2 - 4 * time + 1) * tangent1.b + (-t2 + time) * 6 * value2.b + (3 * t2 - 2 * time) * tangent2.b;
1059
- }
1060
- /**
1061
- * Returns a Color3 value containing a red color
1062
- * @returns a new Color3 object
1063
- */
1064
- static Red() {
1065
- return new Color3(1, 0, 0);
1066
- }
1067
- /**
1068
- * Returns a Color3 value containing a green color
1069
- * @returns a new Color3 object
1070
- */
1071
- static Green() {
1072
- return new Color3(0, 1, 0);
1073
- }
1074
- /**
1075
- * Returns a Color3 value containing a blue color
1076
- * @returns a new Color3 object
1077
- */
1078
- static Blue() {
1079
- return new Color3(0, 0, 1);
1080
- }
1081
- /**
1082
- * Returns a Color3 value containing a black color
1083
- * @returns a new Color3 object
1084
- */
1085
- static Black() {
1086
- return new Color3(0, 0, 0);
1087
- }
1088
- /**
1089
- * Gets a Color3 value containing a black color that must not be updated
1090
- */
1091
- static get BlackReadOnly() {
1092
- return Color3._BlackReadOnly;
1093
- }
1094
- /**
1095
- * Returns a Color3 value containing a white color
1096
- * @returns a new Color3 object
1097
- */
1098
- static White() {
1099
- return new Color3(1, 1, 1);
1100
- }
1101
- /**
1102
- * Returns a Color3 value containing a purple color
1103
- * @returns a new Color3 object
1104
- */
1105
- static Purple() {
1106
- return new Color3(0.5, 0, 0.5);
1107
- }
1108
- /**
1109
- * Returns a Color3 value containing a magenta color
1110
- * @returns a new Color3 object
1111
- */
1112
- static Magenta() {
1113
- return new Color3(1, 0, 1);
1114
- }
1115
- /**
1116
- * Returns a Color3 value containing a yellow color
1117
- * @returns a new Color3 object
1118
- */
1119
- static Yellow() {
1120
- return new Color3(1, 1, 0);
1121
- }
1122
- /**
1123
- * Returns a Color3 value containing a gray color
1124
- * @returns a new Color3 object
1125
- */
1126
- static Gray() {
1127
- return new Color3(0.5, 0.5, 0.5);
1128
- }
1129
- /**
1130
- * Returns a Color3 value containing a teal color
1131
- * @returns a new Color3 object
1132
- */
1133
- static Teal() {
1134
- return new Color3(0, 1.0, 1.0);
1135
- }
1136
- /**
1137
- * Returns a Color3 value containing a random color
1138
- * @returns a new Color3 object
1139
- */
1140
- static Random() {
1141
- return new Color3(Math.random(), Math.random(), Math.random());
1142
- }
1143
- }
1144
- // Statics
1145
- Color3._BlackReadOnly = Color3.Black();
1146
- /**
1147
- * Class used to hold a RBGA color
1148
- */
1149
- class Color4 {
1150
- /**
1151
- * Creates a new Color4 object from red, green, blue values, all between 0 and 1
1152
- * @param r defines the red component (between 0 and 1, default is 0)
1153
- * @param g defines the green component (between 0 and 1, default is 0)
1154
- * @param b defines the blue component (between 0 and 1, default is 0)
1155
- * @param a defines the alpha component (between 0 and 1, default is 1)
1156
- */
1157
- constructor(
1158
- /**
1159
- * Defines the red component (between 0 and 1, default is 0)
1160
- */
1161
- r = 0,
1162
- /**
1163
- * Defines the green component (between 0 and 1, default is 0)
1164
- */
1165
- g = 0,
1166
- /**
1167
- * Defines the blue component (between 0 and 1, default is 0)
1168
- */
1169
- b = 0,
1170
- /**
1171
- * Defines the alpha component (between 0 and 1, default is 1)
1172
- */
1173
- a = 1) {
1174
- this.r = r;
1175
- this.g = g;
1176
- this.b = b;
1177
- this.a = a;
1178
- }
1179
- // Operators
1180
- /**
1181
- * Adds in place the given Color4 values to the current Color4 object
1182
- * @param right defines the second operand
1183
- * @returns the current updated Color4 object
1184
- */
1185
- addInPlace(right) {
1186
- this.r += right.r;
1187
- this.g += right.g;
1188
- this.b += right.b;
1189
- this.a += right.a;
1190
- return this;
1191
- }
1192
- /**
1193
- * Creates a new array populated with 4 numeric elements : red, green, blue, alpha values
1194
- * @returns the new array
1195
- */
1196
- asArray() {
1197
- return [this.r, this.g, this.b, this.a];
1198
- }
1199
- /**
1200
- * Stores from the starting index in the given array the Color4 successive values
1201
- * @param array defines the array where to store the r,g,b components
1202
- * @param index defines an optional index in the target array to define where to start storing values
1203
- * @returns the current Color4 object
1204
- */
1205
- toArray(array, index = 0) {
1206
- array[index] = this.r;
1207
- array[index + 1] = this.g;
1208
- array[index + 2] = this.b;
1209
- array[index + 3] = this.a;
1210
- return this;
1211
- }
1212
- /**
1213
- * Update the current color with values stored in an array from the starting index of the given array
1214
- * @param array defines the source array
1215
- * @param offset defines an offset in the source array
1216
- * @returns the current Color4 object
1217
- */
1218
- fromArray(array, offset = 0) {
1219
- Color4.FromArrayToRef(array, offset, this);
1220
- return this;
1221
- }
1222
- /**
1223
- * Determines equality between Color4 objects
1224
- * @param otherColor defines the second operand
1225
- * @returns true if the rgba values are equal to the given ones
1226
- */
1227
- equals(otherColor) {
1228
- return otherColor && this.r === otherColor.r && this.g === otherColor.g && this.b === otherColor.b && this.a === otherColor.a;
1229
- }
1230
- /**
1231
- * Creates a new Color4 set with the added values of the current Color4 and of the given one
1232
- * @param right defines the second operand
1233
- * @returns a new Color4 object
1234
- */
1235
- add(right) {
1236
- return new Color4(this.r + right.r, this.g + right.g, this.b + right.b, this.a + right.a);
1237
- }
1238
- /**
1239
- * Creates a new Color4 set with the subtracted values of the given one from the current Color4
1240
- * @param right defines the second operand
1241
- * @returns a new Color4 object
1242
- */
1243
- subtract(right) {
1244
- return new Color4(this.r - right.r, this.g - right.g, this.b - right.b, this.a - right.a);
1245
- }
1246
- /**
1247
- * Subtracts the given ones from the current Color4 values and stores the results in "result"
1248
- * @param right defines the second operand
1249
- * @param result defines the Color4 object where to store the result
1250
- * @returns the current Color4 object
1251
- */
1252
- subtractToRef(right, result) {
1253
- result.r = this.r - right.r;
1254
- result.g = this.g - right.g;
1255
- result.b = this.b - right.b;
1256
- result.a = this.a - right.a;
1257
- return this;
1258
- }
1259
- /**
1260
- * Creates a new Color4 with the current Color4 values multiplied by scale
1261
- * @param scale defines the scaling factor to apply
1262
- * @returns a new Color4 object
1263
- */
1264
- scale(scale) {
1265
- return new Color4(this.r * scale, this.g * scale, this.b * scale, this.a * scale);
1266
- }
1267
- /**
1268
- * Multiplies the Color4 values by the float "scale"
1269
- * @param scale defines the scaling factor to apply
1270
- * @returns the current updated Color4
1271
- */
1272
- scaleInPlace(scale) {
1273
- this.r *= scale;
1274
- this.g *= scale;
1275
- this.b *= scale;
1276
- this.a *= scale;
1277
- return this;
1278
- }
1279
- /**
1280
- * Multiplies the current Color4 values by scale and stores the result in "result"
1281
- * @param scale defines the scaling factor to apply
1282
- * @param result defines the Color4 object where to store the result
1283
- * @returns the current unmodified Color4
1284
- */
1285
- scaleToRef(scale, result) {
1286
- result.r = this.r * scale;
1287
- result.g = this.g * scale;
1288
- result.b = this.b * scale;
1289
- result.a = this.a * scale;
1290
- return this;
1291
- }
1292
- /**
1293
- * Scale the current Color4 values by a factor and add the result to a given Color4
1294
- * @param scale defines the scale factor
1295
- * @param result defines the Color4 object where to store the result
1296
- * @returns the unmodified current Color4
1297
- */
1298
- scaleAndAddToRef(scale, result) {
1299
- result.r += this.r * scale;
1300
- result.g += this.g * scale;
1301
- result.b += this.b * scale;
1302
- result.a += this.a * scale;
1303
- return this;
1304
- }
1305
- /**
1306
- * Clamps the rgb values by the min and max values and stores the result into "result"
1307
- * @param min defines minimum clamping value (default is 0)
1308
- * @param max defines maximum clamping value (default is 1)
1309
- * @param result defines color to store the result into.
1310
- * @returns the current Color4
1311
- */
1312
- clampToRef(min = 0, max = 1, result) {
1313
- result.r = Scalar.Clamp(this.r, min, max);
1314
- result.g = Scalar.Clamp(this.g, min, max);
1315
- result.b = Scalar.Clamp(this.b, min, max);
1316
- result.a = Scalar.Clamp(this.a, min, max);
1317
- return this;
1318
- }
1319
- /**
1320
- * Multiply an Color4 value by another and return a new Color4 object
1321
- * @param color defines the Color4 value to multiply by
1322
- * @returns a new Color4 object
1323
- */
1324
- multiply(color) {
1325
- return new Color4(this.r * color.r, this.g * color.g, this.b * color.b, this.a * color.a);
1326
- }
1327
- /**
1328
- * Multiply a Color4 value by another and push the result in a reference value
1329
- * @param color defines the Color4 value to multiply by
1330
- * @param result defines the Color4 to fill the result in
1331
- * @returns the result Color4
1332
- */
1333
- multiplyToRef(color, result) {
1334
- result.r = this.r * color.r;
1335
- result.g = this.g * color.g;
1336
- result.b = this.b * color.b;
1337
- result.a = this.a * color.a;
1338
- return result;
1339
- }
1340
- /**
1341
- * Creates a string with the Color4 current values
1342
- * @returns the string representation of the Color4 object
1343
- */
1344
- toString() {
1345
- return "{R: " + this.r + " G:" + this.g + " B:" + this.b + " A:" + this.a + "}";
1346
- }
1347
- /**
1348
- * Returns the string "Color4"
1349
- * @returns "Color4"
1350
- */
1351
- getClassName() {
1352
- return "Color4";
1353
- }
1354
- /**
1355
- * Compute the Color4 hash code
1356
- * @returns an unique number that can be used to hash Color4 objects
1357
- */
1358
- getHashCode() {
1359
- let hash = (this.r * 255) | 0;
1360
- hash = (hash * 397) ^ ((this.g * 255) | 0);
1361
- hash = (hash * 397) ^ ((this.b * 255) | 0);
1362
- hash = (hash * 397) ^ ((this.a * 255) | 0);
1363
- return hash;
1364
- }
1365
- /**
1366
- * Creates a new Color4 copied from the current one
1367
- * @returns a new Color4 object
1368
- */
1369
- clone() {
1370
- return new Color4(this.r, this.g, this.b, this.a);
1371
- }
1372
- /**
1373
- * Copies the given Color4 values into the current one
1374
- * @param source defines the source Color4 object
1375
- * @returns the current updated Color4 object
1376
- */
1377
- copyFrom(source) {
1378
- this.r = source.r;
1379
- this.g = source.g;
1380
- this.b = source.b;
1381
- this.a = source.a;
1382
- return this;
1383
- }
1384
- /**
1385
- * Copies the given float values into the current one
1386
- * @param r defines the red component to read from
1387
- * @param g defines the green component to read from
1388
- * @param b defines the blue component to read from
1389
- * @param a defines the alpha component to read from
1390
- * @returns the current updated Color4 object
1391
- */
1392
- copyFromFloats(r, g, b, a) {
1393
- this.r = r;
1394
- this.g = g;
1395
- this.b = b;
1396
- this.a = a;
1397
- return this;
1398
- }
1399
- /**
1400
- * Copies the given float values into the current one
1401
- * @param r defines the red component to read from
1402
- * @param g defines the green component to read from
1403
- * @param b defines the blue component to read from
1404
- * @param a defines the alpha component to read from
1405
- * @returns the current updated Color4 object
1406
- */
1407
- set(r, g, b, a) {
1408
- return this.copyFromFloats(r, g, b, a);
1409
- }
1410
- /**
1411
- * Compute the Color4 hexadecimal code as a string
1412
- * @param returnAsColor3 defines if the string should only contains RGB values (off by default)
1413
- * @returns a string containing the hexadecimal representation of the Color4 object
1414
- */
1415
- toHexString(returnAsColor3 = false) {
1416
- const intR = Math.round(this.r * 255);
1417
- const intG = Math.round(this.g * 255);
1418
- const intB = Math.round(this.b * 255);
1419
- if (returnAsColor3) {
1420
- return "#" + Scalar.ToHex(intR) + Scalar.ToHex(intG) + Scalar.ToHex(intB);
1421
- }
1422
- const intA = Math.round(this.a * 255);
1423
- return "#" + Scalar.ToHex(intR) + Scalar.ToHex(intG) + Scalar.ToHex(intB) + Scalar.ToHex(intA);
1424
- }
1425
- /**
1426
- * Computes a new Color4 converted from the current one to linear space
1427
- * @param exact defines if the conversion will be done in an exact way which is slower but more accurate (default is false)
1428
- * @returns a new Color4 object
1429
- */
1430
- toLinearSpace(exact = false) {
1431
- const convertedColor = new Color4();
1432
- this.toLinearSpaceToRef(convertedColor, exact);
1433
- return convertedColor;
1434
- }
1435
- /**
1436
- * Converts the Color4 values to linear space and stores the result in "convertedColor"
1437
- * @param convertedColor defines the Color4 object where to store the linear space version
1438
- * @param exact defines if the conversion will be done in an exact way which is slower but more accurate (default is false)
1439
- * @returns the unmodified Color4
1440
- */
1441
- toLinearSpaceToRef(convertedColor, exact = false) {
1442
- if (exact) {
1443
- convertedColor.r = colorChannelToLinearSpaceExact(this.r);
1444
- convertedColor.g = colorChannelToLinearSpaceExact(this.g);
1445
- convertedColor.b = colorChannelToLinearSpaceExact(this.b);
1446
- }
1447
- else {
1448
- convertedColor.r = colorChannelToLinearSpace(this.r);
1449
- convertedColor.g = colorChannelToLinearSpace(this.g);
1450
- convertedColor.b = colorChannelToLinearSpace(this.b);
1451
- }
1452
- convertedColor.a = this.a;
1453
- return this;
1454
- }
1455
- /**
1456
- * Computes a new Color4 converted from the current one to gamma space
1457
- * @param exact defines if the conversion will be done in an exact way which is slower but more accurate (default is false)
1458
- * @returns a new Color4 object
1459
- */
1460
- toGammaSpace(exact = false) {
1461
- const convertedColor = new Color4();
1462
- this.toGammaSpaceToRef(convertedColor, exact);
1463
- return convertedColor;
1464
- }
1465
- /**
1466
- * Converts the Color4 values to gamma space and stores the result in "convertedColor"
1467
- * @param convertedColor defines the Color4 object where to store the gamma space version
1468
- * @param exact defines if the conversion will be done in an exact way which is slower but more accurate (default is false)
1469
- * @returns the unmodified Color4
1470
- */
1471
- toGammaSpaceToRef(convertedColor, exact = false) {
1472
- if (exact) {
1473
- convertedColor.r = colorChannelToGammaSpaceExact(this.r);
1474
- convertedColor.g = colorChannelToGammaSpaceExact(this.g);
1475
- convertedColor.b = colorChannelToGammaSpaceExact(this.b);
1476
- }
1477
- else {
1478
- convertedColor.r = colorChannelToGammaSpace(this.r);
1479
- convertedColor.g = colorChannelToGammaSpace(this.g);
1480
- convertedColor.b = colorChannelToGammaSpace(this.b);
1481
- }
1482
- convertedColor.a = this.a;
1483
- return this;
1484
- }
1485
- // Statics
1486
- /**
1487
- * Creates a new Color4 from the string containing valid hexadecimal values.
1488
- *
1489
- * A valid hex string is either in the format #RRGGBB or #RRGGBBAA.
1490
- *
1491
- * When a hex string without alpha is passed, the resulting Color4 has
1492
- * its alpha value set to 1.0.
1493
- *
1494
- * An invalid string results in a Color with all its channels set to 0.0,
1495
- * i.e. "transparent black".
1496
- *
1497
- * @param hex defines a string containing valid hexadecimal values
1498
- * @returns a new Color4 object
1499
- */
1500
- static FromHexString(hex) {
1501
- if (hex.substring(0, 1) !== "#" || (hex.length !== 9 && hex.length !== 7)) {
1502
- return new Color4(0.0, 0.0, 0.0, 0.0);
1503
- }
1504
- const r = parseInt(hex.substring(1, 3), 16);
1505
- const g = parseInt(hex.substring(3, 5), 16);
1506
- const b = parseInt(hex.substring(5, 7), 16);
1507
- const a = hex.length === 9 ? parseInt(hex.substring(7, 9), 16) : 255;
1508
- return Color4.FromInts(r, g, b, a);
1509
- }
1510
- /**
1511
- * Creates a new Color4 object set with the linearly interpolated values of "amount" between the left Color4 object and the right Color4 object
1512
- * @param left defines the start value
1513
- * @param right defines the end value
1514
- * @param amount defines the gradient factor
1515
- * @returns a new Color4 object
1516
- */
1517
- static Lerp(left, right, amount) {
1518
- const result = new Color4(0.0, 0.0, 0.0, 0.0);
1519
- Color4.LerpToRef(left, right, amount, result);
1520
- return result;
1521
- }
1522
- /**
1523
- * Set the given "result" with the linearly interpolated values of "amount" between the left Color4 object and the right Color4 object
1524
- * @param left defines the start value
1525
- * @param right defines the end value
1526
- * @param amount defines the gradient factor
1527
- * @param result defines the Color4 object where to store data
1528
- */
1529
- static LerpToRef(left, right, amount, result) {
1530
- result.r = left.r + (right.r - left.r) * amount;
1531
- result.g = left.g + (right.g - left.g) * amount;
1532
- result.b = left.b + (right.b - left.b) * amount;
1533
- result.a = left.a + (right.a - left.a) * amount;
1534
- }
1535
- /**
1536
- * Interpolate between two Color4 using Hermite interpolation
1537
- * @param value1 defines first Color4
1538
- * @param tangent1 defines the incoming tangent
1539
- * @param value2 defines second Color4
1540
- * @param tangent2 defines the outgoing tangent
1541
- * @param amount defines the target Color4
1542
- * @returns the new interpolated Color4
1543
- */
1544
- static Hermite(value1, tangent1, value2, tangent2, amount) {
1545
- const squared = amount * amount;
1546
- const cubed = amount * squared;
1547
- const part1 = 2.0 * cubed - 3.0 * squared + 1.0;
1548
- const part2 = -2.0 * cubed + 3.0 * squared;
1549
- const part3 = cubed - 2.0 * squared + amount;
1550
- const part4 = cubed - squared;
1551
- const r = value1.r * part1 + value2.r * part2 + tangent1.r * part3 + tangent2.r * part4;
1552
- const g = value1.g * part1 + value2.g * part2 + tangent1.g * part3 + tangent2.g * part4;
1553
- const b = value1.b * part1 + value2.b * part2 + tangent1.b * part3 + tangent2.b * part4;
1554
- const a = value1.a * part1 + value2.a * part2 + tangent1.a * part3 + tangent2.a * part4;
1555
- return new Color4(r, g, b, a);
1556
- }
1557
- /**
1558
- * Returns a new Color4 which is the 1st derivative of the Hermite spline defined by the colors "value1", "value2", "tangent1", "tangent2".
1559
- * @param value1 defines the first control point
1560
- * @param tangent1 defines the first tangent
1561
- * @param value2 defines the second control point
1562
- * @param tangent2 defines the second tangent
1563
- * @param time define where the derivative must be done
1564
- * @returns 1st derivative
1565
- */
1566
- static Hermite1stDerivative(value1, tangent1, value2, tangent2, time) {
1567
- const result = new Color4();
1568
- this.Hermite1stDerivativeToRef(value1, tangent1, value2, tangent2, time, result);
1569
- return result;
1570
- }
1571
- /**
1572
- * Update a Color4 with the 1st derivative of the Hermite spline defined by the colors "value1", "value2", "tangent1", "tangent2".
1573
- * @param value1 defines the first control point
1574
- * @param tangent1 defines the first tangent
1575
- * @param value2 defines the second control point
1576
- * @param tangent2 defines the second tangent
1577
- * @param time define where the derivative must be done
1578
- * @param result define where to store the derivative
1579
- */
1580
- static Hermite1stDerivativeToRef(value1, tangent1, value2, tangent2, time, result) {
1581
- const t2 = time * time;
1582
- result.r = (t2 - time) * 6 * value1.r + (3 * t2 - 4 * time + 1) * tangent1.r + (-t2 + time) * 6 * value2.r + (3 * t2 - 2 * time) * tangent2.r;
1583
- result.g = (t2 - time) * 6 * value1.g + (3 * t2 - 4 * time + 1) * tangent1.g + (-t2 + time) * 6 * value2.g + (3 * t2 - 2 * time) * tangent2.g;
1584
- result.b = (t2 - time) * 6 * value1.b + (3 * t2 - 4 * time + 1) * tangent1.b + (-t2 + time) * 6 * value2.b + (3 * t2 - 2 * time) * tangent2.b;
1585
- result.a = (t2 - time) * 6 * value1.a + (3 * t2 - 4 * time + 1) * tangent1.a + (-t2 + time) * 6 * value2.a + (3 * t2 - 2 * time) * tangent2.a;
1586
- }
1587
- /**
1588
- * Creates a new Color4 from a Color3 and an alpha value
1589
- * @param color3 defines the source Color3 to read from
1590
- * @param alpha defines the alpha component (1.0 by default)
1591
- * @returns a new Color4 object
1592
- */
1593
- static FromColor3(color3, alpha = 1.0) {
1594
- return new Color4(color3.r, color3.g, color3.b, alpha);
1595
- }
1596
- /**
1597
- * Creates a new Color4 from the starting index element of the given array
1598
- * @param array defines the source array to read from
1599
- * @param offset defines the offset in the source array
1600
- * @returns a new Color4 object
1601
- */
1602
- static FromArray(array, offset = 0) {
1603
- return new Color4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
1604
- }
1605
- /**
1606
- * Creates a new Color4 from the starting index element of the given array
1607
- * @param array defines the source array to read from
1608
- * @param offset defines the offset in the source array
1609
- * @param result defines the target Color4 object
1610
- */
1611
- static FromArrayToRef(array, offset = 0, result) {
1612
- result.r = array[offset];
1613
- result.g = array[offset + 1];
1614
- result.b = array[offset + 2];
1615
- result.a = array[offset + 3];
1616
- }
1617
- /**
1618
- * Creates a new Color3 from integer values (< 256)
1619
- * @param r defines the red component to read from (value between 0 and 255)
1620
- * @param g defines the green component to read from (value between 0 and 255)
1621
- * @param b defines the blue component to read from (value between 0 and 255)
1622
- * @param a defines the alpha component to read from (value between 0 and 255)
1623
- * @returns a new Color3 object
1624
- */
1625
- static FromInts(r, g, b, a) {
1626
- return new Color4(r / 255.0, g / 255.0, b / 255.0, a / 255.0);
1627
- }
1628
- /**
1629
- * Check the content of a given array and convert it to an array containing RGBA data
1630
- * If the original array was already containing count * 4 values then it is returned directly
1631
- * @param colors defines the array to check
1632
- * @param count defines the number of RGBA data to expect
1633
- * @returns an array containing count * 4 values (RGBA)
1634
- */
1635
- static CheckColors4(colors, count) {
1636
- // Check if color3 was used
1637
- if (colors.length === count * 3) {
1638
- const colors4 = [];
1639
- for (let index = 0; index < colors.length; index += 3) {
1640
- const newIndex = (index / 3) * 4;
1641
- colors4[newIndex] = colors[index];
1642
- colors4[newIndex + 1] = colors[index + 1];
1643
- colors4[newIndex + 2] = colors[index + 2];
1644
- colors4[newIndex + 3] = 1.0;
1645
- }
1646
- return colors4;
1647
- }
1648
- return colors;
1649
- }
1650
- }
1651
- /**
1652
- * @internal
1653
- */
1654
- class TmpColors {
1655
- }
1656
- TmpColors.Color3 = ArrayTools.BuildArray(3, Color3.Black);
1657
- TmpColors.Color4 = ArrayTools.BuildArray(3, () => new Color4(0, 0, 0, 0));
1658
- RegisterClass("BABYLON.Color3", Color3);
1659
- RegisterClass("BABYLON.Color4", Color4);
1660
-
1661
- export { ArrayTools as A, Color4 as C, Epsilon as E, GetClass as G, RegisterClass as R, Scalar as S, ToLinearSpace as T, _ObserveArray as _, Color3 as a, ToGammaSpace as b, TmpColors as c };