unity3D:游戏分解之曲线

时间:2022-05-18 18:48:02

一提到曲线,很多新手就头疼了,包括我。查了很多资料,终于有个大概的了解。想深入了解曲线原理的,推荐一个链接http://www.cnblogs.com/jay-dong/archive/2012/09/26/2704188.html

之前写了一篇博文《unity3D:游戏分解之角色移动和相机跟随》,里面用到了曲线插值,这里算是对上篇博文的一个补充

先看一下曲线的效果

unity3D:游戏分解之曲线

在使用NGUI的过程中,发现iTween.cs里面有两个很有用的方法,一个是输入指定路点数组,一个就是曲线的插值算法。今天我们主要就用到这两个方法来实现曲线效果。

 public static Vector3[] PathControlPointGenerator(Vector3[] path)
{
Vector3[] suppliedPath;
Vector3[] vector3s; //create and store path points:
suppliedPath = path; //populate calculate path;
int offset = ;
vector3s = new Vector3[suppliedPath.Length + offset];
Array.Copy(suppliedPath, , vector3s, , suppliedPath.Length); //populate start and end control points:
//vector3s[0] = vector3s[1] - vector3s[2];
vector3s[] = vector3s[] + (vector3s[] - vector3s[]);
vector3s[vector3s.Length - ] = vector3s[vector3s.Length - ] + (vector3s[vector3s.Length - ] - vector3s[vector3s.Length - ]); //is this a closed, continuous loop? yes? well then so let's make a continuous Catmull-Rom spline!
if (vector3s[] == vector3s[vector3s.Length - ])
{
Vector3[] tmpLoopSpline = new Vector3[vector3s.Length];
Array.Copy(vector3s, tmpLoopSpline, vector3s.Length);
tmpLoopSpline[] = tmpLoopSpline[tmpLoopSpline.Length - ];
tmpLoopSpline[tmpLoopSpline.Length - ] = tmpLoopSpline[];
vector3s = new Vector3[tmpLoopSpline.Length];
Array.Copy(tmpLoopSpline, vector3s, tmpLoopSpline.Length);
} return (vector3s);
} //andeeee from the Unity forum's steller Catmull-Rom class ( http://forum.unity3d.com/viewtopic.php?p=218400#218400 ):
public static Vector3 Interp(Vector3[] pts, float t)
{
int numSections = pts.Length - ;
int currPt = Mathf.Min(Mathf.FloorToInt(t * (float)numSections), numSections - );
float u = t * (float)numSections - (float)currPt; if(currPt == )
{
int dsd = ;
} Vector3 a = pts[currPt];
Vector3 b = pts[currPt + ];
Vector3 c = pts[currPt + ];
Vector3 d = pts[currPt + ]; return .5f * (
(-a + 3f * b - 3f * c + d) * (u * u * u)
+ (2f * a - 5f * b + 4f * c - d) * (u * u)
+ (-a + c) * u
+ 2f * b
);
}

直接上完整代码,把这个脚本放到相机上,然后在场景中拖几个物件作为路点,就可以实现上面的效果

 using System;
using System.Collections.Generic;
using UnityEngine; namespace Fish.Study.Curve
{
/// <summary>
/// 曲线测试
/// </summary>
public class CurveTest : MonoBehaviour
{
//路点
public GameObject[] GameObjectList;
//各路点的坐标
public List<Vector3> TransDataList = new List<Vector3>(); void Start()
{
} //Gizmos
void OnDrawGizmos()
{
//1个点是不能画出曲线的,2个点实际上是直线
if (GameObjectList.Length <= ) return; TransDataList.Clear();
for (int i = ; i < GameObjectList.Length; ++i)
{
TransDataList.Add(GameObjectList[i].transform.position);
} if (TransDataList != null && TransDataList.Count > )
{
DrawPathHelper(TransDataList.ToArray(), Color.red);
}
} public Vector3[] GetCurveData()
{
if (TransDataList != null && TransDataList.Count > )
{
var v3 = (TransDataList.ToArray());
Vector3[] vector3s = PathControlPointGenerator(v3);
return vector3s;
} return null;
} //NGUI iTween.cs中的方法,输入路径点
public static Vector3[] PathControlPointGenerator(Vector3[] path)
{
Vector3[] suppliedPath;
Vector3[] vector3s; //create and store path points:
suppliedPath = path; //populate calculate path;
int offset = ;
vector3s = new Vector3[suppliedPath.Length + offset];
Array.Copy(suppliedPath, , vector3s, , suppliedPath.Length); //populate start and end control points:
vector3s[] = vector3s[] + (vector3s[] - vector3s[]);
vector3s[vector3s.Length - ] = vector3s[vector3s.Length - ] + (vector3s[vector3s.Length - ] - vector3s[vector3s.Length - ]); //is this a closed, continuous loop? yes? well then so let's make a continuous Catmull-Rom spline!
if (vector3s[] == vector3s[vector3s.Length - ])
{
Vector3[] tmpLoopSpline = new Vector3[vector3s.Length];
Array.Copy(vector3s, tmpLoopSpline, vector3s.Length);
tmpLoopSpline[] = tmpLoopSpline[tmpLoopSpline.Length - ];
tmpLoopSpline[tmpLoopSpline.Length - ] = tmpLoopSpline[];
vector3s = new Vector3[tmpLoopSpline.Length];
Array.Copy(tmpLoopSpline, vector3s, tmpLoopSpline.Length);
} return (vector3s);
} //曲线插值函数
public static Vector3 Interp(Vector3[] pts, float t)
{
int numSections = pts.Length - ;
int currPt = Mathf.Min(Mathf.FloorToInt(t * (float)numSections), numSections - );
float u = t * (float)numSections - (float)currPt; Vector3 a = pts[currPt];
Vector3 b = pts[currPt + ];
Vector3 c = pts[currPt + ];
Vector3 d = pts[currPt + ]; return .5f * (
(-a + 3f * b - 3f * c + d) * (u * u * u)
+ (2f * a - 5f * b + 4f * c - d) * (u * u)
+ (-a + c) * u
+ 2f * b
);
} //画曲线
private void DrawPathHelper(Vector3[] path, Color color)
{
Vector3[] vector3s = PathControlPointGenerator(path); //Line Draw:
Vector3 prevPt = Interp(vector3s, );
int SmoothAmount = path.Length * ;
for (int i = ; i <= SmoothAmount; i++)
{
float pm = (float)i / SmoothAmount;
Vector3 currPt = Interp(vector3s, pm); Gizmos.color = color;
Gizmos.DrawSphere(currPt, 0.2f);
prevPt = currPt;
}
}
}
}