diff options
Diffstat (limited to 'modules/opensimplex')
-rw-r--r-- | modules/opensimplex/noise_texture.cpp | 53 | ||||
-rw-r--r-- | modules/opensimplex/open_simplex_noise.cpp | 30 | ||||
-rw-r--r-- | modules/opensimplex/register_types.cpp | 1 |
3 files changed, 44 insertions, 40 deletions
diff --git a/modules/opensimplex/noise_texture.cpp b/modules/opensimplex/noise_texture.cpp index 1a08ec416f..1181e69cd3 100644 --- a/modules/opensimplex/noise_texture.cpp +++ b/modules/opensimplex/noise_texture.cpp @@ -34,7 +34,7 @@ NoiseTexture::NoiseTexture() { update_queued = false; - noise_thread = NULL; + noise_thread = nullptr; regen_queued = false; first_time = true; @@ -59,7 +59,6 @@ NoiseTexture::~NoiseTexture() { } void NoiseTexture::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_width", "width"), &NoiseTexture::set_width); ClassDB::bind_method(D_METHOD("set_height", "height"), &NoiseTexture::set_height); @@ -88,7 +87,6 @@ void NoiseTexture::_bind_methods() { } void NoiseTexture::_validate_property(PropertyInfo &property) const { - if (property.name == "bump_strength") { if (!as_normalmap) { property.usage = PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL; @@ -110,11 +108,10 @@ void NoiseTexture::_set_texture_data(const Ref<Image> &p_image) { } void NoiseTexture::_thread_done(const Ref<Image> &p_image) { - _set_texture_data(p_image); Thread::wait_to_finish(noise_thread); memdelete(noise_thread); - noise_thread = NULL; + noise_thread = nullptr; if (regen_queued) { noise_thread = Thread::create(_thread_function, this); regen_queued = false; @@ -127,24 +124,28 @@ void NoiseTexture::_thread_function(void *p_ud) { } void NoiseTexture::_queue_update() { - - if (update_queued) + if (update_queued) { return; + } update_queued = true; call_deferred("_update_texture"); } Ref<Image> NoiseTexture::_generate_texture() { + // Prevent memdelete due to unref() on other thread. + Ref<OpenSimplexNoise> ref_noise = noise; - if (noise.is_null()) return Ref<Image>(); + if (ref_noise.is_null()) { + return Ref<Image>(); + } Ref<Image> image; if (seamless) { - image = noise->get_seamless_image(size.x); + image = ref_noise->get_seamless_image(size.x); } else { - image = noise->get_image(size.x, size.y); + image = ref_noise->get_image(size.x, size.y); } if (as_normalmap) { @@ -164,7 +165,6 @@ void NoiseTexture::_update_texture() { use_thread = false; #endif if (use_thread) { - if (!noise_thread) { noise_thread = Thread::create(_thread_function, this); regen_queued = false; @@ -180,8 +180,9 @@ void NoiseTexture::_update_texture() { } void NoiseTexture::set_noise(Ref<OpenSimplexNoise> p_noise) { - if (p_noise == noise) + if (p_noise == noise) { return; + } if (noise.is_valid()) { noise->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &NoiseTexture::_queue_update)); } @@ -197,19 +198,25 @@ Ref<OpenSimplexNoise> NoiseTexture::get_noise() { } void NoiseTexture::set_width(int p_width) { - if (p_width == size.x) return; + if (p_width == size.x) { + return; + } size.x = p_width; _queue_update(); } void NoiseTexture::set_height(int p_height) { - if (p_height == size.y) return; + if (p_height == size.y) { + return; + } size.y = p_height; _queue_update(); } void NoiseTexture::set_seamless(bool p_seamless) { - if (p_seamless == seamless) return; + if (p_seamless == seamless) { + return; + } seamless = p_seamless; _queue_update(); } @@ -219,7 +226,9 @@ bool NoiseTexture::get_seamless() { } void NoiseTexture::set_as_normalmap(bool p_as_normalmap) { - if (p_as_normalmap == as_normalmap) return; + if (p_as_normalmap == as_normalmap) { + return; + } as_normalmap = p_as_normalmap; _queue_update(); _change_notify(); @@ -230,25 +239,24 @@ bool NoiseTexture::is_normalmap() { } void NoiseTexture::set_bump_strength(float p_bump_strength) { - - if (p_bump_strength == bump_strength) return; + if (p_bump_strength == bump_strength) { + return; + } bump_strength = p_bump_strength; - if (as_normalmap) + if (as_normalmap) { _queue_update(); + } } float NoiseTexture::get_bump_strength() { - return bump_strength; } int NoiseTexture::get_width() const { - return size.x; } int NoiseTexture::get_height() const { - return size.y; } @@ -261,6 +269,5 @@ RID NoiseTexture::get_rid() const { } Ref<Image> NoiseTexture::get_data() const { - return data; } diff --git a/modules/opensimplex/open_simplex_noise.cpp b/modules/opensimplex/open_simplex_noise.cpp index 238faa4130..00b3d47db9 100644 --- a/modules/opensimplex/open_simplex_noise.cpp +++ b/modules/opensimplex/open_simplex_noise.cpp @@ -33,7 +33,6 @@ #include "core/core_string_names.h" OpenSimplexNoise::OpenSimplexNoise() { - seed = 0; persistence = 0.5; octaves = 3; @@ -53,9 +52,9 @@ void OpenSimplexNoise::_init_seeds() { } void OpenSimplexNoise::set_seed(int p_seed) { - - if (seed == p_seed) + if (seed == p_seed) { return; + } seed = p_seed; @@ -65,12 +64,13 @@ void OpenSimplexNoise::set_seed(int p_seed) { } int OpenSimplexNoise::get_seed() { - return seed; } void OpenSimplexNoise::set_octaves(int p_octaves) { - if (p_octaves == octaves) return; + if (p_octaves == octaves) { + return; + } ERR_FAIL_COND_MSG(p_octaves > MAX_OCTAVES, vformat("The number of OpenSimplexNoise octaves is limited to %d; ignoring the new value.", MAX_OCTAVES)); @@ -79,25 +79,30 @@ void OpenSimplexNoise::set_octaves(int p_octaves) { } void OpenSimplexNoise::set_period(float p_period) { - if (p_period == period) return; + if (p_period == period) { + return; + } period = p_period; emit_changed(); } void OpenSimplexNoise::set_persistence(float p_persistence) { - if (p_persistence == persistence) return; + if (p_persistence == persistence) { + return; + } persistence = p_persistence; emit_changed(); } void OpenSimplexNoise::set_lacunarity(float p_lacunarity) { - if (p_lacunarity == lacunarity) return; + if (p_lacunarity == lacunarity) { + return; + } lacunarity = p_lacunarity; emit_changed(); } Ref<Image> OpenSimplexNoise::get_image(int p_width, int p_height) { - Vector<uint8_t> data; data.resize(p_width * p_height * 4); @@ -120,7 +125,6 @@ Ref<Image> OpenSimplexNoise::get_image(int p_width, int p_height) { } Ref<Image> OpenSimplexNoise::get_seamless_image(int p_size) { - Vector<uint8_t> data; data.resize(p_size * p_size * 4); @@ -128,7 +132,6 @@ Ref<Image> OpenSimplexNoise::get_seamless_image(int p_size) { for (int i = 0; i < p_size; i++) { for (int j = 0; j < p_size; j++) { - float ii = (float)i / (float)p_size; float jj = (float)j / (float)p_size; @@ -157,7 +160,6 @@ Ref<Image> OpenSimplexNoise::get_seamless_image(int p_size) { } void OpenSimplexNoise::_bind_methods() { - ClassDB::bind_method(D_METHOD("get_seed"), &OpenSimplexNoise::get_seed); ClassDB::bind_method(D_METHOD("set_seed", "seed"), &OpenSimplexNoise::set_seed); @@ -192,12 +194,10 @@ void OpenSimplexNoise::_bind_methods() { } float OpenSimplexNoise::get_noise_1d(float x) { - return get_noise_2d(x, 1.0); } float OpenSimplexNoise::get_noise_2d(float x, float y) { - x /= period; y /= period; @@ -218,7 +218,6 @@ float OpenSimplexNoise::get_noise_2d(float x, float y) { } float OpenSimplexNoise::get_noise_3d(float x, float y, float z) { - x /= period; y /= period; z /= period; @@ -241,7 +240,6 @@ float OpenSimplexNoise::get_noise_3d(float x, float y, float z) { } float OpenSimplexNoise::get_noise_4d(float x, float y, float z, float w) { - x /= period; y /= period; z /= period; diff --git a/modules/opensimplex/register_types.cpp b/modules/opensimplex/register_types.cpp index 6fae1fe415..fef90cdce3 100644 --- a/modules/opensimplex/register_types.cpp +++ b/modules/opensimplex/register_types.cpp @@ -33,7 +33,6 @@ #include "open_simplex_noise.h" void register_opensimplex_types() { - ClassDB::register_class<OpenSimplexNoise>(); ClassDB::register_class<NoiseTexture>(); } |