-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
scene.js
914 lines (790 loc) · 26.6 KB
/
scene.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
import { Debug } from '../core/debug.js';
import { EventHandler } from '../core/event-handler.js';
import { Color } from '../core/math/color.js';
import { Vec3 } from '../core/math/vec3.js';
import { Quat } from '../core/math/quat.js';
import { math } from '../core/math/math.js';
import { Mat3 } from '../core/math/mat3.js';
import { Mat4 } from '../core/math/mat4.js';
import { GraphicsDeviceAccess } from '../platform/graphics/graphics-device-access.js';
import { PIXELFORMAT_RGBA8, ADDRESS_CLAMP_TO_EDGE, FILTER_LINEAR } from '../platform/graphics/constants.js';
import { BAKE_COLORDIR, FOG_NONE, GAMMA_SRGB, LAYERID_IMMEDIATE } from './constants.js';
import { Sky } from './sky.js';
import { LightingParams } from './lighting/lighting-params.js';
import { Immediate } from './immediate/immediate.js';
import { EnvLighting } from './graphics/env-lighting.js';
/**
* A scene is graphical representation of an environment. It manages the scene hierarchy, all
* graphical objects, lights, and scene-wide properties.
*
* @augments EventHandler
* @category Graphics
*/
class Scene extends EventHandler {
/**
* If enabled, the ambient lighting will be baked into lightmaps. This will be either the
* {@link Scene#skybox} if set up, otherwise {@link Scene#ambientLight}. Defaults to false.
*
* @type {boolean}
*/
ambientBake = false;
/**
* If {@link Scene#ambientBake} is true, this specifies the brightness of ambient occlusion.
* Typical range is -1 to 1. Defaults to 0, representing no change to brightness.
*
* @type {number}
*/
ambientBakeOcclusionBrightness = 0;
/**
* If {@link Scene#ambientBake} is true, this specifies the contrast of ambient occlusion.
* Typical range is -1 to 1. Defaults to 0, representing no change to contrast.
*
* @type {number}
*/
ambientBakeOcclusionContrast = 0;
/**
* The color of the scene's ambient light. Defaults to black (0, 0, 0).
*
* @type {Color}
*/
ambientLight = new Color(0, 0, 0);
/**
* The luminosity of the scene's ambient light in lux (lm/m^2). Used if physicalUnits is true. Defaults to 0.
*
* @type {number}
*/
ambientLuminance = 0;
/**
* The exposure value tweaks the overall brightness of the scene. Ignored if physicalUnits is true. Defaults to 1.
*
* @type {number}
*/
exposure = 1;
/**
* The color of the fog (if enabled). Defaults to black (0, 0, 0).
*
* @type {Color}
*/
fogColor = new Color(0, 0, 0);
/**
* The density of the fog (if enabled). This property is only valid if the fog property is set
* to {@link FOG_EXP} or {@link FOG_EXP2}. Defaults to 0.
*
* @type {number}
*/
fogDensity = 0;
/**
* The distance from the viewpoint where linear fog reaches its maximum. This property is only
* valid if the fog property is set to {@link FOG_LINEAR}. Defaults to 1000.
*
* @type {number}
*/
fogEnd = 1000;
/**
* The distance from the viewpoint where linear fog begins. This property is only valid if the
* fog property is set to {@link FOG_LINEAR}. Defaults to 1.
*
* @type {number}
*/
fogStart = 1;
/**
* The lightmap resolution multiplier. Defaults to 1.
*
* @type {number}
*/
lightmapSizeMultiplier = 1;
/**
* The maximum lightmap resolution. Defaults to 2048.
*
* @type {number}
*/
lightmapMaxResolution = 2048;
/**
* The lightmap baking mode. Can be:
*
* - {@link BAKE_COLOR}: single color lightmap
* - {@link BAKE_COLORDIR}: single color lightmap + dominant light direction (used for bump or
* specular). Only lights with bakeDir=true will be used for generating the dominant light
* direction.
*
* Defaults to {@link BAKE_COLORDIR}.
*
* @type {number}
*/
lightmapMode = BAKE_COLORDIR;
/**
* Enables bilateral filter on runtime baked color lightmaps, which removes the noise and
* banding while preserving the edges. Defaults to false. Note that the filtering takes place
* in the image space of the lightmap, and it does not filter across lightmap UV space seams,
* often making the seams more visible. It's important to balance the strength of the filter
* with number of samples used for lightmap baking to limit the visible artifacts.
*
* @type {boolean}
*/
lightmapFilterEnabled = false;
/**
* Enables HDR lightmaps. This can result in smoother lightmaps especially when many samples
* are used. Defaults to false.
*
* @type {boolean}
*/
lightmapHDR = false;
/**
* The root entity of the scene, which is usually the only child to the {@link Application}
* root entity.
*
* @type {import('../framework/entity.js').Entity}
*/
root = null;
/**
* The sky of the scene.
*
* @type {Sky}
* @ignore
*/
sky = null;
/**
* @type {boolean}
* @private
*/
_skyboxProjectionEnabled = false;
/**
* @type {Vec3}
* @private
*/
_skyboxProjectionCenter = new Vec3();
/**
* @type {number}
* @private
*/
_skyboxProjectionElevation = 0;
/**
* @type {number}
* @private
*/
_skyboxProjectionRadius = 100;
/**
* Use physically based units for cameras and lights. When used, the exposure value is ignored.
*
* @type {boolean}
*/
physicalUnits = false;
/**
* Environment lighting atlas
*
* @type {import('../platform/graphics/texture.js').Texture|null}
* @private
*/
_envAtlas = null;
/**
* The skybox cubemap as set by user (gets used when skyboxMip === 0)
*
* @type {import('../platform/graphics/texture.js').Texture|null}
* @private
*/
_skyboxCubeMap = null;
/**
* Create a new Scene instance.
*
* @param {import('../platform/graphics/graphics-device.js').GraphicsDevice} graphicsDevice -
* The graphics device used to manage this scene.
* @hideconstructor
*/
constructor(graphicsDevice) {
super();
Debug.assertDeprecated(graphicsDevice, "Scene constructor takes a GraphicsDevice as a parameter, and it was not provided.");
this.device = graphicsDevice || GraphicsDeviceAccess.get();
this._gravity = new Vec3(0, -9.8, 0);
/**
* @type {import('./composition/layer-composition.js').LayerComposition}
* @private
*/
this._layers = null;
this._fog = FOG_NONE;
this._gammaCorrection = GAMMA_SRGB;
this._toneMapping = 0;
/**
* Array of 6 prefiltered lighting data cubemaps.
*
* @type {import('../platform/graphics/texture.js').Texture[]}
* @private
*/
this._prefilteredCubemaps = [];
// internally generated envAtlas owned by the scene
this._internalEnvAtlas = null;
this._skyboxIntensity = 1;
this._skyboxLuminance = 0;
this._skyboxMip = 0;
this._skyboxRotationShaderInclude = false;
this._skyboxRotation = new Quat();
this._skyboxRotationMat3 = new Mat3();
this._skyboxRotationMat4 = new Mat4();
// ambient light lightmapping properties
this._ambientBakeNumSamples = 1;
this._ambientBakeSpherePart = 0.4;
this._lightmapFilterRange = 10;
this._lightmapFilterSmoothness = 0.2;
// clustered lighting
this._clusteredLightingEnabled = true;
this._lightingParams = new LightingParams(this.device.supportsAreaLights, this.device.maxTextureSize, () => {
this.updateShaders = true;
});
this._stats = {
meshInstances: 0,
lights: 0,
dynamicLights: 0,
bakedLights: 0,
updateShadersTime: 0 // deprecated
};
/**
* This flag indicates changes were made to the scene which may require recompilation of
* shaders that reference global settings.
*
* @type {boolean}
* @ignore
*/
this.updateShaders = true;
this._shaderVersion = 0;
// immediate rendering
this.immediate = new Immediate(this.device);
}
/**
* Fired when the skybox is set.
*
* @event Scene#set:skybox
* @param {import('../platform/graphics/texture.js').Texture} usedTex - Previously used cubemap
* texture. New is in the {@link Scene#skybox}.
*/
/**
* Fired when the layer composition is set. Use this event to add callbacks or advanced
* properties to your layers.
*
* @event Scene#set:layers
* @param {import('./composition/layer-composition.js').LayerComposition} oldComp - Previously
* used {@link LayerComposition}.
* @param {import('./composition/layer-composition.js').LayerComposition} newComp - Newly set
* {@link LayerComposition}.
* @example
* this.app.scene.on('set:layers', function (oldComp, newComp) {
* const list = newComp.layerList;
* for (let i = 0; i < list.length; i++) {
* const layer = list[i];
* switch (layer.name) {
* case 'MyLayer':
* layer.onEnable = myOnEnableFunction;
* layer.onDisable = myOnDisableFunction;
* break;
* case 'MyOtherLayer':
* layer.shaderPass = myShaderPass;
* break;
* }
* }
* });
*/
/**
* Returns the default layer used by the immediate drawing functions.
*
* @type {import('./layer.js').Layer}
* @private
*/
get defaultDrawLayer() {
return this.layers.getLayerById(LAYERID_IMMEDIATE);
}
/**
* If {@link Scene#ambientBake} is true, this specifies the number of samples used to bake the
* ambient light into the lightmap. Defaults to 1. Maximum value is 255.
*
* @type {number}
*/
set ambientBakeNumSamples(value) {
this._ambientBakeNumSamples = math.clamp(Math.floor(value), 1, 255);
}
get ambientBakeNumSamples() {
return this._ambientBakeNumSamples;
}
/**
* If {@link Scene#ambientBake} is true, this specifies a part of the sphere which represents
* the source of ambient light. The valid range is 0..1, representing a part of the sphere from
* top to the bottom. A value of 0.5 represents the upper hemisphere. A value of 1 represents a
* full sphere. Defaults to 0.4, which is a smaller upper hemisphere as this requires fewer
* samples to bake.
*
* @type {number}
*/
set ambientBakeSpherePart(value) {
this._ambientBakeSpherePart = math.clamp(value, 0.001, 1);
}
get ambientBakeSpherePart() {
return this._ambientBakeSpherePart;
}
/**
* True if the clustered lighting is enabled. Set to false before the first frame is rendered
* to use non-clustered lighting. Defaults to true.
*
* @type {boolean}
*/
set clusteredLightingEnabled(value) {
if (this.device.isWebGPU && !value) {
Debug.warnOnce('WebGPU currently only supports clustered lighting, and this cannot be disabled.');
return;
}
if (!this._clusteredLightingEnabled && value) {
console.error('Turning on disabled clustered lighting is not currently supported');
return;
}
this._clusteredLightingEnabled = value;
}
get clusteredLightingEnabled() {
return this._clusteredLightingEnabled;
}
/**
* The environment lighting atlas.
*
* @type {import('../platform/graphics/texture.js').Texture}
*/
set envAtlas(value) {
if (value !== this._envAtlas) {
this._envAtlas = value;
// make sure required options are set up on the texture
if (value) {
value.addressU = ADDRESS_CLAMP_TO_EDGE;
value.addressV = ADDRESS_CLAMP_TO_EDGE;
value.minFilter = FILTER_LINEAR;
value.magFilter = FILTER_LINEAR;
value.mipmaps = false;
}
this._prefilteredCubemaps = [];
if (this._internalEnvAtlas) {
this._internalEnvAtlas.destroy();
this._internalEnvAtlas = null;
}
this._resetSky();
}
}
get envAtlas() {
return this._envAtlas;
}
/**
* The type of fog used by the scene. Can be:
*
* - {@link FOG_NONE}
* - {@link FOG_LINEAR}
* - {@link FOG_EXP}
* - {@link FOG_EXP2}
*
* Defaults to {@link FOG_NONE}.
*
* @type {string}
*/
set fog(type) {
if (type !== this._fog) {
this._fog = type;
this.updateShaders = true;
}
}
get fog() {
return this._fog;
}
/**
* The gamma correction to apply when rendering the scene. Can be:
*
* - {@link GAMMA_NONE}
* - {@link GAMMA_SRGB}
*
* Defaults to {@link GAMMA_SRGB}.
*
* @type {number}
*/
set gammaCorrection(value) {
if (value !== this._gammaCorrection) {
this._gammaCorrection = value;
this.updateShaders = true;
}
}
get gammaCorrection() {
return this._gammaCorrection;
}
/**
* A {@link LayerComposition} that defines rendering order of this scene.
*
* @type {import('./composition/layer-composition.js').LayerComposition}
*/
set layers(layers) {
const prev = this._layers;
this._layers = layers;
this.fire('set:layers', prev, layers);
}
get layers() {
return this._layers;
}
/**
* A {@link LightingParams} that defines lighting parameters.
*
* @type {LightingParams}
*/
get lighting() {
return this._lightingParams;
}
/**
* A range parameter of the bilateral filter. It's used when {@link Scene#lightmapFilterEnabled}
* is enabled. Larger value applies more widespread blur. This needs to be a positive non-zero
* value. Defaults to 10.
*
* @type {number}
*/
set lightmapFilterRange(value) {
this._lightmapFilterRange = Math.max(value, 0.001);
}
get lightmapFilterRange() {
return this._lightmapFilterRange;
}
/**
* A spatial parameter of the bilateral filter. It's used when {@link Scene#lightmapFilterEnabled}
* is enabled. Larger value blurs less similar colors. This needs to be a positive non-zero
* value. Defaults to 0.2.
*
* @type {number}
*/
set lightmapFilterSmoothness(value) {
this._lightmapFilterSmoothness = Math.max(value, 0.001);
}
get lightmapFilterSmoothness() {
return this._lightmapFilterSmoothness;
}
/**
* Set of 6 prefiltered cubemaps.
*
* @type {import('../platform/graphics/texture.js').Texture[]}
*/
set prefilteredCubemaps(value) {
value = value || [];
const cubemaps = this._prefilteredCubemaps;
const changed = cubemaps.length !== value.length || cubemaps.some((c, i) => c !== value[i]);
if (changed) {
const complete = value.length === 6 && value.every(c => !!c);
if (complete) {
// update env atlas
this._internalEnvAtlas = EnvLighting.generatePrefilteredAtlas(value, {
target: this._internalEnvAtlas
});
this._envAtlas = this._internalEnvAtlas;
} else {
if (this._internalEnvAtlas) {
this._internalEnvAtlas.destroy();
this._internalEnvAtlas = null;
}
this._envAtlas = null;
}
this._prefilteredCubemaps = value.slice();
this._resetSky();
}
}
get prefilteredCubemaps() {
return this._prefilteredCubemaps;
}
/**
* The base cubemap texture used as the scene's skybox, if mip level is 0. Defaults to null.
*
* @type {import('../platform/graphics/texture.js').Texture}
*/
set skybox(value) {
if (value !== this._skyboxCubeMap) {
this._skyboxCubeMap = value;
this._resetSky();
}
}
get skybox() {
return this._skyboxCubeMap;
}
/**
* Multiplier for skybox intensity. Defaults to 1. Unused if physical units are used.
*
* @type {number}
*/
set skyboxIntensity(value) {
if (value !== this._skyboxIntensity) {
this._skyboxIntensity = value;
this._resetSky();
}
}
get skyboxIntensity() {
return this._skyboxIntensity;
}
/**
* Luminance (in lm/m^2) of skybox. Defaults to 0. Only used if physical units are used.
*
* @type {number}
*/
set skyboxLuminance(value) {
if (value !== this._skyboxLuminance) {
this._skyboxLuminance = value;
this._resetSky();
}
}
get skyboxLuminance() {
return this._skyboxLuminance;
}
/**
* The mip level of the skybox to be displayed. Only valid for prefiltered cubemap skyboxes.
* Defaults to 0 (base level).
*
* @type {number}
*/
set skyboxMip(value) {
if (value !== this._skyboxMip) {
this._skyboxMip = value;
this._resetSky();
}
}
get skyboxMip() {
return this._skyboxMip;
}
/**
* The rotation of the skybox to be displayed. Defaults to {@link Quat.IDENTITY}.
*
* @type {Quat}
*/
set skyboxRotation(value) {
if (!this._skyboxRotation.equals(value)) {
const isIdentity = value.equals(Quat.IDENTITY);
this._skyboxRotation.copy(value);
if (isIdentity) {
this._skyboxRotationMat3.setIdentity();
} else {
this._skyboxRotationMat4.setTRS(Vec3.ZERO, value, Vec3.ONE);
this._skyboxRotationMat3.invertMat4(this._skyboxRotationMat4);
}
// only reset sky / rebuild scene shaders if rotation changed away from identity for the first time
if (!this._skyboxRotationShaderInclude && !isIdentity) {
this._skyboxRotationShaderInclude = true;
this._resetSky();
}
}
}
get skyboxRotation() {
return this._skyboxRotation;
}
/**
* The tonemapping transform to apply when writing fragments to the frame buffer. Can be:
*
* - {@link TONEMAP_LINEAR}
* - {@link TONEMAP_FILMIC}
* - {@link TONEMAP_HEJL}
* - {@link TONEMAP_ACES}
* - {@link TONEMAP_ACES2}
*
* Defaults to {@link TONEMAP_LINEAR}.
*
* @type {number}
*/
set toneMapping(value) {
if (value !== this._toneMapping) {
this._toneMapping = value;
this.updateShaders = true;
}
}
get toneMapping() {
return this._toneMapping;
}
destroy() {
this._resetSky();
this.root = null;
this.off();
}
drawLine(start, end, color = Color.WHITE, depthTest = true, layer = this.defaultDrawLayer) {
const batch = this.immediate.getBatch(layer, depthTest);
batch.addLines([start, end], [color, color]);
}
drawLines(positions, colors, depthTest = true, layer = this.defaultDrawLayer) {
const batch = this.immediate.getBatch(layer, depthTest);
batch.addLines(positions, colors);
}
drawLineArrays(positions, colors, depthTest = true, layer = this.defaultDrawLayer) {
const batch = this.immediate.getBatch(layer, depthTest);
batch.addLinesArrays(positions, colors);
}
applySettings(settings) {
const physics = settings.physics;
const render = settings.render;
// settings
this._gravity.set(physics.gravity[0], physics.gravity[1], physics.gravity[2]);
this.ambientLight.set(render.global_ambient[0], render.global_ambient[1], render.global_ambient[2]);
this.ambientLuminance = render.ambientLuminance;
this._fog = render.fog;
this.fogColor.set(render.fog_color[0], render.fog_color[1], render.fog_color[2]);
this.fogStart = render.fog_start;
this.fogEnd = render.fog_end;
this.fogDensity = render.fog_density;
this._gammaCorrection = render.gamma_correction;
this._toneMapping = render.tonemapping;
this.lightmapSizeMultiplier = render.lightmapSizeMultiplier;
this.lightmapMaxResolution = render.lightmapMaxResolution;
this.lightmapMode = render.lightmapMode;
this.exposure = render.exposure;
this._skyboxIntensity = render.skyboxIntensity ?? 1;
this._skyboxLuminance = render.skyboxLuminance ?? 20000;
this._skyboxMip = render.skyboxMip ?? 0;
if (render.skyboxRotation) {
this.skyboxRotation = (new Quat()).setFromEulerAngles(render.skyboxRotation[0], render.skyboxRotation[1], render.skyboxRotation[2]);
}
this.clusteredLightingEnabled = render.clusteredLightingEnabled ?? false;
this.lighting.applySettings(render);
// bake settings
[
'lightmapFilterEnabled',
'lightmapFilterRange',
'lightmapFilterSmoothness',
'ambientBake',
'ambientBakeNumSamples',
'ambientBakeSpherePart',
'ambientBakeOcclusionBrightness',
'ambientBakeOcclusionContrast'
].forEach((setting) => {
if (render.hasOwnProperty(setting)) {
this[setting] = render[setting];
}
});
this._resetSky();
}
// get the actual texture to use for skybox rendering
_getSkyboxTex() {
const cubemaps = this._prefilteredCubemaps;
if (this._skyboxMip) {
// skybox selection for some reason has always skipped the 32x32 prefiltered mipmap, presumably a bug.
// we can't simply fix this and map 3 to the correct level, since doing so has the potential
// to change the look of existing scenes dramatically.
// NOTE: the table skips the 32x32 mipmap
const skyboxMapping = [0, 1, /* 2 */ 3, 4, 5, 6];
// select blurry texture for use on the skybox
return cubemaps[skyboxMapping[this._skyboxMip]] || this._envAtlas || cubemaps[0] || this._skyboxCubeMap;
}
return this._skyboxCubeMap || cubemaps[0] || this._envAtlas;
}
_updateSky(device) {
if (!this.sky) {
const texture = this._getSkyboxTex();
if (texture) {
this.sky = new Sky(device, this, texture);
this.fire('set:skybox', texture);
}
}
}
_resetSky() {
this.sky?.destroy();
this.sky = null;
this.updateShaders = true;
}
/**
* Sets the cubemap for the scene skybox.
*
* @param {import('../platform/graphics/texture.js').Texture[]} [cubemaps] - An array of
* cubemaps corresponding to the skybox at different mip levels. If undefined, scene will
* remove skybox. Cubemap array should be of size 7, with the first element (index 0)
* corresponding to the base cubemap (mip level 0) with original resolution. Each remaining
* element (index 1-6) corresponds to a fixed prefiltered resolution (128x128, 64x64, 32x32,
* 16x16, 8x8, 4x4).
*/
setSkybox(cubemaps) {
if (!cubemaps) {
this.skybox = null;
this.envAtlas = null;
} else {
this.skybox = cubemaps[0] || null;
if (cubemaps[1] && !cubemaps[1].cubemap) {
// prefiltered data is an env atlas
this.envAtlas = cubemaps[1];
} else {
// prefiltered data is a set of cubemaps
this.prefilteredCubemaps = cubemaps.slice(1);
}
}
}
updateSkyboxProjection() {
if (this._skyboxProjectionEnabled) {
const scope = this.device.scope;
const center = this._skyboxProjectionCenter;
const radius = this._skyboxProjectionRadius;
const domeElevation = this._skyboxProjectionElevation;
scope.resolve('projectedSkydomeCenter').setValue([
center.x,
center.y * radius,
center.z,
1.0
]);
scope.resolve('projectedSkydomeDome').setValue([
center.x,
center.y + radius * domeElevation,
center.z,
radius * radius
]);
scope.resolve('projectedSkydomePlane').setValue([
0,
1,
0,
-center.y
]);
}
}
/**
* When enabled, the skybox will be projected into a sphere dome and a ground plane. Otherwise
* the skybox will be projected onto an infinite cube. Defaults to false.
*
* @type {boolean}
*/
set skyboxProjectionEnabled(value) {
if (value !== this._skyboxProjectionEnabled) {
this._skyboxProjectionEnabled = value;
this.updateSkyboxProjection();
this.updateShaders = true;
}
}
get skyboxProjectionEnabled() {
return this._skyboxProjectionEnabled;
}
/**
* The center of the skybox projection. The y-component of the vector represents the height of
* the camera from the ground plane. Defaults to (0, 0, 0).
*
* @type {Vec3}
*/
set skyboxProjectionCenter(value) {
this._skyboxProjectionCenter.copy(value);
this.updateSkyboxProjection();
}
get skyboxProjectionCenter() {
return this._skyboxProjectionCenter;
}
/**
* The elevation of the skybox projection - both the sphere and the plane parts. Defaults to 0.
*
* @type {number}
*/
set skyboxProjectionElevation(value) {
this._skyboxProjectionElevation = value;
this.updateSkyboxProjection();
}
get skyboxProjectionElevation() {
return this._skyboxProjectionElevation;
}
/**
* The radius of the skybox projection sphere. Defaults to 100.
*
* @type {number}
*/
set skyboxProjectionRadius(value) {
this._skyboxProjectionRadius = value;
this.updateSkyboxProjection();
}
get skyboxProjectionRadius() {
return this._skyboxProjectionRadius;
}
/**
* The lightmap pixel format.
*
* @type {number}
*/
get lightmapPixelFormat() {
return this.lightmapHDR && this.device.getRenderableHdrFormat() || PIXELFORMAT_RGBA8;
}
}
export { Scene };