我找到了下面的类,它接收一个Vector3数组点,并返回一个新点的Vector3数组来制作一条曲线。您可以在以下屏幕截图中看到结果,其中多维数据集是原始点,蓝线是该类的结果。
我想要实现的是曲线,但是线条连接到立方体就像第一个和最后一个立方体一样,这样的结果(蓝点=原点;橙点=新点):
public class Curver : MonoBehaviour{
//arrayToCurve is original Vector3 array, smoothness is the number of interpolations.
public static Vector3[] MakeSmoothCurve(Vector3[] arrayToCurve, float smoothness){
List<Vector3> points;
List<Vector3> curvedPoints;
int pointsLength = 0;
int curvedLength = 0;
if(smoothness < 1.0f) smoothness = 1.0f;
pointsLength = arrayToCurve.Length;
curvedLength = (pointsLength*Mathf.RoundToInt(smoothness))-1;
curvedPoints = new List<Vector3>(curvedLength);
float t = 0.0f;
for(int pointInTimeOnCurve = 0;pointInTimeOnCurve < curvedLength+1;pointInTimeOnCurve++){
t = Mathf.InverseLerp(0,curvedLength,pointInTimeOnCurve);
points = new List<Vector3>(arrayToCurve);
for(int j = pointsLength-1; j > 0; j--){
for (int i = 0; i < j; i++){
points[i] = (1-t)*points[i] + t*points[i+1];
}
}
curvedPoints.Add(points[0]);
}
return(curvedPoints.ToArray());
}
}
以下是我使用该课程的方式:
void OnDrawGizmos(){
Vector3[] points = Curver.MakeSmoothCurve(toVector3Array(wayPoints), 10f);
bool ptset = false;
Vector3 lastpt = Vector3.zero;
for(int j = 0; j < points.Length; j++){
Vector3 wayPoint = points[j];
if(ptset){
Gizmos.color = new Color(0, 0, 1, 0.5f);
Gizmos.DrawLine(lastpt, wayPoint);
}
lastpt = wayPoint;
ptset = true;
}
if(isCircular){
Gizmos.DrawLine(lastpt, wayPoints[0].position);
}
}