noise.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /**************************************************************************/
  2. /* noise.cpp */
  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. #include "noise.h"
  31. #include <float.h>
  32. 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 {
  33. ERR_FAIL_COND_V(p_width <= 0 || p_height <= 0 || p_depth <= 0, Vector<Ref<Image>>());
  34. int skirt_width = MAX(1, p_width * p_blend_skirt);
  35. int skirt_height = MAX(1, p_height * p_blend_skirt);
  36. int skirt_depth = MAX(1, p_depth * p_blend_skirt);
  37. int src_width = p_width + skirt_width;
  38. int src_height = p_height + skirt_height;
  39. int src_depth = p_depth + skirt_depth;
  40. Vector<Ref<Image>> src = _get_image(src_width, src_height, src_depth, p_invert, p_in_3d_space, p_normalize);
  41. bool grayscale = (src[0]->get_format() == Image::FORMAT_L8);
  42. if (grayscale) {
  43. return _generate_seamless_image<uint8_t>(src, p_width, p_height, p_depth, p_invert, p_blend_skirt);
  44. } else {
  45. return _generate_seamless_image<uint32_t>(src, p_width, p_height, p_depth, p_invert, p_blend_skirt);
  46. }
  47. }
  48. 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 {
  49. Vector<Ref<Image>> images = _get_seamless_image(p_width, p_height, 1, p_invert, p_in_3d_space, p_blend_skirt, p_normalize);
  50. if (images.size() == 0) {
  51. return Ref<Image>();
  52. }
  53. return images[0];
  54. }
  55. 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 {
  56. Vector<Ref<Image>> images = _get_seamless_image(p_width, p_height, p_depth, p_invert, true, p_blend_skirt, p_normalize);
  57. TypedArray<Image> ret;
  58. ret.resize(images.size());
  59. for (int i = 0; i < images.size(); i++) {
  60. ret[i] = images[i];
  61. }
  62. return ret;
  63. }
  64. // Template specialization for faster grayscale blending.
  65. template <>
  66. uint8_t Noise::_alpha_blend<uint8_t>(uint8_t p_bg, uint8_t p_fg, int p_alpha) const {
  67. uint16_t alpha = p_alpha + 1;
  68. uint16_t inv_alpha = 256 - p_alpha;
  69. return (uint8_t)((alpha * p_fg + inv_alpha * p_bg) >> 8);
  70. }
  71. 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 {
  72. ERR_FAIL_COND_V(p_width <= 0 || p_height <= 0 || p_depth <= 0, Vector<Ref<Image>>());
  73. Vector<Ref<Image>> images;
  74. images.resize(p_depth);
  75. if (p_normalize) {
  76. // Get all values and identify min/max values.
  77. LocalVector<real_t> values;
  78. values.resize(p_width * p_height * p_depth);
  79. real_t min_val = FLT_MAX;
  80. real_t max_val = -FLT_MAX;
  81. int idx = 0;
  82. for (int d = 0; d < p_depth; d++) {
  83. for (int y = 0; y < p_height; y++) {
  84. for (int x = 0; x < p_width; x++) {
  85. values[idx] = p_in_3d_space ? get_noise_3d(x, y, d) : get_noise_2d(x, y);
  86. if (values[idx] > max_val) {
  87. max_val = values[idx];
  88. }
  89. if (values[idx] < min_val) {
  90. min_val = values[idx];
  91. }
  92. idx++;
  93. }
  94. }
  95. }
  96. idx = 0;
  97. // Normalize values and write to texture.
  98. for (int d = 0; d < p_depth; d++) {
  99. Vector<uint8_t> data;
  100. data.resize(p_width * p_height);
  101. uint8_t *wd8 = data.ptrw();
  102. uint8_t ivalue;
  103. for (int y = 0; y < p_height; y++) {
  104. for (int x = 0; x < p_width; x++) {
  105. if (max_val == min_val) {
  106. ivalue = 0;
  107. } else {
  108. ivalue = static_cast<uint8_t>(CLAMP((values[idx] - min_val) / (max_val - min_val) * 255.f, 0, 255));
  109. }
  110. if (p_invert) {
  111. ivalue = 255 - ivalue;
  112. }
  113. wd8[x + y * p_width] = ivalue;
  114. idx++;
  115. }
  116. }
  117. Ref<Image> img = memnew(Image(p_width, p_height, false, Image::FORMAT_L8, data));
  118. images.write[d] = img;
  119. }
  120. } else {
  121. // Without normalization, the expected range of the noise function is [-1, 1].
  122. for (int d = 0; d < p_depth; d++) {
  123. Vector<uint8_t> data;
  124. data.resize(p_width * p_height);
  125. uint8_t *wd8 = data.ptrw();
  126. uint8_t ivalue;
  127. int idx = 0;
  128. for (int y = 0; y < p_height; y++) {
  129. for (int x = 0; x < p_width; x++) {
  130. float value = (p_in_3d_space ? get_noise_3d(x, y, d) : get_noise_2d(x, y));
  131. ivalue = static_cast<uint8_t>(CLAMP(value * 127.5f + 127.5f, 0.0f, 255.0f));
  132. wd8[idx] = p_invert ? (255 - ivalue) : ivalue;
  133. idx++;
  134. }
  135. }
  136. Ref<Image> img = memnew(Image(p_width, p_height, false, Image::FORMAT_L8, data));
  137. images.write[d] = img;
  138. }
  139. }
  140. return images;
  141. }
  142. Ref<Image> Noise::get_image(int p_width, int p_height, bool p_invert, bool p_in_3d_space, bool p_normalize) const {
  143. Vector<Ref<Image>> images = _get_image(p_width, p_height, 1, p_invert, p_in_3d_space, p_normalize);
  144. if (images.is_empty()) {
  145. return Ref<Image>();
  146. }
  147. return images[0];
  148. }
  149. TypedArray<Image> Noise::get_image_3d(int p_width, int p_height, int p_depth, bool p_invert, bool p_normalize) const {
  150. Vector<Ref<Image>> images = _get_image(p_width, p_height, p_depth, p_invert, true, p_normalize);
  151. TypedArray<Image> ret;
  152. ret.resize(images.size());
  153. for (int i = 0; i < images.size(); i++) {
  154. ret[i] = images[i];
  155. }
  156. return ret;
  157. }
  158. void Noise::_bind_methods() {
  159. // Noise functions.
  160. ClassDB::bind_method(D_METHOD("get_noise_1d", "x"), &Noise::get_noise_1d);
  161. ClassDB::bind_method(D_METHOD("get_noise_2d", "x", "y"), &Noise::get_noise_2d);
  162. ClassDB::bind_method(D_METHOD("get_noise_2dv", "v"), &Noise::get_noise_2dv);
  163. ClassDB::bind_method(D_METHOD("get_noise_3d", "x", "y", "z"), &Noise::get_noise_3d);
  164. ClassDB::bind_method(D_METHOD("get_noise_3dv", "v"), &Noise::get_noise_3dv);
  165. // Textures.
  166. ClassDB::bind_method(D_METHOD("get_image", "width", "height", "invert", "in_3d_space", "normalize"), &Noise::get_image, DEFVAL(false), DEFVAL(false), DEFVAL(true));
  167. 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));
  168. ClassDB::bind_method(D_METHOD("get_image_3d", "width", "height", "depth", "invert", "normalize"), &Noise::get_image_3d, DEFVAL(false), DEFVAL(true));
  169. 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));
  170. }