Gorilla.Terrain.Sphere Unit Reference

Spherical terrain component for planetary surfaces.

Unit: Gorilla.Terrain.Sphere

TGorillaSphereTerrain

Spherical terrain component that deforms a sphere surface using heightmaps or procedural algorithms.

Inheritance: TGorillaMesh → TGorillaSphereTerrain

Platforms: All Gorilla platforms

Overview

TGorillaSphereTerrain creates planetary terrain by:

  1. Generating a UV-sphere with configurable resolution
  2. Sampling heightmap values at each vertex's UV coordinates
  3. Deforming vertices along their normals based on heightmap values

This is ideal for creating planets, moons, asteroids, and other spherical celestial bodies.

Constructor/Destructor

Create

constructor Create(AOwner: TComponent); override;

Creates a sphere terrain instance with default settings.

Destroy

destructor Destroy; override;

Frees resources.

AfterConstruction

procedure AfterConstruction; override;

Initializes default values:

  • Resolution: 512×512
  • TwoFace: False
  • StaticBuffering: True
  • AutoDecimate: False
  • HeightScale: 0.1
  • Dimensions: 1×1×1 (unit sphere)

Properties

All properties from TGorillaTerrain are available with these differences:

HeightScale

property HeightScale: Single read FHeightScale write SetHeightScale;

Multiplier for height deformation from sphere surface.

Default: 0.1

Range: 0.0 (no deformation) to higher values

Note: Unlike planar terrain, this property is not deprecated for sphere terrain.

Behavior:

  • Vertices are displaced along their normal vectors
  • Positive values push vertices outward (mountains)
  • Negative values pull vertices inward (valleys)
  • Value of 0.1 means 10% of sphere radius

ResolutionX / ResolutionY

property ResolutionX: Integer read GetResolutionX write SetResolutionX;
property ResolutionY: Integer read GetResolutionY write SetResolutionY;

For sphere terrain:

  • ResolutionX = Axes subdivisions (longitude, horizontal)
  • ResolutionY = Height subdivisions (latitude, vertical)

Default: 512×512

Recommended: Higher values for larger spheres, lower for small objects

Methods

Same methods as TGorillaTerrain:

  • SetXYResolution(X, Y: Integer)
  • RebuildTerrain(AStaticBuffering, ADecimate: Boolean)
  • RandomTerrain(...)
  • Decimate(...)
  • BeginUpdate / EndUpdate

Internally uses TGorillaTerrainHelper.BuildSphereTerrainFromHeightMap() instead of planar method.

Examples

Basic Planetary Terrain

var
  LPlanet: TGorillaSphereTerrain;
  LHeightMap: TBitmap;
begin
  LPlanet := TGorillaSphereTerrain.Create(Self);
  LPlanet.Parent := GorillaViewport1;

  // Configure sphere
  LPlanet.Width := 50;   // Radius in all directions
  LPlanet.Height := 50;
  LPlanet.Depth := 50;
  LPlanet.SetXYResolution(512, 512);
  LPlanet.HeightScale := 0.15;  // 15% deformation

  // Load planet heightmap
  LHeightMap := TBitmap.Create;
  try
    LHeightMap.LoadFromFile('mars_heightmap.png');
    LPlanet.HeightMap := LHeightMap;
    LPlanet.RebuildTerrain;
  finally
    LHeightMap.Free;
  end;
end;

Procedural Planet

var
  LMoon: TGorillaSphereTerrain;
  LAlg: TPerlinNoiseTerrain;
begin
  LMoon := TGorillaSphereTerrain.Create(Self);
  LMoon.Parent := GorillaViewport1;
  LMoon.Width := 30;
  LMoon.Height := 30;
  LMoon.Depth := 30;
  LMoon.SetXYResolution(256, 256);
  LMoon.HeightScale := 0.08;

  // Generate procedural surface
  LAlg := TPerlinNoiseTerrain.Create;
  try
    LAlg.Size := 8;
    LAlg.Intensity := 0.3;
    LAlg.Interpolation := TPerlinNoiseInterpolation.Cosine;
    LMoon.RandomTerrain(LAlg);
  finally
    LAlg.Free;
  end;
end;

Asteroid with Craters

var
  LAsteroid: TGorillaSphereTerrain;
  LAlg: TPlateauTerrain;
begin
  LAsteroid := TGorillaSphereTerrain.Create(Self);
  LAsteroid.Parent := GorillaViewport1;
  LAsteroid.Width := 10;
  LAsteroid.Height := 10;
  LAsteroid.Depth := 10;
  LAsteroid.SetXYResolution(128, 128);
  LAsteroid.HeightScale := 0.2;

  // Use inverted plateaus to create craters
  LAlg := TPlateauTerrain.Create;
  try
    LAlg.Size := 7;
    LAlg.Plateaus := 15;
    LAlg.MinSize := 10;
    LAlg.MaxSize := 50;
    LAlg.MinHeight := 0.3;
    LAlg.MaxHeight := 0.8;
    LAlg.Inverted := True;  // Create indentations (craters)
    LAsteroid.RandomTerrain(LAlg);
  finally
    LAlg.Free;
  end;
