Fix compatibility breakage from adding NoiseTexture3D

Also optimize some of the Noise methods
This commit is contained in:
clayjohn 2023-04-28 13:19:42 -07:00
parent 9f12e7b52d
commit ccff24597f
8 changed files with 282 additions and 253 deletions

View File

@ -15,13 +15,24 @@
<return type="Image" />
<param index="0" name="width" type="int" />
<param index="1" name="height" type="int" />
<param index="2" name="invert" type="bool" default="false" />
<param index="3" name="in_3d_space" type="bool" default="false" />
<param index="4" name="normalize" type="bool" default="true" />
<description>
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].
</description>
</method>
<method name="get_image_3d" qualifiers="const">
<return type="Image[]" />
<param index="0" name="width" type="int" />
<param index="1" name="height" type="int" />
<param index="2" name="depth" type="int" />
<param index="3" name="invert" type="bool" default="false" />
<param index="4" name="in_3d_space" type="bool" default="false" />
<param index="5" name="normalize" type="bool" default="true" />
<param index="4" name="normalize" type="bool" default="true" />
<description>
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].
</description>
</method>
<method name="get_noise_1d" qualifiers="const">
@ -66,14 +77,26 @@
<return type="Image" />
<param index="0" name="width" type="int" />
<param index="1" name="height" type="int" />
<param index="2" name="invert" type="bool" default="false" />
<param index="3" name="in_3d_space" type="bool" default="false" />
<param index="4" name="skirt" type="float" default="0.1" />
<param index="5" name="normalize" type="bool" default="true" />
<description>
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].
</description>
</method>
<method name="get_seamless_image_3d" qualifiers="const">
<return type="Image[]" />
<param index="0" name="width" type="int" />
<param index="1" name="height" type="int" />
<param index="2" name="depth" type="int" />
<param index="3" name="invert" type="bool" default="false" />
<param index="4" name="in_3d_space" type="bool" default="false" />
<param index="5" name="skirt" type="float" default="0.1" />
<param index="6" name="normalize" type="bool" default="true" />
<param index="4" name="skirt" type="float" default="0.1" />
<param index="5" name="normalize" type="bool" default="true" />
<description>
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].
</description>
</method>
</methods>

View File

@ -5,13 +5,12 @@
</brief_description>
<description>
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]
</description>
<tutorials>

View File

@ -32,23 +32,42 @@
#include <float.h>
Ref<Image> 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<Image>());
Vector<Ref<Image>> 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<Ref<Image>>());
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<Ref<Image>> 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<Image> 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<uint8_t>(src, p_width, p_height, p_invert, p_blend_skirt);
return _generate_seamless_image<uint8_t>(src, p_width, p_height, p_depth, p_invert, p_blend_skirt);
} else {
return _generate_seamless_image<uint32_t>(src, p_width, p_height, p_invert, p_blend_skirt);
return _generate_seamless_image<uint32_t>(src, p_width, p_height, p_depth, p_invert, p_blend_skirt);
}
}
Ref<Image> 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<Ref<Image>> images = _get_seamless_image(p_width, p_height, 1, p_invert, p_in_3d_space, p_blend_skirt, p_normalize);
return images[0];
}
TypedArray<Image> 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<Ref<Image>> images = _get_seamless_image(p_width, p_height, p_depth, p_invert, true, p_blend_skirt, p_normalize);
TypedArray<Image> 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>(uint8_t p_bg, uint8_t p_fg, int p_alpha) const {
@ -58,61 +77,104 @@ uint8_t Noise::_alpha_blend<uint8_t>(uint8_t p_bg, uint8_t p_fg, int p_alpha) co
return (uint8_t)((alpha * p_fg + inv_alpha * p_bg) >> 8);
}
Ref<Image> 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<Image>());
Vector<Ref<Image>> 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<Ref<Image>>());
Vector<uint8_t> data;
data.resize(p_width * p_height);
uint8_t *wd8 = data.ptrw();
Vector<Ref<Image>> images;
images.resize(p_depth);
if (p_normalize) {
// Get all values and identify min/max values.
Vector<real_t> values;
values.resize(p_width * p_height);
LocalVector<real_t> 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<uint8_t>(CLAMP((values[x] - min_val) / (max_val - min_val) * 255.f, 0, 255));
}
for (int d = 0; d < p_depth; d++) {
Vector<uint8_t> 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<uint8_t>(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<Image> 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<uint8_t>(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<uint8_t> 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<uint8_t>(CLAMP(value * 127.5f + 127.5f, 0.0f, 255.0f));
wd8[idx] = p_invert ? (255 - ivalue) : ivalue;
idx++;
}
}
Ref<Image> 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<Image> Noise::get_image(int p_width, int p_height, bool p_invert, bool p_in_3d_space, bool p_normalize) const {
Vector<Ref<Image>> images = _get_image(p_width, p_height, 1, p_invert, p_in_3d_space, p_normalize);
return images[0];
}
TypedArray<Image> Noise::get_image_3d(int p_width, int p_height, int p_depth, bool p_invert, bool p_normalize) const {
Vector<Ref<Image>> images = _get_image(p_width, p_height, p_depth, p_invert, true, p_normalize);
TypedArray<Image> 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));
}

View File

