浏览代码

Made THREE.Cache per Loader instead of global.

Mr.doob 11 年之前
父节点
当前提交
a072f1f862
共有 4 个文件被更改,包括 23 次插入12 次删除
  1. 8 2
      src/loaders/Cache.js
  2. 5 3
      src/loaders/ImageLoader.js
  3. 5 4
      src/loaders/Loader.js
  4. 5 3
      src/loaders/XHRLoader.js

+ 8 - 2
src/loaders/Cache.js

@@ -2,9 +2,15 @@
  * @author mrdoob / http://mrdoob.com/
  * @author mrdoob / http://mrdoob.com/
  */
  */
 
 
-THREE.Cache = {
+THREE.Cache = function () {
 
 
-	files: {},
+	this.files = {};
+
+};
+
+THREE.Cache.prototype = {
+
+	constructor: THREE.Cache,
 
 
 	add: function ( key, file ) {
 	add: function ( key, file ) {
 
 

+ 5 - 3
src/loaders/ImageLoader.js

@@ -4,6 +4,7 @@
 
 
 THREE.ImageLoader = function ( manager ) {
 THREE.ImageLoader = function ( manager ) {
 
 
+	this.cache = new THREE.Cache();
 	this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
 	this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
 
 
 };
 };
@@ -14,7 +15,9 @@ THREE.ImageLoader.prototype = {
 
 
 	load: function ( url, onLoad, onProgress, onError ) {
 	load: function ( url, onLoad, onProgress, onError ) {
 
 
-		var cached = THREE.Cache.get( url );
+		var scope = this;
+
+		var cached = scope.cache.get( url );
 
 
 		if ( cached !== undefined ) {
 		if ( cached !== undefined ) {
 
 
@@ -23,14 +26,13 @@ THREE.ImageLoader.prototype = {
 
 
 		}
 		}
 
 
-		var scope = this;
 		var image = document.createElement( 'img' );
 		var image = document.createElement( 'img' );
 
 
 		if ( onLoad !== undefined ) {
 		if ( onLoad !== undefined ) {
 
 
 			image.addEventListener( 'load', function ( event ) {
 			image.addEventListener( 'load', function ( event ) {
 
 
-				THREE.Cache.add( url, this );
+				scope.cache.add( url, this );
 
 
 				onLoad( this );
 				onLoad( this );
 				scope.manager.itemEnd( url );
 				scope.manager.itemEnd( url );

+ 5 - 4
src/loaders/Loader.js

@@ -7,6 +7,8 @@ THREE.Loader = function ( showStatus ) {
 	this.showStatus = showStatus;
 	this.showStatus = showStatus;
 	this.statusDomElement = showStatus ? THREE.Loader.prototype.addStatusElement() : null;
 	this.statusDomElement = showStatus ? THREE.Loader.prototype.addStatusElement() : null;
 
 
+	this.imageLoader = new THREE.ImageLoader();
+
 	this.onLoadStart = function () {};
 	this.onLoadStart = function () {};
 	this.onLoadProgress = function () {};
 	this.onLoadProgress = function () {};
 	this.onLoadComplete = function () {};
 	this.onLoadComplete = function () {};
@@ -101,7 +103,7 @@ THREE.Loader.prototype = {
 
 
 	createMaterial: function ( m, texturePath ) {
 	createMaterial: function ( m, texturePath ) {
 
 
-		var _this = this;
+		var scope = this;
 
 
 		function nearest_pow2( n ) {
 		function nearest_pow2( n ) {
 
 
@@ -169,9 +171,8 @@ THREE.Loader.prototype = {
 
 
 				var texture = where[ name ];
 				var texture = where[ name ];
 
 
-				var loader = new THREE.ImageLoader();
-				loader.crossOrigin = _this.crossOrigin;
-				loader.load( fullPath, function ( image ) {
+				scope.imageLoader.crossOrigin = scope.crossOrigin;
+				scope.imageLoader.load( fullPath, function ( image ) {
 
 
 					if ( THREE.Math.isPowerOfTwo( image.width ) === false ||
 					if ( THREE.Math.isPowerOfTwo( image.width ) === false ||
 						 THREE.Math.isPowerOfTwo( image.height ) === false ) {
 						 THREE.Math.isPowerOfTwo( image.height ) === false ) {

+ 5 - 3
src/loaders/XHRLoader.js

@@ -4,6 +4,7 @@
 
 
 THREE.XHRLoader = function ( manager ) {
 THREE.XHRLoader = function ( manager ) {
 
 
+	this.cache = new THREE.Cache();
 	this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
 	this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
 
 
 };
 };
@@ -14,7 +15,9 @@ THREE.XHRLoader.prototype = {
 
 
 	load: function ( url, onLoad, onProgress, onError ) {
 	load: function ( url, onLoad, onProgress, onError ) {
 
 
-		var cached = THREE.Cache.get( url );
+		var scope = this;
+
+		var cached = scope.cache.get( url );
 
 
 		if ( cached !== undefined ) {
 		if ( cached !== undefined ) {
 
 
@@ -23,14 +26,13 @@ THREE.XHRLoader.prototype = {
 
 
 		}
 		}
 
 
-		var scope = this;
 		var request = new XMLHttpRequest();
 		var request = new XMLHttpRequest();
 
 
 		if ( onLoad !== undefined ) {
 		if ( onLoad !== undefined ) {
 
 
 			request.addEventListener( 'load', function ( event ) {
 			request.addEventListener( 'load', function ( event ) {
 
 
-				THREE.Cache.add( url, event.target.responseText );
+				scope.cache.add( url, event.target.responseText );
 
 
 				onLoad( event.target.responseText );
 				onLoad( event.target.responseText );
 				scope.manager.itemEnd( url );
 				scope.manager.itemEnd( url );