| | | 1 | | //======================================================================= |
| | | 2 | | // HeightmapCompression.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using System; |
| | | 9 | | using FixedMathSharp; |
| | | 10 | | |
| | | 11 | | namespace Trailblazer.Heightmaps; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Describes deterministic quantization for compact heightmap samples. |
| | | 15 | | /// </summary> |
| | | 16 | | public readonly struct HeightmapCompression |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Ground height represented by a compressed value of zero. |
| | | 20 | | /// </summary> |
| | | 21 | | public Fixed64 ReferenceHeight { get; } |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// World-height delta represented by one compressed unit. |
| | | 25 | | /// </summary> |
| | | 26 | | public Fixed64 HeightStep { get; } |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Gets whether this compression metadata can be used for height conversion. |
| | | 30 | | /// </summary> |
| | 241 | 31 | | public bool IsValid => HeightStep > Fixed64.Zero; |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Creates compression metadata for short-backed height samples. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="referenceHeight">Ground height represented by compressed value zero.</param> |
| | | 37 | | /// <param name="heightStep">Positive world-height delta represented by one compressed unit.</param> |
| | | 38 | | /// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="heightStep"/> is not positive.</except |
| | | 39 | | public HeightmapCompression(Fixed64 referenceHeight, Fixed64 heightStep) |
| | | 40 | | { |
| | 56 | 41 | | SwiftThrowHelper.ThrowIfArgumentOutOfRange( |
| | 56 | 42 | | heightStep <= Fixed64.Zero, |
| | 56 | 43 | | actualValue: null, |
| | 56 | 44 | | paramName: nameof(heightStep), |
| | 56 | 45 | | message: "Height step must be positive."); |
| | | 46 | | |
| | 54 | 47 | | ReferenceHeight = referenceHeight; |
| | 54 | 48 | | HeightStep = heightStep; |
| | 54 | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Converts a compressed sample into environment ground/contact Y. |
| | | 53 | | /// </summary> |
| | | 54 | | public Fixed64 Decompress(short compressed) |
| | | 55 | | { |
| | 140 | 56 | | ThrowIfInvalid(); |
| | 139 | 57 | | return ReferenceHeight + compressed * HeightStep; |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Quantizes a ground/contact Y value to the nearest representable short sample. |
| | | 62 | | /// </summary> |
| | | 63 | | public short CompressClamped(Fixed64 groundY) |
| | | 64 | | { |
| | 50 | 65 | | ThrowIfInvalid(); |
| | | 66 | | |
| | 49 | 67 | | Fixed64 scaled = (groundY - ReferenceHeight) / HeightStep; |
| | 49 | 68 | | if (scaled >= (Fixed64)short.MaxValue) |
| | 1 | 69 | | return short.MaxValue; |
| | 48 | 70 | | if (scaled <= (Fixed64)short.MinValue) |
| | 1 | 71 | | return short.MinValue; |
| | | 72 | | |
| | 47 | 73 | | return (short)scaled.RoundToInt(); |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | private void ThrowIfInvalid() |
| | | 77 | | { |
| | 190 | 78 | | SwiftThrowHelper.ThrowIfTrue( |
| | 190 | 79 | | !IsValid, |
| | 190 | 80 | | message: "Heightmap compression requires a positive height step."); |
| | 188 | 81 | | } |
| | | 82 | | } |