From ccff24597f821bd6c3f4224e852b807766acf412 Mon Sep 17 00:00:00 2001 From: clayjohn Date: Fri, 28 Apr 2023 13:19:42 -0700 Subject: [PATCH] Fix compatibility breakage from adding NoiseTexture3D Also optimize some of the Noise methods --- modules/noise/doc_classes/Noise.xml | 41 +++- modules/noise/doc_classes/NoiseTexture3D.xml | 3 +- modules/noise/noise.cpp | 150 ++++++++++---- modules/noise/noise.h | 202 ++++++++++++------- modules/noise/noise_texture_2d.cpp | 4 +- modules/noise/noise_texture_3d.cpp | 127 +----------- modules/noise/noise_texture_3d.h | 2 - modules/noise/tests/test_fastnoise_lite.h | 6 +- 8 files changed, 282 insertions(+), 253 deletions(-) diff --git a/modules/noise/doc_classes/Noise.xml b/modules/noise/doc_classes/Noise.xml index a5cdb5bcbca6..c075b5b62916 100644 --- a/modules/noise/doc_classes/Noise.xml +++ b/modules/noise/doc_classes/Noise.xml @@ -15,13 +15,24 @@ + + + + + Returns an [Image] containing 2D noise values. + [b]Note:[/b] 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]. + + + + + + - - + - 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]. + Returns an [Array] of [Image]s containing 3D noise values for use with [method ImageTexture3D.create]. + [b]Note:[/b] 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]. @@ -66,14 +77,26 @@ + + + + + + Returns an [Image] containing seamless 2D noise values. + [b]Note:[/b] 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]. + + + + + + - - - + + - 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]. + Returns an [Array] of [Image]s containing seamless 3D noise values for use with [method ImageTexture3D.create]. + [b]Note:[/b] 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 index 0b385d9b9cdf..7394e7ff08ea 100644 --- a/modules/noise/doc_classes/NoiseTexture3D.xml +++ b/modules/noise/doc_classes/NoiseTexture3D.xml @@ -5,13 +5,12 @@ 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: + 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: [codeblock] var texture = NoiseTexture3D.new() texture.noise = FastNoiseLite.new() await texture.changed var data = texture.get_data() - var image = data[0] [/codeblock] diff --git a/modules/noise/noise.cpp b/modules/noise/noise.cpp index b8c1587ec398..1115d92f588c 100644 --- a/modules/noise/noise.cpp +++ b/modules/noise/noise.cpp @@ -32,23 +32,42 @@ #include -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()); +Vector> 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 || p_depth <= 0, Vector>()); int skirt_width = MAX(1, p_width * p_blend_skirt); int skirt_height = MAX(1, p_height * p_blend_skirt); + int skirt_depth = MAX(1, p_depth * p_blend_skirt); int src_width = p_width + skirt_width; int src_height = p_height + skirt_height; + int src_depth = p_depth + skirt_depth; + + Vector> src = _get_image(src_width, src_height, src_depth, p_invert, p_in_3d_space, p_normalize); + bool grayscale = (src[0]->get_format() == Image::FORMAT_L8); - 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); + return _generate_seamless_image(src, p_width, p_height, p_depth, p_invert, p_blend_skirt); } else { - return _generate_seamless_image(src, p_width, p_height, p_invert, p_blend_skirt); + return _generate_seamless_image(src, p_width, p_height, p_depth, p_invert, p_blend_skirt); } } +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 { + Vector> images = _get_seamless_image(p_width, p_height, 1, p_invert, p_in_3d_space, p_blend_skirt, p_normalize); + return images[0]; +} + +TypedArray Noise::get_seamless_image_3d(int p_width, int p_height, int p_depth, bool p_invert, real_t p_blend_skirt, bool p_normalize) const { + Vector> images = _get_seamless_image(p_width, p_height, p_depth, p_invert, true, p_blend_skirt, p_normalize); + + TypedArray ret; + ret.resize(images.size()); + for (int i = 0; i < images.size(); i++) { + ret[i] = images[i]; + } + return ret; +} + // Template specialization for faster grayscale blending. template <> uint8_t Noise::_alpha_blend(uint8_t p_bg, uint8_t p_fg, int p_alpha) const { @@ -58,61 +77,104 @@ 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, 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> 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 || p_depth <= 0, Vector>()); - Vector data; - data.resize(p_width * p_height); - - uint8_t *wd8 = data.ptrw(); + Vector> images; + images.resize(p_depth); if (p_normalize) { // Get all values and identify min/max values. - Vector values; - values.resize(p_width * p_height); + LocalVector values; + values.resize(p_width * p_height * p_depth); + real_t min_val = FLT_MAX; 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, p_depth) : get_noise_2d(x, y)); - if (values[i] > max_val) { - max_val = values[i]; - } - if (values[i] < min_val) { - min_val = values[i]; + int idx = 0; + for (int d = 0; d < p_depth; d++) { + for (int y = 0; y < p_height; y++) { + for (int x = 0; x < p_width; x++) { + values[idx] = p_in_3d_space ? get_noise_3d(x, y, d) : get_noise_2d(x, y); + if (values[idx] > max_val) { + max_val = values[idx]; + } + if (values[idx] < min_val) { + min_val = values[idx]; + } + idx++; } } } + idx = 0; // Normalize values and write to texture. - uint8_t ivalue; - for (int i = 0, x = 0; i < p_height; i++) { - for (int j = 0; j < p_width; j++, x++) { - if (max_val == min_val) { - ivalue = 0; - } else { - ivalue = static_cast(CLAMP((values[x] - min_val) / (max_val - min_val) * 255.f, 0, 255)); - } + for (int d = 0; d < p_depth; d++) { + Vector data; + data.resize(p_width * p_height); - if (p_invert) { - ivalue = 255 - ivalue; - } + uint8_t *wd8 = data.ptrw(); + uint8_t ivalue; - wd8[x] = ivalue; + for (int y = 0; y < p_height; y++) { + for (int x = 0; x < p_width; x++) { + if (max_val == min_val) { + ivalue = 0; + } else { + ivalue = static_cast(CLAMP((values[idx] - min_val) / (max_val - min_val) * 255.f, 0, 255)); + } + + if (p_invert) { + ivalue = 255 - ivalue; + } + + wd8[x + y * p_width] = ivalue; + idx++; + } } + Ref img = memnew(Image(p_width, p_height, false, Image::FORMAT_L8, data)); + images.write[d] = img; } } else { // Without normalization, the expected range of the noise function is [-1, 1]. - 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, 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; + + for (int d = 0; d < p_depth; d++) { + Vector data; + data.resize(p_width * p_height); + + uint8_t *wd8 = data.ptrw(); + + uint8_t ivalue; + int idx = 0; + for (int y = 0; y < p_height; y++) { + for (int x = 0; x < p_width; x++) { + float value = (p_in_3d_space ? get_noise_3d(x, y, d) : get_noise_2d(x, y)); + ivalue = static_cast(CLAMP(value * 127.5f + 127.5f, 0.0f, 255.0f)); + wd8[idx] = p_invert ? (255 - ivalue) : ivalue; + idx++; + } } + + Ref img = memnew(Image(p_width, p_height, false, Image::FORMAT_L8, data)); + images.write[d] = img; } } - return memnew(Image(p_width, p_height, false, Image::FORMAT_L8, data)); + return images; +} + +Ref Noise::get_image(int p_width, int p_height, bool p_invert, bool p_in_3d_space, bool p_normalize) const { + Vector> images = _get_image(p_width, p_height, 1, p_invert, p_in_3d_space, p_normalize); + return images[0]; +} + +TypedArray Noise::get_image_3d(int p_width, int p_height, int p_depth, bool p_invert, bool p_normalize) const { + Vector> images = _get_image(p_width, p_height, p_depth, p_invert, true, p_normalize); + + TypedArray ret; + ret.resize(images.size()); + for (int i = 0; i < images.size(); i++) { + ret[i] = images[i]; + } + return ret; } void Noise::_bind_methods() { @@ -124,6 +186,8 @@ 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", "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)); + 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_3d", "width", "height", "depth", "invert", "normalize"), &Noise::get_image_3d, DEFVAL(false), DEFVAL(true)); + ClassDB::bind_method(D_METHOD("get_seamless_image_3d", "width", "height", "depth", "invert", "skirt", "normalize"), &Noise::get_seamless_image_3d, DEFVAL(false), DEFVAL(0.1), DEFVAL(true)); } diff --git a/modules/noise/noise.h b/modules/noise/noise.h index 856bef8c31fb..6c49c12bc227 100644 --- a/modules/noise/noise.h +++ b/modules/noise/noise.h @@ -32,6 +32,7 @@ #define NOISE_H #include "core/io/image.h" +#include "core/variant/typed_array.h" class Noise : public Resource { GDCLASS(Noise, Resource); @@ -81,7 +82,7 @@ class Noise : public Resource { }; template - Ref _generate_seamless_image(Ref p_src, int p_width, int p_height, bool p_invert, real_t p_blend_skirt) const { + Vector> _generate_seamless_image(Vector> p_src, int p_width, int p_height, int p_depth, bool p_invert, real_t p_blend_skirt) const { /* To make a seamless image, we swap the quadrants so the edges are perfect matches. We initially get a 10% larger image so we have an overlap we can use to blend over the seams. @@ -101,7 +102,7 @@ class Noise : public Resource { on Source it's translated to corner of Q1/s3 unless the ALT_XY modulo moves it to Q4 */ - ERR_FAIL_COND_V(p_blend_skirt < 0, Ref()); + ERR_FAIL_COND_V(p_blend_skirt < 0, Vector>()); int skirt_width = MAX(1, p_width * p_blend_skirt); int skirt_height = MAX(1, p_height * p_blend_skirt); @@ -112,83 +113,139 @@ class Noise : public Resource { int skirt_edge_x = half_width + skirt_width; int skirt_edge_y = half_height + skirt_height; - Vector dest; - dest.resize(p_width * p_height * Image::get_format_pixel_size(p_src->get_format())); + Image::Format format = p_src[0]->get_format(); + int pixel_size = Image::get_format_pixel_size(format); - img_buff rd_src = { - (T *)p_src->get_data().ptr(), - src_width, src_height, - half_width, half_height, - p_width, p_height - }; + Vector> images; + images.resize(p_src.size()); - // `wr` is setup for straight x/y coordinate array access. - img_buff wr = { - (T *)dest.ptrw(), - p_width, p_height, - 0, 0, 0, 0 - }; - // `rd_dest` is a readable pointer to `wr`, i.e. what has already been written to the output buffer. - img_buff rd_dest = { - (T *)dest.ptr(), - p_width, p_height, - 0, 0, 0, 0 - }; + // First blend across x and y for all slices. + for (int d = 0; d < images.size(); d++) { + Vector dest; + dest.resize(p_width * p_height * pixel_size); - // Swap the quadrants to make edges seamless. - for (int y = 0; y < p_height; y++) { - for (int x = 0; x < p_width; x++) { - // rd_src has a half offset and the shorter modulo ignores the skirt. - // It reads and writes in Q1-4 order (see map above), skipping the skirt. - wr(x, y) = rd_src(x, y, img_buff::ALT_XY); - } - } + img_buff rd_src = { + (T *)p_src[d]->get_data().ptr(), + src_width, src_height, + half_width, half_height, + p_width, p_height + }; - // Blend the vertical skirt over the middle seam. - for (int x = half_width; x < skirt_edge_x; x++) { - int alpha = 255 * (1 - Math::smoothstep(0.1f, 0.9f, float(x - half_width) / float(skirt_width))); + // `wr` is setup for straight x/y coordinate array access. + img_buff wr = { + (T *)dest.ptrw(), + p_width, p_height, + 0, 0, 0, 0 + }; + // `rd_dest` is a readable pointer to `wr`, i.e. what has already been written to the output buffer. + img_buff rd_dest = { + (T *)dest.ptr(), + p_width, p_height, + 0, 0, 0, 0 + }; + + // Swap the quadrants to make edges seamless. for (int y = 0; y < p_height; y++) { - // Skip the center square - if (y == half_height) { - y = skirt_edge_y - 1; - } else { - // Starts reading at s2, ALT_Y skips s3, and continues with s1. - wr(x, y) = _alpha_blend(rd_dest(x, y), rd_src(x, y, img_buff::ALT_Y), alpha); + for (int x = 0; x < p_width; x++) { + // rd_src has a half offset and the shorter modulo ignores the skirt. + // It reads and writes in Q1-4 order (see map above), skipping the skirt. + wr(x, y) = rd_src(x, y, img_buff::ALT_XY); } } - } - // Blend the horizontal skirt over the middle seam. - for (int y = half_height; y < skirt_edge_y; y++) { - int alpha = 255 * (1 - Math::smoothstep(0.1f, 0.9f, float(y - half_height) / float(skirt_height))); - for (int x = 0; x < p_width; x++) { - // Skip the center square - if (x == half_width) { - x = skirt_edge_x - 1; - } else { - // Starts reading at s4, skips s3, continues with s5. - wr(x, y) = _alpha_blend(rd_dest(x, y), rd_src(x, y, img_buff::ALT_X), alpha); - } - } - } - - // Fill in the center square. Wr starts at the top left of Q4, which is the equivalent of the top left of s3, unless a modulo is used. - for (int y = half_height; y < skirt_edge_y; y++) { + // Blend the vertical skirt over the middle seam. for (int x = half_width; x < skirt_edge_x; x++) { - int xpos = 255 * (1 - Math::smoothstep(0.1f, 0.9f, float(x - half_width) / float(skirt_width))); - int ypos = 255 * (1 - Math::smoothstep(0.1f, 0.9f, float(y - half_height) / float(skirt_height))); - - // Blend s3(Q1) onto s5(Q2) for the top half. - T top_blend = _alpha_blend(rd_src(x, y, img_buff::ALT_X), rd_src(x, y, img_buff::DEFAULT), xpos); - // Blend s1(Q3) onto Q4 for the bottom half. - T bottom_blend = _alpha_blend(rd_src(x, y, img_buff::ALT_XY), rd_src(x, y, img_buff::ALT_Y), xpos); - // Blend the top half onto the bottom half. - wr(x, y) = _alpha_blend(bottom_blend, top_blend, ypos); + int alpha = 255 * (1 - Math::smoothstep(0.1f, 0.9f, float(x - half_width) / float(skirt_width))); + for (int y = 0; y < p_height; y++) { + // Skip the center square + if (y == half_height) { + y = skirt_edge_y - 1; + } else { + // Starts reading at s2, ALT_Y skips s3, and continues with s1. + wr(x, y) = _alpha_blend(rd_dest(x, y), rd_src(x, y, img_buff::ALT_Y), alpha); + } + } } + + // Blend the horizontal skirt over the middle seam. + for (int y = half_height; y < skirt_edge_y; y++) { + int alpha = 255 * (1 - Math::smoothstep(0.1f, 0.9f, float(y - half_height) / float(skirt_height))); + for (int x = 0; x < p_width; x++) { + // Skip the center square + if (x == half_width) { + x = skirt_edge_x - 1; + } else { + // Starts reading at s4, skips s3, continues with s5. + wr(x, y) = _alpha_blend(rd_dest(x, y), rd_src(x, y, img_buff::ALT_X), alpha); + } + } + } + + // Fill in the center square. Wr starts at the top left of Q4, which is the equivalent of the top left of s3, unless a modulo is used. + for (int y = half_height; y < skirt_edge_y; y++) { + for (int x = half_width; x < skirt_edge_x; x++) { + int xpos = 255 * (1 - Math::smoothstep(0.1f, 0.9f, float(x - half_width) / float(skirt_width))); + int ypos = 255 * (1 - Math::smoothstep(0.1f, 0.9f, float(y - half_height) / float(skirt_height))); + + // Blend s3(Q1) onto s5(Q2) for the top half. + T top_blend = _alpha_blend(rd_src(x, y, img_buff::ALT_X), rd_src(x, y, img_buff::DEFAULT), xpos); + // Blend s1(Q3) onto Q4 for the bottom half. + T bottom_blend = _alpha_blend(rd_src(x, y, img_buff::ALT_XY), rd_src(x, y, img_buff::ALT_Y), xpos); + // Blend the top half onto the bottom half. + wr(x, y) = _alpha_blend(bottom_blend, top_blend, ypos); + } + } + Ref image = memnew(Image(p_width, p_height, false, format, dest)); + p_src.write[d].unref(); + images.write[d] = image; } - Ref image = memnew(Image(p_width, p_height, false, p_src->get_format(), dest)); - p_src.unref(); - return image; + + // Now blend across z. + if (p_depth > 1) { + int skirt_depth = MAX(1, p_depth * p_blend_skirt); + 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 = alpha + 1; + uint16_t 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; + } + return images; } template @@ -233,8 +290,13 @@ 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, 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; + Vector> _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_image(int p_width, int p_height, bool p_invert = false, bool p_in_3d_space = false, bool p_normalize = true) const; + virtual TypedArray get_image_3d(int p_width, int p_height, int p_depth, bool p_invert = false, bool p_normalize = true) const; + + Vector> _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; + 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 TypedArray get_seamless_image_3d(int p_width, int p_height, int p_depth, bool p_invert = 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 a31f77a38d93..e4b2e0b4ac09 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, 0, invert, in_3d_space, seamless_blend_skirt, normalize); + new_image = ref_noise->get_seamless_image(size.x, size.y, invert, in_3d_space, seamless_blend_skirt, normalize); } else { - new_image = ref_noise->get_image(size.x, size.y, 0, invert, in_3d_space, normalize); + new_image = ref_noise->get_image(size.x, size.y, 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 index 58403397de21..25d75b8ffbab 100644 --- a/modules/noise/noise_texture_3d.cpp +++ b/modules/noise/noise_texture_3d.cpp @@ -44,7 +44,9 @@ NoiseTexture3D::~NoiseTexture3D() { if (texture.is_valid()) { RS::get_singleton()->free(texture); } - noise_thread.wait_to_finish(); + if (noise_thread.is_started()) { + noise_thread.wait_to_finish(); + } } void NoiseTexture3D::_bind_methods() { @@ -147,18 +149,9 @@ TypedArray NoiseTexture3D::_generate_texture() { Vector> images; if (seamless) { - images = _get_seamless(width, height, depth, invert, seamless_blend_skirt); + images = ref_noise->_get_seamless_image(width, height, depth, invert, true, seamless_blend_skirt, normalize); } 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 individually as it would result in visible artifacts on z (depth) axis. - if (normalize) { - images = _normalize(images); + images = ref_noise->_get_image(width, height, depth, invert, true, normalize); } if (color_ramp.is_valid()) { @@ -177,116 +170,6 @@ TypedArray NoiseTexture3D::_generate_texture() { 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(); diff --git a/modules/noise/noise_texture_3d.h b/modules/noise/noise_texture_3d.h index b5dab10321fe..397711ca9812 100644 --- a/modules/noise/noise_texture_3d.h +++ b/modules/noise/noise_texture_3d.h @@ -68,8 +68,6 @@ private: 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: diff --git a/modules/noise/tests/test_fastnoise_lite.h b/modules/noise/tests/test_fastnoise_lite.h index db489c66720d..0a435c6a5c78 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, 0, false, false, 0.0); + Ref img = noise.get_seamless_image(11, 11, 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, 0, false, false, 0.1); + Ref img = noise.get_seamless_image(11, 11, 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, 0, false, false, 0.1); + Ref img = noise.get_seamless_image(11, 11, 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);