fastnoise_lite.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /**************************************************************************/
  2. /* fastnoise_lite.h */
  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. #ifndef FASTNOISE_LITE_H
  31. #define FASTNOISE_LITE_H
  32. #include "noise.h"
  33. #include "core/io/image.h"
  34. #include "core/object/ref_counted.h"
  35. #include "scene/resources/gradient.h"
  36. #include <thirdparty/noise/FastNoiseLite.h>
  37. typedef fastnoiselite::FastNoiseLite _FastNoiseLite;
  38. class FastNoiseLite : public Noise {
  39. GDCLASS(FastNoiseLite, Noise);
  40. OBJ_SAVE_TYPE(FastNoiseLite);
  41. public:
  42. enum NoiseType {
  43. TYPE_SIMPLEX = _FastNoiseLite::NoiseType_OpenSimplex2,
  44. TYPE_SIMPLEX_SMOOTH = _FastNoiseLite::NoiseType_OpenSimplex2S,
  45. TYPE_CELLULAR = _FastNoiseLite::NoiseType_Cellular,
  46. TYPE_PERLIN = _FastNoiseLite::NoiseType_Perlin,
  47. TYPE_VALUE_CUBIC = _FastNoiseLite::NoiseType_ValueCubic,
  48. TYPE_VALUE = _FastNoiseLite::NoiseType_Value,
  49. };
  50. enum FractalType {
  51. FRACTAL_NONE = _FastNoiseLite::FractalType_None,
  52. FRACTAL_FBM = _FastNoiseLite::FractalType_FBm,
  53. FRACTAL_RIDGED = _FastNoiseLite::FractalType_Ridged,
  54. FRACTAL_PING_PONG = _FastNoiseLite::FractalType_PingPong,
  55. };
  56. enum CellularDistanceFunction {
  57. DISTANCE_EUCLIDEAN = _FastNoiseLite::CellularDistanceFunction_Euclidean,
  58. DISTANCE_EUCLIDEAN_SQUARED = _FastNoiseLite::CellularDistanceFunction_EuclideanSq,
  59. DISTANCE_MANHATTAN = _FastNoiseLite::CellularDistanceFunction_Manhattan,
  60. DISTANCE_HYBRID = _FastNoiseLite::CellularDistanceFunction_Hybrid
  61. };
  62. enum CellularReturnType {
  63. RETURN_CELL_VALUE = _FastNoiseLite::CellularReturnType_CellValue,
  64. RETURN_DISTANCE = _FastNoiseLite::CellularReturnType_Distance,
  65. RETURN_DISTANCE2 = _FastNoiseLite::CellularReturnType_Distance2,
  66. RETURN_DISTANCE2_ADD = _FastNoiseLite::CellularReturnType_Distance2Add,
  67. RETURN_DISTANCE2_SUB = _FastNoiseLite::CellularReturnType_Distance2Sub,
  68. RETURN_DISTANCE2_MUL = _FastNoiseLite::CellularReturnType_Distance2Mul,
  69. RETURN_DISTANCE2_DIV = _FastNoiseLite::CellularReturnType_Distance2Div
  70. };
  71. enum DomainWarpType {
  72. DOMAIN_WARP_SIMPLEX = _FastNoiseLite::DomainWarpType_OpenSimplex2,
  73. DOMAIN_WARP_SIMPLEX_REDUCED = _FastNoiseLite::DomainWarpType_OpenSimplex2Reduced,
  74. DOMAIN_WARP_BASIC_GRID = _FastNoiseLite::DomainWarpType_BasicGrid
  75. };
  76. enum DomainWarpFractalType {
  77. DOMAIN_WARP_FRACTAL_NONE,
  78. DOMAIN_WARP_FRACTAL_PROGRESSIVE,
  79. DOMAIN_WARP_FRACTAL_INDEPENDENT
  80. };
  81. protected:
  82. static void _bind_methods();
  83. void _validate_property(PropertyInfo &p_property) const;
  84. private:
  85. _FastNoiseLite _noise;
  86. _FastNoiseLite _domain_warp_noise;
  87. Vector3 offset;
  88. NoiseType noise_type = TYPE_SIMPLEX_SMOOTH;
  89. int seed = 0;
  90. real_t frequency = 0.01;
  91. // Fractal specific.
  92. FractalType fractal_type = FRACTAL_FBM;
  93. int fractal_octaves = 5;
  94. real_t fractal_lacunarity = 2;
  95. real_t fractal_gain = 0.5;
  96. real_t fractal_weighted_strength = 0;
  97. real_t fractal_ping_pong_strength = 2;
  98. // Cellular specific.
  99. CellularDistanceFunction cellular_distance_function = DISTANCE_EUCLIDEAN;
  100. CellularReturnType cellular_return_type = RETURN_DISTANCE;
  101. real_t cellular_jitter = 1.0;
  102. // Domain warp specific.
  103. bool domain_warp_enabled = false;
  104. DomainWarpType domain_warp_type = DOMAIN_WARP_SIMPLEX;
  105. real_t domain_warp_amplitude = 30.0;
  106. real_t domain_warp_frequency = 0.05;
  107. DomainWarpFractalType domain_warp_fractal_type = DOMAIN_WARP_FRACTAL_PROGRESSIVE;
  108. int domain_warp_fractal_octaves = 5;
  109. real_t domain_warp_fractal_lacunarity = 6;
  110. real_t domain_warp_fractal_gain = 0.5;
  111. // This needs manual conversion because Godots Inspector property API does not support discontiguous enum indices.
  112. _FastNoiseLite::FractalType _convert_domain_warp_fractal_type_enum(DomainWarpFractalType p_domain_warp_fractal_type);
  113. public:
  114. FastNoiseLite();
  115. ~FastNoiseLite();
  116. // General noise settings.
  117. void set_noise_type(NoiseType p_noise_type);
  118. NoiseType get_noise_type() const;
  119. void set_seed(int p_seed);
  120. int get_seed() const;
  121. void set_frequency(real_t p_freq);
  122. real_t get_frequency() const;
  123. void set_offset(Vector3 p_offset);
  124. Vector3 get_offset() const;
  125. // Fractal specific.
  126. void set_fractal_type(FractalType p_type);
  127. FractalType get_fractal_type() const;
  128. void set_fractal_octaves(int p_octaves);
  129. int get_fractal_octaves() const;
  130. void set_fractal_lacunarity(real_t p_lacunarity);
  131. real_t get_fractal_lacunarity() const;
  132. void set_fractal_gain(real_t p_gain);
  133. real_t get_fractal_gain() const;
  134. void set_fractal_weighted_strength(real_t p_weighted_strength);
  135. real_t get_fractal_weighted_strength() const;
  136. void set_fractal_ping_pong_strength(real_t p_ping_pong_strength);
  137. real_t get_fractal_ping_pong_strength() const;
  138. // Cellular specific.
  139. void set_cellular_distance_function(CellularDistanceFunction p_func);
  140. CellularDistanceFunction get_cellular_distance_function() const;
  141. void set_cellular_return_type(CellularReturnType p_ret);
  142. CellularReturnType get_cellular_return_type() const;
  143. void set_cellular_jitter(real_t p_jitter);
  144. real_t get_cellular_jitter() const;
  145. // Domain warp specific.
  146. void set_domain_warp_enabled(bool p_enabled);
  147. bool is_domain_warp_enabled() const;
  148. void set_domain_warp_type(DomainWarpType p_domain_warp_type);
  149. DomainWarpType get_domain_warp_type() const;
  150. void set_domain_warp_amplitude(real_t p_amplitude);
  151. real_t get_domain_warp_amplitude() const;
  152. void set_domain_warp_frequency(real_t p_frequency);
  153. real_t get_domain_warp_frequency() const;
  154. void set_domain_warp_fractal_type(DomainWarpFractalType p_domain_warp_fractal_type);
  155. DomainWarpFractalType get_domain_warp_fractal_type() const;
  156. void set_domain_warp_fractal_octaves(int p_octaves);
  157. int get_domain_warp_fractal_octaves() const;
  158. void set_domain_warp_fractal_lacunarity(real_t p_lacunarity);
  159. real_t get_domain_warp_fractal_lacunarity() const;
  160. void set_domain_warp_fractal_gain(real_t p_gain);
  161. real_t get_domain_warp_fractal_gain() const;
  162. // Interface methods.
  163. real_t get_noise_1d(real_t p_x) const override;
  164. real_t get_noise_2dv(Vector2 p_v) const override;
  165. real_t get_noise_2d(real_t p_x, real_t p_y) const override;
  166. real_t get_noise_3dv(Vector3 p_v) const override;
  167. real_t get_noise_3d(real_t p_x, real_t p_y, real_t p_z) const override;
  168. void _changed();
  169. };
  170. VARIANT_ENUM_CAST(FastNoiseLite::NoiseType);
  171. VARIANT_ENUM_CAST(FastNoiseLite::FractalType);
  172. VARIANT_ENUM_CAST(FastNoiseLite::CellularDistanceFunction);
  173. VARIANT_ENUM_CAST(FastNoiseLite::CellularReturnType);
  174. VARIANT_ENUM_CAST(FastNoiseLite::DomainWarpType);
  175. VARIANT_ENUM_CAST(FastNoiseLite::DomainWarpFractalType);
  176. #endif // FASTNOISE_LITE_H