| 1 | /**************************************************************************/ |
| 2 | /* noise.h */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #ifndef NOISE_H |
| 32 | #define NOISE_H |
| 33 | |
| 34 | #include "core/io/image.h" |
| 35 | #include "core/variant/typed_array.h" |
| 36 | |
| 37 | class Noise : public Resource { |
| 38 | GDCLASS(Noise, Resource); |
| 39 | |
| 40 | // Helper struct for get_seamless_image(). See comments in .cpp for usage. |
| 41 | template <typename T> |
| 42 | struct img_buff { |
| 43 | T *img = nullptr; |
| 44 | int width; // Array dimensions & default modulo for image. |
| 45 | int height; |
| 46 | int offset_x; // Offset index location on image (wrapped by specified modulo). |
| 47 | int offset_y; |
| 48 | int alt_width; // Alternate module for image. |
| 49 | int alt_height; |
| 50 | |
| 51 | enum ALT_MODULO { |
| 52 | DEFAULT = 0, |
| 53 | ALT_X, |
| 54 | ALT_Y, |
| 55 | ALT_XY |
| 56 | }; |
| 57 | |
| 58 | // Multi-dimensional array indexer (e.g. img[x][y]) that supports multiple modulos. |
| 59 | T &operator()(int x, int y, ALT_MODULO mode = DEFAULT) { |
| 60 | switch (mode) { |
| 61 | case ALT_XY: |
| 62 | return img[(x + offset_x) % alt_width + ((y + offset_y) % alt_height) * width]; |
| 63 | case ALT_X: |
| 64 | return img[(x + offset_x) % alt_width + ((y + offset_y) % height) * width]; |
| 65 | case ALT_Y: |
| 66 | return img[(x + offset_x) % width + ((y + offset_y) % alt_height) * width]; |
| 67 | default: |
| 68 | return img[(x + offset_x) % width + ((y + offset_y) % height) * width]; |
| 69 | } |
| 70 | } |
| 71 | }; |
| 72 | |
| 73 | union l2c { |
| 74 | uint32_t l; |
| 75 | uint8_t c[4]; |
| 76 | struct { |
| 77 | uint8_t r; |
| 78 | uint8_t g; |
| 79 | uint8_t b; |
| 80 | uint8_t a; |
| 81 | }; |
| 82 | }; |
| 83 | |
| 84 | template <typename T> |
| 85 | 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 { |
| 86 | /* |
| 87 | To make a seamless image, we swap the quadrants so the edges are perfect matches. |
| 88 | We initially get a 10% larger image so we have an overlap we can use to blend over the seams. |
| 89 | |
| 90 | Noise::img_buff::operator() acts as a multi-dimensional array indexer. |
| 91 | It does the array math, translates between the flipped and non-flipped quadrants, and manages offsets and modulos. |
| 92 | |
| 93 | Here is how the larger source image and final output image map to each other: |
| 94 | |
| 95 | Output size = p_width*p_height Source w/ extra 10% skirt `s` size = src_width*src_height |
| 96 | Q1 Q2 Q4 Q3 s1 |
| 97 | Q3 Q4 Q2 Q1 s2 |
| 98 | s5 s4 s3 |
| 99 | |
| 100 | All of the loops use output coordinates, so Output:Q1 == Source:Q1 |
| 101 | Ex: Output(half_width, half_height) [the midpoint, corner of Q1/Q4] => |
| 102 | on Source it's translated to |
| 103 | corner of Q1/s3 unless the ALT_XY modulo moves it to Q4 |
| 104 | */ |
| 105 | ERR_FAIL_COND_V(p_blend_skirt < 0, Vector<Ref<Image>>()); |
| 106 | |
| 107 | int skirt_width = MAX(1, p_width * p_blend_skirt); |
| 108 | int skirt_height = MAX(1, p_height * p_blend_skirt); |
| 109 | int src_width = p_width + skirt_width; |
| 110 | int src_height = p_height + skirt_height; |
| 111 | int half_width = p_width * 0.5; |
| 112 | int half_height = p_height * 0.5; |
| 113 | int skirt_edge_x = half_width + skirt_width; |
| 114 | int skirt_edge_y = half_height + skirt_height; |
| 115 | |
| 116 | Image::Format format = p_src[0]->get_format(); |
| 117 | int pixel_size = Image::get_format_pixel_size(format); |
| 118 | |
| 119 | Vector<Ref<Image>> images; |
| 120 | images.resize(p_src.size()); |
| 121 | |
| 122 | // First blend across x and y for all slices. |
| 123 | for (int d = 0; d < images.size(); d++) { |
| 124 | Vector<uint8_t> dest; |
| 125 | dest.resize(p_width * p_height * pixel_size); |
| 126 | |
| 127 | img_buff<T> rd_src = { |
| 128 | (T *)p_src[d]->get_data().ptr(), |
| 129 | src_width, src_height, |
| 130 | half_width, half_height, |
| 131 | p_width, p_height |
| 132 | }; |
| 133 | |
| 134 | // `wr` is setup for straight x/y coordinate array access. |
| 135 | img_buff<T> wr = { |
| 136 | (T *)dest.ptrw(), |
| 137 | p_width, p_height, |
| 138 | 0, 0, 0, 0 |
| 139 | }; |
| 140 | // `rd_dest` is a readable pointer to `wr`, i.e. what has already been written to the output buffer. |
| 141 | img_buff<T> rd_dest = { |
| 142 | (T *)dest.ptr(), |
| 143 | p_width, p_height, |
| 144 | 0, 0, 0, 0 |
| 145 | }; |
| 146 | |
| 147 | // Swap the quadrants to make edges seamless. |
| 148 | for (int y = 0; y < p_height; y++) { |
| 149 | for (int x = 0; x < p_width; x++) { |
| 150 | // rd_src has a half offset and the shorter modulo ignores the skirt. |
| 151 | // It reads and writes in Q1-4 order (see map above), skipping the skirt. |
| 152 | wr(x, y) = rd_src(x, y, img_buff<T>::ALT_XY); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | // Blend the vertical skirt over the middle seam. |
| 157 | for (int x = half_width; x < skirt_edge_x; x++) { |
| 158 | int alpha = 255 * (1 - Math::smoothstep(0.1f, 0.9f, float(x - half_width) / float(skirt_width))); |
| 159 | for (int y = 0; y < p_height; y++) { |
| 160 | // Skip the center square |
| 161 | if (y == half_height) { |
| 162 | y = skirt_edge_y - 1; |
| 163 | } else { |
| 164 | // Starts reading at s2, ALT_Y skips s3, and continues with s1. |
| 165 | wr(x, y) = _alpha_blend<T>(rd_dest(x, y), rd_src(x, y, img_buff<T>::ALT_Y), alpha); |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | // Blend the horizontal skirt over the middle seam. |
| 171 | for (int y = half_height; y < skirt_edge_y; y++) { |
| 172 | int alpha = 255 * (1 - Math::smoothstep(0.1f, 0.9f, float(y - half_height) / float(skirt_height))); |
| 173 | for (int x = 0; x < p_width; x++) { |
| 174 | // Skip the center square |
| 175 | if (x == half_width) { |
| 176 | x = skirt_edge_x - 1; |
| 177 | } else { |
| 178 | // Starts reading at s4, skips s3, continues with s5. |
| 179 | wr(x, y) = _alpha_blend<T>(rd_dest(x, y), rd_src(x, y, img_buff<T>::ALT_X), alpha); |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | // 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. |
| 185 | for (int y = half_height; y < skirt_edge_y; y++) { |
| 186 | for (int x = half_width; x < skirt_edge_x; x++) { |
| 187 | int xpos = 255 * (1 - Math::smoothstep(0.1f, 0.9f, float(x - half_width) / float(skirt_width))); |
| 188 | int ypos = 255 * (1 - Math::smoothstep(0.1f, 0.9f, float(y - half_height) / float(skirt_height))); |
| 189 | |
| 190 | // Blend s3(Q1) onto s5(Q2) for the top half. |
| 191 | T top_blend = _alpha_blend<T>(rd_src(x, y, img_buff<T>::ALT_X), rd_src(x, y, img_buff<T>::DEFAULT), xpos); |
| 192 | // Blend s1(Q3) onto Q4 for the bottom half. |
| 193 | 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); |
| 194 | // Blend the top half onto the bottom half. |
| 195 | wr(x, y) = _alpha_blend<T>(bottom_blend, top_blend, ypos); |
| 196 | } |
| 197 | } |
| 198 | Ref<Image> image = memnew(Image(p_width, p_height, false, format, dest)); |
| 199 | p_src.write[d].unref(); |
| 200 | images.write[d] = image; |
| 201 | } |
| 202 | |
| 203 | // Now blend across z. |
| 204 | if (p_depth > 1) { |
| 205 | int skirt_depth = MAX(1, p_depth * p_blend_skirt); |
| 206 | int half_depth = p_depth * 0.5; |
| 207 | int skirt_edge_z = half_depth + skirt_depth; |
| 208 | |
| 209 | // Swap halves on depth. |
| 210 | for (int i = 0; i < half_depth; i++) { |
| 211 | Ref<Image> img = images[i]; |
| 212 | images.write[i] = images[i + half_depth]; |
| 213 | images.write[i + half_depth] = img; |
| 214 | } |
| 215 | |
| 216 | Vector<Ref<Image>> new_images = images; |
| 217 | new_images.resize(p_depth); |
| 218 | |
| 219 | // Scale seamless generation to third dimension. |
| 220 | for (int z = half_depth; z < skirt_edge_z; z++) { |
| 221 | int alpha = 255 * (1 - Math::smoothstep(0.1f, 0.9f, float(z - half_depth) / float(skirt_depth))); |
| 222 | |
| 223 | Vector<uint8_t> img = images[z % p_depth]->get_data(); |
| 224 | Vector<uint8_t> skirt = images[(z - half_depth) + p_depth]->get_data(); |
| 225 | |
| 226 | Vector<uint8_t> dest; |
| 227 | dest.resize(images[0]->get_width() * images[0]->get_height() * Image::get_format_pixel_size(images[0]->get_format())); |
| 228 | |
| 229 | for (int i = 0; i < img.size(); i++) { |
| 230 | uint8_t fg, bg, out; |
| 231 | |
| 232 | fg = skirt[i]; |
| 233 | bg = img[i]; |
| 234 | |
| 235 | uint16_t a = alpha + 1; |
| 236 | uint16_t inv_a = 256 - alpha; |
| 237 | |
| 238 | out = (uint8_t)((a * fg + inv_a * bg) >> 8); |
| 239 | |
| 240 | dest.write[i] = out; |
| 241 | } |
| 242 | |
| 243 | Ref<Image> new_image = memnew(Image(images[0]->get_width(), images[0]->get_height(), false, images[0]->get_format(), dest)); |
| 244 | new_images.write[z % p_depth] = new_image; |
| 245 | } |
| 246 | return new_images; |
| 247 | } |
| 248 | return images; |
| 249 | } |
| 250 | |
| 251 | template <typename T> |
| 252 | T _alpha_blend(T p_bg, T p_fg, int p_alpha) const { |
| 253 | l2c fg, bg, out; |
| 254 | |
| 255 | fg.l = p_fg; |
| 256 | bg.l = p_bg; |
| 257 | |
| 258 | uint16_t alpha; |
| 259 | uint16_t inv_alpha; |
| 260 | |
| 261 | // If no alpha argument specified, use the alpha channel in the color |
| 262 | if (p_alpha == -1) { |
| 263 | alpha = fg.c[3] + 1; |
| 264 | inv_alpha = 256 - fg.c[3]; |
| 265 | } else { |
| 266 | alpha = p_alpha + 1; |
| 267 | inv_alpha = 256 - p_alpha; |
| 268 | } |
| 269 | |
| 270 | out.c[0] = (uint8_t)((alpha * fg.c[0] + inv_alpha * bg.c[0]) >> 8); |
| 271 | out.c[1] = (uint8_t)((alpha * fg.c[1] + inv_alpha * bg.c[1]) >> 8); |
| 272 | out.c[2] = (uint8_t)((alpha * fg.c[2] + inv_alpha * bg.c[2]) >> 8); |
| 273 | out.c[3] = 0xFF; |
| 274 | |
| 275 | return out.l; |
| 276 | } |
| 277 | |
| 278 | protected: |
| 279 | static void _bind_methods(); |
| 280 | |
| 281 | public: |
| 282 | // Virtual destructor so we can delete any Noise derived object when referenced as a Noise*. |
| 283 | virtual ~Noise() {} |
| 284 | |
| 285 | virtual real_t get_noise_1d(real_t p_x) const = 0; |
| 286 | |
| 287 | virtual real_t get_noise_2dv(Vector2 p_v) const = 0; |
| 288 | virtual real_t get_noise_2d(real_t p_x, real_t p_y) const = 0; |
| 289 | |
| 290 | virtual real_t get_noise_3dv(Vector3 p_v) const = 0; |
| 291 | virtual real_t get_noise_3d(real_t p_x, real_t p_y, real_t p_z) const = 0; |
| 292 | |
| 293 | 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; |
| 294 | 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; |
| 295 | virtual TypedArray<Image> get_image_3d(int p_width, int p_height, int p_depth, bool p_invert = false, bool p_normalize = true) const; |
| 296 | |
| 297 | 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; |
| 298 | 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; |
| 299 | 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; |
| 300 | }; |
| 301 | |
| 302 | #endif // NOISE_H |
| 303 | |