- 最后登录
- 2016-8-29
- 注册时间
- 2012-8-25
- 阅读权限
- 90
- 积分
- 23585
  
- 纳金币
- 20645
- 精华
- 62
|
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- //iTween里抄来的
- public class otherTest : MonoBehaviour {
- /// <summary>
- /// 三点计算抛物线.
- /// </summary>
- /// <returns>组成抛物线的点.</returns>
- /// <param name="path">确定抛物线的三个点或者更多的点的数组.</param>
- public static List<Vector3> DrawPathHelper(Vector3[] path){
- List<Vector3> array = new List<Vector3>(177);
- Vector3[] vector3s = PathControlPointGenerator(path);
- //Line Draw:
- Vector3 prevPt = Interp(vector3s,0);
- int SmoothAmount = path.Length*20;
- for (int i = 1; i <= SmoothAmount; i++)
- {
- float pm = (float) i / SmoothAmount;
- Vector3 currPt = Interp(vector3s,pm);
- array.Add(currPt);
- prevPt = currPt;
- }
- return array;
- }
-
- private static Vector3[] PathControlPointGenerator(Vector3[] path){
- Vector3[] suppliedPath;
- Vector3[] vector3s;
-
- //create and store path points:
- suppliedPath = path;
-
- //populate calculate path;
- int offset = 2;
- vector3s = new Vector3[suppliedPath.Length+offset];
- System.Array.Copy(suppliedPath,0,vector3s,1,suppliedPath.Length);
-
- //populate start and end control points:
- //vector3s[0] = vector3s[1] - vector3s[2];
- vector3s[0] = vector3s[1] + (vector3s[1] - vector3s[2]);
- vector3s[vector3s.Length-1] = vector3s[vector3s.Length-2] + (vector3s[vector3s.Length-2] - vector3s[vector3s.Length-3]);
-
- //is this a closed, continuous loop? yes? well then so let's make a continuous Catmull-Rom spline!
- if(vector3s[1] == vector3s[vector3s.Length-2]){
- Vector3[] tmpLoopspline = new Vector3[vector3s.Length];
- System.Array.Copy(vector3s,tmpLoopSpline,vector3s.Length);
- tmpLoopSpline[0]=tmpLoopSpline[tmpLoopSpline.Length-3];
- tmpLoopSpline[tmpLoopSpline.Length-1]=tmpLoopSpline[2];
- vector3s=new Vector3[tmpLoopSpline.Length];
- System.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 ):
- private static Vector3 Interp(Vector3[] pts, float t){
- int numSections = pts.Length - 3;
- int currPt = Mathf.Min(Mathf.FloorToInt(t * (float) numSections), numSections - 1);
- float u = t * (float) numSections - (float) currPt;
-
- Vector3 a = pts[currPt];
- Vector3 b = pts[currPt + 1];
- Vector3 c = pts[currPt + 2];
- Vector3 d = pts[currPt + 3];
-
- 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
- );
- }
- }
复制代码 |
|