浏览代码

Added line color option, plus minor cleanup

WestLangley 11 年之前
父节点
当前提交
8aa6af037d
共有 1 个文件被更改,包括 21 次插入19 次删除
  1. 21 19
      src/extras/helpers/WireframeHelper.js

+ 21 - 19
src/extras/helpers/WireframeHelper.js

@@ -2,12 +2,14 @@
  * @author mrdoob / http://mrdoob.com/
  */
 
-THREE.WireframeHelper = function ( object ) {
+THREE.WireframeHelper = function ( object, hex ) {
+
+	var color = ( hex !== undefined ) ? hex : 0xffffff;
 
 	var edge = [ 0, 0 ], hash = {};
 	var sortFunction = function ( a, b ) { return a - b };
 
-	var keys = [ 'a', 'b', 'c', 'd' ];
+	var keys = [ 'a', 'b', 'c' ];
 	var geometry = new THREE.BufferGeometry();
 
 	if ( object.geometry instanceof THREE.Geometry ) {
@@ -44,7 +46,8 @@ THREE.WireframeHelper = function ( object ) {
 
 		}
 
-		geometry.addAttribute( 'position', Float32Array, 2 * numEdges , 3 );
+		geometry.addAttribute( 'position', Float32Array, 2 * numEdges, 3 );
+
 		var coords = geometry.attributes.position.array;
 
 		for ( var i = 0, l = numEdges; i < l; i ++ ) {
@@ -62,7 +65,7 @@ THREE.WireframeHelper = function ( object ) {
 
 		}
 
-	} else if ( object.geometry.offsets.length ) {
+	} else if ( object.geometry instanceof THREE.BufferGeometry && object.geometry.attributes.index !== undefined ) { // indexed BufferGeometry
 
 		var vertices = object.geometry.attributes.position.array;
 		var indices = object.geometry.attributes.index.array;
@@ -103,7 +106,7 @@ THREE.WireframeHelper = function ( object ) {
 
 		}
 
-		geometry.addAttribute( 'position', Float32Array, 2 * numEdges , 3 );
+		geometry.addAttribute( 'position', Float32Array, 2 * numEdges, 3 );
 
 		var coords = geometry.attributes.position.array;
 
@@ -121,32 +124,31 @@ THREE.WireframeHelper = function ( object ) {
 
 		}
 
-	} else {
+	} else if ( object.geometry instanceof THREE.BufferGeometry	) { // non-indexed BufferGeometry
 
 		var vertices = object.geometry.attributes.position.array;
 		var numEdges = vertices.length / 3;
 		var numTris = numEdges / 3;
 
-		geometry.addAttribute( 'position', Float32Array, 2 * numEdges , 3 );
+		geometry.addAttribute( 'position', Float32Array, 2 * numEdges, 3 );
+
 		var coords = geometry.attributes.position.array;
 
 		for ( var i = 0, l = numTris; i < l; i ++ ) {
 
-			var index = i * 9;
-
 			for ( var j = 0; j < 3; j ++ ) {
 
-				var index2 = 2 * index + 6 * j;
-				var vertex1 = j * 3;
-				var vertex2 = ( ( j + 1 ) % 3 ) * 3;
+				var index = 18 * i + 6 * j;
 
-				coords[ index2 + 0 ] = vertices[ index + vertex1 ];
-				coords[ index2 + 1 ] = vertices[ index + vertex1 + 1 ];
-				coords[ index2 + 2 ] = vertices[ index + vertex1 + 2 ];
+				var index1 = 9 * i + 3 * j;
+				coords[ index + 0 ] = vertices[ index1 ];
+				coords[ index + 1 ] = vertices[ index1 + 1 ];
+				coords[ index + 2 ] = vertices[ index1 + 2 ];
 
-				coords[ index2 + 3 ] = vertices[ index + vertex2 ];
-				coords[ index2 + 4 ] = vertices[ index + vertex2 + 1 ];
-				coords[ index2 + 5 ] = vertices[ index + vertex2 + 2 ];
+				var index2 = 9 * i + 3 * ( ( j + 1 ) % 3 );
+				coords[ index + 3 ] = vertices[ index2 ];
+				coords[ index + 4 ] = vertices[ index2 + 1 ];
+				coords[ index + 5 ] = vertices[ index2 + 2 ];
 
 			}
 
@@ -154,7 +156,7 @@ THREE.WireframeHelper = function ( object ) {
 
 	}
 
-	THREE.Line.call( this, geometry, new THREE.LineBasicMaterial( { color: 0xffffff } ), THREE.LinePieces );
+	THREE.Line.call( this, geometry, new THREE.LineBasicMaterial( { color: color } ), THREE.LinePieces );
 
 	this.matrixAutoUpdate = false;
 	this.matrixWorld = object.matrixWorld;