using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CusCurve {
/// <summary>
/// 获取曲线 计算多少个点,组成线
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static Vector3[] GetCrvePaths(Vector3[] path, float sinuosity, float sinuosityOffset,int pointLength,int pointInterval)
{
if (path == null || path.Length < 2)
{
return null;
}
Vector3 sinuosityVec = Vector3.Lerp(path[0], path[1], sinuosity);
//DebugLogHelper.Instance.Log("sinuosityVec:" + sinuosityVec);
sinuosityVec.y += sinuosityOffset;
//DebugLogHelper.Instance.Log("sinuosityVec Offset:" + sinuosityVec);
path = new Vector3[] { path[0], sinuosityVec, path[1] };
float dis = 0;
int length = path.Length;
for (int i = 0; i < length; i++)
{
if (i + 1 == length)
{
continue;
}
dis += Vector3.Distance(path[i], path[i + 1]);
}
int SmoothAmount = (int)dis / (pointLength * pointInterval);
Vector3[] paths = new Vector3[SmoothAmount+1];
try
{
Vector3[] vector3s = PathControlPointGenerator(path);
Vector3 prevPt = Interp(vector3s, 0);
paths[0] = prevPt;
for (int i = 1; i <= SmoothAmount; i++)
{
float pm = (float)i / SmoothAmount;
Vector3 currPt = Interp(vector3s, pm);
prevPt = currPt;
paths[i] = prevPt;
}
// path[SmoothAmount]
}
catch (System.Exception ex)
{
}
return paths;
}
/// <summary>
/// 获取UI曲线 通用
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static Vector3[] GetCrvePaths(Vector3[] path, int pointLength, int pointInterval)
{
if (path == null || path.Length < 2)
{
return null;
}
float dis = 0;
int length = path.Length;
for (int i = 0; i < length; i++)
{
if (i + 1 == length)
{
continue;
}
dis += Vector3.Distance(path[i], path[i + 1]);
}
int SmoothAmount = (int)dis / (pointLength * pointInterval);
Vector3[] paths = new Vector3[SmoothAmount + 1];
try
{
Vector3[] vector3s = PathControlPointGenerator(path);
Vector3 prevPt = Interp(vector3s, 0);
paths[0] = prevPt;
for (int i = 1; i <= SmoothAmount; i++)
{
float pm = (float)i / SmoothAmount;
Vector3 currPt = Interp(vector3s, pm);
prevPt = currPt;
paths[i] = prevPt;
}
// path[SmoothAmount]
}
catch (System.Exception ex)
{
}
return paths;
}
/// <summary>
/// 获取曲线 通用
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static Vector3[] GetCrvePaths(Vector3[] path)
{
int SmoothAmount = path.Length*10;
Vector3[] paths = new Vector3[SmoothAmount];
try
{
Vector3[] vector3s = PathControlPointGenerator(path);
Vector3 prevPt = Interp(vector3s, 0);
paths[0] = prevPt;
for (int i = 1; i <= SmoothAmount; i++)
{
float pm = (float)i / SmoothAmount;
Vector3 currPt = Interp(vector3s, pm);
prevPt = currPt;
paths[i] = prevPt;
}
}
catch (System.Exception ex)
{
}
return paths;
}
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];
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];
Array.Copy(vector3s, tmpLoopSpline, vector3s.Length);
tmpLoopSpline[0] = tmpLoopSpline[tmpLoopSpline.Length - 3];
tmpLoopSpline[tmpLoopSpline.Length - 1] = tmpLoopSpline[2];
vector3s = new Vector3[tmpLoopSpline.Length];
Array.Copy(tmpLoopSpline, vector3s, tmpLoopSpline.Length);
}
return (vector3s);
}
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
);
}
}
计算曲线
最新推荐文章于 2024-10-15 21:22:21 发布