From ed1d045ab12d78bda01c5840c057e793326fdf72 Mon Sep 17 00:00:00 2001 From: Sara Date: Sun, 22 Feb 2026 23:39:46 +0100 Subject: [PATCH] chore: adjusting terrain logic --- modules/terrain/terrain.cpp | 46 ++++++++++++++++---------------- modules/terrain/terrain.h | 8 +++--- modules/terrain/terrain_chunk.h | 2 +- project/scenes/terrain_test.tscn | 17 ++++++++---- 4 files changed, 41 insertions(+), 32 deletions(-) diff --git a/modules/terrain/terrain.cpp b/modules/terrain/terrain.cpp index 3ace296b..c80ed45b 100644 --- a/modules/terrain/terrain.cpp +++ b/modules/terrain/terrain.cpp @@ -18,29 +18,6 @@ void Terrain::update_modifier_list() { } } -void Terrain::construct_chunk_grid() { - size_t const chunks_per_side{ this->side_length / this->chunk_size }; - Vector3 const origin{ -(float)chunks_per_side / 2.f * (float)this->chunk_size, 0.f, -(float)chunks_per_side / 2.f * (float)this->chunk_size }; - for (size_t y{ 0 }; y < chunks_per_side; ++y) { - for (size_t x{ 0 }; x < chunks_per_side; ++x) { - TerrainChunkMesh *chunk{ memnew(TerrainChunkMesh) }; - chunk->set_size(this->chunk_size); - chunk->set_detail(this->detail); - chunk->set_terrain(this); - chunk->set_position(origin + Vector3{ (float)this->chunk_size * (float)x, 0.f, (float)this->chunk_size * (float)y }); - add_child(chunk); - chunk->set_owner(this); - this->meshes.push_back(chunk); - } - } -} - -void Terrain::generate_meshes() { - for (TerrainChunkMesh *mesh : this->meshes) { - mesh->update_mesh(); - } -} - void Terrain::child_entered(Node *node) { if (cast_to(node)) { update_modifier_list(); @@ -71,6 +48,29 @@ void Terrain::_notification(int what) { } } +void Terrain::construct_chunk_grid() { + size_t const chunks_per_side{ this->side_length / this->chunk_size }; + Vector3 const origin{ -(float)this->side_length / 2.f, 0.f, -(float)this->side_length / 2.f }; + for (size_t y{ 0 }; y < chunks_per_side; ++y) { + for (size_t x{ 0 }; x < chunks_per_side; ++x) { + TerrainChunkMesh *chunk{ memnew(TerrainChunkMesh) }; + chunk->set_size(this->chunk_size); + chunk->set_detail(this->detail); + chunk->set_terrain(this); + chunk->set_position(origin + Vector3{ (float)this->chunk_size * (float)x, 0.f, (float)this->chunk_size * (float)y }); + add_child(chunk); + chunk->set_owner(this); + this->meshes.push_back(chunk); + } + } +} + +void Terrain::generate_meshes() { + for (TerrainChunkMesh *mesh : this->meshes) { + mesh->update_mesh(); + } +} + float Terrain::height_at(Vector2 world_coordinate) { float height{ 0 }; for (TerrainModifier *mod : this->modifiers) { diff --git a/modules/terrain/terrain.h b/modules/terrain/terrain.h index 10ad437d..1b23eebc 100644 --- a/modules/terrain/terrain.h +++ b/modules/terrain/terrain.h @@ -3,6 +3,7 @@ #include "core/templates/vector.h" #include "macros.h" #include "scene/main/node.h" +#include "scene/main/timer.h" class TerrainChunkMesh; class TerrainModifier; @@ -11,8 +12,6 @@ class Terrain : public Node { static void _bind_methods(); void ready(); void update_modifier_list(); - void construct_chunk_grid(); - void generate_meshes(); void child_entered(Node *node); void child_exiting(Node *node); @@ -21,11 +20,14 @@ protected: void _notification(int what); public: + void construct_chunk_grid(); + void generate_meshes(); float height_at(Vector2 world_coordinate); private: + Timer *timer{ nullptr }; size_t side_length{ 100 }; - size_t chunk_size{ 10 }; + size_t chunk_size{ 50 }; size_t detail{ 1 }; public: diff --git a/modules/terrain/terrain_chunk.h b/modules/terrain/terrain_chunk.h index c10e4270..e248da8b 100644 --- a/modules/terrain/terrain_chunk.h +++ b/modules/terrain/terrain_chunk.h @@ -17,7 +17,7 @@ public: private: Ref surface{ memnew(SurfaceTool) }; - Terrain *terrain{}; + Terrain *terrain{ nullptr }; size_t detail{ 1 }; size_t size{ 1 }; diff --git a/project/scenes/terrain_test.tscn b/project/scenes/terrain_test.tscn index 7d549704..ec879def 100644 --- a/project/scenes/terrain_test.tscn +++ b/project/scenes/terrain_test.tscn @@ -13,13 +13,13 @@ background_mode = 2 sky = SubResource("Sky_w3uoq") [sub_resource type="Curve" id="Curve_kbmr5"] -_limits = [0.0, 1.0, 0.0, 10.0] -_data = [Vector2(0, 1), 0.0, 0.0, 0, 0, Vector2(5.1981277, 1), 0.0, 0.0, 0, 0, Vector2(10, 0), 0.0, 0.0, 0, 0] +_limits = [0.0, 1.0, 0.0, 50.0] +_data = [Vector2(0, 1), 0.0, 0.0, 0, 0, Vector2(34.188484, 1), 0.0, 0.0, 0, 0, Vector2(50, 1), -0.002701958, 0.0, 0, 0] point_count = 3 [sub_resource type="Curve" id="Curve_w3uoq"] -_limits = [-10.0, 0.0, 0.0, 10.0] -_data = [Vector2(0, -0.02282238), 0.0, 0.0, 0, 0, Vector2(10, -10), -0.10877486, 0.0, 0, 0] +_limits = [-10.0, 0.0, 0.0, 50.0] +_data = [Vector2(0, -0.02282238), 0.0, -0.0014457313, 0, 0, Vector2(50, -10), -0.3114828, 0.0, 0, 0] point_count = 2 [node name="Node3D" type="Node3D" unique_id=289500437] @@ -30,6 +30,13 @@ environment = SubResource("Environment_o3i6r") [node name="Terrain" type="Terrain" parent="." unique_id=1169843565] [node name="TerrainModifierDistance" type="TerrainModifierDistance" parent="Terrain" unique_id=1885116624] -transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 13.434517, 8.531313, 11.648043) +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 13.434517, 11.863001, 11.648043) +blend_distance = 4.0 +distance_weight_curve = SubResource("Curve_kbmr5") +distance_height_curve = SubResource("Curve_w3uoq") + +[node name="TerrainModifierDistance2" type="TerrainModifierDistance" parent="Terrain" unique_id=826291582] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -13.987091, 11.256919, -20.878822) +blend_distance = 4.0 distance_weight_curve = SubResource("Curve_kbmr5") distance_height_curve = SubResource("Curve_w3uoq")