瀏覽代碼

get tangent from Spline.getPointAt

trethaller 5 年之前
父節點
當前提交
cc24f7bc15
共有 1 個文件被更改,包括 13 次插入6 次删除
  1. 13 6
      hrt/prefab/l3d/Spline.hx

+ 13 - 6
hrt/prefab/l3d/Spline.hx

@@ -159,7 +159,7 @@ class Spline extends Object3D {
 	}
 	}
 
 
 	// Return an interpolation of two samples at t, 0 <= t <= 1
 	// Return an interpolation of two samples at t, 0 <= t <= 1
-	public function getPointAt( t : Float ) : h3d.col.Point {
+	public function getPointAt( t : Float, ?pos: h3d.col.Point, ?tangent: h3d.col.Point ) : h3d.col.Point {
 		if( data == null )
 		if( data == null )
 			computeSplineData();
 			computeSplineData();
 
 
@@ -172,17 +172,24 @@ class Spline extends Object3D {
 		s1 = hxd.Math.iclamp(s1, 0, data.samples.length - 1);
 		s1 = hxd.Math.iclamp(s1, 0, data.samples.length - 1);
 		s2 = hxd.Math.iclamp(s2, 0, data.samples.length - 1);
 		s2 = hxd.Math.iclamp(s2, 0, data.samples.length - 1);
 
 
+		if(pos == null)
+			pos = new h3d.col.Point();
+
 		// End/Beginning of the curve, just return the point
 		// End/Beginning of the curve, just return the point
-		if( s1 == s2 )
-			return data.samples[s1].pos;
+		if( s1 == s2 ) {
+			pos.load(data.samples[s1].pos);
+			if(tangent != null)
+				tangent.load(data.samples[s1].tangent);
+		}
 		// Linear interpolation between the two samples
 		// Linear interpolation between the two samples
 		else {
 		else {
 			var segmentLength = data.samples[s1].pos.distance(data.samples[s2].pos);
 			var segmentLength = data.samples[s1].pos.distance(data.samples[s2].pos);
 			var t = (l - (s1 * step)) / segmentLength;
 			var t = (l - (s1 * step)) / segmentLength;
-			var result = new h3d.Vector();
-			result.lerp(data.samples[s1].pos.toVector(), data.samples[s2].pos.toVector(), t);
-			return result.toPoint();
+			pos.lerp(data.samples[s1].pos, data.samples[s2].pos, t);
+			if(tangent != null)
+				tangent.lerp(data.samples[s1].tangent, data.samples[s2].tangent, t);
 		}
 		}
+		return pos;
 	}
 	}
 
 
 	// Return the euclidean distance between the two points
 	// Return the euclidean distance between the two points