123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- using System.Collections.Generic;
- using UnityEngine;
- namespace Core.Utility
- {
- /// <summary>
- /// 贝塞尔曲线
- /// </summary>
- public class BesselPath
- {
- public List<Vector3> PosList;
- private List<float> _time = new List<float>();
- public void SetPos(List<Vector3> PosList)
- {
- this.PosList = PosList;
- List<float> allbs = new List<float>();
- float allb = 0;
- for (int i = 0; i < PosList.Count; i += 3)
- {
- if (i + 3 > PosList.Count)
- {
- break;
- }
- Vector3 p1 = PosList[i];
- Vector3 p2 = PosList[i + 1];
- Vector3 p3 = PosList[i + 2];
- float b = GetPathCount(p1, p2, p3, 100);
- allb += b;
- allbs.Add(b);
- }
- _time.Clear();
- float allt = 0;
- for (int i = 0; i < allbs.Count; i++)
- {
- float currt = allbs[i] / allb;
- allt+=currt;
- _time.Add(allt);
-
- }
- }
- private float GetPathCount(Vector3 p1, Vector3 p2, Vector3 p3, int count)
- {
- float step = 1f / count;
- Vector3 startPos = p1;
- float d = 0;
- for (int i = 1; i < count; i++)
- {
- float t = i * count;
- Vector3 a = Vector3.Lerp(p1, p2, t);
- Vector3 b = Vector3.Lerp(a, p3, t);
- d += Vector3.Distance(startPos, b);
- startPos = b;
- }
- return d;
- }
- public float GetPathCount(int count)
- {
- float step = 1f / count;
- Vector3 startPos = GetValue(0);
- float d = 0;
- for (int i = 1; i < count; i++)
- {
- Vector3 currPos = GetValue(step * i);
- d += Vector3.Distance(startPos, currPos);
- }
- return d;
- }
- public Vector3 GetValue(float t)
- {
- int startIndex = 0;
- float allt = 0;
- float startt = 0;
-
- for (int i = 0; i < _time.Count; i++)
- {
- if (t < _time[i])
- {
- startIndex = i * 3;
- allt = _time[i];
- if (i > 0)
- {
- allt= _time[i] - _time[i - 1];
- startt= _time[i - 1];
- }
- break;
- }
- }
-
- float t1 = (t - startt) / allt;
- if (t1 > 1)
- {
- t1 = 1;
- }
- if (t1 < 0)
- {
- t1 = 0;
- }
-
-
- Vector3 a = PosList[startIndex];
- Vector3 p2 = PosList[startIndex + 1];
- Vector3 p3 = PosList[startIndex + 2];
- Vector3 b = Vector3.Lerp(a, p2, t1);
- Vector3 c = Vector3.Lerp(b, p3, t1);
- return c;
- }
- }
- }
|