using System.Collections.Generic; using UnityEngine; namespace Core.Utility { /// /// 贝塞尔曲线 /// public class BesselPath { public List PosList; private List _time = new List(); public void SetPos(List PosList) { this.PosList = PosList; List allbs = new List(); 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; } } }