From 394d0584207378ecf330dad5cd3bd008ad535b47 Mon Sep 17 00:00:00 2001 From: Aaron Franke Date: Wed, 24 Aug 2022 18:08:09 -0500 Subject: Rename 2D NoiseTexture to NoiseTexture2D --- modules/noise/config.py | 2 +- modules/noise/doc_classes/NoiseTexture.xml | 58 ---- modules/noise/doc_classes/NoiseTexture2D.xml | 58 ++++ modules/noise/editor/noise_editor_plugin.cpp | 4 +- modules/noise/noise_texture.cpp | 378 --------------------------- modules/noise/noise_texture.h | 123 --------- modules/noise/noise_texture_2d.cpp | 378 +++++++++++++++++++++++++++ modules/noise/noise_texture_2d.h | 123 +++++++++ modules/noise/register_types.cpp | 5 +- 9 files changed, 565 insertions(+), 564 deletions(-) delete mode 100644 modules/noise/doc_classes/NoiseTexture.xml create mode 100644 modules/noise/doc_classes/NoiseTexture2D.xml delete mode 100644 modules/noise/noise_texture.cpp delete mode 100644 modules/noise/noise_texture.h create mode 100644 modules/noise/noise_texture_2d.cpp create mode 100644 modules/noise/noise_texture_2d.h (limited to 'modules/noise') diff --git a/modules/noise/config.py b/modules/noise/config.py index 74db20f2a4..2318d28c53 100644 --- a/modules/noise/config.py +++ b/modules/noise/config.py @@ -10,7 +10,7 @@ def get_doc_classes(): return [ "FastNoiseLite", "Noise", - "NoiseTexture", + "NoiseTexture2D", ] diff --git a/modules/noise/doc_classes/NoiseTexture.xml b/modules/noise/doc_classes/NoiseTexture.xml deleted file mode 100644 index 62a223b387..0000000000 --- a/modules/noise/doc_classes/NoiseTexture.xml +++ /dev/null @@ -1,58 +0,0 @@ - - - - A texture filled with noise generated by a [Noise] object. - - - Uses [FastNoiseLite] or other libraries to fill the texture data of your desired size. - NoiseTexture can also generate normalmap textures. - The class uses [Thread]s to generate the texture data internally, so [method Texture2D.get_image] may return [code]null[/code] if the generation process has not completed yet. In that case, you need to wait for the texture to be generated before accessing the image and the generated byte data: - [codeblock] - var texture = NoiseTexture.new() - texture.noise = FastNoiseLite.new() - await texture.changed - var image = texture.get_image() - var data = image.get_data() - [/codeblock] - - - - - - If [code]true[/code], the resulting texture contains a normal map created from the original noise interpreted as a bump map. - - - Strength of the bump maps used in this texture. A higher value will make the bump maps appear larger while a lower value will make them appear softer. - - - A [Gradient] which is used to map the luminance of each pixel to a color value. - - - Determines whether mipmaps are generated for this texture. - Enabling this results in less texture aliasing, but the noise texture generation may take longer. - Requires (anisotropic) mipmap filtering to be enabled for a material to have an effect. - - - Height of the generated texture. - - - Determines whether the noise image is calculated in 3D space. May result in reduced contrast. - - - If [code]true[/code], inverts the noise texture. White becomes black, black becomes white. - - - The instance of the [Noise] object. - - - If [code]true[/code], a seamless texture is requested from the [Noise] resource. - [b]Note:[/b] Seamless noise textures may take longer to generate and/or can have a lower contrast compared to non-seamless noise depending on the used [Noise] resource. This is because some implementations use higher dimensions for generating seamless noise. - - - Used for the default/fallback implementation of the seamless texture generation. It determines the distance over which the seams are blended. High values may result in less details and contrast. See [Noise] for further details. - - - Width of the generated texture. - - - diff --git a/modules/noise/doc_classes/NoiseTexture2D.xml b/modules/noise/doc_classes/NoiseTexture2D.xml new file mode 100644 index 0000000000..9eea2738c5 --- /dev/null +++ b/modules/noise/doc_classes/NoiseTexture2D.xml @@ -0,0 +1,58 @@ + + + + A texture filled with noise generated by a [Noise] object. + + + Uses [FastNoiseLite] or other libraries to fill the texture data of your desired size. + NoiseTexture2D can also generate normalmap textures. + The class uses [Thread]s to generate the texture data internally, so [method Texture2D.get_image] may return [code]null[/code] if the generation process has not completed yet. In that case, you need to wait for the texture to be generated before accessing the image and the generated byte data: + [codeblock] + var texture = NoiseTexture2D.new() + texture.noise = FastNoiseLite.new() + await texture.changed + var image = texture.get_image() + var data = image.get_data() + [/codeblock] + + + + + + If [code]true[/code], the resulting texture contains a normal map created from the original noise interpreted as a bump map. + + + Strength of the bump maps used in this texture. A higher value will make the bump maps appear larger while a lower value will make them appear softer. + + + A [Gradient] which is used to map the luminance of each pixel to a color value. + + + Determines whether mipmaps are generated for this texture. + Enabling this results in less texture aliasing, but the noise texture generation may take longer. + Requires (anisotropic) mipmap filtering to be enabled for a material to have an effect. + + + Height of the generated texture. + + + Determines whether the noise image is calculated in 3D space. May result in reduced contrast. + + + If [code]true[/code], inverts the noise texture. White becomes black, black becomes white. + + + The instance of the [Noise] object. + + + If [code]true[/code], a seamless texture is requested from the [Noise] resource. + [b]Note:[/b] Seamless noise textures may take longer to generate and/or can have a lower contrast compared to non-seamless noise depending on the used [Noise] resource. This is because some implementations use higher dimensions for generating seamless noise. + + + Used for the default/fallback implementation of the seamless texture generation. It determines the distance over which the seams are blended. High values may result in less details and contrast. See [Noise] for further details. + + + Width of the generated texture. + + + diff --git a/modules/noise/editor/noise_editor_plugin.cpp b/modules/noise/editor/noise_editor_plugin.cpp index 27a86f45b5..b6f7cbd2f8 100644 --- a/modules/noise/editor/noise_editor_plugin.cpp +++ b/modules/noise/editor/noise_editor_plugin.cpp @@ -35,7 +35,7 @@ #include "editor/editor_scale.h" #include "modules/noise/noise.h" -#include "modules/noise/noise_texture.h" +#include "modules/noise/noise_texture_2d.h" class NoisePreview : public Control { GDCLASS(NoisePreview, Control) @@ -102,7 +102,7 @@ private: void update_preview() { if (MIN(_preview_texture_size.width, _preview_texture_size.height) > 0) { - Ref tex; + Ref tex; tex.instantiate(); tex->set_width(_preview_texture_size.width); tex->set_height(_preview_texture_size.height); diff --git a/modules/noise/noise_texture.cpp b/modules/noise/noise_texture.cpp deleted file mode 100644 index 923b420581..0000000000 --- a/modules/noise/noise_texture.cpp +++ /dev/null @@ -1,378 +0,0 @@ -/*************************************************************************/ -/* noise_texture.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "noise_texture.h" - -#include "core/core_string_names.h" -#include "noise.h" - -NoiseTexture::NoiseTexture() { - noise = Ref(); - - _queue_update(); -} - -NoiseTexture::~NoiseTexture() { - if (texture.is_valid()) { - RS::get_singleton()->free(texture); - } - noise_thread.wait_to_finish(); -} - -void NoiseTexture::_bind_methods() { - ClassDB::bind_method(D_METHOD("_update_texture"), &NoiseTexture::_update_texture); - ClassDB::bind_method(D_METHOD("_generate_texture"), &NoiseTexture::_generate_texture); - ClassDB::bind_method(D_METHOD("_thread_done", "image"), &NoiseTexture::_thread_done); - - ClassDB::bind_method(D_METHOD("set_width", "width"), &NoiseTexture::set_width); - ClassDB::bind_method(D_METHOD("set_height", "height"), &NoiseTexture::set_height); - - ClassDB::bind_method(D_METHOD("set_invert", "invert"), &NoiseTexture::set_invert); - ClassDB::bind_method(D_METHOD("get_invert"), &NoiseTexture::get_invert); - - ClassDB::bind_method(D_METHOD("set_in_3d_space", "enable"), &NoiseTexture::set_in_3d_space); - ClassDB::bind_method(D_METHOD("is_in_3d_space"), &NoiseTexture::is_in_3d_space); - - ClassDB::bind_method(D_METHOD("set_generate_mipmaps", "invert"), &NoiseTexture::set_generate_mipmaps); - ClassDB::bind_method(D_METHOD("is_generating_mipmaps"), &NoiseTexture::is_generating_mipmaps); - - ClassDB::bind_method(D_METHOD("set_seamless", "seamless"), &NoiseTexture::set_seamless); - ClassDB::bind_method(D_METHOD("get_seamless"), &NoiseTexture::get_seamless); - - ClassDB::bind_method(D_METHOD("set_seamless_blend_skirt", "seamless_blend_skirt"), &NoiseTexture::set_seamless_blend_skirt); - ClassDB::bind_method(D_METHOD("get_seamless_blend_skirt"), &NoiseTexture::get_seamless_blend_skirt); - - ClassDB::bind_method(D_METHOD("set_as_normal_map", "as_normal_map"), &NoiseTexture::set_as_normal_map); - ClassDB::bind_method(D_METHOD("is_normal_map"), &NoiseTexture::is_normal_map); - - ClassDB::bind_method(D_METHOD("set_bump_strength", "bump_strength"), &NoiseTexture::set_bump_strength); - ClassDB::bind_method(D_METHOD("get_bump_strength"), &NoiseTexture::get_bump_strength); - - ClassDB::bind_method(D_METHOD("set_color_ramp", "gradient"), &NoiseTexture::set_color_ramp); - ClassDB::bind_method(D_METHOD("get_color_ramp"), &NoiseTexture::get_color_ramp); - - ClassDB::bind_method(D_METHOD("set_noise", "noise"), &NoiseTexture::set_noise); - ClassDB::bind_method(D_METHOD("get_noise"), &NoiseTexture::get_noise); - - ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "1,2048,1,or_greater,suffix:px"), "set_width", "get_width"); - ADD_PROPERTY(PropertyInfo(Variant::INT, "height", PROPERTY_HINT_RANGE, "1,2048,1,or_greater,suffix:px"), "set_height", "get_height"); - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "invert"), "set_invert", "get_invert"); - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "in_3d_space"), "set_in_3d_space", "is_in_3d_space"); - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "generate_mipmaps"), "set_generate_mipmaps", "is_generating_mipmaps"); - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "seamless"), "set_seamless", "get_seamless"); - ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "seamless_blend_skirt", PROPERTY_HINT_RANGE, "0.05,1,0.001"), "set_seamless_blend_skirt", "get_seamless_blend_skirt"); - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "as_normal_map"), "set_as_normal_map", "is_normal_map"); - ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bump_strength", PROPERTY_HINT_RANGE, "0,32,0.1,or_greater"), "set_bump_strength", "get_bump_strength"); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_ramp", PROPERTY_HINT_RESOURCE_TYPE, "Gradient"), "set_color_ramp", "get_color_ramp"); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "noise", PROPERTY_HINT_RESOURCE_TYPE, "Noise"), "set_noise", "get_noise"); -} - -void NoiseTexture::_validate_property(PropertyInfo &p_property) const { - if (p_property.name == "bump_strength") { - if (!as_normal_map) { - p_property.usage = PROPERTY_USAGE_NO_EDITOR; - } - } - - if (p_property.name == "seamless_blend_skirt") { - if (!seamless) { - p_property.usage = PROPERTY_USAGE_NO_EDITOR; - } - } -} - -void NoiseTexture::_set_texture_image(const Ref &p_image) { - image = p_image; - if (image.is_valid()) { - if (texture.is_valid()) { - RID new_texture = RS::get_singleton()->texture_2d_create(p_image); - RS::get_singleton()->texture_replace(texture, new_texture); - } else { - texture = RS::get_singleton()->texture_2d_create(p_image); - } - } - emit_changed(); -} - -void NoiseTexture::_thread_done(const Ref &p_image) { - _set_texture_image(p_image); - noise_thread.wait_to_finish(); - if (regen_queued) { - noise_thread.start(_thread_function, this); - regen_queued = false; - } -} - -void NoiseTexture::_thread_function(void *p_ud) { - NoiseTexture *tex = static_cast(p_ud); - tex->call_deferred(SNAME("_thread_done"), tex->_generate_texture()); -} - -void NoiseTexture::_queue_update() { - if (update_queued) { - return; - } - - update_queued = true; - call_deferred(SNAME("_update_texture")); -} - -Ref NoiseTexture::_generate_texture() { - // Prevent memdelete due to unref() on other thread. - Ref ref_noise = noise; - - if (ref_noise.is_null()) { - return Ref(); - } - - Ref image; - - if (seamless) { - image = ref_noise->get_seamless_image(size.x, size.y, invert, in_3d_space, seamless_blend_skirt); - } else { - image = ref_noise->get_image(size.x, size.y, invert, in_3d_space); - } - if (color_ramp.is_valid()) { - image = _modulate_with_gradient(image, color_ramp); - } - if (as_normal_map) { - image->bump_map_to_normal_map(bump_strength); - } - if (generate_mipmaps) { - image->generate_mipmaps(); - } - - return image; -} - -Ref NoiseTexture::_modulate_with_gradient(Ref p_image, Ref p_gradient) { - int width = p_image->get_width(); - int height = p_image->get_height(); - - Ref new_image; - new_image.instantiate(); - new_image->create(width, height, false, Image::FORMAT_RGBA8); - - for (int row = 0; row < height; row++) { - for (int col = 0; col < width; col++) { - Color pixel_color = p_image->get_pixel(col, row); - Color ramp_color = color_ramp->get_color_at_offset(pixel_color.get_luminance()); - new_image->set_pixel(col, row, ramp_color); - } - } - - return new_image; -} - -void NoiseTexture::_update_texture() { - bool use_thread = true; - if (first_time) { - use_thread = false; - first_time = false; - } -#ifdef NO_THREADS - use_thread = false; -#endif - if (use_thread) { - if (!noise_thread.is_started()) { - noise_thread.start(_thread_function, this); - regen_queued = false; - } else { - regen_queued = true; - } - - } else { - Ref image = _generate_texture(); - _set_texture_image(image); - } - update_queued = false; -} - -void NoiseTexture::set_noise(Ref p_noise) { - if (p_noise == noise) { - return; - } - if (noise.is_valid()) { - noise->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &NoiseTexture::_queue_update)); - } - noise = p_noise; - if (noise.is_valid()) { - noise->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &NoiseTexture::_queue_update)); - } - _queue_update(); -} - -Ref NoiseTexture::get_noise() { - return noise; -} - -void NoiseTexture::set_width(int p_width) { - ERR_FAIL_COND(p_width <= 0); - if (p_width == size.x) { - return; - } - size.x = p_width; - _queue_update(); -} - -void NoiseTexture::set_height(int p_height) { - ERR_FAIL_COND(p_height <= 0); - if (p_height == size.y) { - return; - } - size.y = p_height; - _queue_update(); -} - -void NoiseTexture::set_invert(bool p_invert) { - if (p_invert == invert) { - return; - } - invert = p_invert; - _queue_update(); -} - -bool NoiseTexture::get_invert() const { - return invert; -} - -void NoiseTexture::set_in_3d_space(bool p_enable) { - if (p_enable == in_3d_space) { - return; - } - in_3d_space = p_enable; - _queue_update(); -} -bool NoiseTexture::is_in_3d_space() const { - return in_3d_space; -} - -void NoiseTexture::set_generate_mipmaps(bool p_enable) { - if (p_enable == generate_mipmaps) { - return; - } - generate_mipmaps = p_enable; - _queue_update(); -} - -bool NoiseTexture::is_generating_mipmaps() const { - return generate_mipmaps; -} - -void NoiseTexture::set_seamless(bool p_seamless) { - if (p_seamless == seamless) { - return; - } - seamless = p_seamless; - _queue_update(); - notify_property_list_changed(); -} - -bool NoiseTexture::get_seamless() { - return seamless; -} - -void NoiseTexture::set_seamless_blend_skirt(real_t p_blend_skirt) { - ERR_FAIL_COND(p_blend_skirt < 0.05 || p_blend_skirt > 1); - - if (p_blend_skirt == seamless_blend_skirt) { - return; - } - seamless_blend_skirt = p_blend_skirt; - _queue_update(); -} -real_t NoiseTexture::get_seamless_blend_skirt() { - return seamless_blend_skirt; -} - -void NoiseTexture::set_as_normal_map(bool p_as_normal_map) { - if (p_as_normal_map == as_normal_map) { - return; - } - as_normal_map = p_as_normal_map; - _queue_update(); - notify_property_list_changed(); -} - -bool NoiseTexture::is_normal_map() { - return as_normal_map; -} - -void NoiseTexture::set_bump_strength(float p_bump_strength) { - if (p_bump_strength == bump_strength) { - return; - } - bump_strength = p_bump_strength; - if (as_normal_map) { - _queue_update(); - } -} - -float NoiseTexture::get_bump_strength() { - return bump_strength; -} - -void NoiseTexture::set_color_ramp(const Ref &p_gradient) { - if (p_gradient == color_ramp) { - return; - } - if (color_ramp.is_valid()) { - color_ramp->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &NoiseTexture::_queue_update)); - } - color_ramp = p_gradient; - if (color_ramp.is_valid()) { - color_ramp->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &NoiseTexture::_queue_update)); - } - _queue_update(); -} - -Ref NoiseTexture::get_color_ramp() const { - return color_ramp; -} - -int NoiseTexture::get_width() const { - return size.x; -} - -int NoiseTexture::get_height() const { - return size.y; -} - -RID NoiseTexture::get_rid() const { - if (!texture.is_valid()) { - texture = RS::get_singleton()->texture_2d_placeholder_create(); - } - - return texture; -} - -Ref NoiseTexture::get_image() const { - return image; -} diff --git a/modules/noise/noise_texture.h b/modules/noise/noise_texture.h deleted file mode 100644 index 83fbcc2d10..0000000000 --- a/modules/noise/noise_texture.h +++ /dev/null @@ -1,123 +0,0 @@ -/*************************************************************************/ -/* noise_texture.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#ifndef NOISE_TEXTURE_H -#define NOISE_TEXTURE_H - -#include "noise.h" - -#include "core/object/ref_counted.h" -#include "scene/resources/texture.h" - -class NoiseTexture : public Texture2D { - GDCLASS(NoiseTexture, Texture2D); - -private: - Ref image; - - Thread noise_thread; - - bool first_time = true; - bool update_queued = false; - bool regen_queued = false; - - mutable RID texture; - uint32_t flags = 0; - - Size2i size = Size2i(512, 512); - bool invert = false; - bool in_3d_space = false; - bool generate_mipmaps = true; - bool seamless = false; - real_t seamless_blend_skirt = 0.1; - bool as_normal_map = false; - float bump_strength = 8.0; - - Ref color_ramp; - Ref noise; - - void _thread_done(const Ref &p_image); - static void _thread_function(void *p_ud); - - void _queue_update(); - Ref _generate_texture(); - void _update_texture(); - void _set_texture_image(const Ref &p_image); - - Ref _modulate_with_gradient(Ref p_image, Ref p_gradient); - -protected: - static void _bind_methods(); - void _validate_property(PropertyInfo &p_property) const; - -public: - void set_noise(Ref p_noise); - Ref get_noise(); - - void set_width(int p_width); - void set_height(int p_height); - - void set_invert(bool p_invert); - bool get_invert() const; - - void set_in_3d_space(bool p_enable); - bool is_in_3d_space() const; - - void set_generate_mipmaps(bool p_enable); - bool is_generating_mipmaps() const; - - void set_seamless(bool p_seamless); - bool get_seamless(); - - void set_seamless_blend_skirt(real_t p_blend_skirt); - real_t get_seamless_blend_skirt(); - - void set_as_normal_map(bool p_as_normal_map); - bool is_normal_map(); - - void set_bump_strength(float p_bump_strength); - float get_bump_strength(); - - void set_color_ramp(const Ref &p_gradient); - Ref get_color_ramp() const; - - int get_width() const override; - int get_height() const override; - - virtual RID get_rid() const override; - virtual bool has_alpha() const override { return false; } - - virtual Ref get_image() const override; - - NoiseTexture(); - virtual ~NoiseTexture(); -}; - -#endif // NOISE_TEXTURE_H diff --git a/modules/noise/noise_texture_2d.cpp b/modules/noise/noise_texture_2d.cpp new file mode 100644 index 0000000000..8d279f9dd3 --- /dev/null +++ b/modules/noise/noise_texture_2d.cpp @@ -0,0 +1,378 @@ +/*************************************************************************/ +/* noise_texture_2d.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "noise_texture_2d.h" + +#include "core/core_string_names.h" +#include "noise.h" + +NoiseTexture2D::NoiseTexture2D() { + noise = Ref(); + + _queue_update(); +} + +NoiseTexture2D::~NoiseTexture2D() { + if (texture.is_valid()) { + RS::get_singleton()->free(texture); + } + noise_thread.wait_to_finish(); +} + +void NoiseTexture2D::_bind_methods() { + ClassDB::bind_method(D_METHOD("_update_texture"), &NoiseTexture2D::_update_texture); + ClassDB::bind_method(D_METHOD("_generate_texture"), &NoiseTexture2D::_generate_texture); + ClassDB::bind_method(D_METHOD("_thread_done", "image"), &NoiseTexture2D::_thread_done); + + ClassDB::bind_method(D_METHOD("set_width", "width"), &NoiseTexture2D::set_width); + ClassDB::bind_method(D_METHOD("set_height", "height"), &NoiseTexture2D::set_height); + + ClassDB::bind_method(D_METHOD("set_invert", "invert"), &NoiseTexture2D::set_invert); + ClassDB::bind_method(D_METHOD("get_invert"), &NoiseTexture2D::get_invert); + + ClassDB::bind_method(D_METHOD("set_in_3d_space", "enable"), &NoiseTexture2D::set_in_3d_space); + ClassDB::bind_method(D_METHOD("is_in_3d_space"), &NoiseTexture2D::is_in_3d_space); + + ClassDB::bind_method(D_METHOD("set_generate_mipmaps", "invert"), &NoiseTexture2D::set_generate_mipmaps); + ClassDB::bind_method(D_METHOD("is_generating_mipmaps"), &NoiseTexture2D::is_generating_mipmaps); + + ClassDB::bind_method(D_METHOD("set_seamless", "seamless"), &NoiseTexture2D::set_seamless); + ClassDB::bind_method(D_METHOD("get_seamless"), &NoiseTexture2D::get_seamless); + + ClassDB::bind_method(D_METHOD("set_seamless_blend_skirt", "seamless_blend_skirt"), &NoiseTexture2D::set_seamless_blend_skirt); + ClassDB::bind_method(D_METHOD("get_seamless_blend_skirt"), &NoiseTexture2D::get_seamless_blend_skirt); + + ClassDB::bind_method(D_METHOD("set_as_normal_map", "as_normal_map"), &NoiseTexture2D::set_as_normal_map); + ClassDB::bind_method(D_METHOD("is_normal_map"), &NoiseTexture2D::is_normal_map); + + ClassDB::bind_method(D_METHOD("set_bump_strength", "bump_strength"), &NoiseTexture2D::set_bump_strength); + ClassDB::bind_method(D_METHOD("get_bump_strength"), &NoiseTexture2D::get_bump_strength); + + ClassDB::bind_method(D_METHOD("set_color_ramp", "gradient"), &NoiseTexture2D::set_color_ramp); + ClassDB::bind_method(D_METHOD("get_color_ramp"), &NoiseTexture2D::get_color_ramp); + + ClassDB::bind_method(D_METHOD("set_noise", "noise"), &NoiseTexture2D::set_noise); + ClassDB::bind_method(D_METHOD("get_noise"), &NoiseTexture2D::get_noise); + + ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "1,2048,1,or_greater,suffix:px"), "set_width", "get_width"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "height", PROPERTY_HINT_RANGE, "1,2048,1,or_greater,suffix:px"), "set_height", "get_height"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "invert"), "set_invert", "get_invert"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "in_3d_space"), "set_in_3d_space", "is_in_3d_space"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "generate_mipmaps"), "set_generate_mipmaps", "is_generating_mipmaps"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "seamless"), "set_seamless", "get_seamless"); + ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "seamless_blend_skirt", PROPERTY_HINT_RANGE, "0.05,1,0.001"), "set_seamless_blend_skirt", "get_seamless_blend_skirt"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "as_normal_map"), "set_as_normal_map", "is_normal_map"); + ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bump_strength", PROPERTY_HINT_RANGE, "0,32,0.1,or_greater"), "set_bump_strength", "get_bump_strength"); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_ramp", PROPERTY_HINT_RESOURCE_TYPE, "Gradient"), "set_color_ramp", "get_color_ramp"); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "noise", PROPERTY_HINT_RESOURCE_TYPE, "Noise"), "set_noise", "get_noise"); +} + +void NoiseTexture2D::_validate_property(PropertyInfo &p_property) const { + if (p_property.name == "bump_strength") { + if (!as_normal_map) { + p_property.usage = PROPERTY_USAGE_NO_EDITOR; + } + } + + if (p_property.name == "seamless_blend_skirt") { + if (!seamless) { + p_property.usage = PROPERTY_USAGE_NO_EDITOR; + } + } +} + +void NoiseTexture2D::_set_texture_image(const Ref &p_image) { + image = p_image; + if (image.is_valid()) { + if (texture.is_valid()) { + RID new_texture = RS::get_singleton()->texture_2d_create(p_image); + RS::get_singleton()->texture_replace(texture, new_texture); + } else { + texture = RS::get_singleton()->texture_2d_create(p_image); + } + } + emit_changed(); +} + +void NoiseTexture2D::_thread_done(const Ref &p_image) { + _set_texture_image(p_image); + noise_thread.wait_to_finish(); + if (regen_queued) { + noise_thread.start(_thread_function, this); + regen_queued = false; + } +} + +void NoiseTexture2D::_thread_function(void *p_ud) { + NoiseTexture2D *tex = static_cast(p_ud); + tex->call_deferred(SNAME("_thread_done"), tex->_generate_texture()); +} + +void NoiseTexture2D::_queue_update() { + if (update_queued) { + return; + } + + update_queued = true; + call_deferred(SNAME("_update_texture")); +} + +Ref NoiseTexture2D::_generate_texture() { + // Prevent memdelete due to unref() on other thread. + Ref ref_noise = noise; + + if (ref_noise.is_null()) { + return Ref(); + } + + Ref image; + + if (seamless) { + image = ref_noise->get_seamless_image(size.x, size.y, invert, in_3d_space, seamless_blend_skirt); + } else { + image = ref_noise->get_image(size.x, size.y, invert, in_3d_space); + } + if (color_ramp.is_valid()) { + image = _modulate_with_gradient(image, color_ramp); + } + if (as_normal_map) { + image->bump_map_to_normal_map(bump_strength); + } + if (generate_mipmaps) { + image->generate_mipmaps(); + } + + return image; +} + +Ref NoiseTexture2D::_modulate_with_gradient(Ref p_image, Ref p_gradient) { + int width = p_image->get_width(); + int height = p_image->get_height(); + + Ref new_image; + new_image.instantiate(); + new_image->create(width, height, false, Image::FORMAT_RGBA8); + + for (int row = 0; row < height; row++) { + for (int col = 0; col < width; col++) { + Color pixel_color = p_image->get_pixel(col, row); + Color ramp_color = color_ramp->get_color_at_offset(pixel_color.get_luminance()); + new_image->set_pixel(col, row, ramp_color); + } + } + + return new_image; +} + +void NoiseTexture2D::_update_texture() { + bool use_thread = true; + if (first_time) { + use_thread = false; + first_time = false; + } +#ifdef NO_THREADS + use_thread = false; +#endif + if (use_thread) { + if (!noise_thread.is_started()) { + noise_thread.start(_thread_function, this); + regen_queued = false; + } else { + regen_queued = true; + } + + } else { + Ref image = _generate_texture(); + _set_texture_image(image); + } + update_queued = false; +} + +void NoiseTexture2D::set_noise(Ref p_noise) { + if (p_noise == noise) { + return; + } + if (noise.is_valid()) { + noise->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &NoiseTexture2D::_queue_update)); + } + noise = p_noise; + if (noise.is_valid()) { + noise->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &NoiseTexture2D::_queue_update)); + } + _queue_update(); +} + +Ref NoiseTexture2D::get_noise() { + return noise; +} + +void NoiseTexture2D::set_width(int p_width) { + ERR_FAIL_COND(p_width <= 0); + if (p_width == size.x) { + return; + } + size.x = p_width; + _queue_update(); +} + +void NoiseTexture2D::set_height(int p_height) { + ERR_FAIL_COND(p_height <= 0); + if (p_height == size.y) { + return; + } + size.y = p_height; + _queue_update(); +} + +void NoiseTexture2D::set_invert(bool p_invert) { + if (p_invert == invert) { + return; + } + invert = p_invert; + _queue_update(); +} + +bool NoiseTexture2D::get_invert() const { + return invert; +} + +void NoiseTexture2D::set_in_3d_space(bool p_enable) { + if (p_enable == in_3d_space) { + return; + } + in_3d_space = p_enable; + _queue_update(); +} +bool NoiseTexture2D::is_in_3d_space() const { + return in_3d_space; +} + +void NoiseTexture2D::set_generate_mipmaps(bool p_enable) { + if (p_enable == generate_mipmaps) { + return; + } + generate_mipmaps = p_enable; + _queue_update(); +} + +bool NoiseTexture2D::is_generating_mipmaps() const { + return generate_mipmaps; +} + +void NoiseTexture2D::set_seamless(bool p_seamless) { + if (p_seamless == seamless) { + return; + } + seamless = p_seamless; + _queue_update(); + notify_property_list_changed(); +} + +bool NoiseTexture2D::get_seamless() { + return seamless; +} + +void NoiseTexture2D::set_seamless_blend_skirt(real_t p_blend_skirt) { + ERR_FAIL_COND(p_blend_skirt < 0.05 || p_blend_skirt > 1); + + if (p_blend_skirt == seamless_blend_skirt) { + return; + } + seamless_blend_skirt = p_blend_skirt; + _queue_update(); +} +real_t NoiseTexture2D::get_seamless_blend_skirt() { + return seamless_blend_skirt; +} + +void NoiseTexture2D::set_as_normal_map(bool p_as_normal_map) { + if (p_as_normal_map == as_normal_map) { + return; + } + as_normal_map = p_as_normal_map; + _queue_update(); + notify_property_list_changed(); +} + +bool NoiseTexture2D::is_normal_map() { + return as_normal_map; +} + +void NoiseTexture2D::set_bump_strength(float p_bump_strength) { + if (p_bump_strength == bump_strength) { + return; + } + bump_strength = p_bump_strength; + if (as_normal_map) { + _queue_update(); + } +} + +float NoiseTexture2D::get_bump_strength() { + return bump_strength; +} + +void NoiseTexture2D::set_color_ramp(const Ref &p_gradient) { + if (p_gradient == color_ramp) { + return; + } + if (color_ramp.is_valid()) { + color_ramp->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &NoiseTexture2D::_queue_update)); + } + color_ramp = p_gradient; + if (color_ramp.is_valid()) { + color_ramp->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &NoiseTexture2D::_queue_update)); + } + _queue_update(); +} + +Ref NoiseTexture2D::get_color_ramp() const { + return color_ramp; +} + +int NoiseTexture2D::get_width() const { + return size.x; +} + +int NoiseTexture2D::get_height() const { + return size.y; +} + +RID NoiseTexture2D::get_rid() const { + if (!texture.is_valid()) { + texture = RS::get_singleton()->texture_2d_placeholder_create(); + } + + return texture; +} + +Ref NoiseTexture2D::get_image() const { + return image; +} diff --git a/modules/noise/noise_texture_2d.h b/modules/noise/noise_texture_2d.h new file mode 100644 index 0000000000..8f8e256fb9 --- /dev/null +++ b/modules/noise/noise_texture_2d.h @@ -0,0 +1,123 @@ +/*************************************************************************/ +/* noise_texture_2d.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef NOISE_TEXTURE_2D_H +#define NOISE_TEXTURE_2D_H + +#include "noise.h" + +#include "core/object/ref_counted.h" +#include "scene/resources/texture.h" + +class NoiseTexture2D : public Texture2D { + GDCLASS(NoiseTexture2D, Texture2D); + +private: + Ref image; + + Thread noise_thread; + + bool first_time = true; + bool update_queued = false; + bool regen_queued = false; + + mutable RID texture; + uint32_t flags = 0; + + Size2i size = Size2i(512, 512); + bool invert = false; + bool in_3d_space = false; + bool generate_mipmaps = true; + bool seamless = false; + real_t seamless_blend_skirt = 0.1; + bool as_normal_map = false; + float bump_strength = 8.0; + + Ref color_ramp; + Ref noise; + + void _thread_done(const Ref &p_image); + static void _thread_function(void *p_ud); + + void _queue_update(); + Ref _generate_texture(); + void _update_texture(); + void _set_texture_image(const Ref &p_image); + + Ref _modulate_with_gradient(Ref p_image, Ref p_gradient); + +protected: + static void _bind_methods(); + void _validate_property(PropertyInfo &p_property) const; + +public: + void set_noise(Ref p_noise); + Ref get_noise(); + + void set_width(int p_width); + void set_height(int p_height); + + void set_invert(bool p_invert); + bool get_invert() const; + + void set_in_3d_space(bool p_enable); + bool is_in_3d_space() const; + + void set_generate_mipmaps(bool p_enable); + bool is_generating_mipmaps() const; + + void set_seamless(bool p_seamless); + bool get_seamless(); + + void set_seamless_blend_skirt(real_t p_blend_skirt); + real_t get_seamless_blend_skirt(); + + void set_as_normal_map(bool p_as_normal_map); + bool is_normal_map(); + + void set_bump_strength(float p_bump_strength); + float get_bump_strength(); + + void set_color_ramp(const Ref &p_gradient); + Ref get_color_ramp() const; + + int get_width() const override; + int get_height() const override; + + virtual RID get_rid() const override; + virtual bool has_alpha() const override { return false; } + + virtual Ref get_image() const override; + + NoiseTexture2D(); + virtual ~NoiseTexture2D(); +}; + +#endif // NOISE_TEXTURE_2D_H diff --git a/modules/noise/register_types.cpp b/modules/noise/register_types.cpp index d0cfc4e944..c44bf9828f 100644 --- a/modules/noise/register_types.cpp +++ b/modules/noise/register_types.cpp @@ -32,7 +32,7 @@ #include "fastnoise_lite.h" #include "noise.h" -#include "noise_texture.h" +#include "noise_texture_2d.h" #ifdef TOOLS_ENABLED #include "editor/editor_plugin.h" @@ -41,9 +41,10 @@ void initialize_noise_module(ModuleInitializationLevel p_level) { if (p_level == MODULE_INITIALIZATION_LEVEL_SCENE) { - GDREGISTER_CLASS(NoiseTexture); + GDREGISTER_CLASS(NoiseTexture2D); GDREGISTER_ABSTRACT_CLASS(Noise); GDREGISTER_CLASS(FastNoiseLite); + ClassDB::add_compatibility_class("NoiseTexture", "NoiseTexture2D"); } #ifdef TOOLS_ENABLED -- cgit v1.2.3