Browse Source

KTX2Loader: Cache loaded module promise

Instead of creating a new transcoder instance every time we need to
decode a texture, cache the promise and reuse the module.

Fixes #19793
Arseny Kapoulkine 5 years ago
parent
commit
d6dafa8dc2
1 changed files with 12 additions and 3 deletions
  1. 12 3
      examples/jsm/loaders/KTX2Loader.js

+ 12 - 3
examples/jsm/loaders/KTX2Loader.js

@@ -68,6 +68,7 @@ class KTX2Loader extends CompressedTextureLoader {
 		super( manager );
 		super( manager );
 
 
 		this.basisModule = null;
 		this.basisModule = null;
+		this.basisModulePending = null;
 
 
 		this.transcoderConfig = {};
 		this.transcoderConfig = {};
 
 
@@ -88,14 +89,20 @@ class KTX2Loader extends CompressedTextureLoader {
 
 
 	}
 	}
 
 
-	init () {
+	initModule () {
+
+		if ( this.basisModulePending ) {
+
+			return;
+
+		}
 
 
 		var scope = this;
 		var scope = this;
 
 
 		// The Emscripten wrapper returns a fake Promise, which can cause
 		// The Emscripten wrapper returns a fake Promise, which can cause
 		// infinite recursion when mixed with native Promises. Wrap the module
 		// infinite recursion when mixed with native Promises. Wrap the module
 		// initialization to return a native Promise.
 		// initialization to return a native Promise.
-		return new Promise( function ( resolve ) {
+		scope.basisModulePending = new Promise( function ( resolve ) {
 
 
 			MSC_TRANSCODER().then( function ( basisModule ) {
 			MSC_TRANSCODER().then( function ( basisModule ) {
 
 
@@ -126,7 +133,9 @@ class KTX2Loader extends CompressedTextureLoader {
 
 
 		} );
 		} );
 
 
-		Promise.all( [ bufferPending, this.init() ] ).then( function ( [ buffer ] ) {
+		this.initModule();
+
+		Promise.all( [ bufferPending, this.basisModulePending ] ).then( function ( [ buffer ] ) {
 
 
 			scope.parse( buffer, function ( _texture ) {
 			scope.parse( buffer, function ( _texture ) {