fastnoise_lite.h 8.3 KB

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