123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- 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);
- }
- for (int i = 0; i < allbs.Count; i++)
- {
- _time.Add(allbs[i] / allb);
- }
- }
- 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)
- {
- Vector3 a = PosList[0];
- for (int i = 1; i < PosList.Count; i++)
- {
- Vector3 b = PosList[i];
- Vector3 c = Vector3.Lerp(a, b, t);
- a = c;
- }
- return a;
- }
- }
- }
|