Browse Source

CatmullRomCurve3: Support .closed property, update tests

Joshua Koo 9 năm trước cách đây
mục cha
commit
96de7b7eca

+ 17 - 13
src/extras/curves/CatmullRomCurve3.js

@@ -96,37 +96,41 @@ THREE.CatmullRomCurve3 = ( function() {
 
 			if ( l < 2 ) console.log( 'duh, you need at least 2 points' );
 
-			point = ( l - 1 ) * t;
+			point = ( l - ( this.closed ? 0 : 1 ) ) * t;
 			intPoint = Math.floor( point );
 			weight = point - intPoint;
-
-			if ( weight === 0 && intPoint === l - 1 ) {
+			
+			if ( this.closed ) {
+				
+				intPoint += intPoint > 0 ? 0 : ( Math.floor( Math.abs( intPoint ) / points.length ) + 1 ) * points.length;
+				
+			} else if ( weight === 0 && intPoint === l - 1 ) {
 
 				intPoint = l - 2;
 				weight = 1;
 
 			}
 
-			var p0, p1, p2, p3;
+			var p0, p1, p2, p3; // 4 points
 
-			if ( intPoint === 0 ) {
+			if ( this.closed || intPoint > 0 ) {
 
-				// extrapolate first point
-				tmp.subVectors( points[ 0 ], points[ 1 ] ).add( points[ 0 ] );
-				p0 = tmp;
+				p0 = points[ ( intPoint - 1 ) % l ];
 
 			} else {
 
-				p0 = points[ intPoint - 1 ];
+				// extrapolate first point
+				tmp.subVectors( points[ 0 ], points[ 1 ] ).add( points[ 0 ] );
+				p0 = tmp;
 
 			}
 
-			p1 = points[ intPoint ];
-			p2 = points[ intPoint + 1 ];
+			p1 = points[ intPoint % l ];
+			p2 = points[ ( intPoint + 1 ) % l ];
 
-			if ( intPoint + 2 < l ) {
+			if ( this.closed || intPoint + 2 < l ) {
 
-				p3 = points[ intPoint + 2 ]
+				p3 = points[ ( intPoint + 2 ) % l ]
 
 			} else {
 

+ 28 - 0
test/unit/extras/curves/CatmullRomCurve3.js

@@ -94,4 +94,32 @@ test( "centripetal basic check", function() {
 	var desc = error ? ' ' + error : '';
 	ok( !error, 'Lists of Vectors3 should be equal.' + desc );
 
+});
+
+test( "closed catmullrom basic check", function() {
+
+	var curve = new THREE.CatmullRomCurve3( positions );
+	curve.type = 'catmullrom';
+	curve.closed = true;
+
+	var closedSplinePoints = [
+		new THREE.Vector3(-60,-100,60),
+		new THREE.Vector3(-67.5,-46.25,67.5),
+		new THREE.Vector3(-60,20,60),
+		new THREE.Vector3(-67.5,83.75,67.5),
+		new THREE.Vector3(-60,120,60),
+		new THREE.Vector3(0,83.75,0),
+		new THREE.Vector3(60,20,-60),
+		new THREE.Vector3(75,-46.25,-75),
+		new THREE.Vector3(60,-100,-60),
+		new THREE.Vector3(0,-115,0),
+		new THREE.Vector3(-60,-100,60),
+	];
+
+	var getPoints = curve.getPoints(10);
+	var error = vectorsAreEqual( getPoints , closedSplinePoints );
+	ok( getPoints.length == 11, 'getPoints should be equal.');
+	var desc = error ? ' ' + error : '';
+	ok( !error, 'Lists of Vectors3 should be equal.' + desc );
+
 });