open_simplex_noise.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*************************************************************************/
  2. /* open_simplex_noise.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "open_simplex_noise.h"
  31. #include "core/core_string_names.h"
  32. OpenSimplexNoise::OpenSimplexNoise() {
  33. seed = 0;
  34. persistence = 0.5;
  35. octaves = 3;
  36. period = 64;
  37. lacunarity = 2.0;
  38. _init_seeds();
  39. }
  40. OpenSimplexNoise::~OpenSimplexNoise() {
  41. }
  42. void OpenSimplexNoise::_init_seeds() {
  43. for (int i = 0; i < MAX_OCTAVES; ++i) {
  44. open_simplex_noise(seed + i * 2, &(contexts[i]));
  45. }
  46. }
  47. void OpenSimplexNoise::set_seed(int p_seed) {
  48. if (seed == p_seed)
  49. return;
  50. seed = p_seed;
  51. _init_seeds();
  52. emit_changed();
  53. }
  54. int OpenSimplexNoise::get_seed() {
  55. return seed;
  56. }
  57. void OpenSimplexNoise::set_octaves(int p_octaves) {
  58. if (p_octaves == octaves)
  59. return;
  60. ERR_FAIL_COND_MSG(p_octaves > MAX_OCTAVES, vformat("The number of OpenSimplexNoise octaves is limited to %d; ignoring the new value.", MAX_OCTAVES));
  61. octaves = CLAMP(p_octaves, 1, MAX_OCTAVES);
  62. emit_changed();
  63. }
  64. void OpenSimplexNoise::set_period(float p_period) {
  65. if (p_period == period)
  66. return;
  67. period = p_period;
  68. emit_changed();
  69. }
  70. void OpenSimplexNoise::set_persistence(float p_persistence) {
  71. if (p_persistence == persistence)
  72. return;
  73. persistence = p_persistence;
  74. emit_changed();
  75. }
  76. void OpenSimplexNoise::set_lacunarity(float p_lacunarity) {
  77. if (p_lacunarity == lacunarity)
  78. return;
  79. lacunarity = p_lacunarity;
  80. emit_changed();
  81. }
  82. Ref<Image> OpenSimplexNoise::get_image(int p_width, int p_height) {
  83. Vector<uint8_t> data;
  84. data.resize(p_width * p_height * 4);
  85. uint8_t *wd8 = data.ptrw();
  86. for (int i = 0; i < p_height; i++) {
  87. for (int j = 0; j < p_width; j++) {
  88. float v = get_noise_2d(i, j);
  89. v = v * 0.5 + 0.5; // Normalize [0..1]
  90. uint8_t value = uint8_t(CLAMP(v * 255.0, 0, 255));
  91. wd8[(i * p_width + j) * 4 + 0] = value;
  92. wd8[(i * p_width + j) * 4 + 1] = value;
  93. wd8[(i * p_width + j) * 4 + 2] = value;
  94. wd8[(i * p_width + j) * 4 + 3] = 255;
  95. }
  96. }
  97. Ref<Image> image = memnew(Image(p_width, p_height, false, Image::FORMAT_RGBA8, data));
  98. return image;
  99. }
  100. Ref<Image> OpenSimplexNoise::get_seamless_image(int p_size) {
  101. Vector<uint8_t> data;
  102. data.resize(p_size * p_size * 4);
  103. uint8_t *wd8 = data.ptrw();
  104. for (int i = 0; i < p_size; i++) {
  105. for (int j = 0; j < p_size; j++) {
  106. float ii = (float)i / (float)p_size;
  107. float jj = (float)j / (float)p_size;
  108. ii *= 2.0 * Math_PI;
  109. jj *= 2.0 * Math_PI;
  110. float radius = p_size / (2.0 * Math_PI);
  111. float x = radius * Math::sin(jj);
  112. float y = radius * Math::cos(jj);
  113. float z = radius * Math::sin(ii);
  114. float w = radius * Math::cos(ii);
  115. float v = get_noise_4d(x, y, z, w);
  116. v = v * 0.5 + 0.5; // Normalize [0..1]
  117. uint8_t value = uint8_t(CLAMP(v * 255.0, 0, 255));
  118. wd8[(i * p_size + j) * 4 + 0] = value;
  119. wd8[(i * p_size + j) * 4 + 1] = value;
  120. wd8[(i * p_size + j) * 4 + 2] = value;
  121. wd8[(i * p_size + j) * 4 + 3] = 255;
  122. }
  123. }
  124. Ref<Image> image = memnew(Image(p_size, p_size, false, Image::FORMAT_RGBA8, data));
  125. return image;
  126. }
  127. void OpenSimplexNoise::_bind_methods() {
  128. ClassDB::bind_method(D_METHOD("get_seed"), &OpenSimplexNoise::get_seed);
  129. ClassDB::bind_method(D_METHOD("set_seed", "seed"), &OpenSimplexNoise::set_seed);
  130. ClassDB::bind_method(D_METHOD("set_octaves", "octave_count"), &OpenSimplexNoise::set_octaves);
  131. ClassDB::bind_method(D_METHOD("get_octaves"), &OpenSimplexNoise::get_octaves);
  132. ClassDB::bind_method(D_METHOD("set_period", "period"), &OpenSimplexNoise::set_period);
  133. ClassDB::bind_method(D_METHOD("get_period"), &OpenSimplexNoise::get_period);
  134. ClassDB::bind_method(D_METHOD("set_persistence", "persistence"), &OpenSimplexNoise::set_persistence);
  135. ClassDB::bind_method(D_METHOD("get_persistence"), &OpenSimplexNoise::get_persistence);
  136. ClassDB::bind_method(D_METHOD("set_lacunarity", "lacunarity"), &OpenSimplexNoise::set_lacunarity);
  137. ClassDB::bind_method(D_METHOD("get_lacunarity"), &OpenSimplexNoise::get_lacunarity);
  138. ClassDB::bind_method(D_METHOD("get_image", "width", "height"), &OpenSimplexNoise::get_image);
  139. ClassDB::bind_method(D_METHOD("get_seamless_image", "size"), &OpenSimplexNoise::get_seamless_image);
  140. ClassDB::bind_method(D_METHOD("get_noise_1d", "x"), &OpenSimplexNoise::get_noise_1d);
  141. ClassDB::bind_method(D_METHOD("get_noise_2d", "x", "y"), &OpenSimplexNoise::get_noise_2d);
  142. ClassDB::bind_method(D_METHOD("get_noise_3d", "x", "y", "z"), &OpenSimplexNoise::get_noise_3d);
  143. ClassDB::bind_method(D_METHOD("get_noise_4d", "x", "y", "z", "w"), &OpenSimplexNoise::get_noise_4d);
  144. ClassDB::bind_method(D_METHOD("get_noise_2dv", "pos"), &OpenSimplexNoise::get_noise_2dv);
  145. ClassDB::bind_method(D_METHOD("get_noise_3dv", "pos"), &OpenSimplexNoise::get_noise_3dv);
  146. ADD_PROPERTY(PropertyInfo(Variant::INT, "seed"), "set_seed", "get_seed");
  147. ADD_PROPERTY(PropertyInfo(Variant::INT, "octaves", PROPERTY_HINT_RANGE, vformat("1,%d,1", MAX_OCTAVES)), "set_octaves", "get_octaves");
  148. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "period", PROPERTY_HINT_RANGE, "0.1,256.0,0.1"), "set_period", "get_period");
  149. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "persistence", PROPERTY_HINT_RANGE, "0.0,1.0,0.001"), "set_persistence", "get_persistence");
  150. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "lacunarity", PROPERTY_HINT_RANGE, "0.1,4.0,0.01"), "set_lacunarity", "get_lacunarity");
  151. }
  152. float OpenSimplexNoise::get_noise_1d(float x) {
  153. return get_noise_2d(x, 1.0);
  154. }
  155. float OpenSimplexNoise::get_noise_2d(float x, float y) {
  156. x /= period;
  157. y /= period;
  158. float amp = 1.0;
  159. float max = 1.0;
  160. float sum = _get_octave_noise_2d(0, x, y);
  161. int i = 0;
  162. while (++i < octaves) {
  163. x *= lacunarity;
  164. y *= lacunarity;
  165. amp *= persistence;
  166. max += amp;
  167. sum += _get_octave_noise_2d(i, x, y) * amp;
  168. }
  169. return sum / max;
  170. }
  171. float OpenSimplexNoise::get_noise_3d(float x, float y, float z) {
  172. x /= period;
  173. y /= period;
  174. z /= period;
  175. float amp = 1.0;
  176. float max = 1.0;
  177. float sum = _get_octave_noise_3d(0, x, y, z);
  178. int i = 0;
  179. while (++i < octaves) {
  180. x *= lacunarity;
  181. y *= lacunarity;
  182. z *= lacunarity;
  183. amp *= persistence;
  184. max += amp;
  185. sum += _get_octave_noise_3d(i, x, y, z) * amp;
  186. }
  187. return sum / max;
  188. }
  189. float OpenSimplexNoise::get_noise_4d(float x, float y, float z, float w) {
  190. x /= period;
  191. y /= period;
  192. z /= period;
  193. w /= period;
  194. float amp = 1.0;
  195. float max = 1.0;
  196. float sum = _get_octave_noise_4d(0, x, y, z, w);
  197. int i = 0;
  198. while (++i < octaves) {
  199. x *= lacunarity;
  200. y *= lacunarity;
  201. z *= lacunarity;
  202. w *= lacunarity;
  203. amp *= persistence;
  204. max += amp;
  205. sum += _get_octave_noise_4d(i, x, y, z, w) * amp;
  206. }
  207. return sum / max;
  208. }