曲线插值-N阶Bezier贝塞尔曲线生成

梦想游戏人
目录:
Unity
可用于赛道的生成,当然为了性能 和精度 可以几个低阶的bezier 合并起来作为一个完整的
  • public static List<Vector3> GetBezierPoints(List<Vector3> pathToCurve, int interpolations)
  • {
  • List<Vector3> tempPoints;
  • List<Vector3> curvedPoints;
  • int pointsLength = 0;
  • int curvedLength = 0;
  • if (interpolations < 1)
  • interpolations = 1;
  • pointsLength = pathToCurve.Count;
  • curvedLength = (pointsLength * Mathf.RoundToInt(interpolations)) - 1;
  • curvedPoints = new List<Vector3>(curvedLength);
  • float t = 0.0f;
  • for (int pointInTimeOnCurve = 0; pointInTimeOnCurve < curvedLength + 1; pointInTimeOnCurve++)
  • {
  • t = Mathf.InverseLerp(0, curvedLength, pointInTimeOnCurve);
  • tempPoints = new List<Vector3>(pathToCurve);
  • for (int j = pointsLength - 1; j > 0; j--)
  • {
  • for (int i = 0; i < j; i++)
  • {
  • tempPoints[i] = (1 - t) * tempPoints[i] + t * tempPoints[i + 1];
  • }
  • }
  • curvedPoints.Add(tempPoints[0]);
  • }
  • return curvedPoints;
  • }
  public static List<Vector3> GetBezierPoints(List<Vector3> pathToCurve, int interpolations)
    {
        List<Vector3> tempPoints;
        List<Vector3> curvedPoints;
        int pointsLength = 0;
        int curvedLength = 0;

        if (interpolations < 1)
            interpolations = 1;

        pointsLength = pathToCurve.Count;
        curvedLength = (pointsLength * Mathf.RoundToInt(interpolations)) - 1;
        curvedPoints = new List<Vector3>(curvedLength);

        float t = 0.0f;
        for (int pointInTimeOnCurve = 0; pointInTimeOnCurve < curvedLength + 1; pointInTimeOnCurve++)
        {
            t = Mathf.InverseLerp(0, curvedLength, pointInTimeOnCurve);
            tempPoints = new List<Vector3>(pathToCurve);
            for (int j = pointsLength - 1; j > 0; j--)
            {
                for (int i = 0; i < j; i++)
                {
                    tempPoints[i] = (1 - t) * tempPoints[i] + t * tempPoints[i + 1];
                }
            }
            curvedPoints.Add(tempPoints[0]);
        }

        return curvedPoints;
    }

输入生成的点的个数即可,也可改写为time 来生成

Scroll Up