From a4ff7e9e66909baa35a9a6b458057d88381d28ac Mon Sep 17 00:00:00 2001 From: Relintai Date: Fri, 4 Apr 2025 17:44:42 +0200 Subject: [PATCH] Added use_vertex_lights_3d property to TerrainWorld. --- modules/terraman/world/terrain_world.cpp | 69 +++++++++++++++++++++++- modules/terraman/world/terrain_world.h | 12 +++++ 2 files changed, 79 insertions(+), 2 deletions(-) diff --git a/modules/terraman/world/terrain_world.cpp b/modules/terraman/world/terrain_world.cpp index c1b3898d4..f38cdc03a 100644 --- a/modules/terraman/world/terrain_world.cpp +++ b/modules/terraman/world/terrain_world.cpp @@ -54,6 +54,7 @@ #ifdef MODULE_MESH_DATA_RESOURCE_ENABLED #include "../../mesh_data_resource/props/prop_data_mesh_data.h" +#include "modules/mesh_data_resource/mesh_data_resource.h" #endif #if TOOLS_ENABLED @@ -61,8 +62,9 @@ #include "scene/3d/camera.h" #endif -#ifdef MODULE_MESH_DATA_RESOURCE_ENABLED -#include "modules/mesh_data_resource/mesh_data_resource.h" +#ifdef MODULE_VERTEX_LIGHTS_3D_ENABLED +#include "modules/vertex_lights_3d/vertex_lights_3d_server.h" +#include "scene/resources/world_3d.h" #endif const String TerrainWorld::BINDING_STRING_CHANNEL_TYPE_INFO = "Type,Isolevel,Liquid,Liquid Level"; @@ -173,6 +175,23 @@ void TerrainWorld::set_voxel_scale(const float value) { } } +#ifdef MODULE_VERTEX_LIGHTS_3D_ENABLED +bool TerrainWorld::get_use_vertex_lights_3d() const { + return _use_vertex_lights_3d; +} +void TerrainWorld::set_use_vertex_lights_3d(const bool value) { + _use_vertex_lights_3d = value; + + if (is_inside_tree()) { + if (_use_vertex_lights_3d) { + VertexLights3DServer::get_singleton()->connect("map_changed", this, "_on_vertex_lights_3d_map_changed"); + } else { + VertexLights3DServer::get_singleton()->disconnect("map_changed", this, "_on_vertex_lights_3d_map_changed"); + } + } +} +#endif + int TerrainWorld::get_chunk_spawn_range() const { return _chunk_spawn_range; } @@ -1353,6 +1372,10 @@ TerrainWorld::TerrainWorld() { _player = NULL; _max_frame_chunk_build_steps = 0; _num_frame_chunk_build_steps = 0; + +#ifdef MODULE_VERTEX_LIGHTS_3D_ENABLED + _use_vertex_lights_3d = true; +#endif } TerrainWorld ::~TerrainWorld() { @@ -1397,6 +1420,12 @@ void TerrainWorld::_generate_chunk(Ref chunk) { void TerrainWorld::_notification(int p_what) { switch (p_what) { case NOTIFICATION_ENTER_TREE: { +#ifdef MODULE_VERTEX_LIGHTS_3D_ENABLED + if (_use_vertex_lights_3d) { + VertexLights3DServer::get_singleton()->connect("map_changed", this, "_on_vertex_lights_3d_map_changed"); + } +#endif + set_player_bind(get_node_or_null(get_player_path())); set_process_internal(true); @@ -1504,6 +1533,12 @@ void TerrainWorld::_notification(int p_what) { break; } case NOTIFICATION_EXIT_TREE: { +#ifdef MODULE_VERTEX_LIGHTS_3D_ENABLED + if (_use_vertex_lights_3d) { + VertexLights3DServer::get_singleton()->disconnect("map_changed", this, "_on_vertex_lights_3d_map_changed"); + } +#endif + for (int i = 0; i < _chunks_vector.size(); ++i) { Ref chunk = _chunks_vector[i]; @@ -1534,6 +1569,26 @@ void TerrainWorld::_notification(int p_what) { } } +#ifdef MODULE_VERTEX_LIGHTS_3D_ENABLED +void TerrainWorld::_on_vertex_lights_3d_map_changed(RID p_map) { + if (!_use_vertex_lights_3d) { + return; + } + + if (!is_inside_world()) { + return; + } + + if (get_world_3d()->get_vertex_lights_3d_map() == p_map) { + for (int i = 0; i < _chunks_vector.size(); ++i) { + Ref chunk = _chunks_vector[i]; + + chunk->build(); + } + } +} +#endif + void TerrainWorld::_bind_methods() { ADD_SIGNAL(MethodInfo("chunk_mesh_generation_finished", PropertyInfo(Variant::OBJECT, "chunk", PROPERTY_HINT_RESOURCE_TYPE, "TerrainChunk"))); ADD_SIGNAL(MethodInfo("chunk_added", PropertyInfo(Variant::OBJECT, "chunk", PROPERTY_HINT_RESOURCE_TYPE, "TerrainChunk"))); @@ -1587,6 +1642,12 @@ void TerrainWorld::_bind_methods() { ClassDB::bind_method(D_METHOD("set_voxel_scale", "value"), &TerrainWorld::set_voxel_scale); ADD_PROPERTY(PropertyInfo(Variant::REAL, "voxel_scale"), "set_voxel_scale", "get_voxel_scale"); +#ifdef MODULE_VERTEX_LIGHTS_3D_ENABLED + ClassDB::bind_method(D_METHOD("get_use_vertex_lights_3d"), &TerrainWorld::get_use_vertex_lights_3d); + ClassDB::bind_method(D_METHOD("set_use_vertex_lights_3d", "value"), &TerrainWorld::set_use_vertex_lights_3d); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_vertex_lights_3d"), "set_use_vertex_lights_3d", "get_use_vertex_lights_3d"); +#endif + ClassDB::bind_method(D_METHOD("get_chunk_spawn_range"), &TerrainWorld::get_chunk_spawn_range); ClassDB::bind_method(D_METHOD("set_chunk_spawn_range", "value"), &TerrainWorld::set_chunk_spawn_range); ADD_PROPERTY(PropertyInfo(Variant::INT, "chunk_spawn_range"), "set_chunk_spawn_range", "get_chunk_spawn_range"); @@ -1704,6 +1765,10 @@ void TerrainWorld::_bind_methods() { ClassDB::bind_method(D_METHOD("get_editor_camera"), &TerrainWorld::get_editor_camera); +#ifdef MODULE_VERTEX_LIGHTS_3D_ENABLED + ClassDB::bind_method(D_METHOD("_on_vertex_lights_3d_map_changed"), &TerrainWorld::_on_vertex_lights_3d_map_changed); +#endif + BIND_ENUM_CONSTANT(CHANNEL_TYPE_INFO_TYPE); BIND_ENUM_CONSTANT(CHANNEL_TYPE_INFO_ISOLEVEL); BIND_ENUM_CONSTANT(CHANNEL_TYPE_INFO_LIQUID_TYPE); diff --git a/modules/terraman/world/terrain_world.h b/modules/terraman/world/terrain_world.h index ec22b1f4b..b89dc4475 100644 --- a/modules/terraman/world/terrain_world.h +++ b/modules/terraman/world/terrain_world.h @@ -111,6 +111,11 @@ public: float get_voxel_scale() const; void set_voxel_scale(const float value); +#ifdef MODULE_VERTEX_LIGHTS_3D_ENABLED + bool get_use_vertex_lights_3d() const; + void set_use_vertex_lights_3d(const bool value); +#endif + int get_chunk_spawn_range() const; void set_chunk_spawn_range(const int value); @@ -230,6 +235,10 @@ protected: virtual void _notification(int p_what); static void _bind_methods(); +#ifdef MODULE_VERTEX_LIGHTS_3D_ENABLED + void _on_vertex_lights_3d_map_changed(RID p_map); +#endif + public: struct IntPos { int x; @@ -271,6 +280,9 @@ private: int _data_margin_start; int _data_margin_end; float _world_height; +#ifdef MODULE_VERTEX_LIGHTS_3D_ENABLED + bool _use_vertex_lights_3d; +#endif Ref _library; Ref _level_generator;