| | | 1 | | //======================================================================= |
| | | 2 | | // HeightmapSurface.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 | | using SwiftCollections.Dimensions; |
| | | 11 | | |
| | | 12 | | namespace Trailblazer.Heightmaps; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Immutable compressed X/Z lattice that resolves environment ground/contact Y. |
| | | 16 | | /// </summary> |
| | | 17 | | public sealed class HeightmapSurface |
| | | 18 | | { |
| | | 19 | | private readonly SwiftShortArray2D _samples; |
| | | 20 | | |
| | 49 | 21 | | private HeightmapSurface( |
| | 49 | 22 | | string name, |
| | 49 | 23 | | SwiftShortArray2D samples, |
| | 49 | 24 | | Vector3d minBounds, |
| | 49 | 25 | | Fixed64 interval, |
| | 49 | 26 | | HeightmapCompression compression) |
| | | 27 | | { |
| | 49 | 28 | | Name = name; |
| | 49 | 29 | | _samples = samples; |
| | 49 | 30 | | MinBounds = minBounds; |
| | 49 | 31 | | Interval = interval; |
| | 49 | 32 | | Compression = compression; |
| | 49 | 33 | | Width = samples.Width; |
| | 49 | 34 | | Depth = samples.Height; |
| | 49 | 35 | | MaxBounds = new Vector3d( |
| | 49 | 36 | | minBounds.X + (Width - 1) * interval, |
| | 49 | 37 | | minBounds.Y, |
| | 49 | 38 | | minBounds.Z + (Depth - 1) * interval); |
| | 49 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Stable authored surface name. |
| | | 43 | | /// </summary> |
| | | 44 | | public string Name { get; } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Minimum world-space X/Z sample coordinate. The Y value is descriptive metadata only. |
| | | 48 | | /// </summary> |
| | | 49 | | public Vector3d MinBounds { get; } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Maximum inclusive world-space X/Z sample coordinate. The Y value matches <see cref="MinBounds"/>. |
| | | 53 | | /// </summary> |
| | | 54 | | public Vector3d MaxBounds { get; } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Distance between adjacent height samples along X and Z. |
| | | 58 | | /// </summary> |
| | | 59 | | public Fixed64 Interval { get; } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Number of samples along X. |
| | | 63 | | /// </summary> |
| | | 64 | | public int Width { get; } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Number of samples along Z. |
| | | 68 | | /// </summary> |
| | | 69 | | public int Depth { get; } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Compression metadata used by all stored samples. |
| | | 73 | | /// </summary> |
| | | 74 | | public HeightmapCompression Compression { get; } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Creates a heightmap from already compressed baked samples. This is the preferred runtime |
| | | 78 | | /// construction path because it avoids setup-time quantization work. |
| | | 79 | | /// </summary> |
| | | 80 | | /// <param name="name">Stable authored surface name.</param> |
| | | 81 | | /// <param name="samples">Compressed X/Z samples indexed by X, then Z.</param> |
| | | 82 | | /// <param name="minBounds">Minimum world-space X/Z sample coordinate.</param> |
| | | 83 | | /// <param name="interval">Positive distance between adjacent samples.</param> |
| | | 84 | | /// <param name="compression">Compression metadata used to decompress samples.</param> |
| | | 85 | | public static HeightmapSurface FromCompressed( |
| | | 86 | | string name, |
| | | 87 | | SwiftShortArray2D samples, |
| | | 88 | | Vector3d minBounds, |
| | | 89 | | Fixed64 interval, |
| | | 90 | | HeightmapCompression compression) |
| | | 91 | | { |
| | 11 | 92 | | ValidateName(name); |
| | 11 | 93 | | ValidateSamples(samples); |
| | 9 | 94 | | ValidateInterval(interval); |
| | 7 | 95 | | ValidateCompression(compression); |
| | | 96 | | |
| | 7 | 97 | | SwiftShortArray2D copy = CopySamples(samples); |
| | 7 | 98 | | return new HeightmapSurface(name, copy, minBounds, interval, compression); |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Creates a heightmap from fixed-point heights by quantizing them once into compressed storage. |
| | | 103 | | /// Use this for tests, generated data, or host tooling that already owns fixed-point heights; |
| | | 104 | | /// runtime sampling still uses the same compressed representation as <see cref="FromCompressed"/>. |
| | | 105 | | /// </summary> |
| | | 106 | | /// <param name="name">Stable authored surface name.</param> |
| | | 107 | | /// <param name="heights">Ground/contact Y samples indexed by X, then Z.</param> |
| | | 108 | | /// <param name="minBounds">Minimum world-space X/Z sample coordinate.</param> |
| | | 109 | | /// <param name="interval">Positive distance between adjacent samples.</param> |
| | | 110 | | /// <param name="compression">Compression metadata used to quantize samples.</param> |
| | | 111 | | public static HeightmapSurface FromHeights( |
| | | 112 | | string name, |
| | | 113 | | Fixed64[,] heights, |
| | | 114 | | Vector3d minBounds, |
| | | 115 | | Fixed64 interval, |
| | | 116 | | HeightmapCompression compression) |
| | | 117 | | { |
| | 47 | 118 | | ValidateName(name); |
| | 46 | 119 | | SwiftThrowHelper.ThrowIfNull(heights, nameof(heights)); |
| | 45 | 120 | | ValidateInterval(interval); |
| | 44 | 121 | | ValidateCompression(compression); |
| | | 122 | | |
| | 43 | 123 | | int width = heights.GetLength(0); |
| | 43 | 124 | | int depth = heights.GetLength(1); |
| | 43 | 125 | | SwiftThrowHelper.ThrowIfArgument( |
| | 43 | 126 | | width <= 0 || depth <= 0, |
| | 43 | 127 | | paramName: nameof(heights), |
| | 43 | 128 | | message: "Height samples must have positive width and depth."); |
| | | 129 | | |
| | 42 | 130 | | var compressed = new SwiftShortArray2D(width, depth); |
| | 170 | 131 | | for (int x = 0; x < width; x++) |
| | 176 | 132 | | for (int z = 0; z < depth; z++) |
| | 45 | 133 | | compressed[x, z] = compression.CompressClamped(heights[x, z]); |
| | | 134 | | |
| | 42 | 135 | | return new HeightmapSurface(name, compressed, minBounds, interval, compression); |
| | | 136 | | } |
| | | 137 | | |
| | | 138 | | /// <summary> |
| | | 139 | | /// Attempts to sample environment ground/contact Y at the supplied world X/Z coordinate. |
| | | 140 | | /// </summary> |
| | | 141 | | public bool TrySampleGround(Vector3d worldPosition, out Fixed64 groundY) |
| | | 142 | | { |
| | 38 | 143 | | if (!TryResolveLocalPosition(worldPosition, out int x0, out int z0, out int x1, out int z1, out Fixed64 fraction |
| | | 144 | | { |
| | 4 | 145 | | groundY = Fixed64.Zero; |
| | 4 | 146 | | return false; |
| | | 147 | | } |
| | | 148 | | |
| | 34 | 149 | | Fixed64 h00 = GetHeight(x0, z0); |
| | 34 | 150 | | Fixed64 h10 = GetHeight(x1, z0); |
| | 34 | 151 | | Fixed64 h01 = GetHeight(x0, z1); |
| | 34 | 152 | | Fixed64 h11 = GetHeight(x1, z1); |
| | | 153 | | |
| | 34 | 154 | | Fixed64 xLerp0 = FixedMath.Lerp(h00, h10, fractionX); |
| | 34 | 155 | | Fixed64 xLerp1 = FixedMath.Lerp(h01, h11, fractionX); |
| | 34 | 156 | | groundY = FixedMath.Lerp(xLerp0, xLerp1, fractionZ); |
| | 34 | 157 | | return true; |
| | | 158 | | } |
| | | 159 | | |
| | | 160 | | private bool TryResolveLocalPosition( |
| | | 161 | | Vector3d worldPosition, |
| | | 162 | | out int x0, |
| | | 163 | | out int z0, |
| | | 164 | | out int x1, |
| | | 165 | | out int z1, |
| | | 166 | | out Fixed64 fractionX, |
| | | 167 | | out Fixed64 fractionZ) |
| | | 168 | | { |
| | 38 | 169 | | Fixed64 localX = (worldPosition.X - MinBounds.X) / Interval; |
| | 38 | 170 | | Fixed64 localZ = (worldPosition.Z - MinBounds.Z) / Interval; |
| | 38 | 171 | | Fixed64 maxX = (Fixed64)(Width - 1); |
| | 38 | 172 | | Fixed64 maxZ = (Fixed64)(Depth - 1); |
| | 38 | 173 | | if (localX < Fixed64.Zero || localZ < Fixed64.Zero || localX > maxX || localZ > maxZ) |
| | | 174 | | { |
| | 4 | 175 | | x0 = z0 = x1 = z1 = -1; |
| | 4 | 176 | | fractionX = fractionZ = Fixed64.Zero; |
| | 4 | 177 | | return false; |
| | | 178 | | } |
| | | 179 | | |
| | 34 | 180 | | x0 = localX.FloorToInt(); |
| | 34 | 181 | | z0 = localZ.FloorToInt(); |
| | 34 | 182 | | x1 = Math.Min(x0 + 1, Width - 1); |
| | 34 | 183 | | z1 = Math.Min(z0 + 1, Depth - 1); |
| | 34 | 184 | | fractionX = localX - (Fixed64)x0; |
| | 34 | 185 | | fractionZ = localZ - (Fixed64)z0; |
| | 34 | 186 | | return true; |
| | | 187 | | } |
| | | 188 | | |
| | | 189 | | private Fixed64 GetHeight(int x, int z) |
| | | 190 | | { |
| | 136 | 191 | | return Compression.Decompress(_samples[x, z]); |
| | | 192 | | } |
| | | 193 | | |
| | | 194 | | private static void ValidateName(string name) |
| | | 195 | | { |
| | 58 | 196 | | SwiftThrowHelper.ThrowIfArgument( |
| | 58 | 197 | | string.IsNullOrWhiteSpace(name), |
| | 58 | 198 | | paramName: nameof(name), |
| | 58 | 199 | | message: "Heightmap surface name cannot be null or whitespace."); |
| | 57 | 200 | | } |
| | | 201 | | |
| | | 202 | | private static void ValidateSamples(SwiftShortArray2D samples) |
| | | 203 | | { |
| | 11 | 204 | | SwiftThrowHelper.ThrowIfNull(samples, nameof(samples)); |
| | 10 | 205 | | SwiftThrowHelper.ThrowIfArgument( |
| | 10 | 206 | | samples.Width <= 0 || samples.Height <= 0, |
| | 10 | 207 | | paramName: nameof(samples), |
| | 10 | 208 | | message: "Height samples must have positive width and depth."); |
| | 9 | 209 | | } |
| | | 210 | | |
| | | 211 | | private static void ValidateInterval(Fixed64 interval) |
| | | 212 | | { |
| | 54 | 213 | | SwiftThrowHelper.ThrowIfArgumentOutOfRange( |
| | 54 | 214 | | interval <= Fixed64.Zero, |
| | 54 | 215 | | actualValue: null, |
| | 54 | 216 | | paramName: nameof(interval), |
| | 54 | 217 | | message: "Heightmap interval must be positive."); |
| | 51 | 218 | | } |
| | | 219 | | |
| | | 220 | | private static void ValidateCompression(HeightmapCompression compression) |
| | | 221 | | { |
| | 51 | 222 | | SwiftThrowHelper.ThrowIfArgument( |
| | 51 | 223 | | !compression.IsValid, |
| | 51 | 224 | | paramName: nameof(compression), |
| | 51 | 225 | | message: "Heightmap compression requires a positive height step."); |
| | 50 | 226 | | } |
| | | 227 | | |
| | | 228 | | private static SwiftShortArray2D CopySamples(SwiftShortArray2D source) |
| | | 229 | | { |
| | 7 | 230 | | var copy = new SwiftShortArray2D(source.Width, source.Height); |
| | 42 | 231 | | for (int x = 0; x < source.Width; x++) |
| | 84 | 232 | | for (int z = 0; z < source.Height; z++) |
| | 28 | 233 | | copy[x, z] = source[x, z]; |
| | | 234 | | |
| | 7 | 235 | | return copy; |
| | | 236 | | } |
| | | 237 | | } |