Class HeightmapExtensions
Extension helpers for working with Heightmap data: sampling heights and normals, ray picking, exporting raw height values, generating preview textures and building renderable / collision meshes.
public static class HeightmapExtensions
- Inheritance
-
HeightmapExtensions
Remarks
Typical usage:
// Sample a height and normal
var h = heightmap.GetHeightAt(x, z);
var n = heightmap.GetNormal(x, z);
// Build a mesh (tessellating every 2nd sample)
var mesh = heightmap.ToMesh(graphicsDevice, 1f, 1f, 4f, out var terrainPoints, tessellation: 2);
// Create a grayscale preview texture
var preview = heightmap.ToTexture(graphicsDevice, commandList);
Fields
HeightMultiplier
Used to distinguish between Grey scale heightmaps HeightMultiplier=255.0f (yields a byte 0-255) or float heightmaps HeightMultiplier=10000.0f (based on a short -32,768 to 32,767, sum yields 65,535 levels for much smoother maps).
public static readonly float HeightMultiplier
Field Value
Methods
AsStrideColor(short)
Converts a signed 16-bit height value to a packed Color using a float-to-RGBA conversion.
public static Color AsStrideColor(this short val)
Parameters
valshort
Returns
Remarks
The value is widened to a float and its four bytes become the four channels, least significant byte in red. That is the same reinterpretation a packed-float heightmap texture uses, so a texture built from these colours can be read back the same way.
GetHeightAt(Heightmap, int, int)
Returns the interpolated height at sample coordinate (x,y) or the minimum height if out of range.
public static float GetHeightAt(this Heightmap heightmap, int x, int y)
Parameters
Returns
GetHeightIndex(Heightmap, int, int)
Converts a 2D sample coordinate into the flat array index of Shorts.
public static int GetHeightIndex(this Heightmap heightmap, int x, int y)
Parameters
Returns
GetNormal(Heightmap, int, int)
Estimates a normal vector using central differences at the given sample coordinate.
public static Vector3 GetNormal(this Heightmap heightmap, int x, int y)
Parameters
Returns
GetTangent(Heightmap, int, int)
Estimates the vertex tangent direction at the given sample.
public static Vector3 GetTangent(this Heightmap heightmap, int x, int z)
Parameters
Returns
IntersectsRay(Heightmap, Ray, out Vector3, float, float)
Tests a ray against each sampled height point (approximated as small spheres) and returns the closest intersection.
public static bool IntersectsRay(this Heightmap heightmap, Ray ray, out Vector3 point, float m_QuadSideWidthX = 1, float m_QuadSideWidthZ = 1)
Parameters
heightmapHeightmapSource heightmap.
rayRayRay in heightmap local space.
pointVector3Closest intersection point if any.
m_QuadSideWidthXfloatHorizontal spacing between height samples in world units (X axis).
m_QuadSideWidthZfloatHorizontal spacing between height samples in world units (Z axis).
Returns
- bool
trueif the ray intersects at least one sample; otherwisefalse.
IsValidCoordinate(Heightmap, int, int)
Checks whether the provided coordinate lies within the heightmap sample bounds.
public static bool IsValidCoordinate(this Heightmap heightmap, int x, int y)
Parameters
Returns
ToFloats(Heightmap)
Copies raw height values (as floats) into a new array for processing/export.
public static float[] ToFloats(this Heightmap heightmap)
Parameters
heightmapHeightmap
Returns
- float[]
ToMesh(Heightmap, GraphicsDevice, float, float, float, out Vector3[], int)
Builds a triangle mesh for the heightmap using the specified quad spacing, texture repetition and tessellation stride.
public static Mesh ToMesh(this Heightmap heightmap, GraphicsDevice graphicsDevice, float m_QuadSideWidthX, float m_QuadSideWidthZ, float TEXTURE_REPEAT, out Vector3[] terrainPoints, int tessellation)
Parameters
heightmapHeightmapSource heightmap.
graphicsDeviceGraphicsDeviceGraphics device used for buffer allocation.
m_QuadSideWidthXfloatWidth of a single height sample quad along X.
m_QuadSideWidthZfloatWidth of a single height sample quad along Z.
TEXTURE_REPEATfloatIf > 0, number of times the texture repeats across the terrain extents.
terrainPointsVector3[]Output array of vertex positions.
tessellationintStride between samples (1 = full resolution).
Returns
- Mesh
Constructed mesh.
ToTexture(Heightmap, GraphicsDevice, CommandList)
Generates a grayscale texture representation of the heightmap where each texel encodes height in its color channels. Higher height values map to brighter colors.
public static Texture ToTexture(this Heightmap heightmap, GraphicsDevice graphicsDevice, CommandList commandList)
Parameters
heightmapHeightmapSource heightmap.
graphicsDeviceGraphicsDeviceGraphics device used to allocate the texture.
commandListCommandListCommand list used to upload texture data.
Returns
- Texture
A newly created 2D texture containing the height preview.
ToWorldPoints(Heightmap, float, float)
Converts all sample heights to world-space points using the provided quad spacing.
public static Vector3[] ToWorldPoints(this Heightmap heightmap, float m_QuadSideWidthX, float m_QuadSideWidthZ)
Parameters
Returns
- Vector3[]