From d63e5616932d9545cd71dbd8f0c9e705ae63b826 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 6 Apr 2024 19:44:35 +0200 Subject: [PATCH] Make RAO and VertexLights2D work with the new LayeredTileMap setup. --- modules/layered_tile_map/layered_tile_map.cpp | 75 ++++-------- modules/layered_tile_map/layered_tile_map.h | 2 - .../layered_tile_map_layer.cpp | 112 ++++++++++++++++-- .../layered_tile_map/layered_tile_map_layer.h | 23 +++- 4 files changed, 144 insertions(+), 68 deletions(-) diff --git a/modules/layered_tile_map/layered_tile_map.cpp b/modules/layered_tile_map/layered_tile_map.cpp index 8864e6291..77bdf48bb 100644 --- a/modules/layered_tile_map/layered_tile_map.cpp +++ b/modules/layered_tile_map/layered_tile_map.cpp @@ -383,6 +383,17 @@ void LayeredTileMap::add_layer(int p_to_pos) { move_child(new_layer, 0); new_layer->set_name(vformat("Layer%d", p_to_pos)); new_layer->set_tile_set(tile_set); + +#ifdef MODULE_VERTEX_LIGHTS_2D_ENABLED + new_layer->set_use_vertex_lights(get_use_vertex_lights()); +#endif + +#ifdef MODULE_FASTNOISE_ENABLED + new_layer->rao_set_use(rao_get_use()); + new_layer->rao_set_strength(rao_get_strength()); + new_layer->rao_set_noise_params(rao_get_noise_params()); +#endif + move_child(new_layer, p_to_pos); for (uint32_t i = 0; i < layers.size(); i++) { layers[i]->set_as_tile_map_internal_node(i); @@ -731,29 +742,10 @@ void LayeredTileMap::rao_set_use(bool p_rao) { _use_rao = p_rao; - if (!_use_rao) { - for (uint32_t i = 0; i < layers.size(); ++i) { - LayeredTileMapLayer *layer = layers[i]; + for (uint32_t i = 0; i < layers.size(); ++i) { + LayeredTileMapLayer *layer = layers[i]; - layer->set_rao_noise(Ref()); - } - - } else { - if (_noise_params.is_valid()) { - if (!_rao_noise.is_valid()) { - _rao_noise.instance(); - } - - rao_setup_noise(_rao_noise); - } else { - _rao_noise.unref(); - } - - for (uint32_t i = 0; i < layers.size(); ++i) { - LayeredTileMapLayer *layer = layers[i]; - - layer->set_rao_noise(_rao_noise); - } + layer->rao_set_use(_use_rao); } _emit_changed(); @@ -769,40 +761,26 @@ void LayeredTileMap::rao_set_noise_params(const Ref &noise _noise_params = noise; - if (!_use_rao) { - for (uint32_t i = 0; i < layers.size(); ++i) { - LayeredTileMapLayer *layer = layers[i]; + for (uint32_t i = 0; i < layers.size(); ++i) { + LayeredTileMapLayer *layer = layers[i]; - layer->set_rao_noise(Ref()); - } - } else { - if (_noise_params.is_valid()) { - if (!_rao_noise.is_valid()) { - _rao_noise.instance(); - } - - rao_setup_noise(_rao_noise); - } else { - _rao_noise.unref(); - } - - for (uint32_t i = 0; i < layers.size(); ++i) { - LayeredTileMapLayer *layer = layers[i]; - - layer->set_rao_noise(_rao_noise); - } + layer->rao_set_noise_params(noise); } _emit_changed(); } +Ref LayeredTileMap::rao_get_noise_params() { + return _noise_params; +} + void LayeredTileMap::rao_set_strength(const real_t p_strength) { _rao_strength = p_strength; for (uint32_t i = 0; i < layers.size(); ++i) { LayeredTileMapLayer *layer = layers[i]; - layer->set_rao_strength(p_strength); + layer->rao_set_strength(p_strength); } _emit_changed(); @@ -811,15 +789,6 @@ real_t LayeredTileMap::rao_get_strength() const { return _rao_strength; } -Ref LayeredTileMap::rao_get_noise_params() { - return _noise_params; -} - -void LayeredTileMap::rao_setup_noise(Ref noise) { - if (_noise_params.is_valid()) { - _noise_params->setup_noise(noise); - } -} #endif LayeredTileMapCell LayeredTileMap::get_cell(int p_layer, const Vector2i &p_coords, bool p_use_proxies) const { diff --git a/modules/layered_tile_map/layered_tile_map.h b/modules/layered_tile_map/layered_tile_map.h index 3a8e42ea1..fe79e5637 100644 --- a/modules/layered_tile_map/layered_tile_map.h +++ b/modules/layered_tile_map/layered_tile_map.h @@ -210,8 +210,6 @@ public: void rao_set_noise_params(const Ref &noise); Ref rao_get_noise_params(); - - void rao_setup_noise(Ref noise); #endif // Not exposed to users. diff --git a/modules/layered_tile_map/layered_tile_map_layer.cpp b/modules/layered_tile_map/layered_tile_map_layer.cpp index bfd136032..e475407a0 100644 --- a/modules/layered_tile_map/layered_tile_map_layer.cpp +++ b/modules/layered_tile_map/layered_tile_map_layer.cpp @@ -1902,6 +1902,31 @@ void LayeredTileMapLayer::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "tile_set", PROPERTY_HINT_RESOURCE_TYPE, "LayeredTileSet"), "set_tile_set", "get_tile_set"); + + //VertexLights2D +#ifdef MODULE_VERTEX_LIGHTS_2D_ENABLED + ClassDB::bind_method(D_METHOD("set_use_vertex_lights", "value"), &LayeredTileMapLayer::set_use_vertex_lights); + ClassDB::bind_method(D_METHOD("get_use_vertex_lights"), &LayeredTileMapLayer::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"); + + ClassDB::bind_method(D_METHOD("rao_set_use", "value"), &LayeredTileMapLayer::rao_set_use); + ClassDB::bind_method(D_METHOD("rao_get_use"), &LayeredTileMapLayer::rao_get_use); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "rao_use"), "rao_set_use", "rao_get_use"); + + ClassDB::bind_method(D_METHOD("rao_set_strength", "value"), &LayeredTileMapLayer::rao_set_strength); + ClassDB::bind_method(D_METHOD("rao_get_strength"), &LayeredTileMapLayer::rao_get_strength); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "rao_strength"), "rao_set_strength", "rao_get_strength"); + + ClassDB::bind_method(D_METHOD("rao_set_noise_params", "noise"), &LayeredTileMapLayer::rao_set_noise_params); + ClassDB::bind_method(D_METHOD("rao_get_noise_params"), &LayeredTileMapLayer::rao_get_noise_params); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "rao_noise_params", PROPERTY_HINT_RESOURCE_TYPE, "FastnoiseNoiseParams"), "rao_set_noise_params", "rao_get_noise_params"); +#endif + ADD_GROUP("Rendering", ""); ADD_PROPERTY(PropertyInfo(Variant::INT, "y_sort_origin"), "set_y_sort_origin", "get_y_sort_origin"); ADD_PROPERTY(PropertyInfo(Variant::INT, "rendering_quadrant_size"), "set_rendering_quadrant_size", "get_rendering_quadrant_size"); @@ -2985,12 +3010,61 @@ bool LayeredTileMapLayer::get_use_vertex_lights() const { //RAO #ifdef MODULE_FASTNOISE_ENABLED -Ref LayeredTileMapLayer::get_rao_noise() { - return _rao_noise; + +void LayeredTileMapLayer::rao_set_use(bool p_rao) { + if (_use_rao == p_rao) { + return; + } + + _use_rao = p_rao; + + if (!_use_rao) { + rao_set_noise(Ref()); + } else { + if (_noise_params.is_valid()) { + if (!_rao_noise.is_valid()) { + _rao_noise.instance(); + } + + rao_setup_noise(_rao_noise); + } else { + _rao_noise.unref(); + } + + rao_set_noise(_rao_noise); + } + + dirty.flags[DIRTY_FLAGS_LAYER_RAO] = true; + + _queue_internal_update(); + emit_signal(CoreStringNames::get_singleton()->changed); } -void LayeredTileMapLayer::set_rao_noise(const Ref &noise) { - // Don't check if they are the same! - _rao_noise = noise; +bool LayeredTileMapLayer::rao_get_use() const { + return _use_rao; +} + +void LayeredTileMapLayer::rao_set_noise_params(const Ref &noise) { + if (_noise_params == noise) { + return; + } + + _noise_params = noise; + + if (!_use_rao) { + rao_set_noise(Ref()); + } else { + if (_noise_params.is_valid()) { + if (!_rao_noise.is_valid()) { + _rao_noise.instance(); + } + + rao_setup_noise(_rao_noise); + } else { + _rao_noise.unref(); + } + + rao_set_noise(_rao_noise); + } dirty.flags[DIRTY_FLAGS_LAYER_RAO] = true; @@ -2998,7 +3072,7 @@ void LayeredTileMapLayer::set_rao_noise(const Ref &noise) { emit_signal(CoreStringNames::get_singleton()->changed); } -void LayeredTileMapLayer::set_rao_strength(const real_t p_strength) { +void LayeredTileMapLayer::rao_set_strength(const real_t p_strength) { _rao_strength = p_strength; dirty.flags[DIRTY_FLAGS_LAYER_RAO] = true; @@ -3006,10 +3080,33 @@ void LayeredTileMapLayer::set_rao_strength(const real_t p_strength) { _queue_internal_update(); emit_signal(CoreStringNames::get_singleton()->changed); } -real_t LayeredTileMapLayer::get_rao_strength() const { +real_t LayeredTileMapLayer::rao_get_strength() const { return _rao_strength; } +Ref LayeredTileMapLayer::rao_get_noise_params() { + return _noise_params; +} + +void LayeredTileMapLayer::rao_setup_noise(Ref noise) { + if (_noise_params.is_valid()) { + _noise_params->setup_noise(noise); + } +} + +Ref LayeredTileMapLayer::rao_get_noise() { + return _rao_noise; +} +void LayeredTileMapLayer::rao_set_noise(const Ref &noise) { + // Don't check if they are the same! + _rao_noise = noise; + + dirty.flags[DIRTY_FLAGS_LAYER_RAO] = true; + + _queue_internal_update(); + emit_signal(CoreStringNames::get_singleton()->changed); +} + #endif bool LayeredTileMapLayer::use_tile_data_runtime_update(const Vector2i &p_coords) { @@ -3028,6 +3125,7 @@ LayeredTileMapLayer::LayeredTileMapLayer() { #endif #ifdef MODULE_FASTNOISE_ENABLED + _use_rao = true; _rao_strength = 0.3; #endif } diff --git a/modules/layered_tile_map/layered_tile_map_layer.h b/modules/layered_tile_map/layered_tile_map_layer.h index 809fa40bc..094d4f2e4 100644 --- a/modules/layered_tile_map/layered_tile_map_layer.h +++ b/modules/layered_tile_map/layered_tile_map_layer.h @@ -38,7 +38,8 @@ #include "modules/modules_enabled.gen.h" #ifdef MODULE_FASTNOISE_ENABLED -#include "../fastnoise/noise.h" +#include "modules/fastnoise/fastnoise_noise_params.h" +#include "modules/fastnoise/noise.h" #endif class LayeredTileSetAtlasSource; @@ -330,7 +331,10 @@ private: //RAO #ifdef MODULE_FASTNOISE_ENABLED + bool _use_rao; real_t _rao_strength; + Ref _noise_params; + Ref _rao_noise; #endif @@ -530,7 +534,6 @@ public: bool use_tile_data_runtime_update(const Vector2i &p_coords); void tile_data_runtime_update(const Vector2i &p_coords, LayeredTileData *p_tile_data); - //VertexLights2D #ifdef MODULE_VERTEX_LIGHTS_2D_ENABLED void set_use_vertex_lights(const bool p_use); @@ -539,11 +542,19 @@ public: //RAO #ifdef MODULE_FASTNOISE_ENABLED - Ref get_rao_noise(); - void set_rao_noise(const Ref &noise); + void rao_set_use(bool p_rao); + bool rao_get_use() const; + + void rao_set_strength(const real_t p_strength); + real_t rao_get_strength() const; - void set_rao_strength(const real_t p_strength); - real_t get_rao_strength() const; + void rao_set_noise_params(const Ref &noise); + Ref rao_get_noise_params(); + + void rao_setup_noise(Ref noise); + + Ref rao_get_noise(); + void rao_set_noise(const Ref &noise); #endif LayeredTileMapLayer();