From 4930ecd06a772a275223d342e8ab59b636dc3ca5 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 16 Oct 2026 19:14:15 +0200 Subject: [PATCH] Suns: secondary hue stays on the allowed arc with the primary; ramp never crosses the band The secondary is placed along the allowed arc (reflected at its ends, distance capped at half the arc) so the RGB blend between the two stops stays outside the band for any Settings band. Tests sample the production ramp and pin draw uniformity. Co-Authored-By: Claude Opus 5.5 --- Assets/Scripts/ServerShared/ColorMath.cs | 19 +++++++- Assets/Scripts/ServerShared/ZoneGenerator.cs | 9 ++-- .../SunsNeverGreenTests.cs | 46 +++++++++++++++++++ 3 files changed, 68 insertions(+), 6 deletions(-) diff --git a/Assets/Scripts/ServerShared/ColorMath.cs b/Assets/Scripts/ServerShared/ColorMath.cs index f91ab9ad0..3814272cd 100644 --- a/Assets/Scripts/ServerShared/ColorMath.cs +++ b/Assets/Scripts/ServerShared/ColorMath.cs @@ -22,7 +22,24 @@ public static class ColorMath public static bool HueInBand(float hue, float start, float end) => frac(hue - start) < frac(end - start); // Maps u in [0,1) onto the hues outside the band, so a uniform draw stays uniform over the allowed arc. - public static float HueOutsideBand(float u, float start, float end) => frac(end + u * (1 - frac(end - start))); + public static float HueOutsideBand(float u, float start, float end) => frac(end + u * AllowedArc(start, end)); + + static float AllowedArc(float start, float end) => 1 - frac(end - start); + + // The second hue of a pair whose first is HueOutsideBand(u): distance away along the allowed arc, on the side given by up, + // reflected to the other side if that would leave the arc. Both hues lie on one arc, so the straight RGB blend between + // the two stops never crosses the band. The distance is capped at half the arc, so the reflection always fits and a + // wide band shortens the distance instead of leaving the secondary inside it. + public static float SecondaryHueOutsideBand(float u, bool up, float distance, float start, float end) + { + float arc = AllowedArc(start, end); + float d = min(distance, arc / 2); + float t = u * arc; + float s = up ? t + d : t - d; + if (s < 0 || s > arc) + s = up ? t - d : t + d; + return frac(end + s); + } public static float3 HsvToRgb(in float3 hsv) { diff --git a/Assets/Scripts/ServerShared/ZoneGenerator.cs b/Assets/Scripts/ServerShared/ZoneGenerator.cs index 12a8810e9..c6ec75375 100644 --- a/Assets/Scripts/ServerShared/ZoneGenerator.cs +++ b/Assets/Scripts/ServerShared/ZoneGenerator.cs @@ -159,11 +159,10 @@ public static class ZoneGenerator { if (gas is SunData sun) { - float primary = ColorMath.HueOutsideBand(random.NextFloat(), zoneSettings.SunHueExcludedStart, zoneSettings.SunHueExcludedEnd); - float step = zoneSettings.SunSecondaryColorDistance * (random.NextFloat() > .5 ? 1 : -1); - float secondary = frac(primary + 1 + step); - if (ColorMath.HueInBand(secondary, zoneSettings.SunHueExcludedStart, zoneSettings.SunHueExcludedEnd)) - secondary = frac(primary + 1 - step); + var hueDraw = random.NextFloat(); + float primary = ColorMath.HueOutsideBand(hueDraw, zoneSettings.SunHueExcludedStart, zoneSettings.SunHueExcludedEnd); + float secondary = ColorMath.SecondaryHueOutsideBand(hueDraw, random.NextFloat() > .5, zoneSettings.SunSecondaryColorDistance, + zoneSettings.SunHueExcludedStart, zoneSettings.SunHueExcludedEnd); gas.Colors = new [] { float4(ColorMath.HsvToRgb(float3(primary, zoneSettings.SunColorSaturation, .5f)), 0), diff --git a/tests/Aetheria.Shared.Tests/SunsNeverGreenTests.cs b/tests/Aetheria.Shared.Tests/SunsNeverGreenTests.cs index fc42536cc..e6f5314ef 100644 --- a/tests/Aetheria.Shared.Tests/SunsNeverGreenTests.cs +++ b/tests/Aetheria.Shared.Tests/SunsNeverGreenTests.cs @@ -113,6 +113,52 @@ public sealed class SunsNeverGreenTests : IDisposable Assert.Contains(hues, hue => ColorMath.HueInBand(hue, 75f * Degree, 165f * Degree)); } + [Fact] + public void TheDrawIsUniformOverTheAllowedArc() + { + var settings = new ZoneGenerationSettings(); + const int draws = 1000, bins = 10; + var counts = new int[bins]; + var arc = 1f - (settings.SunHueExcludedEnd - settings.SunHueExcludedStart); + for (var i = 0; i < draws; i++) + { + var hue = ColorMath.HueOutsideBand((i + .5f) / draws, settings.SunHueExcludedStart, settings.SunHueExcludedEnd); + var along = hue - settings.SunHueExcludedEnd; + if (along < 0) along += 1f; + counts[Math.Min(bins - 1, (int)(along / arc * bins))]++; + } + Assert.All(counts, count => Assert.InRange(count, draws / bins - 1, draws / bins + 1)); + } + + // The body blends its two stops in RGB (Gradient Blend), so the blend is sampled, not just the stops. + [Theory] + [InlineData(75f, 165f)] + [InlineData(55f, 185f)] + [InlineData(20f, 250f)] + public void TheRampBetweenTheStopsNeverCrossesTheBand(float startDegrees, float endDegrees) + { + var suns = GenerateSuns(z => + { + z.SunHueExcludedStart = startDegrees * Degree; + z.SunHueExcludedEnd = endDegrees * Degree; + }, out _); + Assert.True(suns.Count >= 10, $"only {suns.Count} suns generated"); + foreach (var sun in suns) + { + var a = sun.Colors[0].xyz; + var b = sun.Colors[1].xyz; + for (var step = 0; step <= 40; step++) + { + var hue = ColorMath.RgbToHsv(a + (b - a) * (step / 40f)).x; + Assert.False(ColorMath.HueInBand(hue, startDegrees * Degree + Slack, endDegrees * Degree - Slack), + $"the ramp passed through the band (hue {hue / Degree:F1} degrees, step {step}/40)"); + } + foreach (var colourHue in Hues(sun)) + Assert.False(ColorMath.HueInBand(colourHue, startDegrees * Degree + Slack, endDegrees * Degree - Slack), + $"a sun colour fell in the band (hue {colourHue / Degree:F1} degrees)"); + } + } + private static IEnumerable Hues(SunData sun) => sun.Colors.Select(c => c.xyz) .Concat(new[] { sun.FogTintColor, sun.LightColor })