Pārlūkot izejas kodu

KTX2Loader: Warn when multiple instances in use. (#22621)

Don McCurdy 3 gadi atpakaļ
vecāks
revīzija
6d41436816

+ 19 - 0
examples/jsm/loaders/KTX2Loader.js

@@ -37,6 +37,8 @@ const KTX2TransferSRGB = 2;
 const KTX2_ALPHA_PREMULTIPLIED = 1;
 const _taskCache = new WeakMap();
 
+let _activeLoaders = 0;
+
 class KTX2Loader extends Loader {
 
 	constructor( manager ) {
@@ -154,6 +156,21 @@ class KTX2Loader extends Loader {
 
 				} );
 
+			if ( _activeLoaders > 0 ) {
+
+				// Each instance loads a transcoder and allocates workers, increasing network and memory cost.
+
+				console.warn(
+
+					'THREE.KTX2Loader: Multiple active KTX2 loaders may cause performance issues.'
+					+ ' Use a single KTX2Loader instance, or call .dispose() on old instances.'
+
+				);
+
+			}
+
+			_activeLoaders++;
+
 		}
 
 		return this.transcoderPending;
@@ -248,6 +265,8 @@ class KTX2Loader extends Loader {
 		URL.revokeObjectURL( this.workerSourceURL );
 		this.workerPool.dispose();
 
+		_activeLoaders--;
+
 		return this;
 
 	}

+ 20 - 9
examples/webgl_loader_texture_ktx2.html

@@ -63,21 +63,32 @@
 			};
 
 			// Samples: sample_etc1s.ktx2, sample_uastc.ktx2, sample_uastc_zstd.ktx2
-			new KTX2Loader()
+			const loader = new KTX2Loader()
 				.setTranscoderPath( 'js/libs/basis/' )
-				.detectSupport( renderer )
-				.load( './textures/compressed/sample_uastc_zstd.ktx2', ( texture ) => {
+				.detectSupport( renderer );
 
-					console.info( `transcoded to ${formatStrings[ texture.format ]}` );
+			animate();
 
-					material.map = texture;
-					material.transparent = true;
+			try {
 
-					material.needsUpdate = true;
+				const texture = await loader.loadAsync( './textures/compressed/sample_uastc_zstd.ktx2' );
 
-				}, ( p ) => console.log( `...${p}` ), ( e ) => console.error( e ) );
+				console.info( `transcoded to ${formatStrings[ texture.format ]}` );
 
-			animate();
+				material.map = texture;
+				material.transparent = true;
+
+				material.needsUpdate = true;
+
+			} catch ( e ) {
+
+				console.error( e );
+
+			} finally {
+
+				loader.dispose();
+
+			}
 
 			function animate() {