Gorilla.Terrain Unit Reference

Main terrain component for rendering planar terrain from heightmaps or procedural algorithms.

TGorillaTerrain

Simple terrain component for rendering from heightmaps or procedural algorithms.

Inheritance: TGorillaSingletonMesh → TGorillaTerrain

Platforms: All Gorilla platforms

Unit: Gorilla.Terrain

Constructor/Destructor

Create

constructor Create(AOwner: TComponent); override;

Creates a new terrain instance with default settings:

  • Resolution: 128×128
  • TwoFace: False
  • StaticBuffering: True
  • AutoDecimate: False
  • Smoothing: 0

Destroy

destructor Destroy; override;

Frees all resources including heightmap and material objects.

Properties

TwoFace

property TwoFace: Boolean read FTwoFace write SetTwoFace;

Defines if front and back of triangles will be rendered. Default: False.

Remarks: Keep disabled for better performance. Only enable if viewing terrain from below.

ResolutionX

property ResolutionX: Integer read GetResolutionX write SetResolutionX;

Columns of the terrain grid. Larger values create more vertices.

See also: ResolutionY, Resolution, SetXYResolution

ResolutionY

property ResolutionY: Integer read GetResolutionY write SetResolutionY;

Rows of the terrain grid. Larger values create more vertices.

See also: ResolutionX, Resolution, SetXYResolution

Resolution

property Resolution: TPoint read GetResolution write SetResolution;

Gets/sets both X and Y resolution simultaneously.

Storage: Not stored (use ResolutionX/ResolutionY in IDE)

HeightMap

property HeightMap: TBitmap read FHeightMap write SetHeightMap;

Heightmap bitmap used to compute Y-value of each vertex. Grayscale images work best (uses luminance for color images).

Triggers: Automatically rebuilds terrain when changed

Smoothing

property Smoothing: Integer read FSmoothing write SetSmoothing;

Number of smoothing iterations applied to heightmap before generating vertices. Range: 0-20.

Default: 0 (no smoothing)

Use case: Smooth low-resolution heightmaps when upscaling to higher terrain resolution

ShowVertices

property ShowVertices: Boolean read FShowVertices write FShowVertices;

Displays vertices as dots after rendering (debug only).

Warning: Only works when StaticBuffering = False. Significantly reduces performance.

ShowGrid

property ShowGrid: Boolean read FShowGrid write FShowGrid;

Displays polygon grid as lines after rendering (debug only).

Warning: Only works when StaticBuffering = False. Significantly reduces performance.

VerticeColor

property VerticeColor: TAlphaColor read GetVerticeColor write SetVerticeColor;

Color for vertices and grid lines when debug visualization is enabled.

Default: Yellow (TAlphaColorRec.Yellow)

StaticBuffering

property StaticBuffering: Boolean read FStaticBuffering write SetStaticBuffering;

When enabled, vertex data is uploaded to GPU memory and local data is cleared.

Default: True

Benefits: Faster rendering, lower memory usage Drawbacks: Cannot show grid/vertices, cannot use Decimate() after rebuild

AutoDecimate

property AutoDecimate: Boolean read FAutoDecimate write SetAutoDecimate;

Automatically reduce vertex data during terrain rebuild.

Default: False

HeightScale (Deprecated)

property HeightScale: Single read GetHeightScale write SetHeightScale; deprecated;

Deprecated: Use the Height property instead for Y-axis scaling.

Methods

SetXYResolution

procedure SetXYResolution(const X, Y: Integer); virtual;

Sets resolution for both axes in one call.

Parameters:

  • X - Column count (ResolutionX)
  • Y - Row count (ResolutionY)

Recommended values: 16, 32, 64, 128, 256, 512, 1024, 2048

Example:

LTerrain.SetXYResolution(256, 256);

RebuildTerrain

procedure RebuildTerrain(const AStaticBuffering: Boolean = true;
  const ADecimate: Boolean = false); virtual;

Clears previous terrain and rebuilds from current heightmap and settings.

Parameters:

  • AStaticBuffering - Upload to GPU and clear local data
  • ADecimate - Automatically decimate mesh with default parameters

Example:

// Standard rebuild with GPU buffering
LTerrain.RebuildTerrain(True, False);

// Rebuild with automatic decimation
LTerrain.RebuildTerrain(True, True);

// Keep data in memory for manual decimation
LTerrain.RebuildTerrain(False, False);

RandomTerrain (by Type)

procedure RandomTerrain(const AAlgorithm: TRandomTerrainAlgorithmType;
  const AStaticBuffering: Boolean = true;
  const ADecimate: Boolean = false); overload;

Builds heightmap using specified procedural algorithm type with default parameters.

