瀏覽代碼

MarchingCubes code clean up.

Mr.doob 9 年之前
父節點
當前提交
f085219709
共有 1 個文件被更改,包括 170 次插入183 次删除
  1. 170 183
      examples/js/MarchingCubes.js

+ 170 - 183
examples/js/MarchingCubes.js

@@ -1,14 +1,20 @@
 /**
  * @author alteredq / http://alteredqualia.com/
- *
- * Port of greggman's ThreeD version of marching cubes to Three.js
- * http://webglsamples.googlecode.com/hg/blob/blob.html
+ * @author mrdoob / http://mrdoob.com
+ * Port of http://webglsamples.org/blob/blob.html
  */
 
 THREE.MarchingCubes = function ( resolution, material, enableUvs, enableColors ) {
 
 	THREE.ImmediateRenderObject.call( this, material );
 
+	var scope = this;
+
+	// temp buffers used in polygonize
+
+	var vlist = new Float32Array( 12 * 3 );
+	var nlist = new Float32Array( 12 * 3 );
+
 	this.enableUvs = enableUvs !== undefined ? enableUvs : false;
 	this.enableColors = enableColors !== undefined ? enableColors : false;
 
@@ -40,11 +46,6 @@ THREE.MarchingCubes = function ( resolution, material, enableUvs, enableColors )
 		this.field = new Float32Array( this.size3 );
 		this.normal_cache = new Float32Array( this.size3 * 3 );
 
-		// temp buffers used in polygonize
-
-		this.vlist = new Float32Array( 12 * 3 );
-		this.nlist = new Float32Array( 12 * 3 );
-
 		// immediate render mode simulator
 
 		this.maxCount = 4096; // TODO: find the fastest size for this buffer
@@ -76,98 +77,98 @@ THREE.MarchingCubes = function ( resolution, material, enableUvs, enableColors )
 	// Polygonization
 	///////////////////////
 
-	this.lerp = function( a, b, t ) {
+	function lerp( a, b, t ) {
 
 		return a + ( b - a ) * t;
 
-	};
+	}
 
-	this.VIntX = function( q, pout, nout, offset, isol, x, y, z, valp1, valp2 ) {
+	function VIntX( q, offset, isol, x, y, z, valp1, valp2 ) {
 
 		var mu = ( isol - valp1 ) / ( valp2 - valp1 ),
-		nc = this.normal_cache;
+		nc = scope.normal_cache;
 
-		pout[ offset ] 	   = x + mu * this.delta;
-		pout[ offset + 1 ] = y;
-		pout[ offset + 2 ] = z;
+		vlist[ offset + 0 ] = x + mu * scope.delta;
+		vlist[ offset + 1 ] = y;
+		vlist[ offset + 2 ] = z;
 
-		nout[ offset ] 	   = this.lerp( nc[ q ],     nc[ q + 3 ], mu );
-		nout[ offset + 1 ] = this.lerp( nc[ q + 1 ], nc[ q + 4 ], mu );
-		nout[ offset + 2 ] = this.lerp( nc[ q + 2 ], nc[ q + 5 ], mu );
+		nlist[ offset + 0 ] = lerp( nc[ q + 0 ], nc[ q + 3 ], mu );
+		nlist[ offset + 1 ] = lerp( nc[ q + 1 ], nc[ q + 4 ], mu );
+		nlist[ offset + 2 ] = lerp( nc[ q + 2 ], nc[ q + 5 ], mu );
 
-	};
+	}
 
-	this.VIntY = function( q, pout, nout, offset, isol, x, y, z, valp1, valp2 ) {
+	function VIntY( q, offset, isol, x, y, z, valp1, valp2 ) {
 
 		var mu = ( isol - valp1 ) / ( valp2 - valp1 ),
-		nc = this.normal_cache;
+		nc = scope.normal_cache;
 
-		pout[ offset ] 	   = x;
-		pout[ offset + 1 ] = y + mu * this.delta;
-		pout[ offset + 2 ] = z;
+		vlist[ offset + 0 ] = x;
+		vlist[ offset + 1 ] = y + mu * scope.delta;
+		vlist[ offset + 2 ] = z;
 
-		var q2 = q + this.yd * 3;
+		var q2 = q + scope.yd * 3;
 
-		nout[ offset ] 	   = this.lerp( nc[ q ],     nc[ q2 ],     mu );
-		nout[ offset + 1 ] = this.lerp( nc[ q + 1 ], nc[ q2 + 1 ], mu );
-		nout[ offset + 2 ] = this.lerp( nc[ q + 2 ], nc[ q2 + 2 ], mu );
+		nlist[ offset + 0 ] = lerp( nc[ q + 0 ], nc[ q2 + 0 ], mu );
+		nlist[ offset + 1 ] = lerp( nc[ q + 1 ], nc[ q2 + 1 ], mu );
+		nlist[ offset + 2 ] = lerp( nc[ q + 2 ], nc[ q2 + 2 ], mu );
 
-	};
+	}
 
-	this.VIntZ = function( q, pout, nout, offset, isol, x, y, z, valp1, valp2 ) {
+	function VIntZ( q, offset, isol, x, y, z, valp1, valp2 ) {
 
 		var mu = ( isol - valp1 ) / ( valp2 - valp1 ),
-		nc = this.normal_cache;
+		nc = scope.normal_cache;
 
-		pout[ offset ] 	   = x;
-		pout[ offset + 1 ] = y;
-		pout[ offset + 2 ] = z + mu * this.delta;
+		vlist[ offset + 0 ] = x;
+		vlist[ offset + 1 ] = y;
+		vlist[ offset + 2 ] = z + mu * scope.delta;
 
-		var q2 = q + this.zd * 3;
+		var q2 = q + scope.zd * 3;
 
-		nout[ offset ] 	   = this.lerp( nc[ q ],     nc[ q2 ],     mu );
-		nout[ offset + 1 ] = this.lerp( nc[ q + 1 ], nc[ q2 + 1 ], mu );
-		nout[ offset + 2 ] = this.lerp( nc[ q + 2 ], nc[ q2 + 2 ], mu );
+		nlist[ offset + 0 ] = lerp( nc[ q + 0 ], nc[ q2 + 0 ], mu );
+		nlist[ offset + 1 ] = lerp( nc[ q + 1 ], nc[ q2 + 1 ], mu );
+		nlist[ offset + 2 ] = lerp( nc[ q + 2 ], nc[ q2 + 2 ], mu );
 
-	};
+	}
 
-	this.compNorm = function( q ) {
+	function compNorm( q ) {
 
 		var q3 = q * 3;
 
-		if ( this.normal_cache[ q3 ] === 0.0 ) {
+		if ( scope.normal_cache[ q3 ] === 0.0 ) {
 
-			this.normal_cache[ q3 ] = this.field[ q - 1 ] 	    - this.field[ q + 1 ];
-			this.normal_cache[ q3 + 1 ] = this.field[ q - this.yd ] - this.field[ q + this.yd ];
-			this.normal_cache[ q3 + 2 ] = this.field[ q - this.zd ] - this.field[ q + this.zd ];
+			scope.normal_cache[ q3 + 0 ] = scope.field[ q - 1 ] 	    - scope.field[ q + 1 ];
+			scope.normal_cache[ q3 + 1 ] = scope.field[ q - scope.yd ] - scope.field[ q + scope.yd ];
+			scope.normal_cache[ q3 + 2 ] = scope.field[ q - scope.zd ] - scope.field[ q + scope.zd ];
 
 		}
 
-	};
+	}
 
 	// Returns total number of triangles. Fills triangles.
 	// (this is where most of time is spent - it's inner work of O(n3) loop )
 
-	this.polygonize = function( fx, fy, fz, q, isol, renderCallback ) {
+	function polygonize( fx, fy, fz, q, isol, renderCallback ) {
 
 		// cache indices
 		var q1 = q + 1,
-			qy = q + this.yd,
-			qz = q + this.zd,
-			q1y = q1 + this.yd,
-			q1z = q1 + this.zd,
-			qyz = q + this.yd + this.zd,
-			q1yz = q1 + this.yd + this.zd;
+			qy = q + scope.yd,
+			qz = q + scope.zd,
+			q1y = q1 + scope.yd,
+			q1z = q1 + scope.zd,
+			qyz = q + scope.yd + scope.zd,
+			q1yz = q1 + scope.yd + scope.zd;
 
 		var cubeindex = 0,
-			field0 = this.field[ q ],
-			field1 = this.field[ q1 ],
-			field2 = this.field[ qy ],
-			field3 = this.field[ q1y ],
-			field4 = this.field[ qz ],
-			field5 = this.field[ q1z ],
-			field6 = this.field[ qyz ],
-			field7 = this.field[ q1yz ];
+			field0 = scope.field[ q ],
+			field1 = scope.field[ q1 ],
+			field2 = scope.field[ qy ],
+			field3 = scope.field[ q1y ],
+			field4 = scope.field[ qz ],
+			field5 = scope.field[ q1z ],
+			field6 = scope.field[ qyz ],
+			field7 = scope.field[ q1yz ];
 
 		if ( field0 < isol ) cubeindex |= 1;
 		if ( field1 < isol ) cubeindex |= 2;
@@ -183,7 +184,7 @@ THREE.MarchingCubes = function ( resolution, material, enableUvs, enableColors )
 		var bits = THREE.edgeTable[ cubeindex ];
 		if ( bits === 0 ) return 0;
 
-		var d = this.delta,
+		var d = scope.delta,
 			fx2 = fx + d,
 			fy2 = fy + d,
 			fz2 = fz + d;
@@ -192,33 +193,33 @@ THREE.MarchingCubes = function ( resolution, material, enableUvs, enableColors )
 
 		if ( bits & 1 ) {
 
-			this.compNorm( q );
-			this.compNorm( q1 );
-			this.VIntX( q * 3, this.vlist, this.nlist, 0, isol, fx, fy, fz, field0, field1 );
+			compNorm( q );
+			compNorm( q1 );
+			VIntX( q * 3, 0, isol, fx, fy, fz, field0, field1 );
 
 		}
 
 		if ( bits & 2 ) {
 
-			this.compNorm( q1 );
-			this.compNorm( q1y );
-			this.VIntY( q1 * 3, this.vlist, this.nlist, 3, isol, fx2, fy, fz, field1, field3 );
+			compNorm( q1 );
+			compNorm( q1y );
+			VIntY( q1 * 3, 3, isol, fx2, fy, fz, field1, field3 );
 
 		}
 
 		if ( bits & 4 ) {
 
-			this.compNorm( qy );
-			this.compNorm( q1y );
-			this.VIntX( qy * 3, this.vlist, this.nlist, 6, isol, fx, fy2, fz, field2, field3 );
+			compNorm( qy );
+			compNorm( q1y );
+			VIntX( qy * 3, 6, isol, fx, fy2, fz, field2, field3 );
 
 		}
 
 		if ( bits & 8 ) {
 
-			this.compNorm( q );
-			this.compNorm( qy );
-			this.VIntY( q * 3, this.vlist, this.nlist, 9, isol, fx, fy, fz, field0, field2 );
+			compNorm( q );
+			compNorm( qy );
+			VIntY( q * 3, 9, isol, fx, fy, fz, field0, field2 );
 
 		}
 
@@ -226,33 +227,33 @@ THREE.MarchingCubes = function ( resolution, material, enableUvs, enableColors )
 
 		if ( bits & 16 ) {
 
-			this.compNorm( qz );
-			this.compNorm( q1z );
-			this.VIntX( qz * 3, this.vlist, this.nlist, 12, isol, fx, fy, fz2, field4, field5 );
+			compNorm( qz );
+			compNorm( q1z );
+			VIntX( qz * 3, 12, isol, fx, fy, fz2, field4, field5 );
 
 		}
 
 		if ( bits & 32 ) {
 
-			this.compNorm( q1z );
-			this.compNorm( q1yz );
-			this.VIntY( q1z * 3,  this.vlist, this.nlist, 15, isol, fx2, fy, fz2, field5, field7 );
+			compNorm( q1z );
+			compNorm( q1yz );
+			VIntY( q1z * 3, 15, isol, fx2, fy, fz2, field5, field7 );
 
 		}
 
 		if ( bits & 64 ) {
 
-			this.compNorm( qyz );
-			this.compNorm( q1yz );
-			this.VIntX( qyz * 3, this.vlist, this.nlist, 18, isol, fx, fy2, fz2, field6, field7 );
+			compNorm( qyz );
+			compNorm( q1yz );
+			VIntX( qyz * 3, 18, isol, fx, fy2, fz2, field6, field7 );
 
 		}
 
 		if ( bits & 128 ) {
 
-			this.compNorm( qz );
-			this.compNorm( qyz );
-			this.VIntY( qz * 3,  this.vlist, this.nlist, 21, isol, fx, fy, fz2, field4, field6 );
+			compNorm( qz );
+			compNorm( qyz );
+			VIntY( qz * 3, 21, isol, fx, fy, fz2, field4, field6 );
 
 		}
 
@@ -260,33 +261,33 @@ THREE.MarchingCubes = function ( resolution, material, enableUvs, enableColors )
 
 		if ( bits & 256 ) {
 
-			this.compNorm( q );
-			this.compNorm( qz );
-			this.VIntZ( q * 3, this.vlist, this.nlist, 24, isol, fx, fy, fz, field0, field4 );
+			compNorm( q );
+			compNorm( qz );
+			VIntZ( q * 3, 24, isol, fx, fy, fz, field0, field4 );
 
 		}
 
 		if ( bits & 512 ) {
 
-			this.compNorm( q1 );
-			this.compNorm( q1z );
-			this.VIntZ( q1 * 3,  this.vlist, this.nlist, 27, isol, fx2, fy,  fz, field1, field5 );
+			compNorm( q1 );
+			compNorm( q1z );
+			VIntZ( q1 * 3, 27, isol, fx2, fy,  fz, field1, field5 );
 
 		}
 
 		if ( bits & 1024 ) {
 
-			this.compNorm( q1y );
-			this.compNorm( q1yz );
-			this.VIntZ( q1y * 3, this.vlist, this.nlist, 30, isol, fx2, fy2, fz, field3, field7 );
+			compNorm( q1y );
+			compNorm( q1yz );
+			VIntZ( q1y * 3, 30, isol, fx2, fy2, fz, field3, field7 );
 
 		}
 
 		if ( bits & 2048 ) {
 
-			this.compNorm( qy );
-			this.compNorm( qyz );
-			this.VIntZ( qy * 3, this.vlist, this.nlist, 33, isol, fx,  fy2, fz, field2, field6 );
+			compNorm( qy );
+			compNorm( qyz );
+			VIntZ( qy * 3, 33, isol, fx,  fy2, fz, field2, field6 );
 
 		}
 
@@ -302,11 +303,11 @@ THREE.MarchingCubes = function ( resolution, material, enableUvs, enableColors )
 			o2 = o1 + 1;
 			o3 = o1 + 2;
 
-			this.posnormtriv( this.vlist, this.nlist,
-							  3 * THREE.triTable[ o1 ],
-							  3 * THREE.triTable[ o2 ],
-							  3 * THREE.triTable[ o3 ],
-							  renderCallback );
+			posnormtriv( vlist, nlist,
+				3 * THREE.triTable[ o1 ],
+				3 * THREE.triTable[ o2 ],
+				3 * THREE.triTable[ o3 ],
+				renderCallback );
 
 			i += 3;
 			numtris ++;
@@ -315,105 +316,105 @@ THREE.MarchingCubes = function ( resolution, material, enableUvs, enableColors )
 
 		return numtris;
 
-	};
+	}
 
 	/////////////////////////////////////
 	// Immediate render mode simulator
 	/////////////////////////////////////
 
-	this.posnormtriv = function( pos, norm, o1, o2, o3, renderCallback ) {
+	function posnormtriv( pos, norm, o1, o2, o3, renderCallback ) {
 
-		var c = this.count * 3;
+		var c = scope.count * 3;
 
 		// positions
 
-		this.positionArray[ c ] 	= pos[ o1 ];
-		this.positionArray[ c + 1 ] = pos[ o1 + 1 ];
-		this.positionArray[ c + 2 ] = pos[ o1 + 2 ];
+		scope.positionArray[ c + 0 ] = pos[ o1 ];
+		scope.positionArray[ c + 1 ] = pos[ o1 + 1 ];
+		scope.positionArray[ c + 2 ] = pos[ o1 + 2 ];
 
-		this.positionArray[ c + 3 ] = pos[ o2 ];
-		this.positionArray[ c + 4 ] = pos[ o2 + 1 ];
-		this.positionArray[ c + 5 ] = pos[ o2 + 2 ];
+		scope.positionArray[ c + 3 ] = pos[ o2 ];
+		scope.positionArray[ c + 4 ] = pos[ o2 + 1 ];
+		scope.positionArray[ c + 5 ] = pos[ o2 + 2 ];
 
-		this.positionArray[ c + 6 ] = pos[ o3 ];
-		this.positionArray[ c + 7 ] = pos[ o3 + 1 ];
-		this.positionArray[ c + 8 ] = pos[ o3 + 2 ];
+		scope.positionArray[ c + 6 ] = pos[ o3 ];
+		scope.positionArray[ c + 7 ] = pos[ o3 + 1 ];
+		scope.positionArray[ c + 8 ] = pos[ o3 + 2 ];
 
 		// normals
 
-		this.normalArray[ c ] 	  = norm[ o1 ];
-		this.normalArray[ c + 1 ] = norm[ o1 + 1 ];
-		this.normalArray[ c + 2 ] = norm[ o1 + 2 ];
+		scope.normalArray[ c + 0 ] = norm[ o1 ];
+		scope.normalArray[ c + 1 ] = norm[ o1 + 1 ];
+		scope.normalArray[ c + 2 ] = norm[ o1 + 2 ];
 
-		this.normalArray[ c + 3 ] = norm[ o2 ];
-		this.normalArray[ c + 4 ] = norm[ o2 + 1 ];
-		this.normalArray[ c + 5 ] = norm[ o2 + 2 ];
+		scope.normalArray[ c + 3 ] = norm[ o2 ];
+		scope.normalArray[ c + 4 ] = norm[ o2 + 1 ];
+		scope.normalArray[ c + 5 ] = norm[ o2 + 2 ];
 
-		this.normalArray[ c + 6 ] = norm[ o3 ];
-		this.normalArray[ c + 7 ] = norm[ o3 + 1 ];
-		this.normalArray[ c + 8 ] = norm[ o3 + 2 ];
+		scope.normalArray[ c + 6 ] = norm[ o3 ];
+		scope.normalArray[ c + 7 ] = norm[ o3 + 1 ];
+		scope.normalArray[ c + 8 ] = norm[ o3 + 2 ];
 
 		// uvs
 
-		if ( this.enableUvs ) {
+		if ( scope.enableUvs ) {
 
-			var d = this.count * 2;
+			var d = scope.count * 2;
 
-			this.uvArray[ d ] 	  = pos[ o1 ];
-			this.uvArray[ d + 1 ] = pos[ o1 + 2 ];
+			scope.uvArray[ d + 0 ] = pos[ o1 ];
+			scope.uvArray[ d + 1 ] = pos[ o1 + 2 ];
 
-			this.uvArray[ d + 2 ] = pos[ o2 ];
-			this.uvArray[ d + 3 ] = pos[ o2 + 2 ];
+			scope.uvArray[ d + 2 ] = pos[ o2 ];
+			scope.uvArray[ d + 3 ] = pos[ o2 + 2 ];
 
-			this.uvArray[ d + 4 ] = pos[ o3 ];
-			this.uvArray[ d + 5 ] = pos[ o3 + 2 ];
+			scope.uvArray[ d + 4 ] = pos[ o3 ];
+			scope.uvArray[ d + 5 ] = pos[ o3 + 2 ];
 
 		}
 
 		// colors
 
-		if ( this.enableColors ) {
+		if ( scope.enableColors ) {
 
-			this.colorArray[ c ] 	 = pos[ o1 ];
-			this.colorArray[ c + 1 ] = pos[ o1 + 1 ];
-			this.colorArray[ c + 2 ] = pos[ o1 + 2 ];
+			scope.colorArray[ c + 0 ] = pos[ o1 ];
+			scope.colorArray[ c + 1 ] = pos[ o1 + 1 ];
+			scope.colorArray[ c + 2 ] = pos[ o1 + 2 ];
 
-			this.colorArray[ c + 3 ] = pos[ o2 ];
-			this.colorArray[ c + 4 ] = pos[ o2 + 1 ];
-			this.colorArray[ c + 5 ] = pos[ o2 + 2 ];
+			scope.colorArray[ c + 3 ] = pos[ o2 ];
+			scope.colorArray[ c + 4 ] = pos[ o2 + 1 ];
+			scope.colorArray[ c + 5 ] = pos[ o2 + 2 ];
 
-			this.colorArray[ c + 6 ] = pos[ o3 ];
-			this.colorArray[ c + 7 ] = pos[ o3 + 1 ];
-			this.colorArray[ c + 8 ] = pos[ o3 + 2 ];
+			scope.colorArray[ c + 6 ] = pos[ o3 ];
+			scope.colorArray[ c + 7 ] = pos[ o3 + 1 ];
+			scope.colorArray[ c + 8 ] = pos[ o3 + 2 ];
 
 		}
 
-		this.count += 3;
+		scope.count += 3;
 
-		if ( this.count >= this.maxCount - 3 ) {
+		if ( scope.count >= scope.maxCount - 3 ) {
 
-			this.hasPositions = true;
-			this.hasNormals = true;
+			scope.hasPositions = true;
+			scope.hasNormals = true;
 
-			if ( this.enableUvs ) {
+			if ( scope.enableUvs ) {
 
-				this.hasUvs = true;
+				scope.hasUvs = true;
 
 			}
 
-			if ( this.enableColors ) {
+			if ( scope.enableColors ) {
 
-				this.hasColors = true;
+				scope.hasColors = true;
 
 			}
 
-			renderCallback( this );
+			renderCallback( scope );
 
 		}
 
 	};
 
-	this.begin = function( ) {
+	this.begin = function () {
 
 		this.count = 0;
 
@@ -424,7 +425,7 @@ THREE.MarchingCubes = function ( resolution, material, enableUvs, enableColors )
 
 	};
 
-	this.end = function( renderCallback ) {
+	this.end = function ( renderCallback ) {
 
 		if ( this.count === 0 ) return;
 
@@ -460,7 +461,10 @@ THREE.MarchingCubes = function ( resolution, material, enableUvs, enableColors )
 	// Adds a reciprocal ball (nice and blobby) that, to be fast, fades to zero after
 	// a fixed distance, determined by strength and subtract.
 
-	this.addBall = function( ballx, bally, ballz, strength, subtract ) {
+	this.addBall = function ( ballx, bally, ballz, strength, subtract ) {
+
+		var sign = Math.sign( strength );
+		strength = Math.abs( strength );
 
 		// Let's solve the equation to find the radius:
 		// 1.0 / (0.000001 + radius^2) * strength - subtract = 0
@@ -503,7 +507,7 @@ THREE.MarchingCubes = function ( resolution, material, enableUvs, enableColors )
 
 					fx = x / this.size - ballx;
 					val = strength / ( 0.000001 + fx * fx + fy2 + fz2 ) - subtract;
-					if ( val > 0.0 ) this.field[ y_offset + x ] += val;
+					if ( val > 0.0 ) this.field[ y_offset + x ] += val * sign;
 
 				}
 
@@ -672,7 +676,7 @@ THREE.MarchingCubes = function ( resolution, material, enableUvs, enableColors )
 					var fx = ( x - this.halfsize ) / this.halfsize; //+ 1
 					var q = y_offset + x;
 
-					this.polygonize( fx, fy, fz, q, this.isolation, renderCallback );
+					polygonize( fx, fy, fz, q, this.isolation, renderCallback );
 
 				}
 
@@ -691,46 +695,29 @@ THREE.MarchingCubes = function ( resolution, material, enableUvs, enableColors )
 
 		var geo_callback = function( object ) {
 
-			var i, x, y, z, vertex, normal,
-				face, a, b, c, na, nb, nc, nfaces;
-
+			for ( var i = 0; i < object.count; i ++ ) {
 
-			for ( i = 0; i < object.count; i ++ ) {
-
-				a = i * 3;
-				b = a + 1;
-				c = a + 2;
-
-				x = object.positionArray[ a ];
-				y = object.positionArray[ b ];
-				z = object.positionArray[ c ];
-				vertex = new THREE.Vector3( x, y, z );
-
-				x = object.normalArray[ a ];
-				y = object.normalArray[ b ];
-				z = object.normalArray[ c ];
-				normal = new THREE.Vector3( x, y, z );
-				normal.normalize();
+				var vertex = new THREE.Vector3().fromArray( object.positionArray, i * 3 );
+				var normal = new THREE.Vector3().fromArray( object.normalArray, i * 3 );
 
 				geo.vertices.push( vertex );
 				normals.push( normal );
 
 			}
 
-			nfaces = object.count / 3;
+			var nfaces = object.count / 3;
 
 			for ( i = 0; i < nfaces; i ++ ) {
 
-				a = ( start + i ) * 3;
-				b = a + 1;
-				c = a + 2;
-
-				na = normals[ a ];
-				nb = normals[ b ];
-				nc = normals[ c ];
+				var a = ( start + i ) * 3;
+				var b = a + 1;
+				var c = a + 2;
 
-				face = new THREE.Face3( a, b, c, [ na, nb, nc ] );
+				var na = normals[ a ];
+				var nb = normals[ b ];
+				var nc = normals[ c ];
 
+				var face = new THREE.Face3( a, b, c, [ na, nb, nc ] );
 				geo.faces.push( face );
 
 			}