浏览代码

ParametricGeometry: Optimize code

Mugen87 8 年之前
父节点
当前提交
e23a75f434
共有 2 个文件被更改,包括 55 次插入22 次删除
  1. 34 15
      examples/js/ParametricGeometries.js
  2. 21 7
      src/geometries/ParametricGeometry.js

+ 34 - 15
examples/js/ParametricGeometries.js

@@ -7,7 +7,9 @@
 
 THREE.ParametricGeometries = {
 
-	klein: function ( v, u ) {
+	klein: function ( v, u, optionalTarget ) {
+
+		var result = optionalTarget || new THREE.Vector3();
 
 		u *= Math.PI;
 		v *= 2 * Math.PI;
@@ -28,25 +30,29 @@ THREE.ParametricGeometries = {
 
 		y = - 2 * ( 1 - Math.cos( u ) / 2 ) * Math.sin( v );
 
-		return new THREE.Vector3( x, y, z );
+		return result.set( x, y, z );
 
 	},
 
 	plane: function ( width, height ) {
 
-		return function( u, v ) {
+		return function( u, v, optionalTarget ) {
+
+			var result = optionalTarget || new THREE.Vector3();
 
 			var x = u * width;
 			var y = 0;
 			var z = v * height;
 
-			return new THREE.Vector3( x, y, z );
+			return result.set( x, y, z );
 
 		};
 
 	},
 
-	mobius: function( u, t ) {
+	mobius: function( u, t, optionalTarget ) {
+
+		var result = optionalTarget || new THREE.Vector3();
 
 		// flat mobius strip
 		// http://www.wolframalpha.com/input/?i=M%C3%B6bius+strip+parametric+equations&lk=1&a=ClashPrefs_*Surface.MoebiusStrip.SurfaceProperty.ParametricEquations-
@@ -56,28 +62,36 @@ THREE.ParametricGeometries = {
 		var x, y, z;
 
 		var a = 2;
+
 		x = Math.cos( v ) * ( a + u * Math.cos( v / 2 ) );
 		y = Math.sin( v ) * ( a + u * Math.cos( v / 2 ) );
 		z = u * Math.sin( v / 2 );
-		return new THREE.Vector3( x, y, z );
+
+		return result.set( x, y, z );
 
 	},
 
-	mobius3d: function( u, t ) {
+	mobius3d: function( u, t, optionalTarget ) {
+
+		var result = optionalTarget || new THREE.Vector3();
 
 		// volumetric mobius strip
+
 		u *= Math.PI;
 		t *= 2 * Math.PI;
 
 		u = u * 2;
 		var phi = u / 2;
 		var major = 2.25, a = 0.125, b = 0.65;
+
 		var x, y, z;
+
 		x = a * Math.cos( t ) * Math.cos( phi ) - b * Math.sin( t ) * Math.sin( phi );
 		z = a * Math.cos( t ) * Math.sin( phi ) + b * Math.sin( t ) * Math.cos( phi );
 		y = ( major + x ) * Math.sin( u );
 		x = ( major + x ) * Math.cos( u );
-		return new THREE.Vector3( x, y, z );
+
+		return result.set( x, y, z );
 
 	}
 
@@ -123,7 +137,9 @@ THREE.ParametricGeometries.TubeGeometry = function( path, segments, radius, segm
 
 
 
-	var ParametricTube = function( u, v ) {
+	var ParametricTube = function( u, v, optionalTarget ) {
+
+		var result = optionalTarget || new THREE.Vector3();
 
 		v *= 2 * Math.PI;
 
@@ -152,7 +168,7 @@ THREE.ParametricGeometries.TubeGeometry = function( path, segments, radius, segm
 		pos2.y += cx * normal.y + cy * binormal.y;
 		pos2.z += cx * normal.z + cy * binormal.z;
 
-		return pos2.clone();
+		return result.copy( pos2 );
 
 	};
 
@@ -222,7 +238,9 @@ THREE.ParametricGeometries.TorusKnotGeometry.prototype.constructor = THREE.Param
   *********************************************/
 THREE.ParametricGeometries.SphereGeometry = function( size, u, v ) {
 
-	function sphere( u, v ) {
+	function sphere( u, v, optionalTarget ) {
+
+		var result = optionalTarget || new THREE.Vector3();
 
 		u *= Math.PI;
 		v *= 2 * Math.PI;
@@ -231,8 +249,7 @@ THREE.ParametricGeometries.SphereGeometry = function( size, u, v ) {
 		var y = size * Math.sin( u ) * Math.sin( v );
 		var z = size * Math.cos( u );
 
-
-		return new THREE.Vector3( x, y, z );
+		return result.set( x, y, z );
 
 	}
 
@@ -252,13 +269,15 @@ THREE.ParametricGeometries.SphereGeometry.prototype.constructor = THREE.Parametr
 
 THREE.ParametricGeometries.PlaneGeometry = function( width, depth, segmentsWidth, segmentsDepth ) {
 
-	function plane( u, v ) {
+	function plane( u, v, optionalTarget ) {
+
+		var result = optionalTarget || new THREE.Vector3();
 
 		var x = u * width;
 		var y = 0;
 		var z = v * depth;
 
-		return new THREE.Vector3( x, y, z );
+		return result.set( x, y, z );
 
 	}
 

+ 21 - 7
src/geometries/ParametricGeometry.js

@@ -58,7 +58,11 @@ function ParametricBufferGeometry( func, slices, stacks ) {
 	var uvs = [];
 
 	var EPS = 0.00001;
-	var pu = new Vector3(), pv = new Vector3(), normal = new Vector3();
+
+	var normal = new Vector3();
+
+	var p0 = new Vector3(), p1 = new Vector3();
+	var pu = new Vector3(), pv = new Vector3();
 
 	var i, j;
 
@@ -74,28 +78,36 @@ function ParametricBufferGeometry( func, slices, stacks ) {
 
 			var u = j / slices;
 
-			var p = func( u, v );
-			vertices.push( p.x, p.y, p.z );
+			// vertex
+
+			p0 = func( u, v, p0 );
+			vertices.push( p0.x, p0.y, p0.z );
+
+			// normal
 
 			// approximate tangent vectors via finite differences
 
 			if ( u - EPS >= 0 ) {
 
-				pu.subVectors( p, func( u - EPS, v ) );
+				p1 = func( u - EPS, v, p1 );
+				pu.subVectors( p0, p1 );
 
 			} else {
 
-				pu.subVectors( func( u + EPS, v ), p );
+				p1 = func( u + EPS, v, p1 );
+				pu.subVectors( p1, p0 );
 
 			}
 
 			if ( v - EPS >= 0 ) {
 
-				pv.subVectors( p, func( u, v - EPS ) );
+				p1 = func( u, v - EPS, p1 );
+				pv.subVectors( p0, p1 );
 
 			} else {
 
-				pv.subVectors( func( u, v + EPS ), p );
+				p1 = func( u, v + EPS, p1 );
+				pv.subVectors( p1, p0 );
 
 			}
 
@@ -104,6 +116,8 @@ function ParametricBufferGeometry( func, slices, stacks ) {
 			normal.crossVectors( pu, pv ).normalize();
 			normals.push( normal.x, normal.y, normal.z );
 
+			// uv
+
 			uvs.push( u, v );
 
 		}