diff --git a/modules/noise/config.py b/modules/noise/config.py
index 2318d28c533..e6d5e1a646f 100644
--- a/modules/noise/config.py
+++ b/modules/noise/config.py
@@ -11,6 +11,7 @@ def get_doc_classes():
"FastNoiseLite",
"Noise",
"NoiseTexture2D",
+ "NoiseTexture3D",
]
diff --git a/modules/noise/doc_classes/Noise.xml b/modules/noise/doc_classes/Noise.xml
index d214a02cf1d..a5cdb5bcbca 100644
--- a/modules/noise/doc_classes/Noise.xml
+++ b/modules/noise/doc_classes/Noise.xml
@@ -15,9 +15,10 @@
-
-
-
+
+
+
+
Returns a 2D [Image] noise image.
Note: With [param normalize] set to [code]false[/code] the default implementation expects the noise generator to return values in the range [code]-1.0[/code] to [code]1.0[/code].
@@ -65,10 +66,11 @@
-
-
-
-
+
+
+
+
+
Returns a seamless 2D [Image] noise image.
Note: With [param normalize] set to [code]false[/code] the default implementation expects the noise generator to return values in the range [code]-1.0[/code] to [code]1.0[/code].
diff --git a/modules/noise/doc_classes/NoiseTexture3D.xml b/modules/noise/doc_classes/NoiseTexture3D.xml
new file mode 100644
index 00000000000..0b385d9b9cd
--- /dev/null
+++ b/modules/noise/doc_classes/NoiseTexture3D.xml
@@ -0,0 +1,50 @@
+
+
+
+ A texture filled with noise generated by a [Noise] object.
+
+
+ Uses [FastNoiseLite] or other libraries to fill the texture data of your desired size.
+ The class uses [Thread]s to generate the texture data internally, so [method Texture3D.get_data] 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 = NoiseTexture3D.new()
+ texture.noise = FastNoiseLite.new()
+ await texture.changed
+ var data = texture.get_data()
+ var image = data[0]
+ [/codeblock]
+
+
+
+
+
+ A [Gradient] which is used to map the luminance of each pixel to a color value.
+
+
+ Depth of the generated texture.
+
+
+ Height of the generated texture.
+
+
+ If [code]true[/code], inverts the noise texture. White becomes black, black becomes white.
+
+
+ The instance of the [Noise] object.
+
+
+ If [code]true[/code], the noise image coming from the noise generator is normalized to the range [code]0.0[/code] to [code]1.0[/code].
+ Turning normalization off can affect the contrast and allows you to generate non repeating tileable noise textures.
+
+
+ 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/noise.cpp b/modules/noise/noise.cpp
index e95788b8635..b8c1587ec39 100644
--- a/modules/noise/noise.cpp
+++ b/modules/noise/noise.cpp
@@ -32,7 +32,7 @@
#include
-Ref Noise::get_seamless_image(int p_width, int p_height, bool p_invert, bool p_in_3d_space, real_t p_blend_skirt, bool p_normalize) const {
+Ref Noise::get_seamless_image(int p_width, int p_height, int p_depth, bool p_invert, bool p_in_3d_space, real_t p_blend_skirt, bool p_normalize) const {
ERR_FAIL_COND_V(p_width <= 0 || p_height <= 0, Ref());
int skirt_width = MAX(1, p_width * p_blend_skirt);
@@ -40,7 +40,7 @@ Ref Noise::get_seamless_image(int p_width, int p_height, bool p_invert, b
int src_width = p_width + skirt_width;
int src_height = p_height + skirt_height;
- Ref src = get_image(src_width, src_height, p_invert, p_in_3d_space, p_normalize);
+ Ref src = get_image(src_width, src_height, p_depth, p_invert, p_in_3d_space, p_normalize);
bool grayscale = (src->get_format() == Image::FORMAT_L8);
if (grayscale) {
return _generate_seamless_image(src, p_width, p_height, p_invert, p_blend_skirt);
@@ -58,7 +58,7 @@ uint8_t Noise::_alpha_blend(uint8_t p_bg, uint8_t p_fg, int p_alpha) co
return (uint8_t)((alpha * p_fg + inv_alpha * p_bg) >> 8);
}
-Ref Noise::get_image(int p_width, int p_height, bool p_invert, bool p_in_3d_space, bool p_normalize) const {
+Ref Noise::get_image(int p_width, int p_height, int p_depth, bool p_invert, bool p_in_3d_space, bool p_normalize) const {
ERR_FAIL_COND_V(p_width <= 0 || p_height <= 0, Ref());
Vector data;
@@ -74,7 +74,7 @@ Ref Noise::get_image(int p_width, int p_height, bool p_invert, bool p_in_
real_t max_val = -FLT_MAX;
for (int y = 0, i = 0; y < p_height; y++) {
for (int x = 0; x < p_width; x++, i++) {
- values.set(i, p_in_3d_space ? get_noise_3d(x, y, 0.0) : get_noise_2d(x, y));
+ values.set(i, p_in_3d_space ? get_noise_3d(x, y, p_depth) : get_noise_2d(x, y));
if (values[i] > max_val) {
max_val = values[i];
}
@@ -105,7 +105,7 @@ Ref Noise::get_image(int p_width, int p_height, bool p_invert, bool p_in_
uint8_t ivalue;
for (int y = 0, i = 0; y < p_height; y++) {
for (int x = 0; x < p_width; x++, i++) {
- float value = (p_in_3d_space ? get_noise_3d(x, y, 0.0) : get_noise_2d(x, y));
+ float value = (p_in_3d_space ? get_noise_3d(x, y, p_depth) : get_noise_2d(x, y));
ivalue = static_cast(CLAMP(value * 127.5f + 127.5f, 0.0f, 255.0f));
wd8[i] = p_invert ? (255 - ivalue) : ivalue;
}
@@ -124,6 +124,6 @@ void Noise::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_noise_3dv", "v"), &Noise::get_noise_3dv);
// Textures.
- ClassDB::bind_method(D_METHOD("get_image", "width", "height", "invert", "in_3d_space", "normalize"), &Noise::get_image, DEFVAL(false), DEFVAL(false), DEFVAL(true));
- ClassDB::bind_method(D_METHOD("get_seamless_image", "width", "height", "invert", "in_3d_space", "skirt", "normalize"), &Noise::get_seamless_image, DEFVAL(false), DEFVAL(false), DEFVAL(0.1), DEFVAL(true));
+ ClassDB::bind_method(D_METHOD("get_image", "width", "height", "depth", "invert", "in_3d_space", "normalize"), &Noise::get_image, DEFVAL(false), DEFVAL(false), DEFVAL(true));
+ ClassDB::bind_method(D_METHOD("get_seamless_image", "width", "height", "depth", "invert", "in_3d_space", "skirt", "normalize"), &Noise::get_seamless_image, DEFVAL(false), DEFVAL(false), DEFVAL(0.1), DEFVAL(true));
}
diff --git a/modules/noise/noise.h b/modules/noise/noise.h
index f7e615c2aab..856bef8c31f 100644
--- a/modules/noise/noise.h
+++ b/modules/noise/noise.h
@@ -233,8 +233,8 @@ public:
virtual real_t get_noise_3dv(Vector3 p_v) const = 0;
virtual real_t get_noise_3d(real_t p_x, real_t p_y, real_t p_z) const = 0;
- virtual Ref get_image(int p_width, int p_height, bool p_invert = false, bool p_in_3d_space = false, bool p_normalize = true) const;
- virtual Ref get_seamless_image(int p_width, int p_height, bool p_invert = false, bool p_in_3d_space = false, real_t p_blend_skirt = 0.1, bool p_normalize = true) const;
+ virtual Ref get_image(int p_width, int p_height, int p_depth, bool p_invert = false, bool p_in_3d_space = false, bool p_normalize = true) const;
+ virtual Ref get_seamless_image(int p_width, int p_height, int p_depth, bool p_invert = false, bool p_in_3d_space = false, real_t p_blend_skirt = 0.1, bool p_normalize = true) const;
};
#endif // NOISE_H
diff --git a/modules/noise/noise_texture_2d.cpp b/modules/noise/noise_texture_2d.cpp
index e4b2e0b4ac0..a31f77a38d9 100644
--- a/modules/noise/noise_texture_2d.cpp
+++ b/modules/noise/noise_texture_2d.cpp
@@ -162,9 +162,9 @@ Ref NoiseTexture2D::_generate_texture() {
Ref new_image;
if (seamless) {
- new_image = ref_noise->get_seamless_image(size.x, size.y, invert, in_3d_space, seamless_blend_skirt, normalize);
+ new_image = ref_noise->get_seamless_image(size.x, size.y, 0, invert, in_3d_space, seamless_blend_skirt, normalize);
} else {
- new_image = ref_noise->get_image(size.x, size.y, invert, in_3d_space, normalize);
+ new_image = ref_noise->get_image(size.x, size.y, 0, invert, in_3d_space, normalize);
}
if (color_ramp.is_valid()) {
new_image = _modulate_with_gradient(new_image, color_ramp);
diff --git a/modules/noise/noise_texture_3d.cpp b/modules/noise/noise_texture_3d.cpp
new file mode 100644
index 00000000000..ab0f52ffb0b
--- /dev/null
+++ b/modules/noise/noise_texture_3d.cpp
@@ -0,0 +1,469 @@
+/**************************************************************************/
+/* noise_texture_3d.cpp */
+/**************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/**************************************************************************/
+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
+/* */
+/* 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_3d.h"
+
+#include "core/core_string_names.h"
+#include "noise.h"
+
+NoiseTexture3D::NoiseTexture3D() {
+ noise = Ref();
+
+ _queue_update();
+}
+
+NoiseTexture3D::~NoiseTexture3D() {
+ ERR_FAIL_NULL(RenderingServer::get_singleton());
+ if (texture.is_valid()) {
+ RS::get_singleton()->free(texture);
+ }
+ noise_thread.wait_to_finish();
+}
+
+void NoiseTexture3D::_bind_methods() {
+ ClassDB::bind_method(D_METHOD("_update_texture"), &NoiseTexture3D::_update_texture);
+ ClassDB::bind_method(D_METHOD("_generate_texture"), &NoiseTexture3D::_generate_texture);
+ ClassDB::bind_method(D_METHOD("_thread_done", "image"), &NoiseTexture3D::_thread_done);
+
+ ClassDB::bind_method(D_METHOD("set_width", "width"), &NoiseTexture3D::set_width);
+ ClassDB::bind_method(D_METHOD("set_height", "height"), &NoiseTexture3D::set_height);
+ ClassDB::bind_method(D_METHOD("set_depth", "depth"), &NoiseTexture3D::set_depth);
+
+ ClassDB::bind_method(D_METHOD("set_invert", "invert"), &NoiseTexture3D::set_invert);
+ ClassDB::bind_method(D_METHOD("get_invert"), &NoiseTexture3D::get_invert);
+
+ ClassDB::bind_method(D_METHOD("set_seamless", "seamless"), &NoiseTexture3D::set_seamless);
+ ClassDB::bind_method(D_METHOD("get_seamless"), &NoiseTexture3D::get_seamless);
+
+ ClassDB::bind_method(D_METHOD("set_seamless_blend_skirt", "seamless_blend_skirt"), &NoiseTexture3D::set_seamless_blend_skirt);
+ ClassDB::bind_method(D_METHOD("get_seamless_blend_skirt"), &NoiseTexture3D::get_seamless_blend_skirt);
+
+ ClassDB::bind_method(D_METHOD("set_normalize", "normalize"), &NoiseTexture3D::set_normalize);
+ ClassDB::bind_method(D_METHOD("is_normalized"), &NoiseTexture3D::is_normalized);
+
+ ClassDB::bind_method(D_METHOD("set_color_ramp", "gradient"), &NoiseTexture3D::set_color_ramp);
+ ClassDB::bind_method(D_METHOD("get_color_ramp"), &NoiseTexture3D::get_color_ramp);
+
+ ClassDB::bind_method(D_METHOD("set_noise", "noise"), &NoiseTexture3D::set_noise);
+ ClassDB::bind_method(D_METHOD("get_noise"), &NoiseTexture3D::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::INT, "depth", PROPERTY_HINT_RANGE, "1,2048,1,or_greater,suffix:px"), "set_depth", "get_depth");
+ ADD_PROPERTY(PropertyInfo(Variant::BOOL, "invert"), "set_invert", "get_invert");
+ 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, "normalize"), "set_normalize", "is_normalized");
+ 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 NoiseTexture3D::_validate_property(PropertyInfo &p_property) const {
+ if (p_property.name == "seamless_blend_skirt") {
+ if (!seamless) {
+ p_property.usage = PROPERTY_USAGE_NO_EDITOR;
+ }
+ }
+}
+
+void NoiseTexture3D::_set_texture_data(const TypedArray &p_data) {
+ if (!p_data.is_empty()) {
+ Vector[> data;
+
+ data.resize(p_data.size());
+
+ for (int i = 0; i < data.size(); i++) {
+ data.write[i] = p_data[i];
+ }
+
+ if (texture.is_valid()) {
+ RID new_texture = RS::get_singleton()->texture_3d_create(data[0]->get_format(), data[0]->get_width(), data[0]->get_height(), data.size(), false, data);
+ RS::get_singleton()->texture_replace(texture, new_texture);
+ } else {
+ texture = RS::get_singleton()->texture_3d_create(data[0]->get_format(), data[0]->get_width(), data[0]->get_height(), data.size(), false, data);
+ }
+ }
+ emit_changed();
+}
+
+void NoiseTexture3D::_thread_done(const TypedArray &p_data) {
+ _set_texture_data(p_data);
+ noise_thread.wait_to_finish();
+ if (regen_queued) {
+ noise_thread.start(_thread_function, this);
+ regen_queued = false;
+ }
+}
+
+void NoiseTexture3D::_thread_function(void *p_ud) {
+ NoiseTexture3D *tex = static_cast(p_ud);
+ tex->call_deferred(SNAME("_thread_done"), tex->_generate_texture());
+}
+
+void NoiseTexture3D::_queue_update() {
+ if (update_queued) {
+ return;
+ }
+
+ update_queued = true;
+ call_deferred(SNAME("_update_texture"));
+}
+
+TypedArray NoiseTexture3D::_generate_texture() {
+ // Prevent memdelete due to unref() on other thread.
+ Ref ref_noise = noise;
+
+ if (ref_noise.is_null()) {
+ return TypedArray();
+ }
+
+ Vector][> images;
+
+ if (seamless) {
+ images = _get_seamless(width, height, depth, invert, seamless_blend_skirt);
+ } else {
+ images.resize(depth);
+
+ for (int i = 0; i < images.size(); i++) {
+ images.write[i] = ref_noise->get_image(width, height, i, invert, true, false);
+ }
+ }
+
+ // Normalize on whole texture at once rather than on each image individualy as it would result in visible artifacts on z (depth) axis.
+ if (normalize) {
+ images = _normalize(images);
+ }
+
+ if (color_ramp.is_valid()) {
+ for (int i = 0; i < images.size(); i++) {
+ images.write[i] = _modulate_with_gradient(images[i], color_ramp);
+ }
+ }
+
+ TypedArray new_data;
+ new_data.resize(images.size());
+
+ for (int i = 0; i < new_data.size(); i++) {
+ new_data[i] = images[i];
+ }
+
+ return new_data;
+}
+
+Vector][> NoiseTexture3D::_get_seamless(int p_width, int p_height, int p_depth, bool p_invert, real_t p_blend_skirt) {
+ // Prevent memdelete due to unref() on other thread.
+ Ref ref_noise = noise;
+
+ if (ref_noise.is_null()) {
+ return Vector][>();
+ }
+
+ int skirt_depth = MAX(1, p_depth * p_blend_skirt);
+ int src_depth = p_depth + skirt_depth;
+
+ Vector][> images;
+ images.resize(src_depth);
+
+ for (int i = 0; i < src_depth; i++) {
+ images.write[i] = ref_noise->get_seamless_image(p_width, p_height, i, p_invert, true, p_blend_skirt, false);
+ }
+
+ int half_depth = p_depth * 0.5;
+ int skirt_edge_z = half_depth + skirt_depth;
+
+ // swap halves on depth.
+ for (int i = 0; i < half_depth; i++) {
+ Ref img = images[i];
+ images.write[i] = images[i + half_depth];
+ images.write[i + half_depth] = img;
+ }
+
+ Vector][> new_images = images;
+ new_images.resize(p_depth);
+
+ // scale seamless generation to third dimension.
+ for (int z = half_depth; z < skirt_edge_z; z++) {
+ int alpha = 255 * (1 - Math::smoothstep(0.1f, 0.9f, float(z - half_depth) / float(skirt_depth)));
+
+ Vector img = images[z % p_depth]->get_data();
+ Vector skirt = images[(z - half_depth) + p_depth]->get_data();
+
+ Vector dest;
+ dest.resize(images[0]->get_width() * images[0]->get_height() * Image::get_format_pixel_size(images[0]->get_format()));
+
+ for (int i = 0; i < img.size(); i++) {
+ uint8_t fg, bg, out;
+
+ fg = skirt[i];
+ bg = img[i];
+
+ uint16_t a;
+ uint16_t inv_a;
+
+ a = alpha + 1;
+ inv_a = 256 - alpha;
+
+ out = (uint8_t)((a * fg + inv_a * bg) >> 8);
+
+ dest.write[i] = out;
+ }
+
+ Ref new_image = memnew(Image(images[0]->get_width(), images[0]->get_height(), false, images[0]->get_format(), dest));
+ new_images.write[z % p_depth] = new_image;
+ }
+
+ return new_images;
+}
+
+Vector][> NoiseTexture3D::_normalize(Vector][> p_images) {
+ real_t min_val = FLT_MAX;
+ real_t max_val = -FLT_MAX;
+
+ int w = p_images[0]->get_width();
+ int h = p_images[0]->get_height();
+
+ for (int i = 0; i < p_images.size(); i++) {
+ Vector data = p_images[i]->get_data();
+
+ for (int j = 0; j < data.size(); j++) {
+ if (data[j] > max_val) {
+ max_val = data[j];
+ }
+ if (data[j] < min_val) {
+ min_val = data[j];
+ }
+ }
+ }
+
+ Vector][> new_images;
+ new_images.resize(p_images.size());
+
+ for (int i = 0; i < p_images.size(); i++) {
+ Vector data = p_images[i]->get_data();
+
+ for (int j = 0; j < data.size(); j++) {
+ uint8_t value;
+
+ if (max_val == min_val) {
+ value = 0;
+ } else {
+ value = static_cast(CLAMP((data[j] - min_val) / (max_val - min_val) * 255.f, 0, 255));
+ }
+
+ data.write[j] = value;
+ }
+
+ Ref new_image = memnew(Image(w, h, false, Image::FORMAT_L8, data));
+ new_images.write[i] = new_image;
+ }
+
+ return new_images;
+}
+
+Ref NoiseTexture3D::_modulate_with_gradient(Ref p_image, Ref p_gradient) {
+ int w = p_image->get_width();
+ int h = p_image->get_height();
+
+ Ref new_image = Image::create_empty(w, h, false, Image::FORMAT_RGBA8);
+
+ for (int row = 0; row < h; row++) {
+ for (int col = 0; col < w; col++) {
+ Color pixel_color = p_image->get_pixel(col, row);
+ Color ramp_color = p_gradient->get_color_at_offset(pixel_color.get_luminance());
+ new_image->set_pixel(col, row, ramp_color);
+ }
+ }
+
+ return new_image;
+}
+
+void NoiseTexture3D::_update_texture() {
+ bool use_thread = true;
+ if (first_time) {
+ use_thread = false;
+ first_time = false;
+ }
+ if (use_thread) {
+ if (!noise_thread.is_started()) {
+ noise_thread.start(_thread_function, this);
+ regen_queued = false;
+ } else {
+ regen_queued = true;
+ }
+
+ } else {
+ TypedArray new_data = _generate_texture();
+ _set_texture_data(new_data);
+ }
+ update_queued = false;
+}
+
+void NoiseTexture3D::set_noise(Ref p_noise) {
+ if (p_noise == noise) {
+ return;
+ }
+ if (noise.is_valid()) {
+ noise->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &NoiseTexture3D::_queue_update));
+ }
+ noise = p_noise;
+ if (noise.is_valid()) {
+ noise->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &NoiseTexture3D::_queue_update));
+ }
+ _queue_update();
+}
+
+Ref NoiseTexture3D::get_noise() {
+ return noise;
+}
+
+void NoiseTexture3D::set_width(int p_width) {
+ ERR_FAIL_COND(p_width <= 0);
+ if (p_width == width) {
+ return;
+ }
+ width = p_width;
+ _queue_update();
+}
+
+void NoiseTexture3D::set_height(int p_height) {
+ ERR_FAIL_COND(p_height <= 0);
+ if (p_height == height) {
+ return;
+ }
+ height = p_height;
+ _queue_update();
+}
+
+void NoiseTexture3D::set_depth(int p_depth) {
+ ERR_FAIL_COND(p_depth <= 0);
+ if (p_depth == depth) {
+ return;
+ }
+ depth = p_depth;
+ _queue_update();
+}
+
+void NoiseTexture3D::set_invert(bool p_invert) {
+ if (p_invert == invert) {
+ return;
+ }
+ invert = p_invert;
+ _queue_update();
+}
+
+bool NoiseTexture3D::get_invert() const {
+ return invert;
+}
+
+void NoiseTexture3D::set_seamless(bool p_seamless) {
+ if (p_seamless == seamless) {
+ return;
+ }
+ seamless = p_seamless;
+ _queue_update();
+ notify_property_list_changed();
+}
+
+bool NoiseTexture3D::get_seamless() {
+ return seamless;
+}
+
+void NoiseTexture3D::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 NoiseTexture3D::get_seamless_blend_skirt() {
+ return seamless_blend_skirt;
+}
+
+void NoiseTexture3D::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, &NoiseTexture3D::_queue_update));
+ }
+ color_ramp = p_gradient;
+ if (color_ramp.is_valid()) {
+ color_ramp->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &NoiseTexture3D::_queue_update));
+ }
+ _queue_update();
+}
+
+void NoiseTexture3D::set_normalize(bool p_normalize) {
+ if (normalize == p_normalize) {
+ return;
+ }
+ normalize = p_normalize;
+ _queue_update();
+}
+
+bool NoiseTexture3D::is_normalized() const {
+ return normalize;
+}
+
+Ref NoiseTexture3D::get_color_ramp() const {
+ return color_ramp;
+}
+
+int NoiseTexture3D::get_width() const {
+ return width;
+}
+
+int NoiseTexture3D::get_height() const {
+ return height;
+}
+
+int NoiseTexture3D::get_depth() const {
+ return depth;
+}
+
+RID NoiseTexture3D::get_rid() const {
+ if (!texture.is_valid()) {
+ texture = RS::get_singleton()->texture_3d_placeholder_create();
+ }
+
+ return texture;
+}
+
+Vector][> NoiseTexture3D::get_data() const {
+ ERR_FAIL_COND_V(!texture.is_valid(), Vector][>());
+ return RS::get_singleton()->texture_3d_get(texture);
+}
+
+Image::Format NoiseTexture3D::get_format() const {
+ ERR_FAIL_COND_V(!texture.is_valid(), Image::FORMAT_L8);
+ return RS::get_singleton()->texture_3d_get(texture)[0]->get_format();
+}
diff --git a/modules/noise/noise_texture_3d.h b/modules/noise/noise_texture_3d.h
new file mode 100644
index 00000000000..b5dab10321f
--- /dev/null
+++ b/modules/noise/noise_texture_3d.h
@@ -0,0 +1,115 @@
+/**************************************************************************/
+/* noise_texture_3d.h */
+/**************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/**************************************************************************/
+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
+/* */
+/* 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_3D_H
+#define NOISE_TEXTURE_3D_H
+
+#include "noise.h"
+
+#include "core/object/ref_counted.h"
+#include "scene/resources/texture.h"
+
+class NoiseTexture3D : public Texture3D {
+ GDCLASS(NoiseTexture3D, Texture3D);
+
+private:
+ Thread noise_thread;
+
+ bool first_time = true;
+ bool update_queued = false;
+ bool regen_queued = false;
+
+ mutable RID texture;
+ uint32_t flags = 0;
+
+ int width = 64;
+ int height = 64;
+ int depth = 64;
+ bool invert = false;
+ bool seamless = false;
+ real_t seamless_blend_skirt = 0.1;
+ bool normalize = true;
+
+ Ref color_ramp;
+ Ref noise;
+
+ void _thread_done(const TypedArray &p_data);
+ static void _thread_function(void *p_ud);
+
+ void _queue_update();
+ TypedArray _generate_texture();
+ void _update_texture();
+ void _set_texture_data(const TypedArray &p_data);
+
+ Vector][> _get_seamless(int p_width, int p_height, int p_depth, bool p_invert, real_t p_blend_skirt);
+ Vector][> _normalize(Vector][> p_images);
+ 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_depth(int p_depth);
+
+ void set_invert(bool p_invert);
+ bool get_invert() 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_normalize(bool p_normalize);
+ bool is_normalized() const;
+
+ void set_color_ramp(const Ref &p_gradient);
+ Ref get_color_ramp() const;
+
+ virtual int get_width() const override;
+ virtual int get_height() const override;
+ virtual int get_depth() const override;
+
+ virtual RID get_rid() const override;
+
+ virtual Vector][> get_data() const override;
+ virtual Image::Format get_format() const override;
+
+ NoiseTexture3D();
+ virtual ~NoiseTexture3D();
+};
+
+#endif // NOISE_TEXTURE_3D_H
diff --git a/modules/noise/register_types.cpp b/modules/noise/register_types.cpp
index 1298d72e988..f48d4e7e4d9 100644
--- a/modules/noise/register_types.cpp
+++ b/modules/noise/register_types.cpp
@@ -33,6 +33,7 @@
#include "fastnoise_lite.h"
#include "noise.h"
#include "noise_texture_2d.h"
+#include "noise_texture_3d.h"
#ifdef TOOLS_ENABLED
#include "editor/editor_plugin.h"
@@ -41,6 +42,7 @@
void initialize_noise_module(ModuleInitializationLevel p_level) {
if (p_level == MODULE_INITIALIZATION_LEVEL_SCENE) {
+ GDREGISTER_CLASS(NoiseTexture3D);
GDREGISTER_CLASS(NoiseTexture2D);
GDREGISTER_ABSTRACT_CLASS(Noise);
GDREGISTER_CLASS(FastNoiseLite);
diff --git a/modules/noise/tests/test_fastnoise_lite.h b/modules/noise/tests/test_fastnoise_lite.h
index 0a435c6a5c7..db489c66720 100644
--- a/modules/noise/tests/test_fastnoise_lite.h
+++ b/modules/noise/tests/test_fastnoise_lite.h
@@ -605,7 +605,7 @@ TEST_CASE("[FastNoiseLite] Generating seamless 2D images (11x11px) and compare t
noise.set_cellular_jitter(0.0);
SUBCASE("Blend skirt 0.0") {
- Ref img = noise.get_seamless_image(11, 11, false, false, 0.0);
+ Ref img = noise.get_seamless_image(11, 11, 0, false, false, 0.0);
Ref ref_img_1 = memnew(Image);
ref_img_1->set_data(11, 11, false, Image::FORMAT_L8, ref_img_1_data);
@@ -614,7 +614,7 @@ TEST_CASE("[FastNoiseLite] Generating seamless 2D images (11x11px) and compare t
}
SUBCASE("Blend skirt 0.1") {
- Ref img = noise.get_seamless_image(11, 11, false, false, 0.1);
+ Ref img = noise.get_seamless_image(11, 11, 0, false, false, 0.1);
Ref ref_img_2 = memnew(Image);
ref_img_2->set_data(11, 11, false, Image::FORMAT_L8, ref_img_2_data);
@@ -623,7 +623,7 @@ TEST_CASE("[FastNoiseLite] Generating seamless 2D images (11x11px) and compare t
}
SUBCASE("Blend skirt 1.0") {
- Ref img = noise.get_seamless_image(11, 11, false, false, 0.1);
+ Ref img = noise.get_seamless_image(11, 11, 0, false, false, 0.1);
Ref ref_img_3 = memnew(Image);
ref_img_3->set_data(11, 11, false, Image::FORMAT_L8, ref_img_3_data);
diff --git a/modules/noise/tests/test_noise_texture_3d.h b/modules/noise/tests/test_noise_texture_3d.h
new file mode 100644
index 00000000000..b46d386296b
--- /dev/null
+++ b/modules/noise/tests/test_noise_texture_3d.h
@@ -0,0 +1,235 @@
+/**************************************************************************/
+/* test_noise_texture_3d.h */
+/**************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/**************************************************************************/
+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
+/* */
+/* 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 TEST_NOISE_TEXTURE_3D_H
+#define TEST_NOISE_TEXTURE_3D_H
+
+#include "tests/test_macros.h"
+
+#include "modules/noise/noise_texture_3d.h"
+
+namespace TestNoiseTexture3D {
+
+class NoiseTexture3DTester : public RefCounted {
+ GDCLASS(NoiseTexture3DTester, RefCounted);
+
+ const NoiseTexture3D *const texture;
+
+public:
+ NoiseTexture3DTester(const NoiseTexture3D *const p_texture) :
+ texture{ p_texture } {};
+
+ Color compute_average_color(const Ref &p_noise_image) {
+ Color r_avg_color{};
+
+ for (int i = 0; i < p_noise_image->get_width(); ++i) {
+ for (int j = 0; j < p_noise_image->get_height(); ++j) {
+ const Color pixel = p_noise_image->get_pixel(i, j);
+ r_avg_color += pixel;
+ }
+ }
+
+ int pixel_count = p_noise_image->get_width() * p_noise_image->get_height();
+ r_avg_color /= pixel_count;
+ return r_avg_color;
+ }
+
+ void check_mip_and_color_ramp() {
+ const Vector][> noise_data = texture->get_data();
+
+ for (int i = 0; i < noise_data.size(); i++) {
+ const Ref noise_image = noise_data[i];
+
+ CHECK(noise_image.is_valid());
+ CHECK(noise_image->get_width() == texture->get_width());
+ CHECK(noise_image->get_height() == texture->get_height());
+
+ CHECK(noise_image->get_format() == Image::FORMAT_RGBA8);
+ CHECK(!noise_image->has_mipmaps());
+
+ Color avg_color = compute_average_color(noise_image);
+
+ // Check that the noise texture is modulated correctly by the color ramp (Gradient).
+ CHECK_FALSE_MESSAGE((avg_color.r + avg_color.g + avg_color.b) == doctest::Approx(0.0), "The noise texture should not be all black");
+ CHECK_FALSE_MESSAGE((avg_color.r + avg_color.g + avg_color.b) == doctest::Approx(noise_image->get_width() * noise_image->get_height() * 3.0), "The noise texture should not be all white");
+ CHECK_MESSAGE(avg_color.g == doctest::Approx(0.0), "The noise texture should not have any green when modulated correctly by the color ramp");
+ }
+ }
+
+ void check_seamless_texture_grayscale() {
+ const Vector][> noise_data = texture->get_data();
+
+ for (int i = 0; i < noise_data.size(); i++) {
+ const Ref noise_image = noise_data[i];
+
+ CHECK(noise_image.is_valid());
+ CHECK(noise_image->get_width() == texture->get_width());
+ CHECK(noise_image->get_height() == texture->get_height());
+
+ CHECK(noise_image->get_format() == Image::FORMAT_L8);
+
+ Color avg_color = compute_average_color(noise_image);
+
+ // Since it's a grayscale image and every channel except the alpha channel has the
+ // same values (conversion happens in Image::get_pixel) we only need to test one channel.
+ CHECK(avg_color.r == doctest::Approx(0.5).epsilon(0.05));
+ }
+ }
+
+ void check_seamless_texture_rgba() {
+ const Vector][> noise_data = texture->get_data();
+
+ for (int i = 0; i < noise_data.size(); i++) {
+ const Ref noise_image = noise_data[i];
+
+ CHECK(noise_image.is_valid());
+ CHECK(noise_image->get_width() == texture->get_width());
+ CHECK(noise_image->get_height() == texture->get_height());
+
+ CHECK(noise_image->get_format() == Image::FORMAT_RGBA8);
+
+ // Check that the noise texture is modulated correctly by the color ramp (Gradient).
+ Color avg_color = compute_average_color(noise_image);
+
+ // We use a default (black to white) gradient, so the average of the red, green and blue channels should be the same.
+ CHECK(avg_color.r == doctest::Approx(0.5).epsilon(0.05));
+ CHECK(avg_color.g == doctest::Approx(0.5).epsilon(0.05));
+ CHECK(avg_color.b == doctest::Approx(0.5).epsilon(0.05));
+ }
+ }
+};
+
+TEST_CASE("[NoiseTexture][SceneTree] Getter and setter") {
+ Ref noise_texture = memnew(NoiseTexture3D);
+
+ Ref noise = memnew(FastNoiseLite);
+ noise_texture->set_noise(noise);
+ CHECK(noise_texture->get_noise() == noise);
+ noise_texture->set_noise(nullptr);
+ CHECK(noise_texture->get_noise() == nullptr);
+
+ noise_texture->set_width(8);
+ noise_texture->set_height(4);
+ noise_texture->set_depth(2);
+ CHECK(noise_texture->get_width() == 8);
+ CHECK(noise_texture->get_height() == 4);
+ CHECK(noise_texture->get_depth() == 2);
+
+ ERR_PRINT_OFF;
+ noise_texture->set_width(-1);
+ noise_texture->set_height(-1);
+ noise_texture->set_depth(-1);
+ ERR_PRINT_ON;
+ CHECK(noise_texture->get_width() == 8);
+ CHECK(noise_texture->get_height() == 4);
+ CHECK(noise_texture->get_depth() == 2);
+
+ noise_texture->set_invert(true);
+ CHECK(noise_texture->get_invert() == true);
+ noise_texture->set_invert(false);
+ CHECK(noise_texture->get_invert() == false);
+
+ noise_texture->set_seamless(true);
+ CHECK(noise_texture->get_seamless() == true);
+ noise_texture->set_seamless(false);
+ CHECK(noise_texture->get_seamless() == false);
+
+ noise_texture->set_seamless_blend_skirt(0.45);
+ CHECK(noise_texture->get_seamless_blend_skirt() == doctest::Approx(0.45));
+
+ ERR_PRINT_OFF;
+ noise_texture->set_seamless_blend_skirt(-1.0);
+ noise_texture->set_seamless_blend_skirt(2.0);
+ CHECK(noise_texture->get_seamless_blend_skirt() == doctest::Approx(0.45));
+ ERR_PRINT_ON;
+
+ Ref gradient = memnew(Gradient);
+ noise_texture->set_color_ramp(gradient);
+ CHECK(noise_texture->get_color_ramp() == gradient);
+ noise_texture->set_color_ramp(nullptr);
+ CHECK(noise_texture->get_color_ramp() == nullptr);
+}
+
+TEST_CASE("[NoiseTexture3D][SceneTree] Generating a basic noise texture with mipmaps and color ramp modulation") {
+ Ref noise_texture = memnew(NoiseTexture3D);
+
+ Ref noise = memnew(FastNoiseLite);
+ noise_texture->set_noise(noise);
+
+ Ref gradient = memnew(Gradient);
+ Vector points;
+ points.push_back({ 0.0, Color(1, 0, 0) });
+ points.push_back({ 1.0, Color(0, 0, 1) });
+ gradient->set_points(points);
+ noise_texture->set_color_ramp(gradient);
+ noise_texture->set_width(16);
+ noise_texture->set_height(16);
+ noise_texture->set_depth(16);
+
+ Ref tester = memnew(NoiseTexture3DTester(noise_texture.ptr()));
+ noise_texture->connect("changed", callable_mp(tester.ptr(), &NoiseTexture3DTester::check_mip_and_color_ramp));
+ MessageQueue::get_singleton()->flush();
+}
+
+TEST_CASE("[NoiseTexture3D][SceneTree] Generating a seamless noise texture") {
+ Ref noise_texture = memnew(NoiseTexture3D);
+
+ Ref noise = memnew(FastNoiseLite);
+ noise->set_frequency(0.5);
+ noise_texture->set_noise(noise);
+ noise_texture->set_width(16);
+ noise_texture->set_height(16);
+ noise_texture->set_depth(16);
+ noise_texture->set_seamless(true);
+
+ Ref tester = memnew(NoiseTexture3DTester(noise_texture.ptr()));
+
+ SUBCASE("Grayscale(L8) 16x16x16, with seamless blend skirt of 0.05") {
+ noise_texture->set_seamless_blend_skirt(0.05);
+ noise_texture->connect("changed", callable_mp(tester.ptr(), &NoiseTexture3DTester::check_seamless_texture_grayscale));
+ MessageQueue::get_singleton()->flush();
+ }
+
+ SUBCASE("16x16x16 modulated with default (transparent)black and white gradient (RGBA8), with seamless blend skirt of 1.0") {
+ Ref gradient = memnew(Gradient);
+ Vector points;
+ points.push_back({ 0.0, Color(0, 0, 0, 0) });
+ points.push_back({ 1.0, Color(1, 1, 1, 1) });
+ gradient->set_points(points);
+ noise_texture->set_color_ramp(gradient);
+ noise_texture->set_seamless_blend_skirt(1.0);
+ noise_texture->connect("changed", callable_mp(tester.ptr(), &NoiseTexture3DTester::check_seamless_texture_rgba));
+ MessageQueue::get_singleton()->flush();
+ }
+}
+
+} //namespace TestNoiseTexture3D
+
+#endif // TEST_NOISE_TEXTURE_3D_H
]