Browse Source

Geometry/MaterialExporter: Added vertex/face colors support.

Mr.doob 12 years ago
parent
commit
3bbaec7d96

+ 1 - 1
editor/index.html

@@ -78,7 +78,7 @@
 	</head>
 	<body>
 
-		<script src="../build/three.js"></script>
+		<script src="../build/three.min.js"></script>
 		<script src="../examples/js/libs/system.min.js"></script>
 
 		<script src="../examples/js/controls/EditorControls.js"></script>

+ 12 - 2
editor/js/Sidebar.Material.js

@@ -244,7 +244,11 @@ Sidebar.Material = function ( signals ) {
 
 		if ( material ) {
 
-			material.name = materialName.getValue();
+			if ( material.name !== undefined ) {
+
+				material.name = materialName.getValue();
+
+			}
 
 			if ( material instanceof materialClasses[ materialClass.getValue() ] == false ) {
 
@@ -450,6 +454,7 @@ Sidebar.Material = function ( signals ) {
 	function updateRows() {
 
 		var properties = {
+			'name': materialNameRow,
 			'color': materialColorRow,
 			'ambient': materialAmbientRow,
 			'emissive': materialEmissiveRow,
@@ -499,7 +504,12 @@ Sidebar.Material = function ( signals ) {
 
 			var material = object.material;
 
-			materialName.setValue( material.name );
+			if ( material.name !== undefined ) {
+
+				materialName.setValue( material.name );
+
+			}
+
 			materialClass.setValue( getMaterialInstanceName( material ) );
 
 			if ( material.color !== undefined ) {

+ 66 - 17
examples/js/exporters/GeometryExporter.js

@@ -28,9 +28,10 @@ THREE.GeometryExporter.prototype = {
 		}
 
 		var faces = [];
-		var uvs = [[]];
 		var normals = [];
 		var normalsHash = {};
+		var colors = [];
+		var colorsHash = {};
 
 		for ( var i = 0; i < geometry.faces.length; i ++ ) {
 
@@ -41,9 +42,9 @@ THREE.GeometryExporter.prototype = {
 			var hasFaceUv = false; // geometry.faceUvs[ 0 ][ i ] !== undefined;
 			var hasFaceVertexUv = false; // geometry.faceVertexUvs[ 0 ][ i ] !== undefined;
 			var hasFaceNormal = face.normal.length() > 0;
-			var hasFaceVertexNormal = face.vertexNormals[ 0 ] !== undefined;
-			var hasFaceColor = false; // face.color;
-			var hasFaceVertexColor = false; // face.vertexColors[ 0 ] !== undefined;
+			var hasFaceVertexNormal = face.vertexNormals.length > 0;
+			var hasFaceColor = face.color.r !== 1 && face.color.g !== 1 && face.color.b !== 1;
+			var hasFaceVertexColor = face.vertexColors.length > 0;
 
 			var faceType = 0;
 
@@ -112,8 +113,7 @@ THREE.GeometryExporter.prototype = {
 
 			if ( hasFaceNormal ) {
 
-				var faceNormal = face.normal;
-				faces.push( getNormalIndex( faceNormal.x, faceNormal.y, faceNormal.z ) );
+				faces.push( getNormalIndex( face.normal ) );
 
 			}
 
@@ -124,18 +124,49 @@ THREE.GeometryExporter.prototype = {
 				if ( isTriangle ) {
 
 					faces.push(
-						getNormalIndex( vertexNormals[ 0 ].x, vertexNormals[ 0 ].y, vertexNormals[ 0 ].z ),
-						getNormalIndex( vertexNormals[ 1 ].x, vertexNormals[ 1 ].y, vertexNormals[ 1 ].z ),
-						getNormalIndex( vertexNormals[ 2 ].x, vertexNormals[ 2 ].y, vertexNormals[ 2 ].z )
+						getNormalIndex( vertexNormals[ 0 ] ),
+						getNormalIndex( vertexNormals[ 1 ] ),
+						getNormalIndex( vertexNormals[ 2 ] )
 					);
 
 				} else {
 
 					faces.push(
-						getNormalIndex( vertexNormals[ 0 ].x, vertexNormals[ 0 ].y, vertexNormals[ 0 ].z ),
-						getNormalIndex( vertexNormals[ 1 ].x, vertexNormals[ 1 ].y, vertexNormals[ 1 ].z ),
-						getNormalIndex( vertexNormals[ 2 ].x, vertexNormals[ 2 ].y, vertexNormals[ 2 ].z ),
-						getNormalIndex( vertexNormals[ 3 ].x, vertexNormals[ 3 ].y, vertexNormals[ 3 ].z )
+						getNormalIndex( vertexNormals[ 0 ] ),
+						getNormalIndex( vertexNormals[ 1 ] ),
+						getNormalIndex( vertexNormals[ 2 ] ),
+						getNormalIndex( vertexNormals[ 3 ] )
+					);
+
+				}
+
+			}
+
+			if ( hasFaceColor ) {
+
+				faces.push( getColorIndex( face.color ) );
+
+			}
+
+			if ( hasFaceVertexColor ) {
+
+				var vertexColors = face.vertexColors;
+
+				if ( isTriangle ) {
+
+					faces.push(
+						getColorIndex( vertexColors[ 0 ] ),
+						getColorIndex( vertexColors[ 1 ] ),
+						getColorIndex( vertexColors[ 2 ] )
+					);
+
+				} else {
+
+					faces.push(
+						getColorIndex( vertexColors[ 0 ] ),
+						getColorIndex( vertexColors[ 1 ] ),
+						getColorIndex( vertexColors[ 2 ] ),
+						getColorIndex( vertexColors[ 3 ] )
 					);
 
 				}
@@ -150,9 +181,9 @@ THREE.GeometryExporter.prototype = {
 
 		}
 
-		function getNormalIndex( x, y, z ) {
+		function getNormalIndex( normal ) {
 
-			var hash = x.toString() + y.toString() + z.toString();
+			var hash = normal.x.toString() + normal.y.toString() + normal.z.toString();
 
 			if ( normalsHash[ hash ] !== undefined ) {
 
@@ -161,15 +192,33 @@ THREE.GeometryExporter.prototype = {
 			}
 
 			normalsHash[ hash ] = normals.length / 3;
-			normals.push( x, y, z );
+			normals.push( normal.x, normal.y, normal.z );
 
 			return normalsHash[ hash ];
 
 		}
 
+		function getColorIndex( color ) {
+
+			var hash = color.r.toString() + color.g.toString() + color.b.toString();
+
+			if ( colorsHash[ hash ] !== undefined ) {
+
+				return colorsHash[ hash ];
+
+			}
+
+			colorsHash[ hash ] = colors.length;
+			colors.push( color.getHex() );
+
+			return colorsHash[ hash ];
+
+		}
+
 		output.vertices = vertices;
 		output.normals = normals;
-		output.uvs = uvs;
+		output.colors = colors;
+		// output.uvs = uvs;
 		output.faces = faces;
 
 		//

+ 3 - 0
examples/js/exporters/MaterialExporter.js

@@ -26,6 +26,7 @@ THREE.MaterialExporter.prototype = {
 
 			output.type = 'MeshBasicMaterial';
 			output.color = material.color.getHex();
+			output.vertexColors = material.vertexColors;
 			output.opacity = material.opacity;
 			output.transparent = material.transparent;
 			output.wireframe = material.wireframe;
@@ -36,6 +37,7 @@ THREE.MaterialExporter.prototype = {
 			output.color = material.color.getHex();
 			output.ambient = material.ambient.getHex();
 			output.emissive = material.emissive.getHex();
+			output.vertexColors = material.vertexColors;
 			output.opacity = material.opacity;
 			output.transparent = material.transparent;
 			output.wireframe = material.wireframe;
@@ -48,6 +50,7 @@ THREE.MaterialExporter.prototype = {
 			output.emissive = material.emissive.getHex();
 			output.specular = material.specular.getHex();
 			output.shininess = material.shininess;
+			output.vertexColors = material.vertexColors;
 			output.opacity = material.opacity;
 			output.transparent = material.transparent;
 			output.wireframe = material.wireframe;

+ 11 - 7
src/loaders/JSONLoader.js

@@ -140,18 +140,22 @@ THREE.JSONLoader.prototype.parse = function ( json, texturePath ) {
 
 		nUvLayers = 0;
 
-		// disregard empty arrays
+		if ( json.uvs !== undefined ) {
 
-		for ( i = 0; i < json.uvs.length; i++ ) {
+			// disregard empty arrays
 
-			if ( json.uvs[ i ].length ) nUvLayers ++;
+			for ( i = 0; i < json.uvs.length; i++ ) {
 
-		}
+				if ( json.uvs[ i ].length ) nUvLayers ++;
+
+			}
 
-		for ( i = 0; i < nUvLayers; i++ ) {
+			for ( i = 0; i < nUvLayers; i++ ) {
 
-			geometry.faceUvs[ i ] = [];
-			geometry.faceVertexUvs[ i ] = [];
+				geometry.faceUvs[ i ] = [];
+				geometry.faceVertexUvs[ i ] = [];
+
+			}
 
 		}
 

+ 3 - 0
src/loaders/MaterialLoader.js

@@ -43,6 +43,7 @@ THREE.MaterialLoader.prototype = {
 				material = new THREE.MeshBasicMaterial( {
 
 					color: json.color,
+					vertexColors: json.vertexColors,
 					opacity: json.opacity,
 					transparent: json.transparent,
 					wireframe: json.wireframe
@@ -58,6 +59,7 @@ THREE.MaterialLoader.prototype = {
 					color: json.color,
 					ambient: json.ambient,
 					emissive: json.emissive,
+					vertexColors: json.vertexColors,
 					opacity: json.opacity,
 					transparent: json.transparent,
 					wireframe: json.wireframe
@@ -75,6 +77,7 @@ THREE.MaterialLoader.prototype = {
 					emissive: json.emissive,
 					specular: json.specular,
 					shininess: json.shininess,
+					vertexColors: json.vertexColors,
 					opacity: json.opacity,
 					transparent: json.transparent,
 					wireframe: json.wireframe