noise.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. #ifndef NOISE_H
  31. #define NOISE_H
  32. #include "core/io/image.h"
  33. class Noise : public Resource {
  34. GDCLASS(Noise, Resource);
  35. // Helper struct for get_seamless_image(). See comments in .cpp for usage.
  36. template <typename T>
  37. struct img_buff {
  38. T *img = nullptr;
  39. int width; // Array dimensions & default modulo for image.
  40. int height;
  41. int offset_x; // Offset index location on image (wrapped by specified modulo).
  42. int offset_y;
  43. int alt_width; // Alternate module for image.
  44. int alt_height;
  45. enum ALT_MODULO {
  46. DEFAULT = 0,
  47. ALT_X,
  48. ALT_Y,
  49. ALT_XY
  50. };
  51. // Multi-dimensional array indexer (e.g. img[x][y]) that supports multiple modulos.
  52. T &operator()(int x, int y, ALT_MODULO mode = DEFAULT) {
  53. switch (mode) {
  54. case ALT_XY:
  55. return img[(x + offset_x) % alt_width + ((y + offset_y) % alt_height) * width];
  56. case ALT_X:
  57. return img[(x + offset_x) % alt_width + ((y + offset_y) % height) * width];
  58. case ALT_Y:
  59. return img[(x + offset_x) % width + ((y + offset_y) % alt_height) * width];
  60. default:
  61. return img[(x + offset_x) % width + ((y + offset_y) % height) * width];
  62. }
  63. }
  64. };
  65. union l2c {
  66. uint32_t l;
  67. uint8_t c[4];
  68. struct {
  69. uint8_t r;
  70. uint8_t g;
  71. uint8_t b;
  72. uint8_t a;
  73. };
  74. };
  75. template <typename T>
  76. Ref<Image> _generate_seamless_image(Ref<Image> p_src, int p_width, int p_height, bool p_invert, real_t p_blend_skirt) const {
  77. /*
  78. To make a seamless image, we swap the quadrants so the edges are perfect matches.
  79. We initially get a 10% larger image so we have an overlap we can use to blend over the seams.
  80. Noise::img_buff::operator() acts as a multi-dimensional array indexer.
  81. It does the array math, translates between the flipped and non-flipped quadrants, and manages offsets and modulos.
  82. Here is how the larger source image and final output image map to each other:
  83. Output size = p_width*p_height Source w/ extra 10% skirt `s` size = src_width*src_height
  84. Q1 Q2 Q4 Q3 s1
  85. Q3 Q4 Q2 Q1 s2
  86. s5 s4 s3
  87. All of the loops use output coordinates, so Output:Q1 == Source:Q1
  88. Ex: Output(half_width, half_height) [the midpoint, corner of Q1/Q4] =>
  89. on Source it's translated to
  90. corner of Q1/s3 unless the ALT_XY modulo moves it to Q4
  91. */
  92. ERR_FAIL_COND_V(p_blend_skirt < 0, Ref<Image>());
  93. int skirt_width = MAX(1, p_width * p_blend_skirt);
  94. int skirt_height = MAX(1, p_height * p_blend_skirt);
  95. int src_width = p_width + skirt_width;
  96. int src_height = p_height + skirt_height;
  97. int half_width = p_width * 0.5;
  98. int half_height = p_height * 0.5;
  99. int skirt_edge_x = half_width + skirt_width;
  100. int skirt_edge_y = half_height + skirt_height;
  101. Vector<uint8_t> dest;
  102. dest.resize(p_width * p_height * Image::get_format_pixel_size(p_src->get_format()));
  103. img_buff<T> rd_src = {
  104. (T *)p_src->get_data().ptr(),
  105. src_width, src_height,
  106. half_width, half_height,
  107. p_width, p_height
  108. };
  109. // `wr` is setup for straight x/y coordinate array access.
  110. img_buff<T> wr = {
  111. (T *)dest.ptrw(),
  112. p_width, p_height,
  113. 0, 0, 0, 0
  114. };
  115. // `rd_dest` is a readable pointer to `wr`, i.e. what has already been written to the output buffer.
  116. img_buff<T> rd_dest = {
  117. (T *)dest.ptr(),
  118. p_width, p_height,
  119. 0, 0, 0, 0
  120. };
  121. // Swap the quadrants to make edges seamless.
  122. for (int y = 0; y < p_height; y++) {
  123. for (int x = 0; x < p_width; x++) {
  124. // rd_src has a half offset and the shorter modulo ignores the skirt.
  125. // It reads and writes in Q1-4 order (see map above), skipping the skirt.
  126. wr(x, y) = rd_src(x, y, img_buff<T>::ALT_XY);
  127. }
  128. }
  129. // Blend the vertical skirt over the middle seam.
  130. for (int x = half_width; x < skirt_edge_x; x++) {
  131. int alpha = 255 * (1 - Math::smoothstep(0.1f, 0.9f, float(x - half_width) / float(skirt_width)));
  132. for (int y = 0; y < p_height; y++) {
  133. // Skip the center square
  134. if (y == half_height) {
  135. y = skirt_edge_y - 1;
  136. } else {
  137. // Starts reading at s2, ALT_Y skips s3, and continues with s1.
  138. wr(x, y) = _alpha_blend<T>(rd_dest(x, y), rd_src(x, y, img_buff<T>::ALT_Y), alpha);
  139. }
  140. }
  141. }
  142. // Blend the horizontal skirt over the middle seam.
  143. for (int y = half_height; y < skirt_edge_y; y++) {
  144. int alpha = 255 * (1 - Math::smoothstep(0.1f, 0.9f, float(y - half_height) / float(skirt_height)));
  145. for (int x = 0; x < p_width; x++) {
  146. // Skip the center square
  147. if (x == half_width) {
  148. x = skirt_edge_x - 1;
  149. } else {
  150. // Starts reading at s4, skips s3, continues with s5.
  151. wr(x, y) = _alpha_blend<T>(rd_dest(x, y), rd_src(x, y, img_buff<T>::ALT_X), alpha);
  152. }
  153. }
  154. }
  155. // 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.
  156. for (int y = half_height; y < skirt_edge_y; y++) {
  157. for (int x = half_width; x < skirt_edge_x; x++) {
  158. int xpos = 255 * (1 - Math::smoothstep(0.1f, 0.9f, float(x - half_width) / float(skirt_width)));
  159. int ypos = 255 * (1 - Math::smoothstep(0.1f, 0.9f, float(y - half_height) / float(skirt_height)));
  160. // Blend s3(Q1) onto s5(Q2) for the top half.
  161. T top_blend = _alpha_blend<T>(rd_src(x, y, img_buff<T>::ALT_X), rd_src(x, y, img_buff<T>::DEFAULT), xpos);
  162. // Blend s1(Q3) onto Q4 for the bottom half.
  163. 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);
  164. // Blend the top half onto the bottom half.
  165. wr(x, y) = _alpha_blend<T>(bottom_blend, top_blend, ypos);
  166. }
  167. }
  168. Ref<Image> image = memnew(Image(p_width, p_height, false, p_src->get_format(), dest));
  169. p_src.unref();
  170. return image;
  171. }
  172. template <typename T>
  173. T _alpha_blend(T p_bg, T p_fg, int p_alpha) const {
  174. l2c fg, bg, out;
  175. fg.l = p_fg;
  176. bg.l = p_bg;
  177. uint16_t alpha;
  178. uint16_t inv_alpha;
  179. // If no alpha argument specified, use the alpha channel in the color
  180. if (p_alpha == -1) {
  181. alpha = fg.c[3] + 1;
  182. inv_alpha = 256 - fg.c[3];
  183. } else {
  184. alpha = p_alpha + 1;
  185. inv_alpha = 256 - p_alpha;
  186. }
  187. out.c[0] = (uint8_t)((alpha * fg.c[0] + inv_alpha * bg.c[0]) >> 8);
  188. out.c[1] = (uint8_t)((alpha * fg.c[1] + inv_alpha * bg.c[1]) >> 8);
  189. out.c[2] = (uint8_t)((alpha * fg.c[2] + inv_alpha * bg.c[2]) >> 8);
  190. out.c[3] = 0xFF;
  191. return out.l;
  192. }
  193. protected:
  194. static void _bind_methods();
  195. public:
  196. // Virtual destructor so we can delete any Noise derived object when referenced as a Noise*.
  197. virtual ~Noise() {}
  198. virtual real_t get_noise_1d(real_t p_x) const = 0;
  199. virtual real_t get_noise_2dv(Vector2 p_v) const = 0;
  200. virtual real_t get_noise_2d(real_t p_x, real_t p_y) const = 0;
  201. virtual real_t get_noise_3dv(Vector3 p_v) const = 0;
  202. virtual real_t get_noise_3d(real_t p_x, real_t p_y, real_t p_z) const = 0;
  203. 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;
  204. 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;
  205. };
  206. #endif // NOISE_H