noise.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. 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 {
  33. ERR_FAIL_COND_V(p_width <= 0 || p_height <= 0, 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 src_width = p_width + skirt_width;
  37. int src_height = p_height + skirt_height;
  38. Ref<Image> src = get_image(src_width, src_height, p_invert, p_in_3d_space, p_normalize);
  39. bool grayscale = (src->get_format() == Image::FORMAT_L8);
  40. if (grayscale) {
  41. return _generate_seamless_image<uint8_t>(src, p_width, p_height, p_invert, p_blend_skirt);
  42. } else {
  43. return _generate_seamless_image<uint32_t>(src, p_width, p_height, p_invert, p_blend_skirt);
  44. }
  45. }
  46. // Template specialization for faster grayscale blending.
  47. template <>
  48. uint8_t Noise::_alpha_blend<uint8_t>(uint8_t p_bg, uint8_t p_fg, int p_alpha) const {
  49. uint16_t alpha = p_alpha + 1;
  50. uint16_t inv_alpha = 256 - p_alpha;
  51. return (uint8_t)((alpha * p_fg + inv_alpha * p_bg) >> 8);
  52. }
  53. Ref<Image> Noise::get_image(int p_width, int p_height, bool p_invert, bool p_in_3d_space, bool p_normalize) const {
  54. ERR_FAIL_COND_V(p_width <= 0 || p_height <= 0, Ref<Image>());
  55. Vector<uint8_t> data;
  56. data.resize(p_width * p_height);
  57. uint8_t *wd8 = data.ptrw();
  58. if (p_normalize) {
  59. // Get all values and identify min/max values.
  60. Vector<real_t> values;
  61. values.resize(p_width * p_height);
  62. real_t min_val = FLT_MAX;
  63. real_t max_val = -FLT_MAX;
  64. for (int y = 0, i = 0; y < p_height; y++) {
  65. for (int x = 0; x < p_width; x++, i++) {
  66. values.set(i, p_in_3d_space ? get_noise_3d(x, y, 0.0) : get_noise_2d(x, y));
  67. if (values[i] > max_val) {
  68. max_val = values[i];
  69. }
  70. if (values[i] < min_val) {
  71. min_val = values[i];
  72. }
  73. }
  74. }
  75. // Normalize values and write to texture.
  76. uint8_t ivalue;
  77. for (int i = 0, x = 0; i < p_height; i++) {
  78. for (int j = 0; j < p_width; j++, x++) {
  79. if (max_val == min_val) {
  80. ivalue = 0;
  81. } else {
  82. ivalue = static_cast<uint8_t>(CLAMP((values[x] - min_val) / (max_val - min_val) * 255.f, 0, 255));
  83. }
  84. if (p_invert) {
  85. ivalue = 255 - ivalue;
  86. }
  87. wd8[x] = ivalue;
  88. }
  89. }
  90. } else {
  91. // Without normalization, the expected range of the noise function is [-1, 1].
  92. uint8_t ivalue;
  93. for (int y = 0, i = 0; y < p_height; y++) {
  94. for (int x = 0; x < p_width; x++, i++) {
  95. float value = (p_in_3d_space ? get_noise_3d(x, y, 0.0) : get_noise_2d(x, y));
  96. ivalue = static_cast<uint8_t>(CLAMP(value * 127.5f + 127.5f, 0.0f, 255.0f));
  97. wd8[i] = p_invert ? (255 - ivalue) : ivalue;
  98. }
  99. }
  100. }
  101. return memnew(Image(p_width, p_height, false, Image::FORMAT_L8, data));
  102. }
  103. void Noise::_bind_methods() {
  104. // Noise functions.
  105. ClassDB::bind_method(D_METHOD("get_noise_1d", "x"), &Noise::get_noise_1d);
  106. ClassDB::bind_method(D_METHOD("get_noise_2d", "x", "y"), &Noise::get_noise_2d);
  107. ClassDB::bind_method(D_METHOD("get_noise_2dv", "v"), &Noise::get_noise_2dv);
  108. ClassDB::bind_method(D_METHOD("get_noise_3d", "x", "y", "z"), &Noise::get_noise_3d);
  109. ClassDB::bind_method(D_METHOD("get_noise_3dv", "v"), &Noise::get_noise_3dv);
  110. // Textures.
  111. ClassDB::bind_method(D_METHOD("get_image", "width", "height", "invert", "in_3d_space", "normalize"), &Noise::get_image, DEFVAL(false), DEFVAL(false), DEFVAL(true));
  112. 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));
  113. }