Fix basic noise generators not updating their internal resource copy

master
Marc Gilleron 2021-03-27 00:57:08 +00:00
parent c7c24a3d95
commit 6e176c1cdd
4 changed files with 51 additions and 4 deletions

View File

@ -1,4 +1,5 @@
#include "voxel_generator_noise.h" #include "voxel_generator_noise.h"
#include <core/core_string_names.h>
#include <core/engine.h> #include <core/engine.h>
VoxelGeneratorNoise::VoxelGeneratorNoise() { VoxelGeneratorNoise::VoxelGeneratorNoise() {
@ -19,15 +20,27 @@ void VoxelGeneratorNoise::set_noise(Ref<OpenSimplexNoise> noise) {
if (_noise == noise) { if (_noise == noise) {
return; return;
} }
if (_noise.is_valid()) {
_noise->disconnect(CoreStringNames::get_singleton()->changed, this, "_on_noise_changed");
}
_noise = noise; _noise = noise;
Ref<OpenSimplexNoise> copy; Ref<OpenSimplexNoise> copy;
if (noise.is_valid()) { if (_noise.is_valid()) {
copy = noise->duplicate(); _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); RWLockWrite wlock(_parameters_lock);
_parameters.noise = copy; _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) { void VoxelGeneratorNoise::set_channel(VoxelBuffer::ChannelId channel) {
ERR_FAIL_INDEX(channel, VoxelBuffer::MAX_CHANNELS); ERR_FAIL_INDEX(channel, VoxelBuffer::MAX_CHANNELS);
bool changed = false; 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("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("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::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::OBJECT, "noise", PROPERTY_HINT_RESOURCE_TYPE, "OpenSimplexNoise"), "set_noise", "get_noise");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "height_start"), "set_height_start", "get_height_start"); ADD_PROPERTY(PropertyInfo(Variant::REAL, "height_start"), "set_height_start", "get_height_start");

View File

@ -27,6 +27,8 @@ public:
void generate_block(VoxelBlockRequest &input) override; void generate_block(VoxelBlockRequest &input) override;
protected: protected:
void _on_noise_changed();
static void _bind_methods(); static void _bind_methods();
private: private:

View File

@ -1,4 +1,5 @@
#include "voxel_generator_noise_2d.h" #include "voxel_generator_noise_2d.h"
#include <core/core_string_names.h>
#include <core/engine.h> #include <core/engine.h>
VoxelGeneratorNoise2D::VoxelGeneratorNoise2D() { VoxelGeneratorNoise2D::VoxelGeneratorNoise2D() {
@ -19,10 +20,15 @@ void VoxelGeneratorNoise2D::set_noise(Ref<OpenSimplexNoise> noise) {
if (_noise == noise) { if (_noise == noise) {
return; return;
} }
if (_noise.is_valid()) {
_noise->disconnect(CoreStringNames::get_singleton()->changed, this, "_on_noise_changed");
}
_noise = noise; _noise = noise;
Ref<OpenSimplexNoise> copy; Ref<OpenSimplexNoise> copy;
if (noise.is_valid()) { if (_noise.is_valid()) {
copy = noise->duplicate(); _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); RWLockWrite wlock(_parameters_lock);
_parameters.noise = copy; _parameters.noise = copy;
@ -36,9 +42,14 @@ void VoxelGeneratorNoise2D::set_curve(Ref<Curve> curve) {
if (_curve == curve) { if (_curve == curve) {
return; return;
} }
if (_curve.is_valid()) {
_curve->disconnect(CoreStringNames::get_singleton()->changed, this, "_on_curve_changed");
}
_curve = curve; _curve = curve;
RWLockWrite wlock(_parameters_lock); RWLockWrite wlock(_parameters_lock);
if (_curve.is_valid()) { 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 = _curve->duplicate();
_parameters.curve->bake(); _parameters.curve->bake();
} else { } else {
@ -78,6 +89,19 @@ void VoxelGeneratorNoise2D::generate_block(VoxelBlockRequest &input) {
out_buffer.compress_uniform_channels(); 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() { void VoxelGeneratorNoise2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_noise", "noise"), &VoxelGeneratorNoise2D::set_noise); ClassDB::bind_method(D_METHOD("set_noise", "noise"), &VoxelGeneratorNoise2D::set_noise);
ClassDB::bind_method(D_METHOD("get_noise"), &VoxelGeneratorNoise2D::get_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("set_curve", "curve"), &VoxelGeneratorNoise2D::set_curve);
ClassDB::bind_method(D_METHOD("get_curve"), &VoxelGeneratorNoise2D::get_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, "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"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_curve", "get_curve");
} }

View File

@ -20,6 +20,9 @@ public:
void generate_block(VoxelBlockRequest &input) override; void generate_block(VoxelBlockRequest &input) override;
private: private:
void _on_noise_changed();
void _on_curve_changed();
static void _bind_methods(); static void _bind_methods();
private: private: