Table of Contents

Class HeightmapExtensions

Namespace
Stride.CommunityToolkit.Bullet
Assembly
Stride.CommunityToolkit.Bullet.dll

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

float

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

val short

Returns

Color

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

heightmap Heightmap
x int
y int

Returns

float

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

heightmap Heightmap
x int
y int

Returns

int

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

heightmap Heightmap
x int
y int

Returns

Vector3

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

heightmap Heightmap
x int
z int

Returns

Vector3

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

heightmap Heightmap

Source heightmap.

ray Ray

Ray in heightmap local space.

point Vector3

Closest intersection point if any.

m_QuadSideWidthX float

Horizontal spacing between height samples in world units (X axis).

m_QuadSideWidthZ float

Horizontal spacing between height samples in world units (Z axis).

Returns

bool

true if the ray intersects at least one sample; otherwise false.

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

heightmap Heightmap
x int
y int

Returns

bool

ToFloats(Heightmap)

Copies raw height values (as floats) into a new array for processing/export.

public static float[] ToFloats(this Heightmap heightmap)

Parameters

heightmap Heightmap

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

heightmap Heightmap

Source heightmap.

graphicsDevice GraphicsDevice

Graphics device used for buffer allocation.

m_QuadSideWidthX float

Width of a single height sample quad along X.

m_QuadSideWidthZ float

Width of a single height sample quad along Z.

TEXTURE_REPEAT float

If > 0, number of times the texture repeats across the terrain extents.

terrainPoints Vector3[]

Output array of vertex positions.

tessellation int

Stride 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

heightmap Heightmap

Source heightmap.

graphicsDevice GraphicsDevice

Graphics device used to allocate the texture.

commandList CommandList

Command 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

heightmap Heightmap
m_QuadSideWidthX float
m_QuadSideWidthZ float

Returns

Vector3[]