Browse Source

Extended gating of scene start also to asynchronous loading of textures.

alteredq 14 years ago
parent
commit
3c1ca82d10
3 changed files with 158 additions and 128 deletions
  1. 120 119
      build/ThreeExtras.js
  2. 2 2
      src/extras/ImageUtils.js
  3. 36 7
      src/extras/SceneUtils.js

File diff suppressed because it is too large
+ 120 - 119
build/ThreeExtras.js


+ 2 - 2
src/extras/ImageUtils.js

@@ -3,7 +3,7 @@ var ImageUtils = {
 	loadTexture: function ( path, mapping, callback ) {
 	loadTexture: function ( path, mapping, callback ) {
 
 
 		var image = new Image();
 		var image = new Image();
-		image.onload = function () { this.loaded = true; };
+		image.onload = function () { this.loaded = true; if( callback ) callback( this ); };
 		image.src = path;
 		image.src = path;
 
 
 		return new THREE.Texture( image, mapping );
 		return new THREE.Texture( image, mapping );
@@ -20,7 +20,7 @@ var ImageUtils = {
 
 
 			images[ i ] = new Image();
 			images[ i ] = new Image();
 			images[ i ].loaded = 0;
 			images[ i ].loaded = 0;
-			images[ i ].onload = function () { images.loadCount += 1; this.loaded = true; };
+			images[ i ].onload = function () { images.loadCount += 1; this.loaded = true; if( callback ) callback( this ); };
 			images[ i ].src = array[ i ];
 			images[ i ].src = array[ i ];
 
 
 		}
 		}

+ 36 - 7
src/extras/SceneUtils.js

@@ -90,16 +90,23 @@ var SceneUtils = {
 					handle_mesh( geo, id );
 					handle_mesh( geo, id );
 					
 					
 					counter_models -= 1;
 					counter_models -= 1;
-					if( counter_models == 0 ) {
-						
-						callback_async( result );
-						
-					}
+					//console.log( "models to load:", counter_models );
+					
+					async_callback_gate();
 					
 					
 				}
 				}
 				
 				
 			};
 			};
 			
 			
+			function async_callback_gate() {
+				
+				if( counter_models == 0 && counter_textures == 0 ) {
+					
+					callback_async( result );
+					
+				}
+				
+			};
 			
 			
 			// geometries
 			// geometries
 			
 			
@@ -169,9 +176,31 @@ var SceneUtils = {
 
 
 			// textures
 			// textures
 			
 			
-			// TODO: keep track of async loading of textures
+			// count how many textures will be loaded asynchronously
 			
 			
-			var callback_texture = function() { };
+			for( dt in data.textures ) {
+				
+				tt = data.textures[ dt ];
+				
+				if( tt.url instanceof Array ) {
+					
+					counter_textures += tt.url.length;
+					
+				} else {
+					
+					counter_textures += 1;
+					
+				}
+				
+			}
+			
+			var callback_texture = function( images ) {
+				
+				counter_textures -= 1; 
+				//console.log( "textures to load:", counter_textures ); 
+				async_callback_gate();  
+				
+			};
 			
 			
 			for( dt in data.textures ) {
 			for( dt in data.textures ) {
 				
 				

Some files were not shown because too many files changed in this diff