Set up VertexLight2D maps.

This commit is contained in:
Relintai 2024-03-24 22:14:26 +01:00
parent 02fe45b08a
commit 3fc75ad15f
3 changed files with 35 additions and 1 deletions

View File

@ -33,6 +33,8 @@
#include "core/config/engine.h" #include "core/config/engine.h"
#include "scene/resources/world_2d.h"
bool VertexLight2D::get_is_enabled() { bool VertexLight2D::get_is_enabled() {
return _enabled; return _enabled;
} }
@ -89,7 +91,7 @@ void VertexLight2D::set_item_cull_mask(const int p_item_cull_mask) {
VertexLight2D::VertexLight2D() { VertexLight2D::VertexLight2D() {
_vertex_light = RID_PRIME(VertexLights2DServer::get_singleton()->light_create()); _vertex_light = RID_PRIME(VertexLights2DServer::get_singleton()->light_create());
_enabled = true; _enabled = true;
_color = Color(1, 1, 1, 1); _color = Color(1, 1, 1, 1);
_item_cull_mask = 1; _item_cull_mask = 1;
@ -105,6 +107,19 @@ VertexLight2D::~VertexLight2D() {
void VertexLight2D::_notification(int p_what) { void VertexLight2D::_notification(int p_what) {
switch (p_what) { switch (p_what) {
case NOTIFICATION_ENTER_TREE: {
Ref<World2D> 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: { case NOTIFICATION_TRANSFORM_CHANGED: {
VertexLights2DServer::get_singleton()->light_set_position(_vertex_light, get_global_transform().get_origin()); VertexLights2DServer::get_singleton()->light_set_position(_vertex_light, get_global_transform().get_origin());
} break; } break;

View File

@ -40,6 +40,12 @@
#include "servers/physics_2d_server.h" #include "servers/physics_2d_server.h"
#include "servers/rendering_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 SpatialIndexer2D {
struct CellRef { struct CellRef {
int ref; int ref;
@ -333,6 +339,10 @@ RID World2D::get_navigation_map() const {
return navigation_map; return navigation_map;
} }
RID World2D::get_vertex_lights_2d_map() {
return vertex_lights_2d_map;
}
void World2D::get_world_list(List<World *> *r_worlds) { void World2D::get_world_list(List<World *> *r_worlds) {
for (RBMap<World *, SpatialIndexer2D::WorldData>::Element *E = indexer->worlds.front(); E; E = E->next()) { for (RBMap<World *, SpatialIndexer2D::WorldData>::Element *E = indexer->worlds.front(); E; E = E->next()) {
r_worlds->push_back(E->key()); 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_canvas"), &World2D::get_canvas);
ClassDB::bind_method(D_METHOD("get_space"), &World2D::get_space); 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_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); 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()); canvas = RID_PRIME(RenderingServer::get_singleton()->canvas_create());
space = RID_PRIME(Physics2DServer::get_singleton()->space_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 //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()->space_set_active(space, true);
Physics2DServer::get_singleton()->area_set_param(space, Physics2DServer::AREA_PARAM_GRAVITY, GLOBAL_DEF("physics/2d/default_gravity", 98)); Physics2DServer::get_singleton()->area_set_param(space, Physics2DServer::AREA_PARAM_GRAVITY, GLOBAL_DEF("physics/2d/default_gravity", 98));

View File

@ -47,6 +47,7 @@ class World2D : public Resource {
RID canvas; RID canvas;
RID space; RID space;
RID navigation_map; RID navigation_map;
RID vertex_lights_2d_map;
SpatialIndexer2D *indexer; SpatialIndexer2D *indexer;
@ -71,6 +72,9 @@ public:
RID get_space(); RID get_space();
RID get_navigation_map() const; 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(); Physics2DDirectSpaceState *get_direct_space_state();
void get_world_list(List<World *> *r_worlds); void get_world_list(List<World *> *r_worlds);