Browse Source

Refactored core and renderer to use new model format.

Work in progress, not expected to work yet.

(among other things, we don't deal yet with new vertex colors for particles/line/ribbons, for the moment they just use geometry.colors as before, also we need to specify which type of uvs to use somewhere, analogous to which type of vertex colors to use)
alteredq 14 years ago
parent
commit
9ab427b7c6

+ 10 - 5
src/core/Face3.js

@@ -1,17 +1,22 @@
 /**
 /**
  * @author mr.doob / http://mrdoob.com/
  * @author mr.doob / http://mrdoob.com/
+ * @author alteredq / http://alteredqualia.com/
  */
  */
 
 
-THREE.Face3 = function ( a, b, c, normal, materials ) {
+THREE.Face3 = function ( a, b, c, normal, color, materials ) {
 
 
-	this.a = a;
+	this.a = a; 
 	this.b = b;
 	this.b = b;
 	this.c = c;
 	this.c = c;
-
-	this.centroid = new THREE.Vector3();
+	
 	this.normal = normal instanceof THREE.Vector3 ? normal : new THREE.Vector3();
 	this.normal = normal instanceof THREE.Vector3 ? normal : new THREE.Vector3();
 	this.vertexNormals = normal instanceof Array ? normal : [];
 	this.vertexNormals = normal instanceof Array ? normal : [];
-
+	
+	this.color = color instanceof THREE.Color ? color : new THREE.Color();
+	this.vertexColors = color instanceof Array ? color : [];
+	
 	this.materials = materials instanceof Array ? materials : [ materials ];
 	this.materials = materials instanceof Array ? materials : [ materials ];
+	
+	this.centroid = new THREE.Vector3();
 
 
 };
 };

+ 10 - 5
src/core/Face4.js

@@ -1,18 +1,23 @@
 /**
 /**
  * @author mr.doob / http://mrdoob.com/
  * @author mr.doob / http://mrdoob.com/
+ * @author alteredq / http://alteredqualia.com/
  */
  */
 
 
-THREE.Face4 = function ( a, b, c, d, normal, materials ) {
+THREE.Face4 = function ( a, b, c, d, normal, color, materials ) {
 
 
-	this.a = a;
+	this.a = a; 
 	this.b = b;
 	this.b = b;
 	this.c = c;
 	this.c = c;
 	this.d = d;
 	this.d = d;
-
-	this.centroid = new THREE.Vector3();
+	
 	this.normal = normal instanceof THREE.Vector3 ? normal : new THREE.Vector3();
 	this.normal = normal instanceof THREE.Vector3 ? normal : new THREE.Vector3();
 	this.vertexNormals = normal instanceof Array ? normal : [];
 	this.vertexNormals = normal instanceof Array ? normal : [];
-
+	
+	this.color = color instanceof THREE.Color ? color : new THREE.Color();
+	this.vertexColors = color instanceof Array ? color : [];
+	
 	this.materials = materials instanceof Array ? materials : [ materials ];
 	this.materials = materials instanceof Array ? materials : [ materials ];
+	
+	this.centroid = new THREE.Vector3();
 
 
 };
 };

+ 7 - 4
src/core/Geometry.js

@@ -10,10 +10,13 @@ THREE.Geometry = function () {
 	this.id = "Geometry" + THREE.GeometryIdCounter ++;
 	this.id = "Geometry" + THREE.GeometryIdCounter ++;
 
 
 	this.vertices = [];
 	this.vertices = [];
+	this.colors = []; // one-to-one vertex colors, used in ParticleSystem, Line and Ribbon
+	
 	this.faces = [];
 	this.faces = [];
-	this.uvs = [];
-	this.uvs2 = [];
-	this.colors = [];
+	
+	this.faceUvs = [];
+	this.faceVertexUvs = [];
+	
 	this.morphTargets = [];
 	this.morphTargets = [];
 
 
 	this.skinWeights = [];
 	this.skinWeights = [];
@@ -277,7 +280,7 @@ THREE.Geometry.prototype = {
 		for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
 		for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
 
 
 			face = this.faces[ f ];
 			face = this.faces[ f ];
-			uv = this.uvs[ f ];
+			uv = this.faceVertexUvs[ f ][ 0 ]; // use UV layer 0 for tangents
 
 
 			if ( face instanceof THREE.Face3 ) {
 			if ( face instanceof THREE.Face3 ) {
 
 

+ 4 - 0
src/materials/Material.js

@@ -5,6 +5,10 @@
 THREE.FlatShading = 0;
 THREE.FlatShading = 0;
 THREE.SmoothShading = 1;
 THREE.SmoothShading = 1;
 
 
+THREE.MaterialColors = 0;
+THREE.FaceColors = 1;
+THREE.VertexColors = 2;
+
 THREE.NormalBlending = 0;
 THREE.NormalBlending = 0;
 THREE.AdditiveBlending = 1;
 THREE.AdditiveBlending = 1;
 THREE.SubtractiveBlending = 2;
 THREE.SubtractiveBlending = 2;

+ 2 - 1
src/materials/MeshBasicMaterial.js

@@ -21,7 +21,7 @@
  *  wireframe: <boolean>,
  *  wireframe: <boolean>,
  *  wireframeLinewidth: <float>,
  *  wireframeLinewidth: <float>,
  
  
- *  vertexColors: <bool>,
+ *  vertexColors: false / THREE.VertexColors / THREE.FaceColors,
  *  skinning: <bool>
  *  skinning: <bool>
  * }
  * }
  */
  */
@@ -53,6 +53,7 @@ THREE.MeshBasicMaterial = function ( parameters ) {
 	this.wireframeLinejoin = 'round'; // implemented just in CanvasRenderer
 	this.wireframeLinejoin = 'round'; // implemented just in CanvasRenderer
 
 
 	this.vertexColors = false;
 	this.vertexColors = false;
+	
 	this.skinning = false;
 	this.skinning = false;
 	this.morphTargets = false;
 	this.morphTargets = false;
 
 

+ 224 - 30
src/renderers/WebGLRenderer.js

@@ -321,8 +321,17 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 
 	function initMeshBuffers ( geometryGroup, object ) {
 	function initMeshBuffers ( geometryGroup, object ) {
 
 
-		var f, fl, nvertices = 0, ntris = 0, nlines = 0,
-			obj_faces = object.geometry.faces,
+		var f, fl, 
+		
+			nvertices = 0, ntris = 0, nlines = 0,
+			
+			uvType,
+			vertexColorType,
+			normalType,
+			materials,
+		
+			geometry = object.geometry,
+			obj_faces = geometry.faces,
 			chunk_faces = geometryGroup.faces;
 			chunk_faces = geometryGroup.faces;
 
 
 		for ( f = 0, fl = chunk_faces.length; f < fl; f++ ) {
 		for ( f = 0, fl = chunk_faces.length; f < fl; f++ ) {
@@ -345,28 +354,60 @@ THREE.WebGLRenderer = function ( parameters ) {
 			}
 			}
 
 
 		}
 		}
+		
+		materials = unrollGroupMaterials( geometryGroup, object );
+		
+		uvType = bufferGuessUVType( materials, geometryGroup, object );
+		vertexColorType = bufferGuessVertexColorType( materials, geometryGroup, object );
+		normalType = bufferGuessNormalType( materials, geometryGroup, object );
 
 
-		// TODO: only create arrays for attributes existing in the object
+		geometryGroup.__vertexArray = new Float32Array( nvertices * 3 );
+		
+		if ( normalType ) {
 
 
-		geometryGroup.__vertexArray  = new Float32Array( nvertices * 3 );
-		geometryGroup.__normalArray  = new Float32Array( nvertices * 3 );
-		geometryGroup.__tangentArray = new Float32Array( nvertices * 4 );
-		geometryGroup.__colorArray = new Float32Array( nvertices * 3 );
-		geometryGroup.__uvArray = new Float32Array( nvertices * 2 );
-		geometryGroup.__uv2Array = new Float32Array( nvertices * 2 );
+			geometryGroup.__normalArray = new Float32Array( nvertices * 3 );
 
 
-		geometryGroup.__skinVertexAArray = new Float32Array( nvertices * 4 );
-		geometryGroup.__skinVertexBArray = new Float32Array( nvertices * 4 );
-		geometryGroup.__skinIndexArray = new Float32Array( nvertices * 4 );
-		geometryGroup.__skinWeightArray = new Float32Array( nvertices * 4 );
+		}
+		
+		if ( geometry.hasTangents ) {
+		
+			geometryGroup.__tangentArray = new Float32Array( nvertices * 4 );
 
 
-		geometryGroup.__faceArray = new Uint16Array( ntris * 3 );
-		geometryGroup.__lineArray = new Uint16Array( nlines * 2 );
+		}
+		
+		if ( vertexColorType ) {
+		
+			geometryGroup.__colorArray = new Float32Array( nvertices * 3 );
 
 
-		geometryGroup.__needsSmoothNormals = bufferNeedsSmoothNormals ( geometryGroup, object );
+		}
 
 
-		geometryGroup.__webGLFaceCount = ntris * 3;
-		geometryGroup.__webGLLineCount = nlines * 2;
+		if ( uvType ) {
+		
+			if ( geometry.faceUvs.length > 0 || geometry.faceVertexUvs.length > 0 ) {
+			
+				geometryGroup.__uvArray = new Float32Array( nvertices * 2 );
+
+			}
+
+			if ( geometry.faceUvs.length > 1 || geometry.faceVertexUvs.length > 1 ) {
+			
+				geometryGroup.__uv2Array = new Float32Array( nvertices * 2 );
+
+			}
+
+		}
+
+		if ( object.geometry.skinWeights.length && object.geometry.skinIndices.length ) {
+
+			geometryGroup.__skinVertexAArray = new Float32Array( nvertices * 4 );
+			geometryGroup.__skinVertexBArray = new Float32Array( nvertices * 4 );
+			geometryGroup.__skinIndexArray = new Float32Array( nvertices * 4 );
+			geometryGroup.__skinWeightArray = new Float32Array( nvertices * 4 );
+
+		}
+
+		geometryGroup.__faceArray = new Uint16Array( ntris * 3 );
+		geometryGroup.__lineArray = new Uint16Array( nlines * 2 );
 
 
 		if( geometryGroup.numMorphTargets ) {
 		if( geometryGroup.numMorphTargets ) {
 			
 			
@@ -379,12 +420,23 @@ THREE.WebGLRenderer = function ( parameters ) {
 			}
 			}
 
 
 		}
 		}
+		
+		geometryGroup.__needsSmoothNormals = ( normalType == THREE.SmoothShading );
+		
+		geometryGroup.__uvType = uvType;
+		geometryGroup.__vertexColorType = vertexColorType;
+		geometryGroup.__normalType = normalType;
+
+		geometryGroup.__webGLFaceCount = ntris * 3;
+		geometryGroup.__webGLLineCount = nlines * 2;		
 
 
 	};
 	};
 
 
 	function setMeshBuffers ( geometryGroup, object, hint ) {
 	function setMeshBuffers ( geometryGroup, object, hint ) {
 
 
-		var f, fl, fi, face, vertexNormals, faceNormal, normal,
+		var f, fl, fi, face, 
+			vertexNormals, faceNormal, normal,
+			vertexColors, faceColor,
 			uv, uv2, v1, v2, v3, v4, t1, t2, t3, t4,
 			uv, uv2, v1, v2, v3, v4, t1, t2, t3, t4,
 			c1, c2, c3, c4,
 			c1, c2, c3, c4,
 			sw1, sw2, sw3, sw4,
 			sw1, sw2, sw3, sw4,
@@ -426,6 +478,8 @@ THREE.WebGLRenderer = function ( parameters ) {
 		lineArray = geometryGroup.__lineArray,
 		lineArray = geometryGroup.__lineArray,
 
 
 		needsSmoothNormals = geometryGroup.__needsSmoothNormals,
 		needsSmoothNormals = geometryGroup.__needsSmoothNormals,
+		
+		vertexColorType = geometryGroup.__vertexColorType,
 
 
 		geometry = object.geometry, // this is shared for all chunks
 		geometry = object.geometry, // this is shared for all chunks
 
 
@@ -440,8 +494,10 @@ THREE.WebGLRenderer = function ( parameters ) {
 		vertices = geometry.vertices,
 		vertices = geometry.vertices,
 		chunk_faces = geometryGroup.faces,
 		chunk_faces = geometryGroup.faces,
 		obj_faces = geometry.faces,
 		obj_faces = geometry.faces,
-		obj_uvs = geometry.uvs,
-		obj_uvs2 = geometry.uvs2,
+		
+		obj_uvs  = geometry.faceVertexUvs[ 0 ],
+		obj_uvs2 = geometry.faceVertexUvs[ 1 ],
+		
 		obj_colors = geometry.colors,
 		obj_colors = geometry.colors,
 
 
 		obj_skinVerticesA = geometry.skinVerticesA,
 		obj_skinVerticesA = geometry.skinVerticesA,
@@ -461,6 +517,9 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 
 			vertexNormals = face.vertexNormals;
 			vertexNormals = face.vertexNormals;
 			faceNormal = face.normal;
 			faceNormal = face.normal;
+			
+			vertexColors = face.vertexColors;
+			faceColor = face.color;
 
 
 			if ( face instanceof THREE.Face3 ) {
 			if ( face instanceof THREE.Face3 ) {
 
 
@@ -603,11 +662,21 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 
 				}
 				}
 
 
-				if ( dirtyColors && obj_colors.length ) {
+				if ( dirtyColors && vertexColorType ) {
+
+					if ( vertexColors.length == 3 && vertexColorType == THREE.VertexColors ) {
 
 
-					c1 = obj_colors[ face.a ];
-					c2 = obj_colors[ face.b ];
-					c3 = obj_colors[ face.c ];
+						c1 = vertexColors[ 0 ];
+						c2 = vertexColors[ 1 ];
+						c3 = vertexColors[ 2 ];
+
+					} else {
+						
+						c1 = faceColor;
+						c2 = faceColor;
+						c3 = faceColor;
+
+					}
 
 
 					colorArray[ offset_color ]     = c1.r;
 					colorArray[ offset_color ]     = c1.r;
 					colorArray[ offset_color + 1 ] = c1.g;
 					colorArray[ offset_color + 1 ] = c1.g;
@@ -911,12 +980,23 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 
 				}
 				}
 
 
-				if ( dirtyColors && obj_colors.length ) {
+				if ( dirtyColors && vertexColorType ) {
+
+					if ( vertexColors.length == 4 && vertexColorType == THREE.VertexColors ) {
 
 
-					c1 = obj_colors[ face.a ];
-					c2 = obj_colors[ face.b ];
-					c3 = obj_colors[ face.c ];
-					c4 = obj_colors[ face.d ];
+						c1 = vertexColors[ 0 ];
+						c2 = vertexColors[ 1 ];
+						c3 = vertexColors[ 2 ];
+						c4 = vertexColors[ 3 ];
+
+					} else {
+						
+						c1 = faceColor;
+						c2 = faceColor;
+						c3 = faceColor;
+						c4 = faceColor;
+
+					}
 
 
 					colorArray[ offset_color ]     = c1.r;
 					colorArray[ offset_color ]     = c1.r;
 					colorArray[ offset_color + 1 ] = c1.g;
 					colorArray[ offset_color + 1 ] = c1.g;
@@ -3349,6 +3429,120 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 
 	};
 	};
 
 
+	function unrollGroupMaterials( geometryGroup, object ) {
+		
+		var m, ml, i, il,
+			material, meshMaterial,
+			materials = [];
+		
+		for ( m = 0, ml = object.materials.length; m < ml; m++ ) {
+
+			meshMaterial = object.materials[ m ];
+
+			if ( meshMaterial instanceof THREE.MeshFaceMaterial ) {
+
+				for ( i = 0, l = geometryGroup.materials.length; i < l; i++ ) {
+
+					material = geometryGroup.materials[ i ];
+
+					if ( material ) {
+						
+						materials.push( material );
+
+					}
+
+				}
+
+			} else {
+
+				material = meshMaterial;
+
+				if ( material ) {
+
+					materials.push( material );
+
+				}
+
+			}
+
+		}
+		
+		return materials;
+
+	};
+	
+	function bufferGuessVertexColorType ( materials, geometryGroup, object ) {
+		
+		var i, m, ml = materials.length;
+			
+		// use vertexColor type from the first material in unrolled materials
+		
+		for ( i = 0; i < ml; i++ ) {
+			
+			m = materials[ i ];
+			
+			if ( m.vertexColors ) {
+				
+				return m.vertexColors;
+
+			}
+			
+		}
+		
+		return false;
+		
+	};
+
+	function bufferGuessNormalType ( materials, geometryGroup, object ) {
+		
+		var i, m, ml = materials.length;
+			
+		// only MeshBasicMaterial and MeshDepthMaterial don't need normals
+		
+		for ( i = 0; i < ml; i++ ) {
+			
+			m = materials[ i ];
+			
+			if ( m instanceof THREE.MeshBasicMaterial || m instanceof MeshDepthMaterial ) continue;
+			
+			if ( materialNeedsSmoothNormals( m ) ) {
+				
+				return THREE.SmoothShading;
+
+			} else {
+
+				return THREE.FlatShading;
+
+			}
+
+		}
+		
+		return false;
+		
+	};
+
+	function bufferGuessUVType ( materials, geometryGroup, object ) {
+		
+		var i, m, ml = materials.length;
+			
+		// material must use some texture to require uvs
+		
+		for ( i = 0; i < ml; i++ ) {
+			
+			m = materials[ i ];
+			
+			if ( m.map || m.lightMap ) {
+				
+				return true;
+				
+			}
+			
+		}
+		
+		return false;
+		
+	};
+	
 	function allocateBones ( object ) {
 	function allocateBones ( object ) {
 		
 		
 		// default for when object is not specified
 		// default for when object is not specified