纳金网

标题: 三个点生成抛物线 ,计算组成抛物线的点 [打印本页]

作者: 王者再临    时间: 2015-10-28 00:30
标题: 三个点生成抛物线 ,计算组成抛物线的点
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. //iTween里抄来的
  5. public class otherTest : MonoBehaviour {

  6.         /// <summary>
  7.         /// 三点计算抛物线.
  8.         /// </summary>
  9.         /// <returns>组成抛物线的点.</returns>
  10.         /// <param name="path">确定抛物线的三个点或者更多的点的数组.</param>
  11.         public static List<Vector3> DrawPathHelper(Vector3[] path){
  12.                 List<Vector3> array = new List<Vector3>(177);
  13.                 Vector3[] vector3s = PathControlPointGenerator(path);
  14.                 //Line Draw:
  15.                 Vector3 prevPt = Interp(vector3s,0);
  16.                 int SmoothAmount = path.Length*20;
  17.                 for (int i = 1; i <= SmoothAmount; i++)
  18.                 {
  19.                         float pm = (float) i / SmoothAmount;
  20.                         Vector3 currPt = Interp(vector3s,pm);
  21.                                 array.Add(currPt);
  22.                         prevPt = currPt;
  23.                 }
  24.                 return  array;
  25.         }
  26.        
  27.         private static Vector3[] PathControlPointGenerator(Vector3[] path){
  28.                 Vector3[] suppliedPath;
  29.                 Vector3[] vector3s;
  30.                
  31.                 //create and store path points:
  32.                 suppliedPath = path;
  33.                
  34.                 //populate calculate path;
  35.                 int offset = 2;
  36.                 vector3s = new Vector3[suppliedPath.Length+offset];
  37.                 System.Array.Copy(suppliedPath,0,vector3s,1,suppliedPath.Length);
  38.                
  39.                 //populate start and end control points:
  40.                 //vector3s[0] = vector3s[1] - vector3s[2];
  41.                 vector3s[0] = vector3s[1] + (vector3s[1] - vector3s[2]);
  42.                 vector3s[vector3s.Length-1] = vector3s[vector3s.Length-2] + (vector3s[vector3s.Length-2] - vector3s[vector3s.Length-3]);
  43.                
  44.                 //is this a closed, continuous loop? yes? well then so let's make a continuous Catmull-Rom spline!
  45.                 if(vector3s[1] == vector3s[vector3s.Length-2]){
  46.                         Vector3[] tmpLoopSpline = new Vector3[vector3s.Length];
  47.                         System.Array.Copy(vector3s,tmpLoopSpline,vector3s.Length);
  48.                         tmpLoopSpline[0]=tmpLoopSpline[tmpLoopSpline.Length-3];
  49.                         tmpLoopSpline[tmpLoopSpline.Length-1]=tmpLoopSpline[2];
  50.                         vector3s=new Vector3[tmpLoopSpline.Length];
  51.                         System.Array.Copy(tmpLoopSpline,vector3s,tmpLoopSpline.Length);
  52.                 }       
  53.                
  54.                 return(vector3s);
  55.         }

  56.         //andeeee from the Unity forum's steller Catmull-Rom class ( http://forum.unity3d.com/viewtopic.php?p=218400#218400 ):
  57.         private static Vector3 Interp(Vector3[] pts, float t){
  58.                 int numSections = pts.Length - 3;
  59.                 int currPt = Mathf.Min(Mathf.FloorToInt(t * (float) numSections), numSections - 1);
  60.                 float u = t * (float) numSections - (float) currPt;
  61.                
  62.                 Vector3 a = pts[currPt];
  63.                 Vector3 b = pts[currPt + 1];
  64.                 Vector3 c = pts[currPt + 2];
  65.                 Vector3 d = pts[currPt + 3];
  66.                
  67.                 return .5f * (
  68.                         (-a + 3f * b - 3f * c + d) * (u * u * u)
  69.                         + (2f * a - 5f * b + 4f * c - d) * (u * u)
  70.                         + (-a + c) * u
  71.                         + 2f * b
  72.                         );
  73.         }       
  74. }
复制代码





欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5