Browse Source

Merge pull request #14408 from donmccurdy/feat-gltfloader-loadingmanager-compat

GLTFLoader: Ensure LoadingManager onLoad is supported.
Mr.doob 7 years ago
parent
commit
3860dc59da
2 changed files with 33 additions and 9 deletions
  1. 29 9
      examples/js/loaders/GLTFLoader.js
  2. 4 0
      examples/webgl_loader_gltf.html

+ 29 - 9
examples/js/loaders/GLTFLoader.js

@@ -27,6 +27,28 @@ THREE.GLTFLoader = ( function () {
 
 			var path = this.path !== undefined ? this.path : THREE.LoaderUtils.extractUrlBase( url );
 
+			// Tells the LoadingManager to track an extra item, which resolves after
+			// the model is fully loaded. This means the count of items loaded will
+			// be incorrect, but ensures manager.onLoad() does not fire early.
+			scope.manager.itemStart( url );
+
+			var _onError = function ( e ) {
+
+				if ( onError ) {
+
+					onError( e );
+
+				} else {
+
+					console.error( e );
+
+				}
+
+				scope.manager.itemEnd( url );
+				scope.manager.itemError( url );
+
+			};
+
 			var loader = new THREE.FileLoader( scope.manager );
 
 			loader.setResponseType( 'arraybuffer' );
@@ -35,23 +57,21 @@ THREE.GLTFLoader = ( function () {
 
 				try {
 
-					scope.parse( data, path, onLoad, onError );
-
-				} catch ( e ) {
+					scope.parse( data, path, function ( gltf ) {
 
-					if ( onError !== undefined ) {
+						onLoad( gltf );
 
-						onError( e );
+						scope.manager.itemEnd( url );
 
-					} else {
+					}, _onError );
 
-						throw e;
+				} catch ( e ) {
 
-					}
+					_onError( e );
 
 				}
 
-			}, onProgress, onError );
+			}, onProgress, _onError );
 
 		},
 

+ 4 - 0
examples/webgl_loader_gltf.html

@@ -97,6 +97,10 @@
 
 					scene.add( gltf.scene );
 
+				}, undefined, function ( e ) {
+
+					console.error( e );
+
 				} );
 
 				renderer = new THREE.WebGLRenderer( { antialias: true } );