diff --git a/Assets/BezierCurves/Scripts/BezierCurve.cs b/Assets/BezierCurves/Scripts/BezierCurve.cs index b4e9a79..b4b41b5 100644 --- a/Assets/BezierCurves/Scripts/BezierCurve.cs +++ b/Assets/BezierCurves/Scripts/BezierCurve.cs @@ -23,6 +23,8 @@ public class BezierCurve : MonoBehaviour #region PublicVariables + [HideInInspector] public int version = 1; // will be updated by BezierCurveUpgrade + /// /// - 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 @@ -192,6 +194,7 @@ void OnDrawGizmos() void Awake() { + BezierCurveUpgrade.Upgrade(this); dirty = true; } diff --git a/Assets/BezierCurves/Scripts/BezierCurveUpgrade.cs b/Assets/BezierCurves/Scripts/BezierCurveUpgrade.cs new file mode 100644 index 0000000..77ae7a5 --- /dev/null +++ b/Assets/BezierCurves/Scripts/BezierCurveUpgrade.cs @@ -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; + } + } +} diff --git a/Assets/BezierCurves/Scripts/BezierCurveUpgrade.cs.meta b/Assets/BezierCurves/Scripts/BezierCurveUpgrade.cs.meta new file mode 100644 index 0000000..dfa7642 --- /dev/null +++ b/Assets/BezierCurves/Scripts/BezierCurveUpgrade.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: fc70f89182753d04cbba2342bc9ee33b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/README.md b/README.md index ea4d8ea..f1421f6 100644 --- a/README.md +++ b/README.md @@ -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.