Parameters:

  • AAlgorithm - Algorithm type (Hill, DiamondSquare, Mandelbrot, PerlinNoise, Plateau, Brownian, Planar)
  • AStaticBuffering - Upload to GPU
  • ADecimate - Auto-decimate

Example:

LTerrain.RandomTerrain(TRandomTerrainAlgorithmType.PerlinNoise);

See also: Gorilla.Terrain.Algorithm

RandomTerrain (by Instance)

procedure RandomTerrain(const AAlgorithm: TRandomTerrainAlgorithm;
  const AStaticBuffering: Boolean = true;
  const ADecimate: Boolean = false); overload;

Builds heightmap using configured algorithm instance.

Parameters:

  • AAlgorithm - Configured algorithm instance
  • AStaticBuffering - Upload to GPU
  • ADecimate - Auto-decimate

Example:

var
  LAlg: TPerlinNoiseTerrain;
begin
  LAlg := TPerlinNoiseTerrain.Create;
  try
    LAlg.Size := 8;
    LAlg.Intensity := 0.5;
    LAlg.Interpolation := TPerlinNoiseInterpolation.Cosine;
    LTerrain.RandomTerrain(LAlg);
  finally
    LAlg.Free;
  end;
end;

See also: Gorilla.Terrain.Algorithm

Decimate

procedure Decimate(const ARatio: Single = 0.2;
  const AAgression: Single = 3.0;
  const ATolerance: Single = 0.0001); virtual;

Reduces terrain mesh complexity using simplification algorithm.

Parameters:

  • ARatio - Target reduction ratio (0.2 = reduce to 20% of original)
  • AAgression - How aggressively to remove vertices (higher = more reduction)
  • ATolerance - Minimum error threshold for vertex removal

Remarks: Terrain must have StaticBuffering = False with mesh data in memory.

Workflow:

LTerrain.RebuildTerrain(False, False);  // Keep in memory
LTerrain.Decimate(0.3, 4.0, 0.0001);
LTerrain.StaticBuffering := True;       // Upload to GPU

BeginUpdate / EndUpdate

procedure BeginUpdate; override;
procedure EndUpdate; override;

Batch multiple property changes to rebuild terrain only once.

Example:

LTerrain.BeginUpdate;
try
  LTerrain.Width := 200;
  LTerrain.Depth := 200;
  LTerrain.Resolution := Point(512, 512);
  LTerrain.Smoothing := 5;
finally
  LTerrain.EndUpdate;  // Rebuilds once
end;

GetMeshMatrix

function GetMeshMatrix: TMatrix3D; override;

Returns the mesh transformation matrix for the terrain vertices.

Returns: 4×4 transformation matrix

GetBodySize

function GetBodySize: TPoint3D; override;

Returns terrain dimensions.

Returns: TPoint3D with Width, Height, Depth

LoadFromDefinition / StoreToDefinition

procedure LoadFromDefinition(const ADef: TCustomDef); override;
procedure StoreToDefinition(const ADef: TCustomDef); override;

Load/save terrain configuration from/to definition objects.

Resize3D

procedure Resize3D; override;

Called when terrain is resized. Triggers terrain rebuild.

Examples

Basic Terrain from Heightmap

var
  LTerrain: TGorillaTerrain;
  LHeightMap: TBitmap;
begin
  LTerrain := TGorillaTerrain.Create(Self);
  LTerrain.Parent := GorillaViewport1;
  LTerrain.Width := 100;
  LTerrain.Height := 20;
  LTerrain.Depth := 100;
  LTerrain.SetXYResolution(256, 256);
  LTerrain.HeightMap.LoadFromFile('terrain.png');
end;

Optimized High-Resolution Terrain

var
  LTerrain: TGorillaTerrain;
begin
  LTerrain := TGorillaTerrain.Create(Self);
  LTerrain.Parent := GorillaViewport1;

  LTerrain.BeginUpdate;
  try
    LTerrain.Width := 200;
    LTerrain.Height := 40;
    LTerrain.Depth := 200;
    LTerrain.SetXYResolution(512, 512);
    LTerrain.HeightMap := MyHeightMap;
    LTerrain.Smoothing := 5;
  finally
    LTerrain.EndUpdate;
  end;

  // Optimize with decimation
  LTerrain.StaticBuffering := False;
  LTerrain.Decimate(0.3, 3.5, 0.0001);  // Reduce to 30%
  LTerrain.StaticBuffering := True;
end;

Debug Visualization

// Enable for debugging (disable StaticBuffering first)
LTerrain.StaticBuffering := False;
LTerrain.RebuildTerrain(False);
LTerrain.ShowGrid := True;
LTerrain.ShowVertices := True;
LTerrain.VerticeColor := TAlphaColorRec.Yellow;

See Also