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