noise.cpp 8.2 KB

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