noise.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*************************************************************************/
  2. /* noise.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. 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) const {
  32. ERR_FAIL_COND_V(p_width <= 0 || p_height <= 0, Ref<Image>());
  33. int skirt_width = p_width * p_blend_skirt;
  34. int skirt_height = p_height * p_blend_skirt;
  35. int src_width = p_width + skirt_width;
  36. int src_height = p_height + skirt_height;
  37. Ref<Image> src = get_image(src_width, src_height, p_invert, p_in_3d_space);
  38. bool grayscale = (src->get_format() == Image::FORMAT_L8);
  39. if (grayscale) {
  40. return _generate_seamless_image<uint8_t>(src, p_width, p_height, p_invert, p_blend_skirt);
  41. } else {
  42. return _generate_seamless_image<uint32_t>(src, p_width, p_height, p_invert, p_blend_skirt);
  43. }
  44. }
  45. // Template specialization for faster grayscale blending.
  46. template <>
  47. uint8_t Noise::_alpha_blend<uint8_t>(uint8_t p_bg, uint8_t p_fg, int p_alpha) const {
  48. uint16_t alpha = p_alpha + 1;
  49. uint16_t inv_alpha = 256 - p_alpha;
  50. return (uint8_t)((alpha * p_fg + inv_alpha * p_bg) >> 8);
  51. }
  52. Ref<Image> Noise::get_image(int p_width, int p_height, bool p_invert, bool p_in_3d_space) const {
  53. ERR_FAIL_COND_V(p_width <= 0 || p_height <= 0, Ref<Image>());
  54. Vector<uint8_t> data;
  55. data.resize(p_width * p_height);
  56. uint8_t *wd8 = data.ptrw();
  57. // Get all values and identify min/max values.
  58. Vector<real_t> values;
  59. values.resize(p_width * p_height);
  60. real_t min_val = 1000;
  61. real_t max_val = -1000;
  62. for (int y = 0, i = 0; y < p_height; y++) {
  63. for (int x = 0; x < p_width; x++, i++) {
  64. values.set(i, p_in_3d_space ? get_noise_3d(x, y, 0.0) : get_noise_2d(x, y));
  65. if (values[i] > max_val) {
  66. max_val = values[i];
  67. }
  68. if (values[i] < min_val) {
  69. min_val = values[i];
  70. }
  71. }
  72. }
  73. // Normalize values and write to texture.
  74. uint8_t value;
  75. for (int i = 0, x = 0; i < p_height; i++) {
  76. for (int j = 0; j < p_width; j++, x++) {
  77. if (max_val == min_val) {
  78. value = 0;
  79. } else {
  80. value = uint8_t(CLAMP((values[x] - min_val) / (max_val - min_val) * 255.f, 0, 255));
  81. }
  82. if (p_invert) {
  83. value = 255 - value;
  84. }
  85. wd8[x] = value;
  86. }
  87. }
  88. return memnew(Image(p_width, p_height, false, Image::FORMAT_L8, data));
  89. }
  90. void Noise::_bind_methods() {
  91. // Noise functions.
  92. ClassDB::bind_method(D_METHOD("get_noise_1d", "x"), &Noise::get_noise_1d);
  93. ClassDB::bind_method(D_METHOD("get_noise_2d", "x", "y"), &Noise::get_noise_2d);
  94. ClassDB::bind_method(D_METHOD("get_noise_2dv", "v"), &Noise::get_noise_2dv);
  95. ClassDB::bind_method(D_METHOD("get_noise_3d", "x", "y", "z"), &Noise::get_noise_3d);
  96. ClassDB::bind_method(D_METHOD("get_noise_3dv", "v"), &Noise::get_noise_3dv);
  97. // Textures.
  98. ClassDB::bind_method(D_METHOD("get_image", "width", "height", "invert", "in_3d_space"), &Noise::get_image, DEFVAL(false), DEFVAL(false));
  99. ClassDB::bind_method(D_METHOD("get_seamless_image", "width", "height", "invert", "in_3d_space", "skirt"), &Noise::get_seamless_image, DEFVAL(false), DEFVAL(false), DEFVAL(0.1));
  100. }