Browse Source

Added handling of texture mapping and material combine parameters to scene loader.

alteredq 14 years ago
parent
commit
3d663b3428
4 changed files with 229 additions and 178 deletions
  1. 159 159
      build/ThreeExtras.js
  2. 38 7
      examples/scenes/test_scene.js
  3. 2 2
      src/extras/ImageUtils.js
  4. 30 10
      src/extras/SceneUtils.js

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


+ 38 - 7
examples/scenes/test_scene.js

@@ -29,7 +29,7 @@ var scene = {
 		"visible"  : true
 	},
 
-	"sphere" : {
+	"sphere_lambert" : {
 		"geometry" : "sphere",
 		"materials": [ "lambert_green" ],
 		"position" : [ -20, -5, 15 ],
@@ -38,6 +38,15 @@ var scene = {
 		"visible"  : true
 	},
 
+	"sphere_refraction" : {
+		"geometry" : "sphere",
+		"materials": [ "basic_refraction" ],
+		"position" : [ 50, 45, -50 ],
+		"rotation" : [ 0, 0, 0 ],
+		"scale"	   : [ 1, 1, 1 ],
+		"visible"  : true
+	},
+
 	"icosahedron" : {
 		"geometry" : "icosahedron",
 		"materials": [ "faceted_white" ],
@@ -86,7 +95,7 @@ var scene = {
 
 	"walt" : {
 		"geometry" : "WaltHead",
-		"materials": [ "phong_white" ],
+		"materials": [ "lambert_cube" ],
 		"position" : [ -45, 10, 0 ],
 		"rotation" : [ 0, 0, 0 ],
 		"scale"	   : [ 0.5, 0.5, 0.5 ],
@@ -257,19 +266,29 @@ var scene = {
 		"parameters": { color:0x000000, specular: 0xaa5500 } 
 	},
 	
+	"basic_refraction": {
+		"type": "MeshBasicMaterial",
+		"parameters": { color: 0xffffff, env_map: "cube_refraction", refraction_ratio: 0.95 } 
+	},
+	
+	"lambert_cube": {
+		"type": "MeshLambertMaterial",
+		"parameters": { color: 0xff6600, env_map: "cube_reflection", combine: "MixOperation", reflectivity: 0.3 }
+	},
+	
 	"chrome": {
 		"type": "MeshLambertMaterial",
-		"parameters": { color: 0xffffff, env_map: "texture_cube" }
+		"parameters": { color: 0xffffff, env_map: "cube_reflection" }
 	},
 
 	"darkerchrome": {
 		"type": "MeshLambertMaterial",
-		"parameters": { color: 0x222222, env_map: "texture_cube" }
+		"parameters": { color: 0x222222, env_map: "cube_reflection" }
 	},
 	
 	"glass": {
 		"type": "MeshLambertMaterial",
-		"parameters": { color: 0x101046, env_map: "texture_cube", opacity: 0.25 }
+		"parameters": { color: 0x101046, env_map: "cube_reflection", opacity: 0.25 }
 	},
 
 	"interior": {
@@ -297,7 +316,7 @@ var scene = {
 "textures": 
 {
 	
-	"texture_cube": {
+	"cube_reflection": {
 		"url": [ "textures/cube/SwedishRoyalCastle/px.jpg",
 				 "textures/cube/SwedishRoyalCastle/nx.jpg",
 				 "textures/cube/SwedishRoyalCastle/py.jpg",
@@ -307,6 +326,17 @@ var scene = {
 				]
 	},
 
+	"cube_refraction": {
+		"url": [ "textures/cube/SwedishRoyalCastle/px.jpg",
+				 "textures/cube/SwedishRoyalCastle/nx.jpg",
+				 "textures/cube/SwedishRoyalCastle/py.jpg",
+				 "textures/cube/SwedishRoyalCastle/ny.jpg",
+				 "textures/cube/SwedishRoyalCastle/nz.jpg",
+				 "textures/cube/SwedishRoyalCastle/pz.jpg"
+				],
+		"mapping": "CubeRefractionMapping"
+	},
+
 	"texture_bg": {
 		"url": "textures/cube/SwedishRoyalCastle/pz.jpg"
 	},
@@ -350,7 +380,8 @@ var scene = {
 	"light1": {
 		"type"		 : "directional",
 		"direction"	 : [0,1,1],
-		"color" 	 : [1,1,1]
+		"color" 	 : [1,1,1],
+		"intensity"	 : 0.8
 	},
 
 	"light2": {

+ 2 - 2
src/extras/ImageUtils.js

@@ -1,6 +1,6 @@
 var ImageUtils = {
 
-	loadTexture: function ( path, mapping ) {
+	loadTexture: function ( path, mapping, callback ) {
 
 		var image = new Image();
 		image.onload = function () { this.loaded = true; };
@@ -10,7 +10,7 @@ var ImageUtils = {
 
 	},
 
-	loadArray: function ( array ) {
+	loadArray: function ( array, callback ) {
 
 		var i, l, images = [];
 

+ 30 - 10
src/extras/SceneUtils.js

@@ -12,12 +12,15 @@ var SceneUtils = {
 				geometry, material, camera, fog, 
 				texture, images,
 				materials,
-				data, loader, async_counter,
+				data, loader, 
+				counter_models, counter_textures,
 				result;
 
 			data = event.data;
 			loader = new THREE.Loader();
-			async_counter = 0;
+			
+			counter_models = 0;
+			counter_textures = 0;
 			
 			result = {
 				
@@ -86,8 +89,8 @@ var SceneUtils = {
 					
 					handle_mesh( geo, id );
 					
-					async_counter -= 1;
-					if( async_counter == 0 ) {
+					counter_models -= 1;
+					if( counter_models == 0 ) {
 						
 						callback_async( result );
 						
@@ -98,7 +101,9 @@ var SceneUtils = {
 			};
 			
 			
-			// geometries			
+			// geometries
+			
+			// count how many models will be loaded asynchronously
 			
 			for( dg in data.geometries ) {
 				
@@ -106,7 +111,7 @@ var SceneUtils = {
 				
 				if ( g.type == "bin_mesh" || g.type == "ascii_mesh" ) {
 					
-					async_counter += 1;
+					counter_models += 1;
 					
 				}
 				
@@ -164,18 +169,28 @@ var SceneUtils = {
 
 			// textures
 			
+			// TODO: keep track of async loading of textures
+			
+			var callback_texture = function() { };
+			
 			for( dt in data.textures ) {
 				
 				tt = data.textures[ dt ];
 				
+				if ( tt.mapping != undefined && THREE[ tt.mapping ] != undefined  ) {
+					
+					tt.mapping = new THREE[ tt.mapping ]();
+				
+				}
+				
 				if( tt.url instanceof Array ) {
 					
-					images = ImageUtils.loadArray( tt.url );
-					texture = new THREE.Texture( images );
+					images = ImageUtils.loadArray( tt.url, callback_texture );
+					texture = new THREE.Texture( images, tt.mapping );
 					
 				} else {
 					
-					texture = ImageUtils.loadTexture( tt.url );
+					texture = ImageUtils.loadTexture( tt.url, tt.mapping, callback_texture );
 					
 					if ( THREE[ tt.min_filter ] != undefined )
 						texture.min_filter = THREE[ tt.min_filter ];
@@ -205,6 +220,10 @@ var SceneUtils = {
 						
 						m.parameters[ pp ] = ( m.parameters[ pp ] == "flat" ) ? THREE.FlatShading : THREE.SmoothShading;
 						
+					} else if ( pp == "combine" ) {
+						
+						m.parameters[ pp ] = ( m.parameters[ pp ] == "MixOperation" ) ? THREE.MixOperation : THREE.MultiplyOperation;
+						
 					}
 					
 				}
@@ -242,7 +261,8 @@ var SceneUtils = {
 				}
 				
 				c = l.color;
-				light.color.setRGB( c[0], c[1], c[2] );
+				i = l.intensity || 1;
+				light.color.setRGB( c[0] * i, c[1] * i, c[2] * i );
 				
 				result.scene.addLight( light );
 				

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