diff --git a/modules/vertex_lights_2d/vertex_light_2d.cpp b/modules/vertex_lights_2d/vertex_light_2d.cpp index 78d2d6d45..039cedd55 100644 --- a/modules/vertex_lights_2d/vertex_light_2d.cpp +++ b/modules/vertex_lights_2d/vertex_light_2d.cpp @@ -33,6 +33,8 @@ #include "core/config/engine.h" +#include "scene/resources/world_2d.h" + bool VertexLight2D::get_is_enabled() { return _enabled; } @@ -89,7 +91,7 @@ void VertexLight2D::set_item_cull_mask(const int p_item_cull_mask) { VertexLight2D::VertexLight2D() { _vertex_light = RID_PRIME(VertexLights2DServer::get_singleton()->light_create()); - + _enabled = true; _color = Color(1, 1, 1, 1); _item_cull_mask = 1; @@ -105,6 +107,19 @@ VertexLight2D::~VertexLight2D() { void VertexLight2D::_notification(int p_what) { switch (p_what) { + case NOTIFICATION_ENTER_TREE: { + Ref world = get_world_2d(); + ERR_FAIL_COND(!world.is_valid()); + RID map = world->get_vertex_lights_2d_map(); + VertexLights2DServer::get_singleton()->light_set_map(_vertex_light, map); + + _update_light_visibility(); + } break; + case NOTIFICATION_EXIT_TREE: { + VertexLights2DServer::get_singleton()->light_set_map(_vertex_light, RID()); + + _update_light_visibility(); + } break; case NOTIFICATION_TRANSFORM_CHANGED: { VertexLights2DServer::get_singleton()->light_set_position(_vertex_light, get_global_transform().get_origin()); } break; diff --git a/scene/resources/world_2d.cpp b/scene/resources/world_2d.cpp index 7290fb624..3b9255466 100644 --- a/scene/resources/world_2d.cpp +++ b/scene/resources/world_2d.cpp @@ -40,6 +40,12 @@ #include "servers/physics_2d_server.h" #include "servers/rendering_server.h" +#include "modules/modules_enabled.gen.h" + +#ifdef MODULE_VERTEX_LIGHTS_2D_ENABLED +#include "modules/vertex_lights_2d/vertex_lights_2d_server.h" +#endif + struct SpatialIndexer2D { struct CellRef { int ref; @@ -333,6 +339,10 @@ RID World2D::get_navigation_map() const { return navigation_map; } +RID World2D::get_vertex_lights_2d_map() { + return vertex_lights_2d_map; +} + void World2D::get_world_list(List *r_worlds) { for (RBMap::Element *E = indexer->worlds.front(); E; E = E->next()) { r_worlds->push_back(E->key()); @@ -353,6 +363,7 @@ void World2D::_bind_methods() { ClassDB::bind_method(D_METHOD("get_canvas"), &World2D::get_canvas); ClassDB::bind_method(D_METHOD("get_space"), &World2D::get_space); ClassDB::bind_method(D_METHOD("get_navigation_map"), &World2D::get_navigation_map); + ClassDB::bind_method(D_METHOD("get_vertex_lights_2d_map"), &World2D::get_vertex_lights_2d_map); ClassDB::bind_method(D_METHOD("get_direct_space_state"), &World2D::get_direct_space_state); @@ -370,6 +381,10 @@ World2D::World2D() { canvas = RID_PRIME(RenderingServer::get_singleton()->canvas_create()); space = RID_PRIME(Physics2DServer::get_singleton()->space_create()); +#ifdef MODULE_VERTEX_LIGHTS_2D_ENABLED + vertex_lights_2d_map = RID_PRIME(VertexLights2DServer::get_singleton()->map_create()); +#endif + //set space2D to be more friendly with pixels than meters, by adjusting some constants Physics2DServer::get_singleton()->space_set_active(space, true); Physics2DServer::get_singleton()->area_set_param(space, Physics2DServer::AREA_PARAM_GRAVITY, GLOBAL_DEF("physics/2d/default_gravity", 98)); diff --git a/scene/resources/world_2d.h b/scene/resources/world_2d.h index 88ed13a6e..ac6c1b457 100644 --- a/scene/resources/world_2d.h +++ b/scene/resources/world_2d.h @@ -47,6 +47,7 @@ class World2D : public Resource { RID canvas; RID space; RID navigation_map; + RID vertex_lights_2d_map; SpatialIndexer2D *indexer; @@ -71,6 +72,9 @@ public: RID get_space(); RID get_navigation_map() const; + // TODO Maybe World should have a callback on creation? + RID get_vertex_lights_2d_map(); + Physics2DDirectSpaceState *get_direct_space_state(); void get_world_list(List *r_worlds);