22
33#include < limitless/instances/model_instance.hpp>
44#include < utility>
5+ #include < limitless/models/model.hpp>
6+
7+ #include < limitless/assets.hpp>
8+ #include < limitless/ms/material.hpp>
9+ #include < limitless/core/texture/texture_builder.hpp>
10+
11+ #include < limitless/util/brush.hpp>
12+ #include " instanced_instance.hpp"
513
614namespace Limitless {
7- class TerrainInstance : public ModelInstance {
15+ class Assets ;
16+
17+ class TerrainInstance : public Instance {
18+ public:
19+ struct control_value final {
20+ uint32_t base_id : 6 ;
21+ uint32_t extra_id : 6 ;
22+ uint32_t blend : 8 ;
23+ uint32_t reserved : 12 ;
24+
25+ static uint32_t encode (struct control_value ctrl) {
26+ uint32_t result = 0 ;
27+ result |= (ctrl.base_id & 0x3F );
28+ result |= (ctrl.extra_id & 0x3F ) << 6 ;
29+ result |= (ctrl.blend & 0xFF ) << 12 ;
30+ result |= (ctrl.reserved & 0xFFF ) << 20 ;
31+ return result;
32+ }
33+
34+ static control_value decode (uint32_t value) {
35+ control_value ctrl {};
36+ ctrl.base_id = value & 0x3F ;
37+ ctrl.extra_id = (value >> 6 ) & 0x3F ;
38+ ctrl.blend = (value >> 12 ) & 0xFF ;
39+ ctrl.reserved = (value >> 20 ) & 0xFFF ;
40+ return ctrl;
41+ }
42+ };
843 public:
9- TerrainInstance (decltype (model) model, const glm::vec3& position)
10- : ModelInstance {InstanceType::Terrain, std::move (model), position} {
44+ // terrain size in world units
45+ float terrain_size = 128 .0f ;
46+
47+ // spacing between vertices
48+ float vertex_spacing = 1 .0f ;
49+
50+ // height scale factor
51+ float height_scale = 1 .0f ;
52+
53+ // texture chunk scale
54+ // to how many tiles is the texture applied
55+ float texture_scale = 4 .0f ;
56+
57+ float vertex_normals_distance = 20 .0f ;
58+
59+ // adds macro variation pattern
60+ bool macro_variation = false ;
61+
62+ float noise1_scale = 0 .225f ;
63+ float noise2_scale = 0 .04f ;
64+ float noise2_angle = 0 .0f ;
65+ float noise2_offset = 0 .5f ;
66+ float noise3_scale = 0 .076f ;
67+
68+ glm::vec3 macro_variation1 = glm::vec3(0 .5f );
69+ glm::vec3 macro_variation2 = glm::vec3(0 .33f );
70+
71+ bool show_tiles = false ;
72+ bool show_terrain_size = false ;
73+ bool show_vertex_normals_distance = false ;
74+ bool show_texture_chunks = false ;
75+
76+ int mesh_size = 32 ;
77+ int mesh_lod_count = 6 ;
78+
79+ struct Mesh {
80+ std::shared_ptr<ModelInstance> cross;
81+
82+ std::shared_ptr<InstancedInstance> tiles;
83+ std::shared_ptr<InstancedInstance> fillers;
84+ std::shared_ptr<InstancedInstance> trims;
85+ std::vector<std::shared_ptr<ModelInstance>> trims_test;
86+ std::shared_ptr<InstancedInstance> seams;
87+ } mesh;
88+
89+ std::shared_ptr<Texture> height_map;
90+ std::shared_ptr<Texture> control;
91+ std::shared_ptr<Texture> albedo;
92+ std::shared_ptr<Texture> normal;
93+ std::shared_ptr<Texture> orm;
94+ std::shared_ptr<Texture> noise;
95+
96+ void snap (const Camera& p_cam_pos);
97+ public:
98+ TerrainInstance (
99+ std::shared_ptr<Texture> height_map,
100+ std::shared_ptr<Texture> control_map,
101+ std::shared_ptr<Texture> albedo_map,
102+ std::shared_ptr<Texture> normal_map,
103+ std::shared_ptr<Texture> orm_map,
104+ std::shared_ptr<Texture> noise
105+ );
106+
107+ void initializeMesh (Assets& assets);
108+
109+ void setHeightMap (const std::shared_ptr<Texture>& height_map);
110+ void setControlMap (const std::shared_ptr<Texture>& control_map);
111+ void setNoise (const std::shared_ptr<Texture>& noise);
112+ void setAlbedoMap (const std::shared_ptr<Texture>& albedo_map);
113+ void setNormalMap (const std::shared_ptr<Texture>& normal_map);
114+ void setOrmMap (const std::shared_ptr<Texture>& orm_map);
115+
116+ void setChunkSize (float chunkSize);
117+ void setVertexSpacing (float vertexSpacing);
118+ void setVertexNormalsDistance (float distance);
119+
120+ void setHeightScale (float heightScale);
121+ void setNoise1Scale (float noise1Scale);
122+ void setNoise2Scale (float noise2Scale);
123+ void setNoise2Angle (float noise2Angle);
124+ void setNoise2Offset (float noise2Offset);
125+ void setNoise3Scale (float noise3Scale);
126+ void setMacroVariation1 (const glm::vec3 ¯oVariation1);
127+ void setMacroVariation2 (const glm::vec3 ¯oVariation2);
128+
129+ void setMeshSize (int meshSize);
130+ void setMeshLodCount (int meshLodCount);
131+
132+ /* *
133+ * Updates full height map from data
134+ *
135+ * expects pointer to UNSIGNED CHAR / std::byte
136+ *
137+ * you should convert your normalized floats in range [0, 1] to unsigned bytes [0, 255]
138+ *
139+ * to get height more than 1.0 set height_scale
140+ */
141+ void updateHeight (const void * data);
142+
143+ /* *
144+ * Updates part of height map with offset and size
145+ */
146+ void updateHeight (glm::uvec2 offset, glm::uvec2 size, const void * data);
147+
148+ /* *
149+ * Updates full control map from data
150+ *
151+ * expects pointer to TerrainInstance::control_value / uint32_t
152+ */
153+ void updateControl (const void * data);
154+
155+ /* *
156+ * Updates part of control map with offset and size
157+ */
158+ void updateControl (glm::uvec2 offset, glm::uvec2 size, const void * data);
159+
160+ [[nodiscard]] const auto & getMesh () const noexcept { return mesh; }
161+
162+ void update (const Limitless::Camera &camera) override ;
11163
164+ std::unique_ptr<Instance> clone () noexcept override {
165+ return std::make_unique<TerrainInstance>(*this );
12166 }
13167 };
14168}
0 commit comments