end;

Multi-Layer Planet Surface

procedure CreateDetailedPlanet(APlanet: TGorillaSphereTerrain);
var
  LBase, LDetail: TBitmap;
  LCombined: TBitmap;
begin
  LBase := TBitmap.Create;
  LDetail := TBitmap.Create;
  LCombined := TBitmap.Create;

  try
    // Load base terrain (large features)
    LBase.LoadFromFile('planet_base.png');

    // Load detail map (small features)
    LDetail.LoadFromFile('planet_detail.png');

    // Combine with different intensities
    CombineHeightMaps(LBase, LDetail, LCombined, 0.7, 0.3);

    APlanet.BeginUpdate;
    try
      APlanet.SetXYResolution(512, 512);
      APlanet.HeightScale := 0.12;
      APlanet.Smoothing := 2;
      APlanet.HeightMap := LCombined;
    finally
      APlanet.EndUpdate;
    end;
  finally
    LBase.Free;
    LDetail.Free;
    LCombined.Free;
  end;
end;

Optimized High-Resolution Planet

var
  LEarth: TGorillaSphereTerrain;
begin
  LEarth := TGorillaSphereTerrain.Create(Self);
  LEarth.Parent := GorillaViewport1;

  LEarth.BeginUpdate;
  try
    LEarth.Width := 100;
    LEarth.Height := 100;
    LEarth.Depth := 100;
    LEarth.SetXYResolution(1024, 1024);  // High detail
    LEarth.HeightScale := 0.05;          // Subtle terrain
    LEarth.HeightMap := EarthHeightMap;
  finally
    LEarth.EndUpdate;
  end;

  // Optimize with decimation
  LEarth.StaticBuffering := False;
  LEarth.Decimate(0.4, 3.0, 0.0001);  // Reduce to 40%
  LEarth.StaticBuffering := True;
end;

Heightmap Considerations

Equirectangular Projection

Sphere terrain uses UV mapping, so heightmaps should be in equirectangular projection (also called lat/long projection):

     0°              180°            360°
   ┌─────────────────────────────────┐  90°N
   │          North Pole             │
   │                                 │
   │                                 │   0° (Equator)
   │                                 │
   │          South Pole             │
   └─────────────────────────────────┘  90°S

Creating Equirectangular Maps:

  • Use tools like NASA's Blue Marble datasets
  • Convert from other projections using mapping software
  • Generate procedurally with 2:1 aspect ratio

UV Coordinate Mapping

U (horizontal): 0.0 (left) to 1.0 (right) → Longitude (0° to 360°)
V (vertical):   0.0 (top) to 1.0 (bottom) → Latitude (90°N to 90°S)

HeightScale Guidelines

HeightScale Effect Use Case
0.01 - 0.05 Subtle Earth-like planets
0.05 - 0.15 Moderate Rocky planets, moons
0.15 - 0.30 Dramatic Asteroids, extreme terrain
0.30+ Extreme Artistic/alien worlds

Performance

Resolution Impact

Sphere terrain requires more vertices than equivalent planar terrain:

Resolution Vertices Triangles Memory
128×128 ~16,000 ~32,000 ~1.5 MB
256×256 ~66,000 ~131,000 ~6.3 MB
512×512 ~263,000 ~524,000 ~25 MB
1024×1024 ~1,050,000 ~2,097,000 ~100 MB

Optimization Tips

  1. Use LOD for distant planets - Lower resolution when far from camera
  2. Enable static buffering - Default is True
  3. Apply decimation - Can reduce vertices by 50-70% with minimal visual impact
  4. Match heightmap resolution - Avoid upscaling
  5. Consider culling - Use frustum culling for multiple planets
  6. Texture optimization - Use compressed textures for heightmaps

Mobile Performance

For mobile devices:

  • Keep resolution ≤ 256×256
  • Use decimation aggressively (0.3-0.5 ratio)
  • Enable static buffering
  • Consider multiple LOD versions

Common Issues

Seam at Edges

If you see a seam where longitude 0° meets 360°:

// Ensure heightmap wraps correctly
// Left edge (0°) should match right edge (360°)

Most procedural algorithms handle this automatically.

Polar Distortion

Vertices bunch up at poles (90°N and 90°S). This is normal for UV-spheres.

Solutions:

  • Accept it (usually not noticeable with textures)
  • Use lower resolution
  • Apply custom vertex distribution (advanced)

Heightmap Stretching

Heightmap pixels stretch more at poles than equator.

Solutions:

  • Design heightmaps accounting for polar stretching
  • Use higher resolution near poles
  • Generate procedurally to avoid stretching artifacts

See Also