@ -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 <typename T>
Ref<Image> _generate_seamless_image(Ref<Image> p_src, int p_width, int p_height, bool p_invert, real_t p_blend_skirt) const {
Vector<Ref<Image>> _generate_seamless_image(Vector<Ref<Image>> 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<Image>());
ERR_FAIL_COND_V(p_blend_skirt < 0, Vector<Ref<Image>>());
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<uint8_t> 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<T> rd_src = {
(T *)p_src->get_data().ptr(),
src_width, src_height,
half_width, half_height,
p_width, p_height
};
Vector<Ref<Image>> images;
images.resize(p_src.size());
// `wr` is setup for straight x/y coordinate array access.
img_buff<T> 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<T> 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<uint8_t> 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<T>::ALT_XY);
}
}
img_buff<T> 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<T> 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<T> 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<T>(rd_dest(x, y), rd_src(x, y, img_buff<T>::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<T>::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<T>(rd_dest(x, y), rd_src(x, y, img_buff<T>::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<T>(rd_src(x, y, img_buff<T>::ALT_X), rd_src(x, y, img_buff<T>::DEFAULT), xpos);
// Blend s1(Q3) onto Q4 for the bottom half.
T bottom_blend = _alpha_blend<T>(rd_src(x, y, img_buff<T>::ALT_XY), rd_src(x, y, img_buff<T>::ALT_Y), xpos);
// Blend the top half onto the bottom half.
wr(x, y) = _alpha_blend<T>(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<T>(rd_dest(x, y), rd_src(x, y, img_buff<T>::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<T>(rd_dest(x, y), rd_src(x, y, img_buff<T>::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<T>(rd_src(x, y, img_buff<T>::ALT_X), rd_src(x, y, img_buff<T>::DEFAULT), xpos);
// Blend s1(Q3) onto Q4 for the bottom half.
T bottom_blend = _alpha_blend<T>(rd_src(x, y, img_buff<T>::ALT_XY), rd_src(x, y, img_buff<T>::ALT_Y), xpos);
// Blend the top half onto the bottom half.
wr(x, y) = _alpha_blend<T>(bottom_blend, top_blend, ypos);
}
}
Ref<Image> image = memnew(Image(p_width, p_height, false, format, dest));
p_src.write[d].unref();
images.write[d] = image;
}
Ref<Image> 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<Image> img = images[i];
images.write[i] = images[i + half_depth];
images.write[i + half_depth] = img;
}
Vector<Ref<Image>> 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<uint8_t> img = images[z % p_depth]->get_data();
Vector<uint8_t> skirt = images[(z - half_depth) + p_depth]->get_data();
Vector<uint8_t> 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<Image> 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 <typename T>
@ -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<Image> 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<Image> 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<Ref<Image>> _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<Image> 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<Image> get_image_3d(int p_width, int p_height, int p_depth, bool p_invert = false, bool p_normalize = true) const;
Vector<Ref<Image>> _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<Image> 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<Image> 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

View File

@ -162,9 +162,9 @@ Ref<Image> NoiseTexture2D::_generate_texture() {
Ref<Image> 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);

View File

@ -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<Image> NoiseTexture3D::_generate_texture() {
Vector<Ref<Image>> 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<Image> NoiseTexture3D::_generate_texture() {
return new_data;
}
Vector<Ref<Image>> 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<Noise> ref_noise = noise;
if (ref_noise.is_null()) {
return Vector<Ref<Image>>();
}
int skirt_depth = MAX(1, p_depth * p_blend_skirt);
int src_depth = p_depth + skirt_depth;
Vector<Ref<Image>> 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<Image> img = images[i];
images.write[i] = images[i + half_depth];
images.write[i + half_depth] = img;
}
Vector<Ref<Image>> 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<uint8_t> img = images[z % p_depth]->get_data();
Vector<uint8_t> skirt = images[(z - half_depth) + p_depth]->get_data();
Vector<uint8_t> 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<Image> 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<Ref<Image>> NoiseTexture3D::_normalize(Vector<Ref<Image>> 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<uint8_t> 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<Ref<Image>> new_images;
new_images.resize(p_images.size());
for (int i = 0; i < p_images.size(); i++) {
Vector<uint8_t> 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<uint8_t>(CLAMP((data[j] - min_val) / (max_val - min_val) * 255.f, 0, 255));
}
data.write[j] = value;
}
Ref<Image> new_image = memnew(Image(w, h, false, Image::FORMAT_L8, data));
new_images.write[i] = new_image;
}
return new_images;
}
Ref<Image> NoiseTexture3D::_modulate_with_gradient(Ref<Image> p_image, Ref<Gradient> p_gradient) {
int w = p_image->get_width();
int h = p_image->get_height();

View File

@ -68,8 +68,6 @@ private:
void _update_texture();
void _set_texture_data(const TypedArray<Image> &p_data);
Vector<Ref<Image>> _get_seamless(int p_width, int p_height, int p_depth, bool p_invert, real_t p_blend_skirt);
Vector<Ref<Image>> _normalize(Vector<Ref<Image>> p_images);
Ref<Image> _modulate_with_gradient(Ref<Image> p_image, Ref<Gradient> p_gradient);
protected:

View File

@ -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<Image> img = noise.get_seamless_image(11, 11, 0, false, false, 0.0);
Ref<Image> img = noise.get_seamless_image(11, 11, false, false, 0.0);
Ref<Image> 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<Image> img = noise.get_seamless_image(11, 11, 0, false, false, 0.1);
Ref<Image> img = noise.get_seamless_image(11, 11, false, false, 0.1);
Ref<Image> 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<Image> img = noise.get_seamless_image(11, 11, 0, false, false, 0.1);
Ref<Image> img = noise.get_seamless_image(11, 11, false, false, 0.1);
Ref<Image> ref_img_3 = memnew(Image);
ref_img_3->set_data(11, 11, false, Image::FORMAT_L8, ref_img_3_data);