| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/io/image.h" |
| 34 |
#include "core/variant/typed_array.h" |
| 35 |
|
| 36 |
class Noise : public Resource { |
| 37 |
GDCLASS(Noise, Resource); |
| 38 |
|
| 39 |
|
| 40 |
template <typename T> |
| 41 |
struct img_buff { |
| 42 |
T *img = nullptr; |
| 43 |
int width; |
| 44 |
int height; |
| 45 |
int offset_x; |
| 46 |
int offset_y; |
| 47 |
int alt_width; |
| 48 |
int alt_height; |
| 49 |
|
| 50 |
enum ALT_MODULO { |
| 51 |
DEFAULT = 0, |
| 52 |
ALT_X, |
| 53 |
ALT_Y, |
| 54 |
ALT_XY |
| 55 |
}; |
| 56 |
|
| 57 |
|
| 58 |
T &operator()(int x, int y, ALT_MODULO mode = DEFAULT) { |
| 59 |
switch (mode) { |
| 60 |
case ALT_XY: |
| 61 |
return img[(x + offset_x) % alt_width + ((y + offset_y) % alt_height) * width]; |
| 62 |
case ALT_X: |
| 63 |
return img[(x + offset_x) % alt_width + ((y + offset_y) % height) * width]; |
| 64 |
case ALT_Y: |
| 65 |
return img[(x + offset_x) % width + ((y + offset_y) % alt_height) * width]; |
| 66 |
default: |
| 67 |
return img[(x + offset_x) % width + ((y + offset_y) % height) * width]; |
| 68 |
} |
| 69 |
} |
| 70 |
}; |
| 71 |
|
| 72 |
union l2c { |
| 73 |
uint32_t l; |
| 74 |
uint8_t c[4]; |
| 75 |
struct { |
| 76 |
uint8_t r; |
| 77 |
uint8_t g; |
| 78 |
uint8_t b; |
| 79 |
uint8_t a; |
| 80 |
}; |
| 81 |
}; |
| 82 |
|
| 83 |
template <typename T> |
| 84 |
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 { |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
ERR_FAIL_COND_V(p_blend_skirt < 0, Vector<Ref<Image>>()); |
| 105 |
|
| 106 |
int skirt_width = MAX(1, p_width * p_blend_skirt); |
| 107 |
int skirt_height = MAX(1, p_height * p_blend_skirt); |
| 108 |
int src_width = p_width + skirt_width; |
| 109 |
int src_height = p_height + skirt_height; |
| 110 |
int half_width = p_width * 0.5; |
| 111 |
int half_height = p_height * 0.5; |
| 112 |
int skirt_edge_x = half_width + skirt_width; |
| 113 |
int skirt_edge_y = half_height + skirt_height; |
| 114 |
|
| 115 |
Image::Format format = p_src[0]->get_format(); |
| 116 |
int pixel_size = Image::get_format_pixel_size(format); |
| 117 |
|
| 118 |
Vector<Ref<Image>> images; |
| 119 |
images.resize(p_src.size()); |
| 120 |
|
| 121 |
|
| 122 |
for (int d = 0; d < images.size(); d++) { |
| 123 |
Vector<uint8_t> dest; |
| 124 |
dest.resize(p_width * p_height * pixel_size); |
| 125 |
|
| 126 |
img_buff<T> rd_src = { |
| 127 |
(T *)p_src[d]->get_data().ptr(), |
| 128 |
src_width, src_height, |
| 129 |
half_width, half_height, |
| 130 |
p_width, p_height |
| 131 |
}; |
| 132 |
|
| 133 |
|
| 134 |
img_buff<T> wr = { |
| 135 |
(T *)dest.ptrw(), |
| 136 |
p_width, p_height, |
| 137 |
0, 0, 0, 0 |
| 138 |
}; |
| 139 |
|
| 140 |
img_buff<T> rd_dest = { |
| 141 |
(T *)dest.ptr(), |
| 142 |
p_width, p_height, |
| 143 |
0, 0, 0, 0 |
| 144 |
}; |
| 145 |
|
| 146 |
|
| 147 |
for (int y = 0; y < p_height; y++) { |
| 148 |
for (int x = 0; x < p_width; x++) { |
| 149 |
|
| 150 |
|
| 151 |
wr(x, y) = rd_src(x, y, img_buff<T>::ALT_XY); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
for (int x = half_width; x < skirt_edge_x; x++) { |
| 157 |
int alpha = 255 * (1 - Math::smoothstep(0.1f, 0.9f, float(x - half_width) / float(skirt_width))); |
| 158 |
for (int y = 0; y < p_height; y++) { |
| 159 |
|
| 160 |
if (y == half_height) { |
| 161 |
y = skirt_edge_y - 1; |
| 162 |
} else { |
| 163 |
|
| 164 |
wr(x, y) = _alpha_blend<T>(rd_dest(x, y), rd_src(x, y, img_buff<T>::ALT_Y), alpha); |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
for (int y = half_height; y < skirt_edge_y; y++) { |
| 171 |
int alpha = 255 * (1 - Math::smoothstep(0.1f, 0.9f, float(y - half_height) / float(skirt_height))); |
| 172 |
for (int x = 0; x < p_width; x++) { |
| 173 |
|
| 174 |
if (x == half_width) { |
| 175 |
x = skirt_edge_x - 1; |
| 176 |
} else { |
| 177 |
|
| 178 |
wr(x, y) = _alpha_blend<T>(rd_dest(x, y), rd_src(x, y, img_buff<T>::ALT_X), alpha); |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
for (int y = half_height; y < skirt_edge_y; y++) { |
| 185 |
for (int x = half_width; x < skirt_edge_x; x++) { |
| 186 |
int xpos = 255 * (1 - Math::smoothstep(0.1f, 0.9f, float(x - half_width) / float(skirt_width))); |
| 187 |
int ypos = 255 * (1 - Math::smoothstep(0.1f, 0.9f, float(y - half_height) / float(skirt_height))); |
| 188 |
|
| 189 |
|
| 190 |
T top_blend = _alpha_blend<T>(rd_src(x, y, img_buff<T>::ALT_X), rd_src(x, y, img_buff<T>::DEFAULT), xpos); |
| 191 |
|
| 192 |
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); |
| 193 |
|
| 194 |
wr(x, y) = _alpha_blend<T>(bottom_blend, top_blend, ypos); |
| 195 |
} |
| 196 |
} |
| 197 |
Ref<Image> image = memnew(Image(p_width, p_height, false, format, dest)); |
| 198 |
p_src.write[d].unref(); |
| 199 |
images.write[d] = image; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
if (p_depth > 1) { |
| 204 |
int skirt_depth = MAX(1, p_depth * p_blend_skirt); |
| 205 |
int half_depth = p_depth * 0.5; |
| 206 |
int skirt_edge_z = half_depth + skirt_depth; |
| 207 |
|
| 208 |
|
| 209 |
for (int i = 0; i < half_depth; i++) { |
| 210 |
Ref<Image> img = images[i]; |
| 211 |
images.write[i] = images[i + half_depth]; |
| 212 |
images.write[i + half_depth] = img; |
| 213 |
} |
| 214 |
|
| 215 |
Vector<Ref<Image>> new_images = images; |
| 216 |
new_images.resize(p_depth); |
| 217 |
|
| 218 |
|
| 219 |
for (int z = half_depth; z < skirt_edge_z; z++) { |
| 220 |
int alpha = 255 * (1 - Math::smoothstep(0.1f, 0.9f, float(z - half_depth) / float(skirt_depth))); |
| 221 |
|
| 222 |
Vector<uint8_t> img = images[z % p_depth]->get_data(); |
| 223 |
Vector<uint8_t> skirt = images[(z - half_depth) + p_depth]->get_data(); |
| 224 |
|
| 225 |
Vector<uint8_t> dest; |
| 226 |
dest.resize(images[0]->get_width() * images[0]->get_height() * Image::get_format_pixel_size(images[0]->get_format())); |
| 227 |
|
| 228 |
for (int i = 0; i < img.size(); i++) { |
| 229 |
uint8_t fg, bg, out; |
| 230 |
|
| 231 |
fg = skirt[i]; |
| 232 |
bg = img[i]; |
| 233 |
|
| 234 |
uint16_t a = alpha + 1; |
| 235 |
uint16_t inv_a = 256 - alpha; |
| 236 |
|
| 237 |
out = (uint8_t)((a * fg + inv_a * bg) >> 8); |
| 238 |
|
| 239 |
dest.write[i] = out; |
| 240 |
} |
| 241 |
|
| 242 |
Ref<Image> new_image = memnew(Image(images[0]->get_width(), images[0]->get_height(), false, images[0]->get_format(), dest)); |
| 243 |
new_images.write[z % p_depth] = new_image; |
| 244 |
} |
| 245 |
return new_images; |
| 246 |
} |
| 247 |
return images; |
| 248 |
} |
| 249 |
|
| 250 |
template <typename T> |
| 251 |
T _alpha_blend(T p_bg, T p_fg, int p_alpha) const { |
| 252 |
l2c fg, bg, out; |
| 253 |
|
| 254 |
fg.l = p_fg; |
| 255 |
bg.l = p_bg; |
| 256 |
|
| 257 |
uint16_t alpha; |
| 258 |
uint16_t inv_alpha; |
| 259 |
|
| 260 |
|
| 261 |
if (p_alpha == -1) { |
| 262 |
alpha = fg.c[3] + 1; |
| 263 |
inv_alpha = 256 - fg.c[3]; |
| 264 |
} else { |
| 265 |
alpha = p_alpha + 1; |
| 266 |
inv_alpha = 256 - p_alpha; |
| 267 |
} |
| 268 |
|
| 269 |
out.c[0] = (uint8_t)((alpha * fg.c[0] + inv_alpha * bg.c[0]) >> 8); |
| 270 |
out.c[1] = (uint8_t)((alpha * fg.c[1] + inv_alpha * bg.c[1]) >> 8); |
| 271 |
out.c[2] = (uint8_t)((alpha * fg.c[2] + inv_alpha * bg.c[2]) >> 8); |
| 272 |
out.c[3] = 0xFF; |
| 273 |
|
| 274 |
return out.l; |
| 275 |
} |
| 276 |
|
| 277 |
protected: |
| 278 |
static void _bind_methods(); |
| 279 |
|
| 280 |
public: |
| 281 |
|
| 282 |
virtual ~Noise() {} |
| 283 |
|
| 284 |
virtual real_t get_noise_1d(real_t p_x) const = 0; |
| 285 |
|
| 286 |
virtual real_t get_noise_2dv(Vector2 p_v) const = 0; |
| 287 |
virtual real_t get_noise_2d(real_t p_x, real_t p_y) const = 0; |
| 288 |
|
| 289 |
virtual real_t get_noise_3dv(Vector3 p_v) const = 0; |
| 290 |
virtual real_t get_noise_3d(real_t p_x, real_t p_y, real_t p_z) const = 0; |
| 291 |
|
| 292 |
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; |
| 293 |
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; |
| 294 |
virtual TypedArray<Image> get_image_3d(int p_width, int p_height, int p_depth, bool p_invert = false, bool p_normalize = true) const; |
| 295 |
|
| 296 |
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; |
| 297 |
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; |
| 298 |
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; |
| 299 |
}; |