@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,816 +0,0 @@
1
- import { P as PostProcess, B as BaseTexture } from './helperFunctions.esm.js';
2
- import './pbrBRDFFunctions.esm.js';
3
- import { V as Vector3, T as TmpVectors } from './webRequest.esm.js';
4
- import { a as Color3, S as Scalar, T as ToLinearSpace } from './math.color.esm.js';
5
- import './math.axis.esm.js';
6
- import { e as ShaderStore } from './engine.esm.js';
7
- import './math.path.esm.js';
8
-
9
- /**
10
- * Apply a post process to a texture
11
- * @param postProcessName name of the fragment post process
12
- * @param internalTexture the texture to encode
13
- * @param scene the scene hosting the texture
14
- * @param type type of the output texture. If not provided, use the one from internalTexture
15
- * @param samplingMode sampling mode to use to sample the source texture. If not provided, use the one from internalTexture
16
- * @param format format of the output texture. If not provided, use the one from internalTexture
17
- * @returns a promise with the internalTexture having its texture replaced by the result of the processing
18
- */
19
- function ApplyPostProcess(postProcessName, internalTexture, scene, type, samplingMode, format, width, height) {
20
- // Gets everything ready.
21
- const engine = internalTexture.getEngine();
22
- internalTexture.isReady = false;
23
- samplingMode = samplingMode !== null && samplingMode !== void 0 ? samplingMode : internalTexture.samplingMode;
24
- type = type !== null && type !== void 0 ? type : internalTexture.type;
25
- format = format !== null && format !== void 0 ? format : internalTexture.format;
26
- width = width !== null && width !== void 0 ? width : internalTexture.width;
27
- height = height !== null && height !== void 0 ? height : internalTexture.height;
28
- if (type === -1) {
29
- type = 0;
30
- }
31
- return new Promise((resolve) => {
32
- // Create the post process
33
- const postProcess = new PostProcess("postprocess", postProcessName, null, null, 1, null, samplingMode, engine, false, undefined, type, undefined, null, false, format);
34
- postProcess.externalTextureSamplerBinding = true;
35
- // Hold the output of the decoding.
36
- const encodedTexture = engine.createRenderTargetTexture({ width: width, height: height }, {
37
- generateDepthBuffer: false,
38
- generateMipMaps: false,
39
- generateStencilBuffer: false,
40
- samplingMode,
41
- type,
42
- format,
43
- });
44
- postProcess.getEffect().executeWhenCompiled(() => {
45
- // PP Render Pass
46
- postProcess.onApply = (effect) => {
47
- effect._bindTexture("textureSampler", internalTexture);
48
- effect.setFloat2("scale", 1, 1);
49
- };
50
- scene.postProcessManager.directRender([postProcess], encodedTexture, true);
51
- // Cleanup
52
- engine.restoreDefaultFramebuffer();
53
- engine._releaseTexture(internalTexture);
54
- if (postProcess) {
55
- postProcess.dispose();
56
- }
57
- // Internal Swap
58
- encodedTexture._swapAndDie(internalTexture);
59
- // Ready to get rolling again.
60
- internalTexture.type = type;
61
- internalTexture.format = 5;
62
- internalTexture.isReady = true;
63
- resolve(internalTexture);
64
- });
65
- });
66
- }
67
- // ref: http://stackoverflow.com/questions/32633585/how-do-you-convert-to-half-floats-in-javascript
68
- let floatView;
69
- let int32View;
70
- /**
71
- * Converts a number to half float
72
- * @param value number to convert
73
- * @returns converted number
74
- */
75
- function ToHalfFloat(value) {
76
- if (!floatView) {
77
- floatView = new Float32Array(1);
78
- int32View = new Int32Array(floatView.buffer);
79
- }
80
- floatView[0] = value;
81
- const x = int32View[0];
82
- let bits = (x >> 16) & 0x8000; /* Get the sign */
83
- let m = (x >> 12) & 0x07ff; /* Keep one extra bit for rounding */
84
- const e = (x >> 23) & 0xff; /* Using int is faster here */
85
- /* If zero, or denormal, or exponent underflows too much for a denormal
86
- * half, return signed zero. */
87
- if (e < 103) {
88
- return bits;
89
- }
90
- /* If NaN, return NaN. If Inf or exponent overflow, return Inf. */
91
- if (e > 142) {
92
- bits |= 0x7c00;
93
- /* If exponent was 0xff and one mantissa bit was set, it means NaN,
94
- * not Inf, so make sure we set one mantissa bit too. */
95
- bits |= (e == 255 ? 0 : 1) && x & 0x007fffff;
96
- return bits;
97
- }
98
- /* If exponent underflows but not too much, return a denormal */
99
- if (e < 113) {
100
- m |= 0x0800;
101
- /* Extra rounding may overflow and set mantissa to 0 and exponent
102
- * to 1, which is OK. */
103
- bits |= (m >> (114 - e)) + ((m >> (113 - e)) & 1);
104
- return bits;
105
- }
106
- bits |= ((e - 112) << 10) | (m >> 1);
107
- bits += m & 1;
108
- return bits;
109
- }
110
- /**
111
- * Converts a half float to a number
112
- * @param value half float to convert
113
- * @returns converted half float
114
- */
115
- function FromHalfFloat(value) {
116
- const s = (value & 0x8000) >> 15;
117
- const e = (value & 0x7c00) >> 10;
118
- const f = value & 0x03ff;
119
- if (e === 0) {
120
- return (s ? -1 : 1) * Math.pow(2, -14) * (f / Math.pow(2, 10));
121
- }
122
- else if (e == 0x1f) {
123
- return f ? NaN : (s ? -1 : 1) * Infinity;
124
- }
125
- return (s ? -1 : 1) * Math.pow(2, e - 15) * (1 + f / Math.pow(2, 10));
126
- }
127
-
128
- /* eslint-disable @typescript-eslint/naming-convention */
129
- // https://dickyjim.wordpress.com/2013/09/04/spherical-harmonics-for-beginners/
130
- // http://silviojemma.com/public/papers/lighting/spherical-harmonic-lighting.pdf
131
- // https://www.ppsloan.org/publications/StupidSH36.pdf
132
- // http://cseweb.ucsd.edu/~ravir/papers/envmap/envmap.pdf
133
- // https://www.ppsloan.org/publications/SHJCGT.pdf
134
- // https://www.ppsloan.org/publications/shdering.pdf
135
- // https://google.github.io/filament/Filament.md.html#annex/sphericalharmonics
136
- // https://patapom.com/blog/SHPortal/
137
- // https://imdoingitwrong.wordpress.com/2011/04/14/spherical-harmonics-wtf/
138
- // Using real SH basis:
139
- // m>0 m m
140
- // y = sqrt(2) * K * P * cos(m*phi) * cos(theta)
141
- // l l l
142
- //
143
- // m<0 m |m|
144
- // y = sqrt(2) * K * P * sin(m*phi) * cos(theta)
145
- // l l l
146
- //
147
- // m=0 0 0
148
- // y = K * P * trigono terms
149
- // l l l
150
- //
151
- // m (2l + 1)(l - |m|)!
152
- // K = sqrt(------------------)
153
- // l 4pi(l + |m|)!
154
- //
155
- // and P by recursion:
156
- //
157
- // P00(x) = 1
158
- // P01(x) = x
159
- // Pll(x) = (-1^l)(2l - 1)!!(1-x*x)^(1/2)
160
- // ((2l - 1)x[Pl-1/m]-(l + m - 1)[Pl-2/m])
161
- // Plm(x) = ---------------------------------------
162
- // l - m
163
- // Leaving the trigonometric terms aside we can precompute the constants to :
164
- const SH3ylmBasisConstants = [
165
- Math.sqrt(1 / (4 * Math.PI)),
166
- -Math.sqrt(3 / (4 * Math.PI)),
167
- Math.sqrt(3 / (4 * Math.PI)),
168
- -Math.sqrt(3 / (4 * Math.PI)),
169
- Math.sqrt(15 / (4 * Math.PI)),
170
- -Math.sqrt(15 / (4 * Math.PI)),
171
- Math.sqrt(5 / (16 * Math.PI)),
172
- -Math.sqrt(15 / (4 * Math.PI)),
173
- Math.sqrt(15 / (16 * Math.PI)), // l22
174
- ];
175
- // cm = cos(m * phi)
176
- // sm = sin(m * phi)
177
- // {x,y,z} = {cos(phi)sin(theta), sin(phi)sin(theta), cos(theta)}
178
- // By recursion on using trigo identities:
179
- const SH3ylmBasisTrigonometricTerms = [
180
- () => 1,
181
- (direction) => direction.y,
182
- (direction) => direction.z,
183
- (direction) => direction.x,
184
- (direction) => direction.x * direction.y,
185
- (direction) => direction.y * direction.z,
186
- (direction) => 3 * direction.z * direction.z - 1,
187
- (direction) => direction.x * direction.z,
188
- (direction) => direction.x * direction.x - direction.y * direction.y, // l22
189
- ];
190
- // Wrap the full compute
191
- const applySH3 = (lm, direction) => {
192
- return SH3ylmBasisConstants[lm] * SH3ylmBasisTrigonometricTerms[lm](direction);
193
- };
194
- // Derived from the integration of the a kernel convolution to SH.
195
- // Great explanation here: https://patapom.com/blog/SHPortal/#about-distant-radiance-and-irradiance-environments
196
- const SHCosKernelConvolution = [Math.PI, (2 * Math.PI) / 3, (2 * Math.PI) / 3, (2 * Math.PI) / 3, Math.PI / 4, Math.PI / 4, Math.PI / 4, Math.PI / 4, Math.PI / 4];
197
- /**
198
- * Class representing spherical harmonics coefficients to the 3rd degree
199
- */
200
- class SphericalHarmonics {
201
- constructor() {
202
- /**
203
- * Defines whether or not the harmonics have been prescaled for rendering.
204
- */
205
- this.preScaled = false;
206
- /**
207
- * The l0,0 coefficients of the spherical harmonics
208
- */
209
- this.l00 = Vector3.Zero();
210
- /**
211
- * The l1,-1 coefficients of the spherical harmonics
212
- */
213
- this.l1_1 = Vector3.Zero();
214
- /**
215
- * The l1,0 coefficients of the spherical harmonics
216
- */
217
- this.l10 = Vector3.Zero();
218
- /**
219
- * The l1,1 coefficients of the spherical harmonics
220
- */
221
- this.l11 = Vector3.Zero();
222
- /**
223
- * The l2,-2 coefficients of the spherical harmonics
224
- */
225
- this.l2_2 = Vector3.Zero();
226
- /**
227
- * The l2,-1 coefficients of the spherical harmonics
228
- */
229
- this.l2_1 = Vector3.Zero();
230
- /**
231
- * The l2,0 coefficients of the spherical harmonics
232
- */
233
- this.l20 = Vector3.Zero();
234
- /**
235
- * The l2,1 coefficients of the spherical harmonics
236
- */
237
- this.l21 = Vector3.Zero();
238
- /**
239
- * The l2,2 coefficients of the spherical harmonics
240
- */
241
- this.l22 = Vector3.Zero();
242
- }
243
- /**
244
- * Adds a light to the spherical harmonics
245
- * @param direction the direction of the light
246
- * @param color the color of the light
247
- * @param deltaSolidAngle the delta solid angle of the light
248
- */
249
- addLight(direction, color, deltaSolidAngle) {
250
- TmpVectors.Vector3[0].set(color.r, color.g, color.b);
251
- const colorVector = TmpVectors.Vector3[0];
252
- const c = TmpVectors.Vector3[1];
253
- colorVector.scaleToRef(deltaSolidAngle, c);
254
- c.scaleToRef(applySH3(0, direction), TmpVectors.Vector3[2]);
255
- this.l00.addInPlace(TmpVectors.Vector3[2]);
256
- c.scaleToRef(applySH3(1, direction), TmpVectors.Vector3[2]);
257
- this.l1_1.addInPlace(TmpVectors.Vector3[2]);
258
- c.scaleToRef(applySH3(2, direction), TmpVectors.Vector3[2]);
259
- this.l10.addInPlace(TmpVectors.Vector3[2]);
260
- c.scaleToRef(applySH3(3, direction), TmpVectors.Vector3[2]);
261
- this.l11.addInPlace(TmpVectors.Vector3[2]);
262
- c.scaleToRef(applySH3(4, direction), TmpVectors.Vector3[2]);
263
- this.l2_2.addInPlace(TmpVectors.Vector3[2]);
264
- c.scaleToRef(applySH3(5, direction), TmpVectors.Vector3[2]);
265
- this.l2_1.addInPlace(TmpVectors.Vector3[2]);
266
- c.scaleToRef(applySH3(6, direction), TmpVectors.Vector3[2]);
267
- this.l20.addInPlace(TmpVectors.Vector3[2]);
268
- c.scaleToRef(applySH3(7, direction), TmpVectors.Vector3[2]);
269
- this.l21.addInPlace(TmpVectors.Vector3[2]);
270
- c.scaleToRef(applySH3(8, direction), TmpVectors.Vector3[2]);
271
- this.l22.addInPlace(TmpVectors.Vector3[2]);
272
- }
273
- /**
274
- * Scales the spherical harmonics by the given amount
275
- * @param scale the amount to scale
276
- */
277
- scaleInPlace(scale) {
278
- this.l00.scaleInPlace(scale);
279
- this.l1_1.scaleInPlace(scale);
280
- this.l10.scaleInPlace(scale);
281
- this.l11.scaleInPlace(scale);
282
- this.l2_2.scaleInPlace(scale);
283
- this.l2_1.scaleInPlace(scale);
284
- this.l20.scaleInPlace(scale);
285
- this.l21.scaleInPlace(scale);
286
- this.l22.scaleInPlace(scale);
287
- }
288
- /**
289
- * Convert from incident radiance (Li) to irradiance (E) by applying convolution with the cosine-weighted hemisphere.
290
- *
291
- * ```
292
- * E_lm = A_l * L_lm
293
- * ```
294
- *
295
- * In spherical harmonics this convolution amounts to scaling factors for each frequency band.
296
- * This corresponds to equation 5 in "An Efficient Representation for Irradiance Environment Maps", where
297
- * the scaling factors are given in equation 9.
298
- */
299
- convertIncidentRadianceToIrradiance() {
300
- // Constant (Band 0)
301
- this.l00.scaleInPlace(SHCosKernelConvolution[0]);
302
- // Linear (Band 1)
303
- this.l1_1.scaleInPlace(SHCosKernelConvolution[1]);
304
- this.l10.scaleInPlace(SHCosKernelConvolution[2]);
305
- this.l11.scaleInPlace(SHCosKernelConvolution[3]);
306
- // Quadratic (Band 2)
307
- this.l2_2.scaleInPlace(SHCosKernelConvolution[4]);
308
- this.l2_1.scaleInPlace(SHCosKernelConvolution[5]);
309
- this.l20.scaleInPlace(SHCosKernelConvolution[6]);
310
- this.l21.scaleInPlace(SHCosKernelConvolution[7]);
311
- this.l22.scaleInPlace(SHCosKernelConvolution[8]);
312
- }
313
- /**
314
- * Convert from irradiance to outgoing radiance for Lambertian BDRF, suitable for efficient shader evaluation.
315
- *
316
- * ```
317
- * L = (1/pi) * E * rho
318
- * ```
319
- *
320
- * This is done by an additional scale by 1/pi, so is a fairly trivial operation but important conceptually.
321
- */
322
- convertIrradianceToLambertianRadiance() {
323
- this.scaleInPlace(1.0 / Math.PI);
324
- // The resultant SH now represents outgoing radiance, so includes the Lambert 1/pi normalisation factor but without albedo (rho) applied
325
- // (The pixel shader must apply albedo after texture fetches, etc).
326
- }
327
- /**
328
- * Integrates the reconstruction coefficients directly in to the SH preventing further
329
- * required operations at run time.
330
- *
331
- * This is simply done by scaling back the SH with Ylm constants parameter.
332
- * The trigonometric part being applied by the shader at run time.
333
- */
334
- preScaleForRendering() {
335
- this.preScaled = true;
336
- this.l00.scaleInPlace(SH3ylmBasisConstants[0]);
337
- this.l1_1.scaleInPlace(SH3ylmBasisConstants[1]);
338
- this.l10.scaleInPlace(SH3ylmBasisConstants[2]);
339
- this.l11.scaleInPlace(SH3ylmBasisConstants[3]);
340
- this.l2_2.scaleInPlace(SH3ylmBasisConstants[4]);
341
- this.l2_1.scaleInPlace(SH3ylmBasisConstants[5]);
342
- this.l20.scaleInPlace(SH3ylmBasisConstants[6]);
343
- this.l21.scaleInPlace(SH3ylmBasisConstants[7]);
344
- this.l22.scaleInPlace(SH3ylmBasisConstants[8]);
345
- }
346
- /**
347
- * update the spherical harmonics coefficients from the given array
348
- * @param data defines the 9x3 coefficients (l00, l1-1, l10, l11, l2-2, l2-1, l20, l21, l22)
349
- * @returns the spherical harmonics (this)
350
- */
351
- updateFromArray(data) {
352
- Vector3.FromArrayToRef(data[0], 0, this.l00);
353
- Vector3.FromArrayToRef(data[1], 0, this.l1_1);
354
- Vector3.FromArrayToRef(data[2], 0, this.l10);
355
- Vector3.FromArrayToRef(data[3], 0, this.l11);
356
- Vector3.FromArrayToRef(data[4], 0, this.l2_2);
357
- Vector3.FromArrayToRef(data[5], 0, this.l2_1);
358
- Vector3.FromArrayToRef(data[6], 0, this.l20);
359
- Vector3.FromArrayToRef(data[7], 0, this.l21);
360
- Vector3.FromArrayToRef(data[8], 0, this.l22);
361
- return this;
362
- }
363
- /**
364
- * update the spherical harmonics coefficients from the given floats array
365
- * @param data defines the 9x3 coefficients (l00, l1-1, l10, l11, l2-2, l2-1, l20, l21, l22)
366
- * @returns the spherical harmonics (this)
367
- */
368
- updateFromFloatsArray(data) {
369
- Vector3.FromFloatsToRef(data[0], data[1], data[2], this.l00);
370
- Vector3.FromFloatsToRef(data[3], data[4], data[5], this.l1_1);
371
- Vector3.FromFloatsToRef(data[6], data[7], data[8], this.l10);
372
- Vector3.FromFloatsToRef(data[9], data[10], data[11], this.l11);
373
- Vector3.FromFloatsToRef(data[12], data[13], data[14], this.l2_2);
374
- Vector3.FromFloatsToRef(data[15], data[16], data[17], this.l2_1);
375
- Vector3.FromFloatsToRef(data[18], data[19], data[20], this.l20);
376
- Vector3.FromFloatsToRef(data[21], data[22], data[23], this.l21);
377
- Vector3.FromFloatsToRef(data[24], data[25], data[26], this.l22);
378
- return this;
379
- }
380
- /**
381
- * Constructs a spherical harmonics from an array.
382
- * @param data defines the 9x3 coefficients (l00, l1-1, l10, l11, l2-2, l2-1, l20, l21, l22)
383
- * @returns the spherical harmonics
384
- */
385
- static FromArray(data) {
386
- const sh = new SphericalHarmonics();
387
- return sh.updateFromArray(data);
388
- }
389
- // Keep for references.
390
- /**
391
- * Gets the spherical harmonics from polynomial
392
- * @param polynomial the spherical polynomial
393
- * @returns the spherical harmonics
394
- */
395
- static FromPolynomial(polynomial) {
396
- const result = new SphericalHarmonics();
397
- result.l00 = polynomial.xx.scale(0.376127).add(polynomial.yy.scale(0.376127)).add(polynomial.zz.scale(0.376126));
398
- result.l1_1 = polynomial.y.scale(0.977204);
399
- result.l10 = polynomial.z.scale(0.977204);
400
- result.l11 = polynomial.x.scale(0.977204);
401
- result.l2_2 = polynomial.xy.scale(1.16538);
402
- result.l2_1 = polynomial.yz.scale(1.16538);
403
- result.l20 = polynomial.zz.scale(1.34567).subtract(polynomial.xx.scale(0.672834)).subtract(polynomial.yy.scale(0.672834));
404
- result.l21 = polynomial.zx.scale(1.16538);
405
- result.l22 = polynomial.xx.scale(1.16538).subtract(polynomial.yy.scale(1.16538));
406
- result.l1_1.scaleInPlace(-1);
407
- result.l11.scaleInPlace(-1);
408
- result.l2_1.scaleInPlace(-1);
409
- result.l21.scaleInPlace(-1);
410
- result.scaleInPlace(Math.PI);
411
- return result;
412
- }
413
- }
414
- /**
415
- * Class representing spherical polynomial coefficients to the 3rd degree
416
- */
417
- class SphericalPolynomial {
418
- constructor() {
419
- /**
420
- * The x coefficients of the spherical polynomial
421
- */
422
- this.x = Vector3.Zero();
423
- /**
424
- * The y coefficients of the spherical polynomial
425
- */
426
- this.y = Vector3.Zero();
427
- /**
428
- * The z coefficients of the spherical polynomial
429
- */
430
- this.z = Vector3.Zero();
431
- /**
432
- * The xx coefficients of the spherical polynomial
433
- */
434
- this.xx = Vector3.Zero();
435
- /**
436
- * The yy coefficients of the spherical polynomial
437
- */
438
- this.yy = Vector3.Zero();
439
- /**
440
- * The zz coefficients of the spherical polynomial
441
- */
442
- this.zz = Vector3.Zero();
443
- /**
444
- * The xy coefficients of the spherical polynomial
445
- */
446
- this.xy = Vector3.Zero();
447
- /**
448
- * The yz coefficients of the spherical polynomial
449
- */
450
- this.yz = Vector3.Zero();
451
- /**
452
- * The zx coefficients of the spherical polynomial
453
- */
454
- this.zx = Vector3.Zero();
455
- }
456
- /**
457
- * The spherical harmonics used to create the polynomials.
458
- */
459
- get preScaledHarmonics() {
460
- if (!this._harmonics) {
461
- this._harmonics = SphericalHarmonics.FromPolynomial(this);
462
- }
463
- if (!this._harmonics.preScaled) {
464
- this._harmonics.preScaleForRendering();
465
- }
466
- return this._harmonics;
467
- }
468
- /**
469
- * Adds an ambient color to the spherical polynomial
470
- * @param color the color to add
471
- */
472
- addAmbient(color) {
473
- TmpVectors.Vector3[0].copyFromFloats(color.r, color.g, color.b);
474
- const colorVector = TmpVectors.Vector3[0];
475
- this.xx.addInPlace(colorVector);
476
- this.yy.addInPlace(colorVector);
477
- this.zz.addInPlace(colorVector);
478
- }
479
- /**
480
- * Scales the spherical polynomial by the given amount
481
- * @param scale the amount to scale
482
- */
483
- scaleInPlace(scale) {
484
- this.x.scaleInPlace(scale);
485
- this.y.scaleInPlace(scale);
486
- this.z.scaleInPlace(scale);
487
- this.xx.scaleInPlace(scale);
488
- this.yy.scaleInPlace(scale);
489
- this.zz.scaleInPlace(scale);
490
- this.yz.scaleInPlace(scale);
491
- this.zx.scaleInPlace(scale);
492
- this.xy.scaleInPlace(scale);
493
- }
494
- /**
495
- * Updates the spherical polynomial from harmonics
496
- * @param harmonics the spherical harmonics
497
- * @returns the spherical polynomial
498
- */
499
- updateFromHarmonics(harmonics) {
500
- this._harmonics = harmonics;
501
- this.x.copyFrom(harmonics.l11);
502
- this.x.scaleInPlace(1.02333).scaleInPlace(-1);
503
- this.y.copyFrom(harmonics.l1_1);
504
- this.y.scaleInPlace(1.02333).scaleInPlace(-1);
505
- this.z.copyFrom(harmonics.l10);
506
- this.z.scaleInPlace(1.02333);
507
- this.xx.copyFrom(harmonics.l00);
508
- TmpVectors.Vector3[0].copyFrom(harmonics.l20).scaleInPlace(0.247708);
509
- TmpVectors.Vector3[1].copyFrom(harmonics.l22).scaleInPlace(0.429043);
510
- this.xx.scaleInPlace(0.886277).subtractInPlace(TmpVectors.Vector3[0]).addInPlace(TmpVectors.Vector3[1]);
511
- this.yy.copyFrom(harmonics.l00);
512
- this.yy.scaleInPlace(0.886277).subtractInPlace(TmpVectors.Vector3[0]).subtractInPlace(TmpVectors.Vector3[1]);
513
- this.zz.copyFrom(harmonics.l00);
514
- TmpVectors.Vector3[0].copyFrom(harmonics.l20).scaleInPlace(0.495417);
515
- this.zz.scaleInPlace(0.886277).addInPlace(TmpVectors.Vector3[0]);
516
- this.yz.copyFrom(harmonics.l2_1);
517
- this.yz.scaleInPlace(0.858086).scaleInPlace(-1);
518
- this.zx.copyFrom(harmonics.l21);
519
- this.zx.scaleInPlace(0.858086).scaleInPlace(-1);
520
- this.xy.copyFrom(harmonics.l2_2);
521
- this.xy.scaleInPlace(0.858086);
522
- this.scaleInPlace(1.0 / Math.PI);
523
- return this;
524
- }
525
- /**
526
- * Gets the spherical polynomial from harmonics
527
- * @param harmonics the spherical harmonics
528
- * @returns the spherical polynomial
529
- */
530
- static FromHarmonics(harmonics) {
531
- const result = new SphericalPolynomial();
532
- return result.updateFromHarmonics(harmonics);
533
- }
534
- /**
535
- * Constructs a spherical polynomial from an array.
536
- * @param data defines the 9x3 coefficients (x, y, z, xx, yy, zz, yz, zx, xy)
537
- * @returns the spherical polynomial
538
- */
539
- static FromArray(data) {
540
- const sp = new SphericalPolynomial();
541
- Vector3.FromArrayToRef(data[0], 0, sp.x);
542
- Vector3.FromArrayToRef(data[1], 0, sp.y);
543
- Vector3.FromArrayToRef(data[2], 0, sp.z);
544
- Vector3.FromArrayToRef(data[3], 0, sp.xx);
545
- Vector3.FromArrayToRef(data[4], 0, sp.yy);
546
- Vector3.FromArrayToRef(data[5], 0, sp.zz);
547
- Vector3.FromArrayToRef(data[6], 0, sp.yz);
548
- Vector3.FromArrayToRef(data[7], 0, sp.zx);
549
- Vector3.FromArrayToRef(data[8], 0, sp.xy);
550
- return sp;
551
- }
552
- }
553
-
554
- class FileFaceOrientation {
555
- constructor(name, worldAxisForNormal, worldAxisForFileX, worldAxisForFileY) {
556
- this.name = name;
557
- this.worldAxisForNormal = worldAxisForNormal;
558
- this.worldAxisForFileX = worldAxisForFileX;
559
- this.worldAxisForFileY = worldAxisForFileY;
560
- }
561
- }
562
- /**
563
- * Helper class dealing with the extraction of spherical polynomial dataArray
564
- * from a cube map.
565
- */
566
- class CubeMapToSphericalPolynomialTools {
567
- /**
568
- * Converts a texture to the according Spherical Polynomial data.
569
- * This extracts the first 3 orders only as they are the only one used in the lighting.
570
- *
571
- * @param texture The texture to extract the information from.
572
- * @returns The Spherical Polynomial data.
573
- */
574
- static ConvertCubeMapTextureToSphericalPolynomial(texture) {
575
- var _a;
576
- if (!texture.isCube) {
577
- // Only supports cube Textures currently.
578
- return null;
579
- }
580
- (_a = texture.getScene()) === null || _a === void 0 ? void 0 : _a.getEngine().flushFramebuffer();
581
- const size = texture.getSize().width;
582
- const rightPromise = texture.readPixels(0, undefined, undefined, false);
583
- const leftPromise = texture.readPixels(1, undefined, undefined, false);
584
- let upPromise;
585
- let downPromise;
586
- if (texture.isRenderTarget) {
587
- upPromise = texture.readPixels(3, undefined, undefined, false);
588
- downPromise = texture.readPixels(2, undefined, undefined, false);
589
- }
590
- else {
591
- upPromise = texture.readPixels(2, undefined, undefined, false);
592
- downPromise = texture.readPixels(3, undefined, undefined, false);
593
- }
594
- const frontPromise = texture.readPixels(4, undefined, undefined, false);
595
- const backPromise = texture.readPixels(5, undefined, undefined, false);
596
- const gammaSpace = texture.gammaSpace;
597
- // Always read as RGBA.
598
- const format = 5;
599
- let type = 0;
600
- if (texture.textureType == 1 || texture.textureType == 2) {
601
- type = 1;
602
- }
603
- return new Promise((resolve) => {
604
- Promise.all([leftPromise, rightPromise, upPromise, downPromise, frontPromise, backPromise]).then(([left, right, up, down, front, back]) => {
605
- const cubeInfo = {
606
- size,
607
- right,
608
- left,
609
- up,
610
- down,
611
- front,
612
- back,
613
- format,
614
- type,
615
- gammaSpace,
616
- };
617
- resolve(this.ConvertCubeMapToSphericalPolynomial(cubeInfo));
618
- });
619
- });
620
- }
621
- /**
622
- * Compute the area on the unit sphere of the rectangle defined by (x,y) and the origin
623
- * See https://www.rorydriscoll.com/2012/01/15/cubemap-texel-solid-angle/
624
- * @param x
625
- * @param y
626
- */
627
- static _AreaElement(x, y) {
628
- return Math.atan2(x * y, Math.sqrt(x * x + y * y + 1));
629
- }
630
- /**
631
- * Converts a cubemap to the according Spherical Polynomial data.
632
- * This extracts the first 3 orders only as they are the only one used in the lighting.
633
- *
634
- * @param cubeInfo The Cube map to extract the information from.
635
- * @returns The Spherical Polynomial data.
636
- */
637
- static ConvertCubeMapToSphericalPolynomial(cubeInfo) {
638
- const sphericalHarmonics = new SphericalHarmonics();
639
- let totalSolidAngle = 0.0;
640
- // The (u,v) range is [-1,+1], so the distance between each texel is 2/Size.
641
- const du = 2.0 / cubeInfo.size;
642
- const dv = du;
643
- const halfTexel = 0.5 * du;
644
- // The (u,v) of the first texel is half a texel from the corner (-1,-1).
645
- const minUV = halfTexel - 1.0;
646
- for (let faceIndex = 0; faceIndex < 6; faceIndex++) {
647
- const fileFace = this._FileFaces[faceIndex];
648
- const dataArray = cubeInfo[fileFace.name];
649
- let v = minUV;
650
- // TODO: we could perform the summation directly into a SphericalPolynomial (SP), which is more efficient than SphericalHarmonic (SH).
651
- // This is possible because during the summation we do not need the SH-specific properties, e.g. orthogonality.
652
- // Because SP is still linear, so summation is fine in that basis.
653
- const stride = cubeInfo.format === 5 ? 4 : 3;
654
- for (let y = 0; y < cubeInfo.size; y++) {
655
- let u = minUV;
656
- for (let x = 0; x < cubeInfo.size; x++) {
657
- // World direction (not normalised)
658
- const worldDirection = fileFace.worldAxisForFileX.scale(u).add(fileFace.worldAxisForFileY.scale(v)).add(fileFace.worldAxisForNormal);
659
- worldDirection.normalize();
660
- const deltaSolidAngle = this._AreaElement(u - halfTexel, v - halfTexel) -
661
- this._AreaElement(u - halfTexel, v + halfTexel) -
662
- this._AreaElement(u + halfTexel, v - halfTexel) +
663
- this._AreaElement(u + halfTexel, v + halfTexel);
664
- let r = dataArray[y * cubeInfo.size * stride + x * stride + 0];
665
- let g = dataArray[y * cubeInfo.size * stride + x * stride + 1];
666
- let b = dataArray[y * cubeInfo.size * stride + x * stride + 2];
667
- // Prevent NaN harmonics with extreme HDRI data.
668
- if (isNaN(r)) {
669
- r = 0;
670
- }
671
- if (isNaN(g)) {
672
- g = 0;
673
- }
674
- if (isNaN(b)) {
675
- b = 0;
676
- }
677
- // Handle Integer types.
678
- if (cubeInfo.type === 0) {
679
- r /= 255;
680
- g /= 255;
681
- b /= 255;
682
- }
683
- // Handle Gamma space textures.
684
- if (cubeInfo.gammaSpace) {
685
- r = Math.pow(Scalar.Clamp(r), ToLinearSpace);
686
- g = Math.pow(Scalar.Clamp(g), ToLinearSpace);
687
- b = Math.pow(Scalar.Clamp(b), ToLinearSpace);
688
- }
689
- // Prevent to explode in case of really high dynamic ranges.
690
- // sh 3 would not be enough to accurately represent it.
691
- const max = this.MAX_HDRI_VALUE;
692
- if (this.PRESERVE_CLAMPED_COLORS) {
693
- const currentMax = Math.max(r, g, b);
694
- if (currentMax > max) {
695
- const factor = max / currentMax;
696
- r *= factor;
697
- g *= factor;
698
- b *= factor;
699
- }
700
- }
701
- else {
702
- r = Scalar.Clamp(r, 0, max);
703
- g = Scalar.Clamp(g, 0, max);
704
- b = Scalar.Clamp(b, 0, max);
705
- }
706
- const color = new Color3(r, g, b);
707
- sphericalHarmonics.addLight(worldDirection, color, deltaSolidAngle);
708
- totalSolidAngle += deltaSolidAngle;
709
- u += du;
710
- }
711
- v += dv;
712
- }
713
- }
714
- // Solid angle for entire sphere is 4*pi
715
- const sphereSolidAngle = 4.0 * Math.PI;
716
- // Adjust the solid angle to allow for how many faces we processed.
717
- const facesProcessed = 6.0;
718
- const expectedSolidAngle = (sphereSolidAngle * facesProcessed) / 6.0;
719
- // Adjust the harmonics so that the accumulated solid angle matches the expected solid angle.
720
- // This is needed because the numerical integration over the cube uses a
721
- // small angle approximation of solid angle for each texel (see deltaSolidAngle),
722
- // and also to compensate for accumulative error due to float precision in the summation.
723
- const correctionFactor = expectedSolidAngle / totalSolidAngle;
724
- sphericalHarmonics.scaleInPlace(correctionFactor);
725
- sphericalHarmonics.convertIncidentRadianceToIrradiance();
726
- sphericalHarmonics.convertIrradianceToLambertianRadiance();
727
- return SphericalPolynomial.FromHarmonics(sphericalHarmonics);
728
- }
729
- }
730
- CubeMapToSphericalPolynomialTools._FileFaces = [
731
- new FileFaceOrientation("right", new Vector3(1, 0, 0), new Vector3(0, 0, -1), new Vector3(0, -1, 0)),
732
- new FileFaceOrientation("left", new Vector3(-1, 0, 0), new Vector3(0, 0, 1), new Vector3(0, -1, 0)),
733
- new FileFaceOrientation("up", new Vector3(0, 1, 0), new Vector3(1, 0, 0), new Vector3(0, 0, 1)),
734
- new FileFaceOrientation("down", new Vector3(0, -1, 0), new Vector3(1, 0, 0), new Vector3(0, 0, -1)),
735
- new FileFaceOrientation("front", new Vector3(0, 0, 1), new Vector3(1, 0, 0), new Vector3(0, -1, 0)),
736
- new FileFaceOrientation("back", new Vector3(0, 0, -1), new Vector3(-1, 0, 0), new Vector3(0, -1, 0)), // -Z bottom
737
- ];
738
- /** @internal */
739
- CubeMapToSphericalPolynomialTools.MAX_HDRI_VALUE = 4096;
740
- /** @internal */
741
- CubeMapToSphericalPolynomialTools.PRESERVE_CLAMPED_COLORS = false;
742
-
743
- BaseTexture.prototype.forceSphericalPolynomialsRecompute = function () {
744
- if (this._texture) {
745
- this._texture._sphericalPolynomial = null;
746
- this._texture._sphericalPolynomialPromise = null;
747
- this._texture._sphericalPolynomialComputed = false;
748
- }
749
- };
750
- Object.defineProperty(BaseTexture.prototype, "sphericalPolynomial", {
751
- get: function () {
752
- if (this._texture) {
753
- if (this._texture._sphericalPolynomial || this._texture._sphericalPolynomialComputed) {
754
- return this._texture._sphericalPolynomial;
755
- }
756
- if (this._texture.isReady) {
757
- if (!this._texture._sphericalPolynomialPromise) {
758
- this._texture._sphericalPolynomialPromise = CubeMapToSphericalPolynomialTools.ConvertCubeMapTextureToSphericalPolynomial(this);
759
- if (this._texture._sphericalPolynomialPromise === null) {
760
- this._texture._sphericalPolynomialComputed = true;
761
- }
762
- else {
763
- this._texture._sphericalPolynomialPromise.then((sphericalPolynomial) => {
764
- this._texture._sphericalPolynomial = sphericalPolynomial;
765
- this._texture._sphericalPolynomialComputed = true;
766
- });
767
- }
768
- }
769
- return null;
770
- }
771
- }
772
- return null;
773
- },
774
- set: function (value) {
775
- if (this._texture) {
776
- this._texture._sphericalPolynomial = value;
777
- }
778
- },
779
- enumerable: true,
780
- configurable: true,
781
- });
782
-
783
- // Do not edit.
784
- const name$1 = "importanceSampling";
785
- const shader$1 = `vec3 hemisphereCosSample(vec2 u) {
786
- // Sideeffect
787
- ShaderStore.IncludesShadersStore[name$1] = shader$1;
788
-
789
- // Do not edit.
790
- const name = "hdrFilteringFunctions";
791
- const shader = `#ifdef NUM_SAMPLES
792
- #if NUM_SAMPLES>0
793
- #if defined(WEBGL2) || defined(WEBGPU) || defined(NATIVE)
794
- float radicalInverse_VdC(uint bits)
795
- float vanDerCorpus(int n,int base)
796
- float log4(float x) {
797
- vec3 irradiance(samplerCube inputTexture,vec3 inputN,vec2 filteringInfo)
798
- for(uint i=0u; i<NUM_SAMPLES; ++i)
799
- for(int i=0; i<NUM_SAMPLES; ++i)
800
- {
801
- c=toLinearSpace(c);
802
- result+=c;
803
- vec3 radiance(float alphaG,samplerCube inputTexture,vec3 inputN,vec2 filteringInfo)
804
- c=toLinearSpace(c);
805
- return c;
806
- for(uint i=0u; i<NUM_SAMPLES; ++i)
807
- for(int i=0; i<NUM_SAMPLES; ++i)
808
- {
809
- c=toLinearSpace(c);
810
- result+=c*NoL;
811
- #endif
812
- `;
813
- // Sideeffect
814
- ShaderStore.IncludesShadersStore[name] = shader;
815
-
816
- export { ApplyPostProcess as A, CubeMapToSphericalPolynomialTools as C, FromHalfFloat as F, SphericalPolynomial as S, ToHalfFloat as T, SphericalHarmonics as a };