StarSim

StarSim.git
git clone git://git.lenczewski.org/StarSim.git
Log | Files | Refs | README | LICENSE

commit d64dded134ede22ef92bf003f23cfd7e70e78935
parent e5f9bdf8cecc39a39e252dc3eb0c6f3958f54be2
Author: Mikolaj Lenczewski <33129490+EnderRifter@users.noreply.github.com>
Date:   Thu, 31 Oct 2019 10:30:40 +0000

Improved chromaticity implementation.

Diffstat:
MStarSim/StarSimLib/Graphics/SimulationDrawer.cs | 21+++++++++++++++------
MStarSim/StarSimLib/Physics/BodyGenerator.cs | 64+++++++++++++++++++++++++++++++++++++---------------------------
MStarSim/StarSimLib/StarSimLib.xml | 14++++++++++++--
3 files changed, 64 insertions(+), 35 deletions(-)

diff --git a/StarSim/StarSimLib/Graphics/SimulationDrawer.cs b/StarSim/StarSimLib/Graphics/SimulationDrawer.cs @@ -68,6 +68,16 @@ namespace StarSimLib.Graphics private const double FieldOfView = 45; /// <summary> + /// The maximum speed that is mapped and rendered using the HSV colour map. + /// </summary> + private const double MaximumRenderedSpeed = 100_000; + + /// <summary> + /// The minimum speed that is mapped and rendered using the HSV colour map. + /// </summary> + private const double MinimumRenderedSpeed = 0; + + /// <summary> /// The closest distance that can be seen on the <see cref="RenderTarget"/>. Any bodies that are closer /// to the camera than this distance (i.e. behind the camera) are culled and not rendered. /// </summary> @@ -269,7 +279,7 @@ namespace StarSimLib.Graphics /// <param name="value">The value for the new colour, between 0-1.</param> /// <returns>The created ARGB colour.</returns> [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static Color ColorFromHSV(double hue, double saturation, double value) + private static Color ColorFromHsv(double hue, double saturation, double value) { double c = value * saturation; double k = hue / 60; @@ -449,13 +459,12 @@ namespace StarSimLib.Graphics { Vector4[] orbitTracerPositions = body.OrbitTracer.PreviousPositions.ToArray(); - double minimumSpeed = 0, maximumSpeed = 75000; - double clampedSpeed = body.Velocity.Abs() < maximumSpeed ? body.Velocity.Abs() : - body.Velocity.Abs() < minimumSpeed ? minimumSpeed : maximumSpeed; - double clampedRainbowColour = Map(clampedSpeed, minimumSpeed, maximumSpeed, 0, 360); + double clampedSpeed = body.Velocity.Abs() < MaximumRenderedSpeed ? body.Velocity.Abs() : + body.Velocity.Abs() < MinimumRenderedSpeed ? MinimumRenderedSpeed : MaximumRenderedSpeed; + double clampedRainbowColour = Map(clampedSpeed, MinimumRenderedSpeed, MaximumRenderedSpeed, 0, 360); // we cache the colours used for the orbit tracers and the background, and pack them into a 4D vector - Color tracerColour = ColorFromHSV(clampedRainbowColour, 1, 1), bgColour = Color.Transparent; + Color tracerColour = ColorFromHsv(clampedRainbowColour, 1, 1), bgColour = Color.Transparent; Vector4 tracerVector = new Vector4(tracerColour.R, tracerColour.G, tracerColour.B, tracerColour.A); Vector4 bgVector = new Vector4(bgColour.R, bgColour.G, bgColour.B, bgColour.A); diff --git a/StarSim/StarSimLib/Physics/BodyGenerator.cs b/StarSim/StarSimLib/Physics/BodyGenerator.cs @@ -50,7 +50,7 @@ namespace StarSimLib.Physics /// bodies of differing mass. /// </summary> public static readonly MassToColourDelegate RainbowColourDelegate = - mass => ColorFromHSV(mass % 360, 1, 1); + mass => ColorFromHsv(mass % 360, 1, 1); /// <summary> /// The current generation of <see cref="Body"/> instances that the generator is on. @@ -65,37 +65,47 @@ namespace StarSimLib.Physics /// <param name="value">The value for the new colour, between 0-1.</param> /// <returns>The created ARGB colour.</returns> [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static Color ColorFromHSV(double hue, double saturation, double value) + private static Color ColorFromHsv(double hue, double saturation, double value) { - int hi = Convert.ToInt32(Math.Floor(hue / 60)) % 6; - double f = hue / 60 - Math.Floor(hue / 60); + double c = value * saturation; + double k = hue / 60; + double x = c * (1 - Math.Abs(k % 2 - 1)); + double m = value - c; - value *= 255; - int v = Convert.ToInt32(value); - int p = Convert.ToInt32(value * (1 - saturation)); - int q = Convert.ToInt32(value * (1 - f * saturation)); - int t = Convert.ToInt32(value * (1 - (1 - f) * saturation)); + double baseRed = 0, baseGreen = 0, baseBlue = 0; - switch (hi) + if (0 <= k && k <= 1) { - case 0: - return new Color(255, (byte)v, (byte)t, (byte)p); - - case 1: - return new Color(255, (byte)q, (byte)v, (byte)p); - - case 2: - return new Color(255, (byte)p, (byte)v, (byte)t); - - case 3: - return new Color(255, (byte)p, (byte)q, (byte)v); - - case 4: - return new Color(255, (byte)t, (byte)p, (byte)v); - - default: - return new Color(255, (byte)v, (byte)p, (byte)q); + baseRed = c; + baseGreen = x; + } + else if (1 < k && k <= 2) + { + baseRed = x; + baseGreen = c; + } + else if (2 < k && k <= 3) + { + baseGreen = c; + baseBlue = x; + } + else if (3 < k && k <= 4) + { + baseGreen = x; + baseBlue = c; + } + else if (4 < k && k <= 5) + { + baseRed = x; + baseBlue = c; + } + else if (5 < k && k <= 6) + { + baseRed = c; + baseBlue = x; } + + return new Color((byte)((baseRed + m) * 255), (byte)((baseGreen + m) * 255), (byte)((baseBlue + m) * 255), 255); } /// <summary> diff --git a/StarSim/StarSimLib/StarSimLib.xml b/StarSim/StarSimLib/StarSimLib.xml @@ -1299,6 +1299,16 @@ The default field of view for the drawer in degrees. </summary> </member> + <member name="F:StarSimLib.Graphics.SimulationDrawer.MaximumRenderedSpeed"> + <summary> + The maximum speed that is mapped and rendered using the HSV colour map. + </summary> + </member> + <member name="F:StarSimLib.Graphics.SimulationDrawer.MinimumRenderedSpeed"> + <summary> + The minimum speed that is mapped and rendered using the HSV colour map. + </summary> + </member> <member name="F:StarSimLib.Graphics.SimulationDrawer.NearDistance"> <summary> The closest distance that can be seen on the <see cref="T:SFML.Graphics.RenderTarget"/>. Any bodies that are closer @@ -1418,7 +1428,7 @@ Current zoom level of the drawer. </summary> </member> - <member name="M:StarSimLib.Graphics.SimulationDrawer.ColorFromHSV(System.Double,System.Double,System.Double)"> + <member name="M:StarSimLib.Graphics.SimulationDrawer.ColorFromHsv(System.Double,System.Double,System.Double)"> <summary> Creates a new ARGB colour from a HSV colour. </summary> @@ -1836,7 +1846,7 @@ The current generation of <see cref="T:StarSimLib.Data_Structures.Body"/> instances that the generator is on. </summary> </member> - <member name="M:StarSimLib.Physics.BodyGenerator.ColorFromHSV(System.Double,System.Double,System.Double)"> + <member name="M:StarSimLib.Physics.BodyGenerator.ColorFromHsv(System.Double,System.Double,System.Double)"> <summary> Creates a new ARGB colour from a HSV colour. </summary>