private Vector3 GetInterpPathV3() { float t = currentPercentage - pathWeight[pathWeight.Length - 1]; if (t >= pathWeight[currentStep]) { pathWeight[pathWeight.Length - 1] += pathWeight[currentStep]; t -= pathWeight[currentStep]; ++currentStep; } t /= pathWeight[currentStep]; switch (pathType) { case PathType.Linear: return(Vector3.Lerp(pathNodes[LimitRangeInt(currentStep, pathNodes.Length - 1)], pathNodes[LimitRangeInt(currentStep + 1, pathNodes.Length - 1)], t)); // 线性插值 case PathType.CatmullRom: return(TweenMath.CatmullRomPoint(pathNodes[LimitRangeInt(currentStep - 1, pathNodes.Length - 1)], pathNodes[LimitRangeInt(currentStep, pathNodes.Length - 1)], pathNodes[LimitRangeInt(currentStep + 1, pathNodes.Length - 1)], pathNodes[LimitRangeInt(currentStep + 2, pathNodes.Length - 1)], t)); default: return(Vector3.zero); } }
void OnDrawGizmos() //画线 { #if UNITY_EDITOR Handles.color = DebugColor; Matrix4x4 mtx = GetCurveMatrix(); Vector3 oldVector3 = Vector3.zero, nowVector3 = Vector3.zero; float part = 20; for (int i = 0; i < localNodes.Count; i++) { if (i != localNodes.Count - 1 || isClosedLoop) { oldVector3 = TweenMath.CatmullRomPoint(localNodes[LimitRangeInt(i - 1, localNodes.Count - 1)], localNodes[LimitRangeInt(i, localNodes.Count - 1)], localNodes[LimitRangeInt(i + 1, localNodes.Count - 1)], localNodes[LimitRangeInt(i + 2, localNodes.Count - 1)], 0); oldVector3 = mtx * ToVec4(oldVector3); for (int j = 1; j <= part; j++) { nowVector3 = TweenMath.CatmullRomPoint(localNodes[LimitRangeInt(i - 1, localNodes.Count - 1)], localNodes[LimitRangeInt(i, localNodes.Count - 1)], localNodes[LimitRangeInt(i + 1, localNodes.Count - 1)], localNodes[LimitRangeInt(i + 2, localNodes.Count - 1)], j / part); nowVector3 = mtx * ToVec4(nowVector3); Handles.DrawLine(oldVector3, nowVector3); oldVector3 = nowVector3; } } } #endif }
private void InitPathWeight() { currentStep = 0; if (pathType == PathType.CatmullRom) { float part = 20; pathWeight = new float[pathNodes.Length]; float sum = 0; Vector3 oldVector3 = Vector3.zero, nowVector3 = Vector3.zero; for (int i = 0; i < pathNodes.Length - 1; i++) { pathWeight[i] = 0; //此时保存长度 oldVector3 = TweenMath.CatmullRomPoint(pathNodes[LimitRangeInt(i - 1, pathNodes.Length - 1)], pathNodes[LimitRangeInt(i, pathNodes.Length - 1)], pathNodes[LimitRangeInt(i + 1, pathNodes.Length - 1)], pathNodes[LimitRangeInt(i + 2, pathNodes.Length - 1)], 0); for (int j = 1; j <= part; j++) { nowVector3 = TweenMath.CatmullRomPoint(pathNodes[LimitRangeInt(i - 1, pathNodes.Length - 1)], pathNodes[LimitRangeInt(i, pathNodes.Length - 1)], pathNodes[LimitRangeInt(i + 1, pathNodes.Length - 1)], pathNodes[LimitRangeInt(i + 2, pathNodes.Length - 1)], j / part); pathWeight[i] += Vector3.Distance(oldVector3, nowVector3); oldVector3 = nowVector3; } sum += pathWeight[i]; } for (int i = 0; i < pathWeight.Length - 1; i++) { pathWeight[i] = pathWeight[i] / sum; // 插值百分比 } pathWeight[pathWeight.Length - 1] = 0; //保存已插值完成的百分比 } if (pathType == PathType.Linear) { // 根据距离分配时间 pathWeight = new float[pathNodes.Length]; float sum = 0; for (int i = 0; i < pathNodes.Length - 1; i++) { pathWeight[i] = Vector3.Distance(pathNodes[i], pathNodes[i + 1]); sum += pathWeight[i]; } for (int i = 0; i < pathWeight.Length - 1; i++) { pathWeight[i] = pathWeight[i] / sum; // 插值百分比 } pathWeight[pathWeight.Length - 1] = 0; //已插值完成的百分比 } }
/// <summary> /// 插值入口 /// </summary> /// <returns></returns> float GetInterp(float fromValue, float aimValue, float current, float total) { switch (easeType) { case Ease.InBack: return(TweenMath.InBack(fromValue, aimValue, current, total)); case Ease.OutBack: return(TweenMath.OutBack(fromValue, aimValue, current, total)); case Ease.InOutBack: return(TweenMath.InOutBack(fromValue, aimValue, current, total)); case Ease.OutInBack: return(TweenMath.OutInBack(fromValue, aimValue, current, total)); case Ease.InQuad: return(TweenMath.InQuad(fromValue, aimValue, current, total)); case Ease.OutQuad: return(TweenMath.OutQuad(fromValue, aimValue, current, total)); case Ease.InoutQuad: return(TweenMath.InoutQuad(fromValue, aimValue, current, total)); case Ease.InCubic: return(TweenMath.InCubic(fromValue, aimValue, current, total)); case Ease.OutCubic: return(TweenMath.OutCubic(fromValue, aimValue, current, total)); case Ease.InoutCubic: return(TweenMath.InoutCubic(fromValue, aimValue, current, total)); case Ease.InQuart: return(TweenMath.InQuart(fromValue, aimValue, current, total)); case Ease.OutQuart: return(TweenMath.OutQuart(fromValue, aimValue, current, total)); case Ease.InOutQuart: return(TweenMath.InOutQuart(fromValue, aimValue, current, total)); case Ease.OutInQuart: return(TweenMath.OutInQuart(fromValue, aimValue, current, total)); case Ease.InQuint: return(TweenMath.InQuint(fromValue, aimValue, current, total)); case Ease.OutQuint: return(TweenMath.OutQuint(fromValue, aimValue, current, total)); case Ease.InOutQuint: return(TweenMath.InOutQuint(fromValue, aimValue, current, total)); case Ease.OutInQuint: return(TweenMath.OutInQuint(fromValue, aimValue, current, total)); case Ease.InSine: return(TweenMath.InSine(fromValue, aimValue, current, total)); case Ease.OutSine: return(TweenMath.OutSine(fromValue, aimValue, current, total)); case Ease.InOutSine: return(TweenMath.InOutSine(fromValue, aimValue, current, total)); case Ease.OutInSine: return(TweenMath.OutInSine(fromValue, aimValue, current, total)); case Ease.InExpo: return(TweenMath.InExpo(fromValue, aimValue, current, total)); case Ease.OutExpo: return(TweenMath.OutExpo(fromValue, aimValue, current, total)); case Ease.InOutExpo: return(TweenMath.InOutExpo(fromValue, aimValue, current, total)); case Ease.OutInExpo: return(TweenMath.OutInExpo(fromValue, aimValue, current, total)); case Ease.InBounce: return(TweenMath.InBounce(fromValue, aimValue, current, total)); case Ease.OutBounce: return(TweenMath.OutBounce(fromValue, aimValue, current, total)); case Ease.InOutBounce: return(TweenMath.InOutBounce(fromValue, aimValue, current, total)); case Ease.OutInBounce: return(TweenMath.OutInBounce(fromValue, aimValue, current, total)); case Ease.Default: return(curve.Evaluate(current / total) * (aimValue - fromValue) + fromValue); // 线性 default: return(Mathf.Lerp(fromValue, aimValue, current / total)); } }