vkmEngine 1.0.15
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
Vkm::Engine::TerrainAsset Struct Reference

A terrain: a square grid of heights, the share of each of four materials painted at each grid vertex, and the plants scattered over it. More...

#include <terrain_asset.h>

Inheritance diagram for Vkm::Engine::TerrainAsset:
Vkm::Engine::Resource

Public Member Functions

float spacing () const
 Metres between neighbouring grid vertices.
float heightAt (float x, float z) const
 The height at local x, z, the surface its triangles draw there.
bool resample (uint32_t to)
 Regrid to to vertices a side over the same size, keeping the shape, the paint and the plants.
glm::vec3 normalAt (int i, int j) const
 The surface's normal at grid vertex i, j, from its neighbours' heights.
Public Member Functions inherited from Vkm::Engine::Resource
 Resource (const Resource &other)
 Duplicate other's contents and name, but not its identity.
const std::string & name () const noexcept
 Serializable identity; non-empty and unique within its type once added.
uint64_t uid () const noexcept
 Process-unique id stamped by add(); names the asset, not the slot it sits in.
uint64_t version () const noexcept
 Change counter the backend keys GPU re-uploads on; moved by commit().
bool isHidden () const noexcept
 Added by ResourceManager::addPrivate: not user-facing, not saved.
nlohmann::json & sourceJson ()
 Mutable source JSON, allocating a null document if there is none.
const nlohmann::json & sourceJson () const
 Const source JSON; asserts hasSource(), so guard the call with it.

Static Public Member Functions

static bool validResolution (uint32_t resolution)
 Whether resolution is one a terrain may have.

Public Attributes

uint32_t resolution = 129
 Grid vertices a side, 2^n + 1 from MIN_ to MAX_RESOLUTION.
float size = 64.0f
 Metres a side.
std::vector< float > heights
 resolution^2, row by row along X: metres up from the entity.
std::vector< uint8_t > weights
 resolution^2 * 4: each vertex's share of the four layers, R to A.
std::array< MaterialHandle, MaterialAsset::MAX_LAYERS > layers {}
 What each share is drawn with; an empty layer draws black where it is painted.
std::array< float, MaterialAsset::MAX_LAYERS > layerTiling {4.0f, 4.0f, 4.0f, 4.0f}
std::vector< TerrainFoliage > foliage
 Up to MAX_FOLIAGE.

Static Public Attributes

static constexpr uint32_t MIN_RESOLUTION = 33
 The fewest grid vertices a side: one chunk's.
static constexpr uint32_t MAX_RESOLUTION = 1025
 The most: what a terrain's meshes, collider and maps are sized for.
static constexpr uint32_t MAX_FOLIAGE = 8
 The most foliage layers.
static constexpr uint32_t MAX_PLANT_PARTS = 8
 The most parts a plant has.

Detailed Description

A terrain: a square grid of heights, the share of each of four materials painted at each grid vertex, and the plants scattered over it.

Centred on its entity: the grid spans size metres along local X and Z, vertex (i, j) at x = (i / (resolution - 1) - 0.5) * size, z alike, its height the local Y. Authored by the editor's brushes and kept in a .terrain file of the project (TerrainFile), cooked as any asset.

Member Function Documentation

◆ validResolution()

bool Vkm::Engine::TerrainAsset::validResolution ( uint32_t resolution)
static

Whether resolution is one a terrain may have.

Parameters
resolutionGrid vertices a side.
Returns
True for 2^n + 1 from MIN_RESOLUTION to MAX_RESOLUTION.

◆ heightAt()

float Vkm::Engine::TerrainAsset::heightAt ( float x,
float z ) const

The height at local x, z, the surface its triangles draw there.

Each grid square is two triangles split along its diagonal from (i, j) to (i + 1, j + 1), as the meshes and the collider make it.

Parameters
xLocal metres along X; clamped to the grid.
zLocal metres along Z; clamped to the grid.
Returns
Local metres up.

◆ resample()

bool Vkm::Engine::TerrainAsset::resample ( uint32_t to)

Regrid to to vertices a side over the same size, keeping the shape, the paint and the plants.

Heights are read off the surface as drawn (heightAt), so a finer grid keeps every old vertex's height and a coarser one keeps those it still has. Layer shares and plant densities blend between the neighbouring vertices, the shares summing to full again.

Parameters
toGrid vertices a side.
Returns
False, changing nothing, when to is no valid resolution or the grid is unsound.

◆ normalAt()

glm::vec3 Vkm::Engine::TerrainAsset::normalAt ( int i,
int j ) const

The surface's normal at grid vertex i, j, from its neighbours' heights.

Parameters
iAlong X, clamped to the grid.
jAlong Z, clamped to the grid.
Returns
Unit normal, local.

Member Data Documentation

◆ layerTiling

std::array<float, MaterialAsset::MAX_LAYERS> Vkm::Engine::TerrainAsset::layerTiling {4.0f, 4.0f, 4.0f, 4.0f}

Metres one repeat of each layer's maps spans: sand finer than a field, whatever the layer's material says for a mesh.


The documentation for this struct was generated from the following file: