Skip to content

Commit

Permalink
Adapt curve resolution for existing curves on scene open
Browse files Browse the repository at this point in the history
This is added in order to prevent Unity performance degradation when
there are large curves with too high resolution.
  • Loading branch information
bgr committed Nov 19, 2018
1 parent 5d18d1e commit 16cf778
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Assets/BezierCurves/Scripts/BezierCurve.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class BezierCurve : MonoBehaviour

#region PublicVariables

[HideInInspector] public int version = 1; // will be updated by BezierCurveUpgrade

/// <summary>
/// - used for calculating the number of interpolated points for each pair of bezier points
/// - the value corresponds approximately to the number of points per meter
Expand Down Expand Up @@ -192,6 +194,7 @@ void OnDrawGizmos()

void Awake()
{
BezierCurveUpgrade.Upgrade(this);
dirty = true;
}

Expand Down
28 changes: 28 additions & 0 deletions Assets/BezierCurves/Scripts/BezierCurveUpgrade.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class BezierCurveUpgrade
{
public static void Upgrade(BezierCurve curve)
{
// resolution logic change - adapt old serialized resolution value to new behavior
if (curve.version < 2)
{
Debug.Log(string.Format("[BezierCurve upgrade] adapting curve resolution value for '{0}'", curve.name), curve);

// will use maximum resolution that matches old curve interpolation density
float shortestSegmentLength = float.MaxValue;
for (int i = 0; i < curve.pointCount - 1; i++)
{
float length = BezierCurve.ApproximateLength(curve[i], curve[i + 1], numPoints: 5);
if (length < shortestSegmentLength) shortestSegmentLength = length;
}
float newRes = curve.resolution / shortestSegmentLength;
Debug.Log(string.Format("Old resolution: {0}, new resolution: {1}", curve.resolution, newRes), curve);

curve.resolution = newRes;
curve.version = 2;
}
}
}
11 changes: 11 additions & 0 deletions Assets/BezierCurves/Scripts/BezierCurveUpgrade.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ Summary:

* Curve interpolation is now more homogenous across segments on same curve
* Curves in existing projects will change in appearance, and might impact performance until you update the values
* You'll have to tweak the values on existing curves to desired precision
* An automatic resolution recalculation will be done when Unity encounters old curves (on scene open, prefab instantiation, play, etc.)
* You might have to tweak the values on existing curves to desired precision
* You might have to update your code to revise how you calculate the resolution value

This change makes sure that all segments have approximately the same resolution - with resolution now meaning the same number of interpolated points **per unit of distance** across the whole curve.

Previously, a curve whose segments are very different in length (e.g. a curve made of 3 points, where one segment is short and one very long) would have all segments interpolated with the same number of points, causing short segments to be interpolated too densely, and long segments to be insufficiently precise, with visible poly-line shape.

Automatic curve resolution recalculation will be done when Unity encounters old curves (on scene open, prefab instantiation, play, etc.), which will update the resolution value by dividing the old resolution by the length of the shortest curve segment. This will cause the longer segments have more interpolated points than before, but will prevent loss of precision on the shortest segment.

0 comments on commit 16cf778

Please sign in to comment.