Skip to main content

Terrain

Struct Terrain 

Source
pub struct Terrain {
    pub sea_level: f32,
    pub heightmap: Grid<f32>,
    pub flow_accumulation: Grid<f32>,
    pub flow: Grid<f32>,
    pub rivers: Grid<u8>,
    pub lakes: Grid<u8>,
    pub ocean: Grid<u8>,
}
Expand description

A generated terrain: elevation plus the hydrology layers derived from it.

All per-cell buffers are Grids, indexed as grid[(x, y)]; their flat row-major storage is available through Grid::as_slice for zero-copy hand-off to FFI, GPUs or file formats.

The layers are independent and raw on purpose: this crate decides what the terrain is, not what it means. Consumers that classify water for their own rules (salinity, drinkability, navigability) should build on ocean, lakes and flow_accumulation rather than on rendered colours or on elevation thresholds.

Fields§

§sea_level: f32

Sea level the terrain was generated with, in [0, 1].

§heightmap: Grid<f32>

Elevation per cell, in [0, 1].

§flow_accumulation: Grid<f32>

Raw D8 flow accumulation per cell, in cells of upstream catchment (each cell contributes 1, including itself). Use this to size channels — river width typically scales with the square root of catchment area. All-zero when river extraction is disabled.

§flow: Grid<f32>

flow_accumulation log-normalised into [0, 1], which is the more useful form as a moisture proxy for biome placement. All-zero when river extraction is disabled.

§rivers: Grid<u8>

River mask per cell: 1 where a river runs, 0 elsewhere.

§lakes: Grid<u8>

Lake mask per cell: 1 where an inland depression holds standing water, 0 elsewhere. Rivers vanish beneath lakes and continue from their outlet. A basin whose floor lies below sea level but which is cut off from the sea counts as a lake, not ocean.

§ocean: Grid<u8>

Ocean mask per cell: 1 where the cell is at or below sea level and connected to the map border through other such cells. Always computed, including when river extraction is disabled, because it is a fact about the heightmap rather than a hydrology result.

Implementations§

Source§

impl Terrain

Source

pub fn builder() -> TerrainBuilder

Starts building a terrain with default parameters.

Source

pub fn width(&self) -> usize

Map width in cells.

Source

pub fn height(&self) -> usize

Map height in cells.

Source

pub fn height_at(&self, x: usize, y: usize) -> f32

Elevation at (x, y), in [0, 1].

Source

pub fn flow_at(&self, x: usize, y: usize) -> f32

Normalised flow accumulation at (x, y), in [0, 1].

Source

pub fn flow_accumulation_at(&self, x: usize, y: usize) -> f32

Raw upstream catchment at (x, y), in cells.

Source

pub fn is_river(&self, x: usize, y: usize) -> bool

Whether a river runs through (x, y).

Source

pub fn is_lake(&self, x: usize, y: usize) -> bool

Whether (x, y) lies in a lake.

Source

pub fn is_ocean(&self, x: usize, y: usize) -> bool

Whether (x, y) is ocean: at or below sea level and connected to the map border. Enclosed low ground is not ocean.

Source

pub fn is_water(&self, x: usize, y: usize) -> bool

Whether (x, y) is covered by water of any kind: ocean, lake or river.

Note this is a union of the three masks, not an elevation test — a closed basin below sea level counts as lake, not ocean.

Source

pub fn to_rgb(&self, style: RenderStyle) -> Vec<u8> ⓘ

Renders the terrain to a row-major RGB byte buffer (width * height * 3).

Source

pub fn save_layer_png<P: AsRef<Path>>( &self, layer: Layer, path: P, ) -> Result<(), RenderError>

Writes a single Layer as a PNG file: the shaded render, or one of the raw data layers (elevation and flow as 16-bit greyscale, masks as 8-bit black/white).

Requires the png cargo feature (enabled by default).

Source

pub fn save_png<P: AsRef<Path>>( &self, path: P, style: RenderStyle, ) -> Result<(), RenderError>

Renders the terrain and writes it as a PNG file.

Requires the png cargo feature (enabled by default).

Trait Implementations§

Source§

impl Clone for Terrain

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Terrain

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.