瀏覽代碼

Adjust initial binormal computations for THREE.TubeGeometry

zz85 13 年之前
父節點
當前提交
607bd948f6
共有 1 個文件被更改,包括 41 次插入43 次删除
  1. 41 43
      src/extras/geometries/TubeGeometry.js

+ 41 - 43
src/extras/geometries/TubeGeometry.js

@@ -48,29 +48,38 @@ THREE.TubeGeometry = function(radius, segments, segmentsRadius, path, debug) {
 		tang = this.path.getTangentAt(u);
 
 		if (oldB === undefined) {
-			//arbitrary vector method 1
-			//oldB = new THREE.Vector3(Math.random(), Math.random(), Math.random()).normalize();
-
-			// Method 2
-			//oldB = new THREE.Vector3( 0, -1, 0 ); // test to see what happens to a known binormal vector
-
-			// Method 3
-			// t1 = u - epsilon;
-			// if (t1 < 0) t1 = 0;
-			// t1 = this.path.getTangentAt(t1);
-			// t2 = u + epsilon;
-			// if (t2 > 1) t2 = 1;
-			// t2 = this.path.getTangentAt(t2);
-
-			// normal.sub(t2, t1).normalize();
-
-			// binormal.cross(tang, normal);
-			// oldB = binormal;
-
-			// Method 4
-
-			// Find the smallest componenet of the tangent vector (x, y, or z) and set the binormal
-			// equal to the unit vector in that direction ((1, 0, 0), (0, 1, 0), or (0, 0, 1))
+			// Method 1, random arbitrary vector
+			// oldB = new THREE.Vector3(Math.random(), Math.random(), Math.random()).normalize();
+
+			// Method 2, use a fixed start binormal. Has dangers of 0 vectors too.
+			// oldB = new THREE.Vector3( 0, -1, 0 );
+
+			// Method 3 - This uses the Frenet–Serret formula for deriving binormal
+			var t1, t2;
+			t1 = u - epsilon;
+			if (t1 < 0) t1 = 0;
+			t1 = this.path.getTangentAt(t1);
+			t2 = u + epsilon;
+			if (t2 > 1) t2 = 1;
+			t2 = this.path.getTangentAt(t2);
+
+			normal.sub(t2, t1).normalize();
+
+			binormal.cross(tang, normal);
+			oldB = binormal;
+
+			if (oldB.length()==0) {
+
+			// When binormal is a zero vector, we could brute force another vector ?
+			// oldB.set( 1, 0, 0 );
+			// if (normal.cross(oldB, tang).normalize().length()==0) {
+			// 	oldB.set( 0, 1, 0 );
+			// 	if (normal.cross(oldB, tang).normalize().length()==0) {
+			// 		oldB.set( 0, 0, 1 );
+			// 	}
+			// }
+
+			// Method 4 - Sets binormal direction in the smallest tangent xyz component
 			var smallest = Number.MAX_VALUE;
 			var x, y, z;
 			var tx = Math.abs(tang.x);
@@ -79,29 +88,20 @@ THREE.TubeGeometry = function(radius, segments, segmentsRadius, path, debug) {
 
 			if (tx <= smallest) {
 				smallest = tx;
-
-				x = 1;
-				y = 0;
-				z = 0;
+				oldB.set(1,0,0);
 			}
-
+ 
 			if (ty <= smallest) {
 				smallest = ty;
-
-				x = 0;
-				y = 1;
-				z = 0;
+				oldB.set(0,1,0);
 			}
-
+ 
 			if (tz <= smallest) {
-
-				x = 0;
-				y = 0;
-				z = 1;
+				oldB.set(0,0,1);
 			}
-
-			oldB = new THREE.Vector3(x, y, z);
+ 
 		}
+
 		normal.cross(oldB, tang).normalize();
 		binormal.cross(tang, normal).normalize();
 		oldB = binormal;
@@ -109,6 +109,8 @@ THREE.TubeGeometry = function(radius, segments, segmentsRadius, path, debug) {
 
 		if (this.debug) {
 			this.debug.add(new THREE.ArrowHelper(normal, pos, radius * 2, 0xff0000));
+			// this.debug.add(new THREE.ArrowHelper(binormal, pos, radius * 2, 0x00ff00));
+			// this.debug.add(new THREE.ArrowHelper(tang, pos, radius * 2, 0x0000ff));
 		}
 		
 
@@ -160,10 +162,6 @@ THREE.TubeGeometry = function(radius, segments, segmentsRadius, path, debug) {
 		return scope.vertices.push(new THREE.Vertex(new THREE.Vector3(x, y, z))) - 1;
 	}
 
-	function getPathPos(u, path) {
-		return path.getPoint(u);
-	}
-
 };
 
 THREE.TubeGeometry.prototype = new THREE.Geometry();