From 64696b534b7118fdf91ccccecb4cb6016d2f98d5 Mon Sep 17 00:00:00 2001 From: Relintai Date: Fri, 18 Apr 2025 19:50:17 +0200 Subject: [PATCH] Don't destroy and re-generate meshes in TerrainChunk's _enter and _exit_tree, just hide / show them. This makes scene tab swithing with TerrainWorlds in the editor a lot simpler. --- .../world/default/terrain_chunk_default.cpp | 5 +---- .../world/default/terrain_chunk_default.h | 3 --- modules/terraman/world/terrain_chunk.cpp | 22 ++++++++++--------- modules/terraman/world/terrain_world.cpp | 13 +++++------ 4 files changed, 19 insertions(+), 24 deletions(-) diff --git a/modules/terraman/world/default/terrain_chunk_default.cpp b/modules/terraman/world/default/terrain_chunk_default.cpp index 69d79f503..4fba764aa 100644 --- a/modules/terraman/world/default/terrain_chunk_default.cpp +++ b/modules/terraman/world/default/terrain_chunk_default.cpp @@ -766,10 +766,7 @@ void TerrainChunkDefault::_visibility_changed(bool visible) { void TerrainChunkDefault::_exit_tree() { TerrainChunk::_exit_tree(); - if (!_is_generating) { - rids_free(); - rids_clear(); - } + visibility_changed(false); } void TerrainChunkDefault::_world_transform_changed() { diff --git a/modules/terraman/world/default/terrain_chunk_default.h b/modules/terraman/world/default/terrain_chunk_default.h index 6a29dd2a2..b2c68c18a 100644 --- a/modules/terraman/world/default/terrain_chunk_default.h +++ b/modules/terraman/world/default/terrain_chunk_default.h @@ -168,9 +168,6 @@ public: void draw_debug_voxel_lights(); void draw_debug_mdr_colliders(); - //Visibility - void visibility_changed(bool visible); - //free void free_chunk(); diff --git a/modules/terraman/world/terrain_chunk.cpp b/modules/terraman/world/terrain_chunk.cpp index 9dc31ca54..d67463877 100644 --- a/modules/terraman/world/terrain_chunk.cpp +++ b/modules/terraman/world/terrain_chunk.cpp @@ -76,7 +76,7 @@ void TerrainChunk::set_visible(const bool value) { if (_is_visible == value) { return; } - + _is_visible = value; visibility_changed(value); @@ -1977,6 +1977,15 @@ TerrainChunk::TerrainChunk() { } TerrainChunk::~TerrainChunk() { + if (_library.is_valid() && _library->supports_caching()) { + if (material_cache_key_has()) { + _library->material_cache_unref(material_cache_key_get()); + + material_cache_key_set(0); + material_cache_key_has_set(false); + } + } + if (_library.is_valid()) { _library.unref(); } @@ -2048,6 +2057,8 @@ void TerrainChunk::_enter_tree() { } } #endif + + visibility_changed(_is_visible); } void TerrainChunk::_exit_tree() { @@ -2067,15 +2078,6 @@ void TerrainChunk::_exit_tree() { } } - if (_library.is_valid() && _library->supports_caching()) { - if (material_cache_key_has()) { - _library->material_cache_unref(material_cache_key_get()); - - material_cache_key_set(0); - material_cache_key_has_set(false); - } - } - #ifdef MODULE_VERTEX_LIGHTS_3D_ENABLED TerrainWorld *world = get_voxel_world(); diff --git a/modules/terraman/world/terrain_world.cpp b/modules/terraman/world/terrain_world.cpp index 8a61a18f4..024d36e45 100644 --- a/modules/terraman/world/terrain_world.cpp +++ b/modules/terraman/world/terrain_world.cpp @@ -54,8 +54,8 @@ #include "../../props/props/prop_data_scene.h" #ifdef MODULE_ENTITY_SPELL_SYSTEM_ENABLED -#include "modules/entity_spell_system/props/prop_data_ess_entity_world_spawner_3d_single.h" #include "modules/entity_spell_system/props/prop_data_ess_entity_world_spawner_3d_area.h" +#include "modules/entity_spell_system/props/prop_data_ess_entity_world_spawner_3d_single.h" #endif #endif @@ -75,8 +75,6 @@ #include "scene/resources/world_3d.h" #endif - - const String TerrainWorld::BINDING_STRING_CHANNEL_TYPE_INFO = "Type,Isolevel,Liquid,Liquid Level"; bool TerrainWorld::get_active() const { @@ -883,7 +881,7 @@ void TerrainWorld::prop_add(Transform transform, const Ref &prop, cons /* TODO Chunks will need a new api for this. Props that should just create nodes using processor_get_node_for(). node_props / managed_props / instanced_props? - + Ref world_Spawner_single_data = entry; if (world_Spawner_single_data.is_valid()) { @@ -895,7 +893,7 @@ void TerrainWorld::prop_add(Transform transform, const Ref &prop, cons continue; } - + Ref world_Spawner_area_data = entry; */ #endif @@ -1610,13 +1608,15 @@ void TerrainWorld::_notification(int p_what) { for (int i = 0; i < _chunks_vector.size(); ++i) { Ref chunk = _chunks_vector[i]; - if (chunk.is_valid()) { + if (chunk.is_valid() && !chunk->get_is_setup()) { chunk_setup(chunk); chunk->set_voxel_world(this); chunk->enter_tree(); chunk->build(); + } else { + chunk->enter_tree(); } } break; @@ -1725,7 +1725,6 @@ void TerrainWorld::_notification(int p_what) { if (chunk.is_valid()) { if (chunk->get_voxel_world() == this) { chunk->exit_tree(); - chunk->set_voxel_world(NULL); } } }