From 600098d845611a45147154690cc09f992c961119 Mon Sep 17 00:00:00 2001 From: Dan Balasescu Date: Wed, 27 Mar 2024 04:05:04 +0900 Subject: [PATCH] Fix bulbs on Catmull sliders --- osu.Game/Rulesets/Objects/SliderPath.cs | 68 +++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 4 deletions(-) diff --git a/osu.Game/Rulesets/Objects/SliderPath.cs b/osu.Game/Rulesets/Objects/SliderPath.cs index f33a07f082..5398d6c45f 100644 --- a/osu.Game/Rulesets/Objects/SliderPath.cs +++ b/osu.Game/Rulesets/Objects/SliderPath.cs @@ -42,6 +42,17 @@ namespace osu.Game.Rulesets.Objects private readonly List cumulativeLength = new List(); private readonly Cached pathCache = new Cached(); + /// + /// Any additional length of the path which was optimised out during piecewise approximation, but should still be considered as part of . + /// + /// + /// This is a hack for Catmull paths. + /// + private double optimisedLength; + + /// + /// The final calculated length of the path. + /// private double calculatedLength; private readonly List segmentEnds = new List(); @@ -244,6 +255,7 @@ namespace osu.Game.Rulesets.Objects { calculatedPath.Clear(); segmentEnds.Clear(); + optimisedLength = 0; if (ControlPoints.Count == 0) return; @@ -268,7 +280,8 @@ namespace osu.Game.Rulesets.Objects calculatedPath.Add(segmentVertices[0]); else if (segmentVertices.Length > 1) { - List subPath = calculateSubPath(segmentVertices, segmentType); + List subPath = calculateSubPath(segmentVertices, segmentType, ref optimisedLength); + // Skip the first vertex if it is the same as the last vertex from the previous segment bool skipFirst = calculatedPath.Count > 0 && subPath.Count > 0 && calculatedPath.Last() == subPath[0]; @@ -287,7 +300,7 @@ namespace osu.Game.Rulesets.Objects } } - private List calculateSubPath(ReadOnlySpan subControlPoints, PathType type) + private static List calculateSubPath(ReadOnlySpan subControlPoints, PathType type, ref double optimisedLength) { switch (type.Type) { @@ -295,6 +308,7 @@ namespace osu.Game.Rulesets.Objects return PathApproximator.LinearToPiecewiseLinear(subControlPoints); case SplineType.PerfectCurve: + { if (subControlPoints.Length != 3) break; @@ -305,9 +319,55 @@ namespace osu.Game.Rulesets.Objects break; return subPath; + } case SplineType.Catmull: - return PathApproximator.CatmullToPiecewiseLinear(subControlPoints); + { + List subPath = PathApproximator.CatmullToPiecewiseLinear(subControlPoints); + + // At draw time, osu!stable optimises paths by only keeping piecewise segments that are 6px apart. + // For the most part we don't care about this optimisation, and its additional heuristics are hard to reproduce in every implementation. + // + // However, it matters for Catmull paths which form "bulbs" around sequential knots with identical positions, + // so we'll apply a very basic form of the optimisation here and return a length representing the optimised portion. + // The returned length is important so that the optimisation doesn't cause the path to get extended to match the value of ExpectedDistance. + + List optimisedPath = new List(subPath.Count); + + Vector2? lastStart = null; + double lengthRemovedSinceStart = 0; + + for (int i = 0; i < subPath.Count; i++) + { + if (lastStart == null) + { + optimisedPath.Add(subPath[i]); + lastStart = subPath[i]; + continue; + } + + Debug.Assert(i > 0); + + double distFromStart = Vector2.Distance(lastStart.Value, subPath[i]); + lengthRemovedSinceStart += Vector2.Distance(subPath[i - 1], subPath[i]); + + // See PathApproximator.catmull_detail. + const int catmull_detail = 50; + const int catmull_segment_length = catmull_detail * 2; + + // Either 6px from the start, the last vertex at every knot, or the end of the path. + if (distFromStart > 6 || (i + 1) % catmull_segment_length == 0 || i == subPath.Count - 1) + { + optimisedPath.Add(subPath[i]); + optimisedLength += lengthRemovedSinceStart - distFromStart; + + lastStart = null; + lengthRemovedSinceStart = 0; + } + } + + return optimisedPath; + } } return PathApproximator.BSplineToPiecewiseLinear(subControlPoints, type.Degree ?? subControlPoints.Length); @@ -315,7 +375,7 @@ namespace osu.Game.Rulesets.Objects private void calculateLength() { - calculatedLength = 0; + calculatedLength = optimisedLength; cumulativeLength.Clear(); cumulativeLength.Add(0);