vkmEngine 1.0.15
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
terrain_asset.h
1#pragma once
2
3#include <array>
4#include <cstdint>
5#include <vector>
6
7#include <glm/glm.hpp>
8
9#include "resource/resource.h"
10#include "resource/resource_handle.h"
11
12#include "resource/asset/material_asset.h"
13#include "resource/asset/mesh_asset.h"
14
15namespace Vkm::Engine {
16
24 MeshHandle mesh;
25 MaterialHandle material;
26 glm::mat4 local{1.0f};
27};
28
36 std::vector<TerrainPlantPart> parts;
37 std::vector<uint8_t> density;
38 float perSquareMetre = 1.0f;
39 float minScale = 0.8f;
40 float maxScale = 1.2f;
41 float maxDistance = 60.0f;
42 uint32_t seed = 0;
43 bool castShadows = false;
44};
45
54struct TerrainAsset : public Resource {
56 static constexpr uint32_t MIN_RESOLUTION = 33;
58 static constexpr uint32_t MAX_RESOLUTION = 1025;
60 static constexpr uint32_t MAX_FOLIAGE = 8;
62 static constexpr uint32_t MAX_PLANT_PARTS = 8;
63
64 uint32_t resolution = 129;
65 float size = 64.0f;
66
67 std::vector<float> heights;
68 std::vector<uint8_t> weights;
69
71 std::array<MaterialHandle, MaterialAsset::MAX_LAYERS> layers{};
74 std::array<float, MaterialAsset::MAX_LAYERS> layerTiling{4.0f, 4.0f, 4.0f, 4.0f};
75
76 std::vector<TerrainFoliage> foliage;
77
79 float spacing() const { return size / static_cast<float>(resolution - 1); }
80
87 static bool validResolution(uint32_t resolution);
88
99 float heightAt(float x, float z) const;
100
112 bool resample(uint32_t to);
113
121 glm::vec3 normalAt(int i, int j) const;
122};
123
124using TerrainHandle = Handle<TerrainAsset>;
125
126} // namespace Vkm::Engine
Opaque, type-safe handle for resources, distinguished by ResourceType.
Definition resource_handle.h:16
A terrain: a square grid of heights, the share of each of four materials painted at each grid vertex,...
Definition terrain_asset.h:54
static bool validResolution(uint32_t resolution)
Whether resolution is one a terrain may have.
float heightAt(float x, float z) const
The height at local x, z, the surface its triangles draw there.
static constexpr uint32_t MAX_PLANT_PARTS
The most parts a plant has.
Definition terrain_asset.h:62
static constexpr uint32_t MIN_RESOLUTION
The fewest grid vertices a side: one chunk's.
Definition terrain_asset.h:56
glm::vec3 normalAt(int i, int j) const
The surface's normal at grid vertex i, j, from its neighbours' heights.
float spacing() const
Metres between neighbouring grid vertices.
Definition terrain_asset.h:79
float size
Metres a side.
Definition terrain_asset.h:65
std::vector< TerrainFoliage > foliage
Up to MAX_FOLIAGE.
Definition terrain_asset.h:76
std::array< float, MaterialAsset::MAX_LAYERS > layerTiling
Definition terrain_asset.h:74
uint32_t resolution
Grid vertices a side, 2^n + 1 from MIN_ to MAX_RESOLUTION.
Definition terrain_asset.h:64
std::vector< float > heights
resolution^2, row by row along X: metres up from the entity.
Definition terrain_asset.h:67
std::vector< uint8_t > weights
resolution^2 * 4: each vertex's share of the four layers, R to A.
Definition terrain_asset.h:68
std::array< MaterialHandle, MaterialAsset::MAX_LAYERS > layers
What each share is drawn with; an empty layer draws black where it is painted.
Definition terrain_asset.h:71
static constexpr uint32_t MAX_RESOLUTION
The most: what a terrain's meshes, collider and maps are sized for.
Definition terrain_asset.h:58
static constexpr uint32_t MAX_FOLIAGE
The most foliage layers.
Definition terrain_asset.h:60
bool resample(uint32_t to)
Regrid to to vertices a side over the same size, keeping the shape, the paint and the plants.
Plants a terrain scatters where their density is painted: grass, flowers, shrubs.
Definition terrain_asset.h:35
uint32_t seed
Picks where each plant stands and how it turns.
Definition terrain_asset.h:42
float perSquareMetre
Plants a square metre where the density is full.
Definition terrain_asset.h:38
std::vector< uint8_t > density
A grid vertex each: 255 is perSquareMetre, 0 none.
Definition terrain_asset.h:37
bool castShadows
Grass need not; a shrub looks afloat without.
Definition terrain_asset.h:43
float maxDistance
Metres from the eye past which none is drawn.
Definition terrain_asset.h:41
float maxScale
The largest.
Definition terrain_asset.h:40
float minScale
The smallest a plant is drawn, a share of its size.
Definition terrain_asset.h:39
std::vector< TerrainPlantPart > parts
One plant.
Definition terrain_asset.h:36
One part of a plant a terrain scatters: a mesh drawn with a material, placed in the plant.
Definition terrain_asset.h:23
glm::mat4 local
Where the part lies in the plant.
Definition terrain_asset.h:26