| | | 1 | | using FixedMathSharp; |
| | | 2 | | using System; |
| | | 3 | | |
| | | 4 | | namespace Trailblazer.Heightmaps; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Describes deterministic quantization for compact heightmap samples. |
| | | 8 | | /// </summary> |
| | | 9 | | public readonly struct HeightmapCompression |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Ground height represented by a compressed value of zero. |
| | | 13 | | /// </summary> |
| | | 14 | | public Fixed64 ReferenceHeight { get; } |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// World-height delta represented by one compressed unit. |
| | | 18 | | /// </summary> |
| | | 19 | | public Fixed64 HeightStep { get; } |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Gets whether this compression metadata can be used for height conversion. |
| | | 23 | | /// </summary> |
| | 194 | 24 | | public bool IsValid => HeightStep > Fixed64.Zero; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Creates compression metadata for short-backed height samples. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="referenceHeight">Ground height represented by compressed value zero.</param> |
| | | 30 | | /// <param name="heightStep">Positive world-height delta represented by one compressed unit.</param> |
| | | 31 | | /// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="heightStep"/> is not positive.</except |
| | | 32 | | public HeightmapCompression(Fixed64 referenceHeight, Fixed64 heightStep) |
| | | 33 | | { |
| | 47 | 34 | | if (heightStep <= Fixed64.Zero) |
| | 2 | 35 | | throw new ArgumentOutOfRangeException(nameof(heightStep), "Height step must be positive."); |
| | | 36 | | |
| | 45 | 37 | | ReferenceHeight = referenceHeight; |
| | 45 | 38 | | HeightStep = heightStep; |
| | 45 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Converts a compressed sample into environment ground/contact Y. |
| | | 43 | | /// </summary> |
| | | 44 | | public Fixed64 Decompress(short compressed) |
| | | 45 | | { |
| | 111 | 46 | | ThrowIfInvalid(); |
| | 111 | 47 | | return ReferenceHeight + compressed * HeightStep; |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Quantizes a ground/contact Y value to the nearest representable short sample. |
| | | 52 | | /// </summary> |
| | | 53 | | public short CompressClamped(Fixed64 groundY) |
| | | 54 | | { |
| | 43 | 55 | | ThrowIfInvalid(); |
| | | 56 | | |
| | 43 | 57 | | Fixed64 scaled = (groundY - ReferenceHeight) / HeightStep; |
| | 43 | 58 | | if (scaled >= (Fixed64)short.MaxValue) |
| | 1 | 59 | | return short.MaxValue; |
| | 42 | 60 | | if (scaled <= (Fixed64)short.MinValue) |
| | 1 | 61 | | return short.MinValue; |
| | | 62 | | |
| | 41 | 63 | | return (short)scaled.RoundToInt(); |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | private void ThrowIfInvalid() |
| | | 67 | | { |
| | 154 | 68 | | if (!IsValid) |
| | 0 | 69 | | throw new InvalidOperationException("Heightmap compression requires a positive height step."); |
| | 154 | 70 | | } |
| | | 71 | | } |