noise.cpp 4.9 KB

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