unity 线渲染器平滑算法
Unity line renderer smooth algorithm
我需要一个!有效!平滑线渲染器的算法(基本上,给定的 Vector3 包含渲染器的点)
类似的东西
这是我的代码,但是它的 fps 非常低:
public static List<Vector3> MakeSmoothCurve(Vector3[] arrayToCurve, float smoothness)
{
List<Vector3> points;
List<Vector3> curvedPoints;
int pointsLength = 0;
int curvedLength = 0;
if (smoothness < 1.0f) smoothness = 1.0f;
pointsLength = arrayToCurve.Length;
curvedLength = (pointsLength * Mathf.RoundToInt(smoothness)) - 1;
curvedPoints = new List<Vector3>(curvedLength);
float t = 0.0f;
for (int pointInTimeOnCurve = 0; pointInTimeOnCurve < curvedLength + 1; pointInTimeOnCurve++)
{
t = Mathf.InverseLerp(0, curvedLength, pointInTimeOnCurve);
points = new List<Vector3>(arrayToCurve);
for (int j = pointsLength - 1; j > 0; j--)
{
for (int i = 0; i < j; i++)
{
points[i] = (1 - t) * points[i] + t * points[i + 1];
}
}
curvedPoints.Add(points[0]);
}
return (curvedPoints);
}
您可以使用 CurveField
https://docs.unity3d.com/ScriptReference/EditorGUILayout.CurveField.html
有了它,您可以轻松 edit/test 您的曲线并在给定时间检索一个点。
https://docs.unity3d.com/ScriptReference/AnimationCurve.Evaluate.html
我需要一个!有效!平滑线渲染器的算法(基本上,给定的 Vector3 包含渲染器的点)
类似的东西
public static List<Vector3> MakeSmoothCurve(Vector3[] arrayToCurve, float smoothness)
{
List<Vector3> points;
List<Vector3> curvedPoints;
int pointsLength = 0;
int curvedLength = 0;
if (smoothness < 1.0f) smoothness = 1.0f;
pointsLength = arrayToCurve.Length;
curvedLength = (pointsLength * Mathf.RoundToInt(smoothness)) - 1;
curvedPoints = new List<Vector3>(curvedLength);
float t = 0.0f;
for (int pointInTimeOnCurve = 0; pointInTimeOnCurve < curvedLength + 1; pointInTimeOnCurve++)
{
t = Mathf.InverseLerp(0, curvedLength, pointInTimeOnCurve);
points = new List<Vector3>(arrayToCurve);
for (int j = pointsLength - 1; j > 0; j--)
{
for (int i = 0; i < j; i++)
{
points[i] = (1 - t) * points[i] + t * points[i + 1];
}
}
curvedPoints.Add(points[0]);
}
return (curvedPoints);
}
您可以使用 CurveField
https://docs.unity3d.com/ScriptReference/EditorGUILayout.CurveField.html
有了它,您可以轻松 edit/test 您的曲线并在给定时间检索一个点。
https://docs.unity3d.com/ScriptReference/AnimationCurve.Evaluate.html