diff --git a/modules/terraman/data/terrain_light.cpp b/modules/terraman/data/terrain_light.cpp index eef2f772c..48c049ad1 100644 --- a/modules/terraman/data/terrain_light.cpp +++ b/modules/terraman/data/terrain_light.cpp @@ -128,6 +128,15 @@ void TerrainLight::set_item_cull_mask(const int p_item_cull_mask) { _item_cull_mask = p_item_cull_mask; } +#ifdef MODULE_VERTEX_LIGHTS_3D_ENABLED +RID TerrainLight::get_vertex_lights_3d_rid() const { + return _vertex_lights_3d_rid; +} +void TerrainLight::set_vertex_lights_3d_rid(const RID p_rid) { + _vertex_lights_3d_rid = p_rid; +} +#endif + Dictionary TerrainLight::to_dict() { Dictionary data; @@ -254,6 +263,11 @@ void TerrainLight::_bind_methods() { ClassDB::bind_method(D_METHOD("to_dict"), &TerrainLight::to_dict); ClassDB::bind_method(D_METHOD("from_dict", "data"), &TerrainLight::from_dict); +#ifdef MODULE_VERTEX_LIGHTS_3D_ENABLED + ClassDB::bind_method(D_METHOD("get_vertex_lights_3d_rid"), &TerrainLight::get_vertex_lights_3d_rid); + ClassDB::bind_method(D_METHOD("set_vertex_lights_3d_rid", "rid"), &TerrainLight::set_vertex_lights_3d_rid); +#endif + BIND_ENUM_CONSTANT(OWNER_TYPE_NONE); #ifdef MODULE_PROPS_ENABLED BIND_ENUM_CONSTANT(OWNER_TYPE_PROP); diff --git a/modules/terraman/data/terrain_light.h b/modules/terraman/data/terrain_light.h index 2e8634a40..f48c01d02 100644 --- a/modules/terraman/data/terrain_light.h +++ b/modules/terraman/data/terrain_light.h @@ -95,6 +95,11 @@ public: int get_item_cull_mask(); void set_item_cull_mask(const int p_item_cull_mask); +#ifdef MODULE_VERTEX_LIGHTS_3D_ENABLED + RID get_vertex_lights_3d_rid() const; + void set_vertex_lights_3d_rid(const RID p_rid); +#endif + Dictionary to_dict(); void from_dict(const Dictionary &p_data); @@ -123,6 +128,10 @@ private: LightMode _light_mode; int _item_cull_mask; + +#ifdef MODULE_VERTEX_LIGHTS_3D_ENABLED + RID _vertex_lights_3d_rid; +#endif }; VARIANT_ENUM_CAST(TerrainLight::OwnerType); diff --git a/modules/terraman/world/terrain_chunk.cpp b/modules/terraman/world/terrain_chunk.cpp index f46db8bf4..9fd3713da 100644 --- a/modules/terraman/world/terrain_chunk.cpp +++ b/modules/terraman/world/terrain_chunk.cpp @@ -50,6 +50,11 @@ #include "modules/lz4/lz4_compressor.h" #endif +#ifdef MODULE_VERTEX_LIGHTS_3D_ENABLED +#include "modules/vertex_lights_3d/vertex_lights_3d_server.h" +#include "scene/resources/world_3d.h" +#endif + _FORCE_INLINE_ bool TerrainChunk::get_process() const { return _is_processing; } @@ -703,6 +708,27 @@ void TerrainChunk::light_add(Ref p_light) { TerrainWorld *world = get_voxel_world(); +#ifdef MODULE_VERTEX_LIGHTS_3D_ENABLED + if (world) { + if (world->get_use_vertex_lights_3d()) { + RID vertex_light_rid = p_light->get_vertex_lights_3d_rid(); + + if (vertex_light_rid == RID()) { + vertex_light_rid = VertexLights3DServer::get_singleton()->light_create(); + p_light->set_vertex_lights_3d_rid(vertex_light_rid); + + VertexLights3DServer::get_singleton()->light_set_attenuation(vertex_light_rid, p_light->get_attenuation()); + VertexLights3DServer::get_singleton()->light_set_color(vertex_light_rid, p_light->get_color()); + VertexLights3DServer::get_singleton()->light_set_item_cull_mask(vertex_light_rid, p_light->get_item_cull_mask()); + VertexLights3DServer::get_singleton()->light_set_mode(vertex_light_rid, (VertexLights3DServer::VertexLight3DMode)(int)p_light->get_light_mode()); + VertexLights3DServer::get_singleton()->light_set_range(vertex_light_rid, p_light->get_range()); + VertexLights3DServer::get_singleton()->light_set_map(vertex_light_rid, world->get_world_3d()->get_vertex_lights_3d_map()); + VertexLights3DServer::get_singleton()->light_set_position(vertex_light_rid, p_light->get_world_data_position() * get_voxel_scale()); + } + } + } +#endif + if (ObjectDB::instance_validate(world)) { world->world_light_added(p_light); } @@ -720,6 +746,19 @@ bool TerrainChunk::light_remove(Ref p_light) { TerrainWorld *world = get_voxel_world(); +#ifdef MODULE_VERTEX_LIGHTS_3D_ENABLED + if (world) { + if (world->get_use_vertex_lights_3d()) { + RID vertex_light_rid = p_light->get_vertex_lights_3d_rid(); + + if (vertex_light_rid != RID()) { + VertexLights3DServer::get_singleton()->free(vertex_light_rid); + p_light->set_vertex_lights_3d_rid(RID()); + } + } + } +#endif + if (ObjectDB::instance_validate(world)) { world->world_light_removed(p_light); } @@ -752,6 +791,19 @@ void TerrainChunk::light_remove_index(const int index) { TerrainWorld *world = get_voxel_world(); +#ifdef MODULE_VERTEX_LIGHTS_3D_ENABLED + if (world) { + if (world->get_use_vertex_lights_3d()) { + RID vertex_light_rid = light->get_vertex_lights_3d_rid(); + + if (vertex_light_rid != RID()) { + VertexLights3DServer::get_singleton()->free(vertex_light_rid); + light->set_vertex_lights_3d_rid(RID()); + } + } + } +#endif + if (ObjectDB::instance_validate(world)) { world->world_light_removed(light); } @@ -851,6 +903,18 @@ void TerrainChunk::owned_lights_set(const Vector &p_lights) { void TerrainChunk::_on_light_moved(const Ref &p_light) { TerrainWorld *world = get_voxel_world(); +#ifdef MODULE_VERTEX_LIGHTS_3D_ENABLED + if (world) { + if (world->get_use_vertex_lights_3d()) { + RID vertex_light_rid = p_light->get_vertex_lights_3d_rid(); + + if (vertex_light_rid != RID()) { + VertexLights3DServer::get_singleton()->light_set_position(vertex_light_rid, p_light->get_world_data_position() * get_voxel_scale()); + } + } + } +#endif + if (ObjectDB::instance_validate(world)) { world->world_light_moved(p_light); } @@ -1944,6 +2008,34 @@ void TerrainChunk::_enter_tree() { for (int i = 0; i < _scenes.size(); ++i) { scene_instance(i); } + +#ifdef MODULE_VERTEX_LIGHTS_3D_ENABLED + TerrainWorld *world = get_voxel_world(); + + if (world) { + if (world->get_use_vertex_lights_3d()) { + for (int i = 0; i < _lights.size(); ++i) { + Ref light = _lights[i]; + + RID vertex_light_rid = light->get_vertex_lights_3d_rid(); + + if (vertex_light_rid == RID()) { + vertex_light_rid = VertexLights3DServer::get_singleton()->light_create(); + light->set_vertex_lights_3d_rid(vertex_light_rid); + + VertexLights3DServer::get_singleton()->light_set_attenuation(vertex_light_rid, light->get_attenuation()); + VertexLights3DServer::get_singleton()->light_set_color(vertex_light_rid, light->get_color()); + VertexLights3DServer::get_singleton()->light_set_item_cull_mask(vertex_light_rid, light->get_item_cull_mask()); + VertexLights3DServer::get_singleton()->light_set_mode(vertex_light_rid, (VertexLights3DServer::VertexLight3DMode)(int)light->get_light_mode()); + VertexLights3DServer::get_singleton()->light_set_range(vertex_light_rid, light->get_range()); + VertexLights3DServer::get_singleton()->light_set_position(vertex_light_rid, light->get_world_data_position() * get_voxel_scale()); + } + + VertexLights3DServer::get_singleton()->light_set_map(vertex_light_rid, world->get_world_3d()->get_vertex_lights_3d_map()); + } + } + } +#endif } void TerrainChunk::_exit_tree() { @@ -1971,6 +2063,25 @@ void TerrainChunk::_exit_tree() { material_cache_key_has_set(false); } } + +#ifdef MODULE_VERTEX_LIGHTS_3D_ENABLED + TerrainWorld *world = get_voxel_world(); + + if (world) { + if (world->get_use_vertex_lights_3d()) { + for (int i = 0; i < _lights.size(); ++i) { + Ref light = _lights[i]; + + RID vertex_light_rid = light->get_vertex_lights_3d_rid(); + + if (vertex_light_rid != RID()) { + VertexLights3DServer::get_singleton()->free(vertex_light_rid); + light->set_vertex_lights_3d_rid(RID()); + } + } + } + } +#endif } void TerrainChunk::_generation_process(const float delta) {