Set up vertex lights 2d support for Layered Tile Maps.

This commit is contained in:
Relintai 2024-03-26 13:07:57 +01:00
parent 645eba011b
commit fa68cf6872
4 changed files with 154 additions and 10 deletions

View File

@ -497,6 +497,24 @@ void LayeredTileMap::set_cells_terrain_path(int p_layer, PoolVector2iArray p_pat
TILEMAP_CALL_FOR_LAYER(p_layer, set_cells_terrain_path, p_path, p_terrain_set, p_terrain, p_ignore_empty_terrains);
}
//VertexLights2D
#ifdef MODULE_VERTEX_LIGHTS_2D_ENABLED
void LayeredTileMap::set_use_vertex_lights(const bool p_use) {
_use_vertex_lights = p_use;
for (uint32_t i = 0; i < layers.size(); ++i) {
LayeredTileMapLayer *layer = layers[i];
layer->set_use_vertex_lights(_use_vertex_lights);
}
_emit_changed();
}
bool LayeredTileMap::get_use_vertex_lights() const {
return _use_vertex_lights;
}
#endif
//RAO
#ifdef MODULE_FASTNOISE_ENABLED
void LayeredTileMap::rao_set_use(bool p_rao) {
@ -1147,6 +1165,13 @@ void LayeredTileMap::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_visibility_mode", PROPERTY_HINT_ENUM, "Default,Force Show,Force Hide"), "set_collision_visibility_mode", "get_collision_visibility_mode");
ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_visibility_mode", PROPERTY_HINT_ENUM, "Default,Force Show,Force Hide"), "set_navigation_visibility_mode", "get_navigation_visibility_mode");
//VertexLights2D
#ifdef MODULE_VERTEX_LIGHTS_2D_ENABLED
ClassDB::bind_method(D_METHOD("set_use_vertex_lights", "value"), &LayeredTileMap::set_use_vertex_lights);
ClassDB::bind_method(D_METHOD("get_use_vertex_lights"), &LayeredTileMap::get_use_vertex_lights);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_vertex_lights"), "set_use_vertex_lights", "get_use_vertex_lights");
#endif
//RAO
#ifdef MODULE_FASTNOISE_ENABLED
ADD_GROUP("RAO", "rao");
@ -1185,6 +1210,11 @@ LayeredTileMap::LayeredTileMap() {
layers.push_back(new_layer);
default_layer = memnew(LayeredTileMapLayer);
//VertexLights2D
#ifdef MODULE_VERTEX_LIGHTS_2D_ENABLED
_use_vertex_lights = false;
#endif
#ifdef MODULE_FASTNOISE_ENABLED
_use_rao = true;
_rao_strength = 0.3;

View File

@ -86,6 +86,11 @@ private:
Transform2D last_valid_transform;
Transform2D new_transform;
//VertexLights2D
#ifdef MODULE_VERTEX_LIGHTS_2D_ENABLED
bool _use_vertex_lights;
#endif
//RAO
#ifdef MODULE_FASTNOISE_ENABLED
bool _use_rao;
@ -178,6 +183,12 @@ public:
void set_cells_terrain_connect(int p_layer, PoolVector2iArray p_cells, int p_terrain_set, int p_terrain, bool p_ignore_empty_terrains = true);
void set_cells_terrain_path(int p_layer, PoolVector2iArray p_path, int p_terrain_set, int p_terrain, bool p_ignore_empty_terrains = true);
//VertexLights2D
#ifdef MODULE_VERTEX_LIGHTS_2D_ENABLED
void set_use_vertex_lights(const bool p_use);
bool get_use_vertex_lights() const;
#endif
//RAO
#ifdef MODULE_FASTNOISE_ENABLED
void rao_set_use(bool p_rao);

View File

@ -43,6 +43,10 @@
#include "servers/navigation_server.h"
#endif // DEBUG_ENABLED
#ifdef MODULE_VERTEX_LIGHTS_2D_ENABLED
#include "modules/vertex_lights_2d/vertex_lights_2d_server.h"
#endif
#ifdef DEBUG_ENABLED
/////////////////////////////// Debug //////////////////////////////////////////
constexpr int TILE_MAP_DEBUG_QUADRANT_SIZE = 16;
@ -224,6 +228,11 @@ void LayeredTileMapLayer::_rendering_update() {
// If so, recreate everything.
bool quandrant_shape_changed = dirty.flags[DIRTY_FLAGS_LAYER_RENDERING_QUADRANT_SIZE] ||
(is_sort_enabled() && (dirty.flags[DIRTY_FLAGS_LAYER_Y_SORT_ENABLED] || dirty.flags[DIRTY_FLAGS_LAYER_Y_SORT_ORIGIN] || dirty.flags[DIRTY_FLAGS_LAYER_LOCAL_TRANSFORM] || dirty.flags[DIRTY_FLAGS_LAYER_GROUP_TILE_SET]
#ifdef MODULE_VERTEX_LIGHTS_2D_ENABLED
|| dirty.flags[DIRTY_FLAGS_LAYER_VERTEX_LIGHTS]
#endif
#ifdef MODULE_FASTNOISE_ENABLED
|| dirty.flags[DIRTY_FLAGS_LAYER_RAO]
#endif
@ -363,24 +372,27 @@ void LayeredTileMapLayer::_rendering_update() {
// Drawing the tile in the canvas item.
Color self_modulate = get_self_modulate();
#ifdef MODULE_VERTEX_LIGHTS_2D_ENABLED
if (_use_vertex_lights) {
//self_modulate = self_modulate.blend(cell_data.vertex_light_color);
self_modulate = cell_data.vertex_light_color;
}
#endif
//RAO
#ifdef MODULE_FASTNOISE_ENABLED
if (_rao_noise.is_valid()) {
float col = (static_cast<float>(cell_data.rao) / 255.0) * _rao_strength;
Color modulate = get_modulate();
Color self_modulate = get_self_modulate();
Color m = Color(modulate.r * self_modulate.r - col, modulate.g * self_modulate.g - col, modulate.b * self_modulate.b - col, modulate.a * self_modulate.a);
LayeredTileMap::draw_tile(ci, local_tile_pos - rendering_quadrant->canvas_items_position, tile_set, cell_data.cell.source_id, cell_data.cell.get_atlas_coords(), cell_data.cell.alternative_tile, -1, m, tile_data, random_animation_offset);
} else {
LayeredTileMap::draw_tile(ci, local_tile_pos - rendering_quadrant->canvas_items_position, tile_set, cell_data.cell.source_id, cell_data.cell.get_atlas_coords(), cell_data.cell.alternative_tile, -1, get_self_modulate(), tile_data, random_animation_offset);
self_modulate = Color(modulate.r * self_modulate.r - col, modulate.g * self_modulate.g - col, modulate.b * self_modulate.b - col, modulate.a * self_modulate.a);
}
#else
LayeredTileMap::draw_tile(ci, local_tile_pos - rendering_quadrant->canvas_items_position, tile_set, cell_data.cell.source_id, cell_data.cell.get_atlas_coords(), cell_data.cell.alternative_tile, -1, get_self_modulate(), tile_data, random_animation_offset);
#endif
LayeredTileMap::draw_tile(ci, local_tile_pos - rendering_quadrant->canvas_items_position, tile_set, cell_data.cell.source_id, cell_data.cell.get_atlas_coords(), cell_data.cell.alternative_tile, -1, self_modulate, tile_data, random_animation_offset);
}
} else {
// Free the quadrant.
@ -537,6 +549,27 @@ void LayeredTileMapLayer::_rendering_quadrants_update_cell(CellData &r_cell_data
}
#endif
#ifdef MODULE_VERTEX_LIGHTS_2D_ENABLED
if (dirty.flags[DIRTY_FLAGS_LAYER_VERTEX_LIGHTS]) {
if (_use_vertex_lights) {
Ref<World2D> world_2d = get_world_2d();
if (world_2d.is_valid()) {
RID vertex_light_map_rid = world_2d->get_vertex_lights_2d_map();
const Vector2 local_tile_pos = tile_set->map_to_local(r_cell_data.coords);
r_cell_data.vertex_light_color = VertexLights2DServer::get_singleton()->sample_light(vertex_light_map_rid, to_global(local_tile_pos));
} else {
r_cell_data.vertex_light_color = Color(1, 1, 1, 1);
}
} else {
r_cell_data.vertex_light_color = Color(1, 1, 1, 1);
}
}
#endif
Ref<RenderingQuadrant> rendering_quadrant;
if (rendering_quadrant_map.has(quadrant_coords)) {
// Reuse existing rendering quadrant.
@ -854,7 +887,7 @@ void LayeredTileMapLayer::_physics_update_cell(CellData &r_cell_data) {
Ref<PhysicsMaterial> physics_material = tile_set->get_physics_layer_physics_material(tile_set_physics_layer);
uint32_t physics_layer = tile_set->get_physics_layer_collision_layer(tile_set_physics_layer);
uint32_t physics_mask = tile_set->get_physics_layer_collision_mask(tile_set_physics_layer);
RID body = r_cell_data.bodies[tile_set_physics_layer];
if (tile_data->get_collision_polygons_count(tile_set_physics_layer) == 0) {
// No body needed, free it if it exists.
@ -1762,6 +1795,15 @@ void LayeredTileMapLayer::_internal_update() {
dirty.cell_list.clear();
}
//VertexLights2D
#ifdef MODULE_VERTEX_LIGHTS_2D_ENABLED
void LayeredTileMapLayer::_on_vertex_lights_map_changed(RID map) {
dirty.flags[DIRTY_FLAGS_LAYER_VERTEX_LIGHTS] = true;
_queue_internal_update();
emit_signal(CoreStringNames::get_singleton()->changed);
}
#endif
void LayeredTileMapLayer::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_POSTINITIALIZE: {
@ -1819,6 +1861,11 @@ void LayeredTileMapLayer::_bind_methods() {
ClassDB::bind_method(D_METHOD("_renamed"), &LayeredTileMapLayer::_renamed);
ADD_SIGNAL(MethodInfo(CoreStringNames::get_singleton()->changed));
//VertexLights2D
#ifdef MODULE_VERTEX_LIGHTS_2D_ENABLED
ClassDB::bind_method(D_METHOD("_on_vertex_lights_map_changed"), &LayeredTileMapLayer::_on_vertex_lights_map_changed);
#endif
}
void LayeredTileMapLayer::set_as_tile_map_internal_node(int p_index) {
@ -2833,6 +2880,31 @@ LayeredTileMapLayer::VisibilityMode LayeredTileMapLayer::get_navigation_visibili
return navigation_visibility_mode;
}
//VertexLights2D
#ifdef MODULE_VERTEX_LIGHTS_2D_ENABLED
void LayeredTileMapLayer::set_use_vertex_lights(const bool p_use) {
_use_vertex_lights = p_use;
dirty.flags[DIRTY_FLAGS_LAYER_VERTEX_LIGHTS] = true;
if (_use_vertex_lights) {
if (!VertexLights2DServer::get_singleton()->is_connected("map_changed", this, "_on_vertex_lights_map_changed")) {
VertexLights2DServer::get_singleton()->connect("map_changed", this, "_on_vertex_lights_map_changed");
}
} else {
if (VertexLights2DServer::get_singleton()->is_connected("map_changed", this, "_on_vertex_lights_map_changed")) {
VertexLights2DServer::get_singleton()->disconnect("map_changed", this, "_on_vertex_lights_map_changed");
}
}
_queue_internal_update();
emit_signal(CoreStringNames::get_singleton()->changed);
}
bool LayeredTileMapLayer::get_use_vertex_lights() const {
return _use_vertex_lights;
}
#endif
//RAO
#ifdef MODULE_FASTNOISE_ENABLED
Ref<FastNoise> LayeredTileMapLayer::get_rao_noise() {
@ -2906,6 +2978,11 @@ void LayeredTileMapLayer::tile_data_runtime_update(const Vector2i &p_coords, Lay
LayeredTileMapLayer::LayeredTileMapLayer() {
set_notify_transform(true);
//VertexLights2D
#ifdef MODULE_VERTEX_LIGHTS_2D_ENABLED
_use_vertex_lights = false;
#endif
#ifdef MODULE_FASTNOISE_ENABLED
_rao_strength = 0.3;
#endif

View File

@ -109,6 +109,11 @@ struct CellData {
uint8_t rao;
#endif
//VertexLights2D
#ifdef MODULE_VERTEX_LIGHTS_2D_ENABLED
Color vertex_light_color;
#endif
// Debug.
SelfList<CellData> debug_quadrant_list_element;
@ -257,6 +262,11 @@ public:
DIRTY_FLAGS_LAYER_NAVIGATION_VISIBILITY_MODE,
DIRTY_FLAGS_LAYER_RUNTIME_UPDATE,
//VertexLights2D
#ifdef MODULE_VERTEX_LIGHTS_2D_ENABLED
DIRTY_FLAGS_LAYER_VERTEX_LIGHTS,
#endif
#ifdef MODULE_FASTNOISE_ENABLED
DIRTY_FLAGS_LAYER_RAO,
#endif
@ -291,6 +301,11 @@ private:
LayeredTileMap *tile_map_node = nullptr;
int layer_index_in_tile_map_node = -1;
//VertexLights2D
#ifdef MODULE_VERTEX_LIGHTS_2D_ENABLED
bool _use_vertex_lights;
#endif
//RAO
#ifdef MODULE_FASTNOISE_ENABLED
real_t _rao_strength;
@ -377,6 +392,11 @@ private:
void _queue_internal_update();
void _deferred_internal_update();
void _internal_update();
//VertexLights2D
#ifdef MODULE_VERTEX_LIGHTS_2D_ENABLED
void _on_vertex_lights_map_changed(RID map);
#endif
protected:
void _notification(int p_what);
@ -454,6 +474,12 @@ public:
void set_navigation_visibility_mode(VisibilityMode p_show_navigation);
VisibilityMode get_navigation_visibility_mode() const;
//VertexLights2D
#ifdef MODULE_VERTEX_LIGHTS_2D_ENABLED
void set_use_vertex_lights(const bool p_use);
bool get_use_vertex_lights() const;
#endif
//RAO
#ifdef MODULE_FASTNOISE_ENABLED
Ref<FastNoise> get_rao_noise();