@@ -33,7 +33,7 @@ import ConvolveSpecularMapVS from "../Shaders/ConvolveSpecularMapVS.js";
33
33
* @typedef {object } DynamicEnvironmentMapManager.ConstructorOptions
34
34
* Options for the DynamicEnvironmentMapManager constructor
35
35
* @property {boolean } [enabled=true] If true, the environment map and related properties will continue to update.
36
- * @property {number } [mipmapLevels=10 ] The number of mipmap levels to generate for specular maps. More mipmap levels will produce a higher resolution specular reflection.
36
+ * @property {number } [mipmapLevels=7 ] The number of mipmap levels to generate for specular maps. More mipmap levels will produce a higher resolution specular reflection.
37
37
* @property {number } [maximumSecondsDifference=3600] The maximum amount of elapsed seconds before a new environment map is created.
38
38
* @property {number } [maximumPositionEpsilon=1000] The maximum difference in position before a new environment map is created, in meters. Small differences in position will not visibly affect results.
39
39
* @property {number } [atmosphereScatteringIntensity=2.0] The intensity of the scattered light emitted from the atmosphere. This should be adjusted relative to the value of {@link Scene.light} intensity.
@@ -81,7 +81,7 @@ function DynamicEnvironmentMapManager(options) {
81
81
options = defaultValue ( options , defaultValue . EMPTY_OBJECT ) ;
82
82
83
83
const mipmapLevels = Math . min (
84
- defaultValue ( options . mipmapLevels , 10 ) ,
84
+ defaultValue ( options . mipmapLevels , 7 ) ,
85
85
Math . log2 ( ContextLimits . maximumCubeMapSize ) ,
86
86
) ;
87
87
@@ -354,6 +354,10 @@ DynamicEnvironmentMapManager._updateCommandQueue = (frameState) => {
354
354
DynamicEnvironmentMapManager . _activeComputeCommandCount ++ ;
355
355
command = DynamicEnvironmentMapManager . _nextFrameCommandQueue . shift ( ) ;
356
356
}
357
+
358
+ if ( defined ( command ) ) {
359
+ DynamicEnvironmentMapManager . _nextFrameCommandQueue . push ( command ) ;
360
+ }
357
361
}
358
362
} ;
359
363
@@ -539,7 +543,8 @@ function updateRadianceMap(manager, frameState) {
539
543
let i = 0 ;
540
544
for ( const face of CubeMap . faceNames ( ) ) {
541
545
let texture = manager . _radianceMapTextures [ i ] ;
542
- if ( defined ( texture ) ) {
546
+ // Destroy any existing textures that have no yet been cleaned up
547
+ if ( defined ( texture ) && ! texture . isDestroyed ( ) ) {
543
548
texture . destroy ( ) ;
544
549
}
545
550
@@ -570,7 +575,6 @@ function updateRadianceMap(manager, frameState) {
570
575
) ;
571
576
} ,
572
577
} ,
573
- persists : true ,
574
578
owner : manager ,
575
579
postExecute : ( ) => {
576
580
const commands = manager . _radianceMapComputeCommands ;
@@ -583,7 +587,6 @@ function updateRadianceMap(manager, frameState) {
583
587
const framebuffer = new Framebuffer ( {
584
588
context : context ,
585
589
colorTextures : [ manager . _radianceMapTextures [ index ] ] ,
586
- destroyAttachments : false ,
587
590
} ) ;
588
591
589
592
// Copy the output texture into the corresponding cubemap face
@@ -639,19 +642,34 @@ function updateSpecularMaps(manager, frameState) {
639
642
facesCopied ++ ;
640
643
DynamicEnvironmentMapManager . _activeComputeCommandCount -- ;
641
644
642
- // All faces and levels have been copied
643
- if ( facesCopied === manager . _specularMapTextures . length ) {
645
+ texture . destroy ( ) ;
646
+ manager . _specularMapTextures [ index ] = undefined ;
647
+
648
+ // All faces for each mipmap level have been copied
649
+ const length = manager . _specularMapTextures . length ;
650
+ if ( facesCopied >= length ) {
644
651
manager . _irradianceCommandDirty = true ;
645
652
radianceCubeMap . sampler = new Sampler ( {
646
653
minificationFilter : TextureMinificationFilter . LINEAR_MIPMAP_LINEAR ,
647
654
} ) ;
655
+
648
656
manager . _shouldRegenerateShaders = true ;
657
+
658
+ // Cleanup shared resources
659
+ manager . _va . destroy ( ) ;
660
+ manager . _va = undefined ;
661
+ manager . _convolveSP . destroy ( ) ;
662
+ manager . _convolveSP = undefined ;
649
663
}
650
664
} ;
651
665
652
666
let index = 0 ;
653
667
for ( let level = 1 ; level < mipmapLevels ; ++ level ) {
654
668
for ( const face of CubeMap . faceNames ( ) ) {
669
+ if ( defined ( manager . _specularMapTextures [ index ] ) ) {
670
+ manager . _specularMapTextures [ index ] . destroy ( ) ;
671
+ }
672
+
655
673
const texture = ( manager . _specularMapTextures [ index ] = new Texture ( {
656
674
context : context ,
657
675
width : width ,
@@ -683,6 +701,8 @@ function updateSpecularMaps(manager, frameState) {
683
701
shaderProgram : shaderProgram ,
684
702
vertexArray : vertexArray ,
685
703
outputTexture : texture ,
704
+ // Persist so we can use a shared shader progam and vertex array across all commands
705
+ // Shared resources are instead destroyed in postExecute
686
706
persists : true ,
687
707
owner : manager ,
688
708
uniformMap : {
@@ -717,17 +737,19 @@ function updateIrradianceResources(manager, frameState) {
717
737
const dimensions = irradianceTextureDimensions ;
718
738
719
739
let texture = manager . _irradianceMapTexture ;
720
- if ( ! defined ( texture ) ) {
721
- texture = new Texture ( {
722
- context : context ,
723
- width : dimensions . x ,
724
- height : dimensions . y ,
725
- pixelDatatype : PixelDatatype . FLOAT ,
726
- pixelFormat : PixelFormat . RGBA ,
727
- } ) ;
728
- manager . _irradianceMapTexture = texture ;
740
+ if ( defined ( texture ) ) {
741
+ texture . destroy ( ) ;
729
742
}
730
743
744
+ texture = new Texture ( {
745
+ context : context ,
746
+ width : dimensions . x ,
747
+ height : dimensions . y ,
748
+ pixelDatatype : PixelDatatype . FLOAT ,
749
+ pixelFormat : PixelFormat . RGBA ,
750
+ } ) ;
751
+ manager . _irradianceMapTexture = texture ;
752
+
731
753
let fs = manager . _irradianceMapFS ;
732
754
if ( ! defined ( fs ) ) {
733
755
fs = new ShaderSource ( {
@@ -750,6 +772,7 @@ function updateIrradianceResources(manager, frameState) {
750
772
manager . _irradianceTextureDirty = false ;
751
773
manager . _irradianceComputeCommand = undefined ;
752
774
manager . _sphericalHarmonicCoefficientsDirty = true ;
775
+ manager . _irradianceMapFS = undefined ;
753
776
754
777
DynamicEnvironmentMapManager . _activeComputeCommandCount -- ;
755
778
} ,
@@ -768,6 +791,11 @@ function updateIrradianceResources(manager, frameState) {
768
791
function updateSphericalHarmonicCoefficients ( manager , frameState ) {
769
792
const context = frameState . context ;
770
793
794
+ if ( ! defined ( manager . _irradianceMapTexture ) ) {
795
+ // Operation was cancelled
796
+ return ;
797
+ }
798
+
771
799
const framebuffer = new Framebuffer ( {
772
800
context : context ,
773
801
colorTextures : [ manager . _irradianceMapTexture ] ,
@@ -793,6 +821,8 @@ function updateSphericalHarmonicCoefficients(manager, frameState) {
793
821
}
794
822
795
823
framebuffer . destroy ( ) ;
824
+ manager . _irradianceMapTexture . destroy ( ) ;
825
+ manager . _irradianceMapTexture = undefined ;
796
826
manager . _shouldRegenerateShaders = true ;
797
827
}
798
828
@@ -910,19 +940,33 @@ DynamicEnvironmentMapManager.prototype.destroy = function () {
910
940
length = this . _radianceMapTextures . length ;
911
941
for ( let i = 0 ; i < length ; ++ i ) {
912
942
this . _radianceMapTextures [ i ] =
913
- this . _radianceMapTextures [ i ] && this . _radianceMapTextures [ i ] . destroy ( ) ;
943
+ this . _radianceMapTextures [ i ] &&
944
+ ! this . _radianceMapTextures [ i ] . isDestroyed ( ) &&
945
+ this . _radianceMapTextures [ i ] . destroy ( ) ;
914
946
}
915
947
916
948
length = this . _specularMapTextures . length ;
917
949
for ( let i = 0 ; i < length ; ++ i ) {
918
950
this . _specularMapTextures [ i ] =
919
- this . _specularMapTextures [ i ] && this . _specularMapTextures [ i ] . destroy ( ) ;
951
+ this . _specularMapTextures [ i ] &&
952
+ ! this . _specularMapTextures [ i ] . isDestroyed ( ) &&
953
+ this . _specularMapTextures [ i ] . destroy ( ) ;
920
954
}
921
955
922
956
this . _radianceCubeMap =
923
957
this . _radianceCubeMap && this . _radianceCubeMap . destroy ( ) ;
924
958
this . _irradianceMapTexture =
925
- this . _irradianceMapTexture && this . _irradianceMapTexture . destroy ( ) ;
959
+ this . _irradianceMapTexture &&
960
+ ! this . _irradianceMapTexture . isDestroyed ( ) &&
961
+ this . _irradianceMapTexture . destroy ( ) ;
962
+
963
+ if ( defined ( this . _va ) ) {
964
+ this . _va . destroy ( ) ;
965
+ }
966
+
967
+ if ( defined ( this . _convolveSP ) ) {
968
+ this . _convolveSP . destroy ( ) ;
969
+ }
926
970
927
971
return destroyObject ( this ) ;
928
972
} ;
0 commit comments