查看: 1228|回复: 0
打印 上一主题 下一主题

[其他] 三个点生成抛物线 ,计算组成抛物线的点

[复制链接]

2317

主题

54

听众

2万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
20645
精华
62

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2015-10-28 00:30:11 |只看该作者 |倒序浏览
  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. }
复制代码
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2025-7-22 13:43 , Processed in 0.074501 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部