From 6e176c1cdd2511bce571a0f8165bf086e742d0ff Mon Sep 17 00:00:00 2001 From: Marc Gilleron Date: Sat, 27 Mar 2021 00:57:08 +0000 Subject: [PATCH] Fix basic noise generators not updating their internal resource copy --- generators/simple/voxel_generator_noise.cpp | 19 ++++++++++-- generators/simple/voxel_generator_noise.h | 2 ++ .../simple/voxel_generator_noise_2d.cpp | 31 +++++++++++++++++-- generators/simple/voxel_generator_noise_2d.h | 3 ++ 4 files changed, 51 insertions(+), 4 deletions(-) diff --git a/generators/simple/voxel_generator_noise.cpp b/generators/simple/voxel_generator_noise.cpp index 2842442d..fe915133 100644 --- a/generators/simple/voxel_generator_noise.cpp +++ b/generators/simple/voxel_generator_noise.cpp @@ -1,4 +1,5 @@ #include "voxel_generator_noise.h" +#include #include VoxelGeneratorNoise::VoxelGeneratorNoise() { @@ -19,15 +20,27 @@ void VoxelGeneratorNoise::set_noise(Ref noise) { if (_noise == noise) { return; } + if (_noise.is_valid()) { + _noise->disconnect(CoreStringNames::get_singleton()->changed, this, "_on_noise_changed"); + } _noise = noise; Ref copy; - if (noise.is_valid()) { - copy = noise->duplicate(); + if (_noise.is_valid()) { + _noise->connect(CoreStringNames::get_singleton()->changed, this, "_on_noise_changed"); + // The OpenSimplexNoise resource is not thread-safe so we make a copy of it for use in threads + copy = _noise->duplicate(); } + // The OpenSimplexNoise resource is not thread-safe so we make a copy of it for use in threads RWLockWrite wlock(_parameters_lock); _parameters.noise = copy; } +void VoxelGeneratorNoise::_on_noise_changed() { + ERR_FAIL_COND(_noise.is_null()); + RWLockWrite wlock(_parameters_lock); + _parameters.noise = _noise->duplicate(); +} + void VoxelGeneratorNoise::set_channel(VoxelBuffer::ChannelId channel) { ERR_FAIL_INDEX(channel, VoxelBuffer::MAX_CHANNELS); bool changed = false; @@ -216,6 +229,8 @@ void VoxelGeneratorNoise::_bind_methods() { ClassDB::bind_method(D_METHOD("set_height_range", "hrange"), &VoxelGeneratorNoise::set_height_range); ClassDB::bind_method(D_METHOD("get_height_range"), &VoxelGeneratorNoise::get_height_range); + ClassDB::bind_method(D_METHOD("_on_noise_changed"), &VoxelGeneratorNoise::_on_noise_changed); + ADD_PROPERTY(PropertyInfo(Variant::INT, "channel", PROPERTY_HINT_ENUM, VoxelBuffer::CHANNEL_ID_HINT_STRING), "set_channel", "get_channel"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "noise", PROPERTY_HINT_RESOURCE_TYPE, "OpenSimplexNoise"), "set_noise", "get_noise"); ADD_PROPERTY(PropertyInfo(Variant::REAL, "height_start"), "set_height_start", "get_height_start"); diff --git a/generators/simple/voxel_generator_noise.h b/generators/simple/voxel_generator_noise.h index e67dc8c9..9a59d491 100644 --- a/generators/simple/voxel_generator_noise.h +++ b/generators/simple/voxel_generator_noise.h @@ -27,6 +27,8 @@ public: void generate_block(VoxelBlockRequest &input) override; protected: + void _on_noise_changed(); + static void _bind_methods(); private: diff --git a/generators/simple/voxel_generator_noise_2d.cpp b/generators/simple/voxel_generator_noise_2d.cpp index c420496a..541f5d30 100644 --- a/generators/simple/voxel_generator_noise_2d.cpp +++ b/generators/simple/voxel_generator_noise_2d.cpp @@ -1,4 +1,5 @@ #include "voxel_generator_noise_2d.h" +#include #include VoxelGeneratorNoise2D::VoxelGeneratorNoise2D() { @@ -19,10 +20,15 @@ void VoxelGeneratorNoise2D::set_noise(Ref noise) { if (_noise == noise) { return; } + if (_noise.is_valid()) { + _noise->disconnect(CoreStringNames::get_singleton()->changed, this, "_on_noise_changed"); + } _noise = noise; Ref copy; - if (noise.is_valid()) { - copy = noise->duplicate(); + if (_noise.is_valid()) { + _noise->connect(CoreStringNames::get_singleton()->changed, this, "_on_noise_changed"); + // The OpenSimplexNoise resource is not thread-safe so we make a copy of it for use in threads + copy = _noise->duplicate(); } RWLockWrite wlock(_parameters_lock); _parameters.noise = copy; @@ -36,9 +42,14 @@ void VoxelGeneratorNoise2D::set_curve(Ref curve) { if (_curve == curve) { return; } + if (_curve.is_valid()) { + _curve->disconnect(CoreStringNames::get_singleton()->changed, this, "_on_curve_changed"); + } _curve = curve; RWLockWrite wlock(_parameters_lock); if (_curve.is_valid()) { + _curve->connect(CoreStringNames::get_singleton()->changed, this, "_on_curve_changed"); + // The Curve resource is not thread-safe so we make a copy of it for use in threads _parameters.curve = _curve->duplicate(); _parameters.curve->bake(); } else { @@ -78,6 +89,19 @@ void VoxelGeneratorNoise2D::generate_block(VoxelBlockRequest &input) { out_buffer.compress_uniform_channels(); } +void VoxelGeneratorNoise2D::_on_noise_changed() { + ERR_FAIL_COND(_noise.is_null()); + RWLockWrite wlock(_parameters_lock); + _parameters.noise = _noise->duplicate(); +} + +void VoxelGeneratorNoise2D::_on_curve_changed() { + ERR_FAIL_COND(_curve.is_null()); + RWLockWrite wlock(_parameters_lock); + _parameters.curve = _curve->duplicate(); + _parameters.curve->bake(); +} + void VoxelGeneratorNoise2D::_bind_methods() { ClassDB::bind_method(D_METHOD("set_noise", "noise"), &VoxelGeneratorNoise2D::set_noise); ClassDB::bind_method(D_METHOD("get_noise"), &VoxelGeneratorNoise2D::get_noise); @@ -85,6 +109,9 @@ void VoxelGeneratorNoise2D::_bind_methods() { ClassDB::bind_method(D_METHOD("set_curve", "curve"), &VoxelGeneratorNoise2D::set_curve); ClassDB::bind_method(D_METHOD("get_curve"), &VoxelGeneratorNoise2D::get_curve); + ClassDB::bind_method(D_METHOD("_on_noise_changed"), &VoxelGeneratorNoise2D::_on_noise_changed); + ClassDB::bind_method(D_METHOD("_on_curve_changed"), &VoxelGeneratorNoise2D::_on_curve_changed); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "noise", PROPERTY_HINT_RESOURCE_TYPE, "OpenSimplexNoise"), "set_noise", "get_noise"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_curve", "get_curve"); } diff --git a/generators/simple/voxel_generator_noise_2d.h b/generators/simple/voxel_generator_noise_2d.h index e5722198..5453639d 100644 --- a/generators/simple/voxel_generator_noise_2d.h +++ b/generators/simple/voxel_generator_noise_2d.h @@ -20,6 +20,9 @@ public: void generate_block(VoxelBlockRequest &input) override; private: + void _on_noise_changed(); + void _on_curve_changed(); + static void _bind_methods(); private: