Allow setting abase color per map for vertex light 2ds. Also added VertexLight2DEnvironment Node so it can be changed easily using the SceneTree.

This commit is contained in:
Relintai 2024-03-26 16:29:02 +01:00
parent b353267679
commit 59a4fcfc70
8 changed files with 190 additions and 1 deletions

View File

@ -8,3 +8,4 @@ module_env.add_source_files(env.modules_sources,"register_types.cpp")
module_env.add_source_files(env.modules_sources,"vertex_lights_2d_server.cpp")
module_env.add_source_files(env.modules_sources,"vertex_light_data.cpp")
module_env.add_source_files(env.modules_sources,"vertex_light_2d.cpp")
module_env.add_source_files(env.modules_sources,"vertex_light_2d_environment.cpp")

View File

@ -12,6 +12,7 @@ def get_doc_classes():
return [
"VertexLights2DServer",
"VertexLight2D",
"VertexLight2DEnvironment",
]
def get_doc_path():

View File

@ -34,6 +34,7 @@
#include "core/config/engine.h"
#include "vertex_light_2d.h"
#include "vertex_light_2d_environment.h"
#include "vertex_lights_2d_server.h"
VertexLights2DServer *vertex_lights_2d_server = NULL;
@ -45,6 +46,7 @@ void register_vertex_lights_2d_types(ModuleRegistrationLevel p_level) {
Engine::get_singleton()->add_singleton(Engine::Singleton("VertexLights2DServer", VertexLights2DServer::get_singleton()));
} else if (p_level == MODULE_REGISTRATION_LEVEL_SCENE) {
ClassDB::register_class<VertexLight2D>();
ClassDB::register_class<VertexLight2DEnvironment>();
} else if (p_level == MODULE_REGISTRATION_LEVEL_FINALIZE) {
vertex_lights_2d_server->init();
}

View File

@ -0,0 +1,96 @@
/*************************************************************************/
/* vertex_light_2d.cpp */
/*************************************************************************/
/* This file is part of: */
/* PANDEMONIUM ENGINE */
/* https://github.com/Relintai/pandemonium_engine */
/*************************************************************************/
/* Copyright (c) 2022-present Péter Magyar. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "vertex_light_2d_environment.h"
#include "core/config/engine.h"
#include "scene/resources/world_2d.h"
#include "vertex_lights_2d_server.h"
bool VertexLight2DEnvironment::get_is_enabled() {
return _enabled;
}
void VertexLight2DEnvironment::set_enabled(const bool p_enabled) {
_enabled = p_enabled;
_update_light_visibility();
}
Color VertexLight2DEnvironment::get_color() {
return _color;
}
void VertexLight2DEnvironment::set_color(const Color &p_color) {
_color = p_color;
_update_light_visibility();
}
VertexLight2DEnvironment::VertexLight2DEnvironment() {
_enabled = true;
}
VertexLight2DEnvironment::~VertexLight2DEnvironment() {
}
void VertexLight2DEnvironment::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE: {
_update_light_visibility();
} break;
case NOTIFICATION_VISIBILITY_CHANGED: {
_update_light_visibility();
} break;
}
}
void VertexLight2DEnvironment::_update_light_visibility() {
if (!is_inside_tree() || !_enabled || !is_visible_in_tree()) {
return;
}
Ref<World2D> world = get_world_2d();
ERR_FAIL_COND(!world.is_valid());
RID map = world->get_vertex_lights_2d_map();
VertexLights2DServer::get_singleton()->map_set_base_color(map, _color);
}
void VertexLight2DEnvironment::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_is_enabled"), &VertexLight2DEnvironment::get_is_enabled);
ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &VertexLight2DEnvironment::set_enabled);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "get_is_enabled");
ClassDB::bind_method(D_METHOD("get_color"), &VertexLight2DEnvironment::get_color);
ClassDB::bind_method(D_METHOD("set_color", "color"), &VertexLight2DEnvironment::set_color);
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");
}

View File

@ -0,0 +1,66 @@
#ifndef VERTEX_LIGHT_2D_ENVIRONMENT_H
#define VERTEX_LIGHT_2D_ENVIRONMENT_H
/*************************************************************************/
/* vertex_2d.h */
/*************************************************************************/
/* This file is part of: */
/* PANDEMONIUM ENGINE */
/* https://github.com/Relintai/pandemonium_engine */
/*************************************************************************/
/* Copyright (c) 2022-present Péter Magyar. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "scene/main/node_2d.h"
#include "core/containers/hash_map.h"
#include "core/containers/vector.h"
#include "core/math/color.h"
#include "core/math/vector2i.h"
class VertexLight2DEnvironment : public Node2D {
GDCLASS(VertexLight2DEnvironment, Node2D);
public:
bool get_is_enabled();
void set_enabled(const bool p_enabled);
Color get_color();
void set_color(const Color &p_color);
VertexLight2DEnvironment();
~VertexLight2DEnvironment();
protected:
void _notification(int p_what);
void _update_light_visibility();
static void _bind_methods();
bool _enabled;
Color _color;
};
#endif

View File

@ -186,7 +186,7 @@ void VertexLights2DServer::VertexLightMap2D::clear() {
}
Color VertexLights2DServer::VertexLightMap2D::sample_light(const Vector2 &p_position, const int p_item_cull_mask, const int p_layer) {
Color c = Color();
Color c = base_color;
Vector2i quadrant_position = to_quadrant_position(p_position);

View File

@ -68,6 +68,22 @@ void VertexLights2DServer::map_set_quadrant_size(RID p_map, const Vector2i &p_si
_map_changed(map);
}
Color VertexLights2DServer::map_get_base_color(RID p_map) const {
const VertexLightMap2D *map = map_owner.getornull(p_map);
ERR_FAIL_COND_V(map == NULL, Color());
return map->base_color;
}
void VertexLights2DServer::map_set_base_color(RID p_map, const Color &p_base_color) {
VertexLightMap2D *map = map_owner.getornull(p_map);
ERR_FAIL_COND(map == NULL);
map->base_color = p_base_color;
_map_changed(map);
}
Array VertexLights2DServer::map_get_lights(RID p_map) const {
VertexLightMap2D *map = map_owner.getornull(p_map);
ERR_FAIL_COND_V(map == NULL, Array());
@ -361,6 +377,9 @@ void VertexLights2DServer::_bind_methods() {
ClassDB::bind_method(D_METHOD("map_get_quadrant_size", "map"), &VertexLights2DServer::map_get_quadrant_size);
ClassDB::bind_method(D_METHOD("map_set_quadrant_size", "map", "size"), &VertexLights2DServer::map_set_quadrant_size);
ClassDB::bind_method(D_METHOD("map_get_base_color", "map"), &VertexLights2DServer::map_get_base_color);
ClassDB::bind_method(D_METHOD("map_set_base_color", "map", "base_color"), &VertexLights2DServer::map_set_base_color);
ClassDB::bind_method(D_METHOD("map_get_lights", "map"), &VertexLights2DServer::map_get_lights);

View File

@ -64,6 +64,9 @@ public:
Vector2i map_get_quadrant_size(RID p_map) const;
void map_set_quadrant_size(RID p_map, const Vector2i &p_size);
Color map_get_base_color(RID p_map) const;
void map_set_base_color(RID p_map, const Color &p_base_color);
Array map_get_lights(RID p_map) const;
@ -185,6 +188,7 @@ protected:
public:
HashMap<Vector2i, VertexLightQuadrant2D *> quadrants;
Vector2i quadrant_size;
Color base_color;
RID self;