Ver código fonte

GLTFLoader: Removed .bind() craziness.

Mr.doob 8 anos atrás
pai
commit
3d9a27a72f
1 arquivos alterados com 77 adições e 52 exclusões
  1. 77 52
      examples/js/loaders/GLTFLoader.js

+ 77 - 52
examples/js/loaders/GLTFLoader.js

@@ -132,9 +132,7 @@ THREE.GLTFLoader = ( function () {
 
 	function GLTFShader( targetNode, allNodes ) {
 
-		var scope = this;
-
-		this.boundUniforms = {};
+		var boundUniforms = {};
 
 		// bind each uniform to its source node
 
@@ -156,7 +154,7 @@ THREE.GLTFLoader = ( function () {
 
 				}
 
-				scope.boundUniforms[ uniformId ] = {
+				boundUniforms[ uniformId ] = {
 					semantic: uniform.semantic,
 					sourceNode: sourceNode,
 					targetNode: targetNode,
@@ -167,6 +165,7 @@ THREE.GLTFLoader = ( function () {
 
 		}
 
+		this.boundUniforms = boundUniforms;
 		this._m4 = new THREE.Matrix4();
 
 	}
@@ -614,6 +613,8 @@ THREE.GLTFLoader = ( function () {
 
 	GLTFParser.prototype.parse = function ( callback ) {
 
+		var json = this.json;
+
 		// Clear the loader cache
 		this.cache.removeAll();
 
@@ -626,7 +627,7 @@ THREE.GLTFLoader = ( function () {
 
 		] ).then( function ( dependencies ) {
 
-			var scene = dependencies.scenes[ this.json.scene ];
+			var scene = dependencies.scenes[ json.scene ];
 
 			var cameras = [];
 
@@ -647,33 +648,39 @@ THREE.GLTFLoader = ( function () {
 
 			callback( scene, cameras, animations );
 
-		}.bind( this ) );
+		} );
 
 	};
 
 	GLTFParser.prototype.loadShaders = function () {
 
-		return _each( this.json.shaders, function ( shader ) {
+		var json = this.json;
+		var options = this.options;
+
+		return _each( json.shaders, function ( shader ) {
 
 			return new Promise( function ( resolve ) {
 
 				var loader = new THREE.FileLoader();
 				loader.responseType = 'text';
-				loader.load( resolveURL( shader.uri, this.options.path ), function ( shaderText ) {
+				loader.load( resolveURL( shader.uri, options.path ), function ( shaderText ) {
 
 					resolve( shaderText );
 
 				} );
 
-			}.bind( this ) );
+			} );
 
-		}.bind( this ) );
+		} );
 
 	};
 
 	GLTFParser.prototype.loadBuffers = function () {
 
-		return _each( this.json.buffers, function ( buffer ) {
+		var json = this.json;
+		var options = this.options;
+
+		return _each( json.buffers, function ( buffer ) {
 
 			if ( buffer.type === 'arraybuffer' ) {
 
@@ -681,29 +688,31 @@ THREE.GLTFLoader = ( function () {
 
 					var loader = new THREE.FileLoader();
 					loader.responseType = 'arraybuffer';
-					loader.load( resolveURL( buffer.uri, this.options.path ), function ( buffer ) {
+					loader.load( resolveURL( buffer.uri, options.path ), function ( buffer ) {
 
 						resolve( buffer );
 
 					} );
 
-				}.bind( this ) );
+				} );
 
 			}
 
-		}.bind( this ) );
+		} );
 
 	};
 
 	GLTFParser.prototype.loadBufferViews = function () {
 
+		var json = this.json;
+
 		return this._withDependencies( [
 
 			"buffers"
 
 		] ).then( function ( dependencies ) {
 
-			return _each( this.json.bufferViews, function ( bufferView ) {
+			return _each( json.bufferViews, function ( bufferView ) {
 
 				var arraybuffer = dependencies.buffers[ bufferView.buffer ];
 
@@ -711,19 +720,21 @@ THREE.GLTFLoader = ( function () {
 
 			} );
 
-		}.bind( this ) );
+		} );
 
 	};
 
 	GLTFParser.prototype.loadAccessors = function () {
 
+		var json = this.json;
+
 		return this._withDependencies( [
 
 			"bufferViews"
 
 		] ).then( function ( dependencies ) {
 
-			return _each( this.json.accessors, function ( accessor ) {
+			return _each( json.accessors, function ( accessor ) {
 
 				var arraybuffer = dependencies.bufferViews[ accessor.bufferView ];
 				var itemSize = WEBGL_TYPE_SIZES[ accessor.type ];
@@ -754,19 +765,22 @@ THREE.GLTFLoader = ( function () {
 
 			} );
 
-		}.bind( this ) );
+		} );
 
 	};
 
 	GLTFParser.prototype.loadTextures = function () {
 
-		return _each( this.json.textures, function ( texture ) {
+		var json = this.json;
+		var options = this.options;
+
+		return _each( json.textures, function ( texture ) {
 
 			if ( texture.source ) {
 
 				return new Promise( function ( resolve ) {
 
-					var source = this.json.images[ texture.source ];
+					var source = json.images[ texture.source ];
 
 					var textureLoader = THREE.Loader.Handlers.get( source.uri );
 
@@ -776,15 +790,15 @@ THREE.GLTFLoader = ( function () {
 
 					}
 
-					textureLoader.crossOrigin = this.options.crossOrigin || false;
+					textureLoader.crossOrigin = options.crossOrigin || false;
 
-					textureLoader.load( resolveURL( source.uri, this.options.path ), function ( _texture ) {
+					textureLoader.load( resolveURL( source.uri, options.path ), function ( _texture ) {
 
 						_texture.flipY = false;
 
 						if ( texture.sampler ) {
 
-							var sampler = this.json.samplers[ texture.sampler ];
+							var sampler = json.samplers[ texture.sampler ];
 
 							_texture.magFilter = WEBGL_FILTERS[ sampler.magFilter ];
 							_texture.minFilter = WEBGL_FILTERS[ sampler.minFilter ];
@@ -795,22 +809,24 @@ THREE.GLTFLoader = ( function () {
 
 						resolve( _texture );
 
-					}.bind( this ), undefined, function () {
+					}, undefined, function () {
 
 						resolve();
 
 					} );
 
-				}.bind( this ) );
+				} );
 
 			}
 
-		}.bind( this ) );
+		} );
 
 	};
 
 	GLTFParser.prototype.loadMaterials = function () {
 
+		var json = this.json;
+
 		return this._withDependencies( [
 
 			"shaders",
@@ -818,7 +834,7 @@ THREE.GLTFLoader = ( function () {
 
 		] ).then( function ( dependencies ) {
 
-			return _each( this.json.materials, function ( material ) {
+			return _each( json.materials, function ( material ) {
 
 				var materialType;
 				var materialValues = {};
@@ -830,9 +846,9 @@ THREE.GLTFLoader = ( function () {
 
 					khr_material = material.extensions.KHR_materials_common;
 
-				} else if ( this.json.extensions && this.json.extensions.KHR_materials_common ) {
+				} else if ( json.extensions && json.extensions.KHR_materials_common ) {
 
-					khr_material = this.json.extensions.KHR_materials_common;
+					khr_material = json.extensions.KHR_materials_common;
 
 				}
 
@@ -881,11 +897,11 @@ THREE.GLTFLoader = ( function () {
 
 					materialType = DeferredShaderMaterial;
 
-					var technique = this.json.techniques[ material.technique ];
+					var technique = json.techniques[ material.technique ];
 
 					materialParams.uniforms = {};
 
-					var program = this.json.programs[ technique.program ];
+					var program = json.programs[ technique.program ];
 
 					if ( program ) {
 
@@ -1090,14 +1106,16 @@ THREE.GLTFLoader = ( function () {
 
 				return _material;
 
-			}.bind( this ) );
+			} );
 
-		}.bind( this ) );
+		} );
 
 	};
 
 	GLTFParser.prototype.loadMeshes = function () {
 
+		var json = this.json;
+
 		return this._withDependencies( [
 
 			"accessors",
@@ -1105,7 +1123,7 @@ THREE.GLTFLoader = ( function () {
 
 		] ).then( function ( dependencies ) {
 
-			return _each( this.json.meshes, function ( mesh ) {
+			return _each( json.meshes, function ( mesh ) {
 
 				var group = new THREE.Object3D();
 				group.name = mesh.name;
@@ -1196,13 +1214,15 @@ THREE.GLTFLoader = ( function () {
 
 			} );
 
-		}.bind( this ) );
+		} );
 
 	};
 
 	GLTFParser.prototype.loadCameras = function () {
 
-		return _each( this.json.cameras, function ( camera ) {
+		var json = this.json;
+
+		return _each( json.cameras, function ( camera ) {
 
 			if ( camera.type == "perspective" && camera.perspective ) {
 
@@ -1232,13 +1252,13 @@ THREE.GLTFLoader = ( function () {
 
 			}
 
-		}.bind( this ) );
+		} );
 
 	};
 
 	GLTFParser.prototype.loadSkins = function () {
 
-		var scope = this;
+		var json = this.json;
 
 		return this._withDependencies( [
 
@@ -1246,7 +1266,7 @@ THREE.GLTFLoader = ( function () {
 
 		] ).then( function ( dependencies ) {
 
-			return _each( scope.json.skins, function ( skin ) {
+			return _each( json.skins, function ( skin ) {
 
 				var _skin = {
 					bindShapeMatrix: new THREE.Matrix4().fromArray( skin.bindShapeMatrix ),
@@ -1264,7 +1284,7 @@ THREE.GLTFLoader = ( function () {
 
 	GLTFParser.prototype.loadAnimations = function () {
 
-		var scope = this;
+		var json = this.json;
 
 		return this._withDependencies( [
 
@@ -1273,7 +1293,7 @@ THREE.GLTFLoader = ( function () {
 
 		] ).then( function ( dependencies ) {
 
-			return _each( scope.json.animations, function ( animation, animationId ) {
+			return _each( json.animations, function ( animation, animationId ) {
 
 				var interps = [];
 
@@ -1322,7 +1342,10 @@ THREE.GLTFLoader = ( function () {
 
 	GLTFParser.prototype.loadNodes = function () {
 
-		return _each( this.json.nodes, function ( node ) {
+		var json = this.json;
+		var scope = this;
+
+		return _each( json.nodes, function ( node ) {
 
 			var matrix = new THREE.Matrix4();
 
@@ -1372,9 +1395,9 @@ THREE.GLTFLoader = ( function () {
 
 			return _node;
 
-		}.bind( this ) ).then( function ( __nodes ) {
+		} ).then( function ( __nodes ) {
 
-			return this._withDependencies( [
+			return scope._withDependencies( [
 
 				"meshes",
 				"skins",
@@ -1385,7 +1408,7 @@ THREE.GLTFLoader = ( function () {
 
 				return _each( __nodes, function ( _node, nodeId ) {
 
-					var node = this.json.nodes[ nodeId ];
+					var node = json.nodes[ nodeId ];
 
 					if ( node.meshes !== undefined ) {
 
@@ -1491,17 +1514,19 @@ THREE.GLTFLoader = ( function () {
 
 					return _node;
 
-				}.bind( this ) );
+				} );
 
-			}.bind( this ) );
+			} );
 
-		}.bind( this ) );
+		} );
 
 	};
 
 	GLTFParser.prototype.loadExtensions = function () {
 
-		return _each( this.json.extensions, function ( extension, extensionId ) {
+		var json = this.json;
+
+		return _each( json.extensions, function ( extension, extensionId ) {
 
 			switch ( extensionId ) {
 
@@ -1563,7 +1588,7 @@ THREE.GLTFLoader = ( function () {
 
 	GLTFParser.prototype.loadScenes = function () {
 
-		var scope = this;
+		var json = this.json;
 
 		// scene node hierachy builder
 
@@ -1572,7 +1597,7 @@ THREE.GLTFLoader = ( function () {
 			var _node = allNodes[ nodeId ];
 			parentObject.add( _node );
 
-			var node = scope.json.nodes[ nodeId ];
+			var node = json.nodes[ nodeId ];
 
 			if ( node.children ) {
 
@@ -1595,7 +1620,7 @@ THREE.GLTFLoader = ( function () {
 
 		] ).then( function ( dependencies ) {
 
-			return _each( scope.json.scenes, function ( scene ) {
+			return _each( json.scenes, function ( scene ) {
 
 				var _scene = new THREE.Scene();
 				_scene.name = scene.name;