From 41b7ea05a1f7aa2b8ecddaa1fd739e64d6970f7e Mon Sep 17 00:00:00 2001 From: Relintai Date: Tue, 24 Sep 2019 12:58:16 +0200 Subject: [PATCH] Added a resource which can store noise parameters. --- fastnoise_noise_params.cpp | 85 ++++++++++++++++++++++++++++++++++++++ fastnoise_noise_params.h | 71 +++++++++++++++++++++++++++++++ noise.cpp | 5 +++ noise.h | 7 ++++ register_types.cpp | 2 + 5 files changed, 170 insertions(+) create mode 100644 fastnoise_noise_params.cpp create mode 100644 fastnoise_noise_params.h diff --git a/fastnoise_noise_params.cpp b/fastnoise_noise_params.cpp new file mode 100644 index 0000000..0e07d79 --- /dev/null +++ b/fastnoise_noise_params.cpp @@ -0,0 +1,85 @@ +#include "fastnoise_noise_params.h" + +void FastnoiseNoiseParams::setup_noise(Ref noise) { + noise->set_seed(_seed); + noise->set_noise_type(_noise_type); + noise->set_interpolation(_interp); + noise->set_frequency(_freq); + noise->set_fractal_octaves(_octave_count); + noise->set_fractal_lacunarity(_lacunarity); + noise->set_fractal_gain(_gain); + noise->set_fractal_type(_type); + noise->set_cellular_distance_function(_func); + noise->set_cellular_return_type(_ret); + noise->set_gradient_perturbation_amplitude(_amp); +} + +FastnoiseNoiseParams::FastnoiseNoiseParams() { + _seed = 1337; + _noise_type = FastNoise::TYPE_SIMPLEX; + _interp = FastNoise::INTERP_QUINTIC; + _freq = 0.01; + _octave_count = 3; + _lacunarity = 2; + _gain = 0.5; + _type = FastNoise::FRACTAL_FBM; + _func = FastNoise::DISTANCE_EUCLIDEAN; + _ret = FastNoise::RETURN_CELL_VALUE; + _jitter = 0.45; + _amp = 1; +} +FastnoiseNoiseParams::~FastnoiseNoiseParams() { + +} + +void FastnoiseNoiseParams::_bind_methods() { + ClassDB::bind_method(D_METHOD("set_seed", "seed"), &FastnoiseNoiseParams::set_seed); + ClassDB::bind_method(D_METHOD("get_seed"), &FastnoiseNoiseParams::get_seed); + ADD_PROPERTY(PropertyInfo(Variant::INT, "seed"), "set_seed", "get_seed"); + + ClassDB::bind_method(D_METHOD("set_noise_type", "type"), &FastnoiseNoiseParams::set_noise_type); + ClassDB::bind_method(D_METHOD("get_noise_type"), &FastnoiseNoiseParams::get_noise_type); + ADD_PROPERTY(PropertyInfo(Variant::INT, "noise_type", PROPERTY_HINT_ENUM, FastNoise::BINDING_STRING_TYPE), "set_noise_type", "get_noise_type"); + + ClassDB::bind_method(D_METHOD("set_interpolation", "interp"), &FastnoiseNoiseParams::set_interpolation); + ClassDB::bind_method(D_METHOD("get_interpolation"), &FastnoiseNoiseParams::get_interpolation); + ADD_PROPERTY(PropertyInfo(Variant::INT, "interpolation", PROPERTY_HINT_ENUM, FastNoise::BINDING_STRING_INTERPOLATION), "set_interpolation", "get_interpolation"); + + ClassDB::bind_method(D_METHOD("set_frequency", "freq"), &FastnoiseNoiseParams::set_frequency); + ClassDB::bind_method(D_METHOD("get_frequency"), &FastnoiseNoiseParams::get_frequency); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "frequency"), "set_frequency", "get_frequency"); + + ClassDB::bind_method(D_METHOD("set_fractal_gain", "gain"), &FastnoiseNoiseParams::set_fractal_gain); + ClassDB::bind_method(D_METHOD("get_fractal_gain"), &FastnoiseNoiseParams::get_fractal_gain); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "fractal_gain"), "set_fractal_gain", "get_fractal_gain"); + + ClassDB::bind_method(D_METHOD("set_fractal_type", "type"), &FastnoiseNoiseParams::set_fractal_type); + ClassDB::bind_method(D_METHOD("get_fractal_type"), &FastnoiseNoiseParams::get_fractal_type); + ADD_PROPERTY(PropertyInfo(Variant::INT, "fractal_type", PROPERTY_HINT_ENUM, FastNoise::BINDING_STRING_FRACTAL_TYPE), "set_fractal_type", "get_fractal_type"); + + ClassDB::bind_method(D_METHOD("set_fractal_octaves", "octave_count"), &FastnoiseNoiseParams::set_fractal_octaves); + ClassDB::bind_method(D_METHOD("get_fractal_octaves"), &FastnoiseNoiseParams::get_fractal_octaves); + ADD_PROPERTY(PropertyInfo(Variant::INT, "fractal_octaves"), "set_fractal_octaves", "get_fractal_octaves"); + + ClassDB::bind_method(D_METHOD("set_fractal_lacunarity", "lacunarity"), &FastnoiseNoiseParams::set_fractal_lacunarity); + ClassDB::bind_method(D_METHOD("get_fractal_lacunarity"), &FastnoiseNoiseParams::get_fractal_lacunarity); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "fractal_lacunarity"), "set_fractal_lacunarity", "get_fractal_lacunarity"); + + ClassDB::bind_method(D_METHOD("set_cellular_distance_function", "func"), &FastnoiseNoiseParams::set_cellular_distance_function); + ClassDB::bind_method(D_METHOD("get_cellular_distance_function"), &FastnoiseNoiseParams::get_cellular_distance_function); + ADD_PROPERTY(PropertyInfo(Variant::INT, "set_cellular_distance_function", PROPERTY_HINT_ENUM, FastNoise::BINDING_STRING_CELLULAR_DISTANCE_FUNCTION), "set_cellular_distance_function", "get_cellular_distance_function"); + + ClassDB::bind_method(D_METHOD("set_cellular_return_type", "ret"), &FastnoiseNoiseParams::set_cellular_return_type); + ClassDB::bind_method(D_METHOD("get_cellular_return_type"), &FastnoiseNoiseParams::get_cellular_return_type); + ADD_PROPERTY(PropertyInfo(Variant::INT, "cellular_return_type", PROPERTY_HINT_ENUM, FastNoise::BINDING_STRING_CELLULAR_RETURN_TYPE), "set_cellular_return_type", "get_cellular_return_type"); + + ClassDB::bind_method(D_METHOD("set_cellular_jitter", "jitter"), &FastnoiseNoiseParams::set_cellular_jitter); + ClassDB::bind_method(D_METHOD("get_cellular_jitter"), &FastnoiseNoiseParams::get_cellular_jitter); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "cellular_jitter"), "set_cellular_jitter", "get_cellular_jitter"); + + ClassDB::bind_method(D_METHOD("set_gradient_perturbation_amplitude", "amp"), &FastnoiseNoiseParams::set_gradient_perturbation_amplitude); + ClassDB::bind_method(D_METHOD("get_gradient_perturbation_amplitude"), &FastnoiseNoiseParams::get_gradient_perturbation_amplitude); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "gradient_perturbation_amplitude"), "set_gradient_perturbation_amplitude", "get_gradient_perturbation_amplitude"); + + ClassDB::bind_method(D_METHOD("setup_noise", "noise"), &FastnoiseNoiseParams::setup_noise); +} diff --git a/fastnoise_noise_params.h b/fastnoise_noise_params.h new file mode 100644 index 0000000..94b3193 --- /dev/null +++ b/fastnoise_noise_params.h @@ -0,0 +1,71 @@ +#ifndef FASTNOISE_NOISE_PARAMS_H +#define FASTNOISE_NOISE_PARAMS_H + +#include "core/resource.h" + +#include "noise.h" + +class FastnoiseNoiseParams : public Resource { + GDCLASS(FastnoiseNoiseParams, Resource); + +public: + int get_seed() const { return _seed; } + void set_seed(int seed) { _seed = seed; } + + void set_noise_type(FastNoise::Type noise_type) { _noise_type = noise_type; } + FastNoise::Type get_noise_type() const { return _noise_type; } + + void set_interpolation(FastNoise::Interpolation interp) { _interp = interp; } + FastNoise::Interpolation get_interpolation() const { return _interp; } + + void set_frequency(real_t freq) { _freq = freq; } + real_t get_frequency() const { return _freq; } + + void set_fractal_octaves(unsigned int octave_count) { _octave_count = octave_count; } + int get_fractal_octaves() const { return _octave_count; } + + void set_fractal_lacunarity(real_t lacunarity) { _lacunarity = lacunarity; } + real_t get_fractal_lacunarity() const { return _lacunarity; } + + void set_fractal_gain(real_t gain) { _gain = gain; } + real_t get_fractal_gain() const { return _gain; } + + void set_fractal_type(FastNoise::FractalType type) { _type = type; } + FastNoise::FractalType get_fractal_type() const { return _type; } + + void set_cellular_distance_function(FastNoise::CellularDistanceFunction func) { _func = func; } + FastNoise::CellularDistanceFunction get_cellular_distance_function() const { return _func; } + + void set_cellular_return_type(FastNoise::CellularReturnType ret) { _ret = ret; } + FastNoise::CellularReturnType get_cellular_return_type() const { return _ret; } + + void set_cellular_jitter(real_t jitter) { _jitter = jitter; } + real_t get_cellular_jitter() const { return _jitter; } + + void set_gradient_perturbation_amplitude(real_t amp) { _amp = amp; } + real_t get_gradient_perturbation_amplitude() const { return _amp; } + + void setup_noise(Ref noise); + + FastnoiseNoiseParams(); + ~FastnoiseNoiseParams(); + +protected: + static void _bind_methods(); + +private: + int _seed; + FastNoise::Type _noise_type; + FastNoise::Interpolation _interp; + real_t _freq; + unsigned int _octave_count; + real_t _lacunarity; + real_t _gain; + FastNoise::FractalType _type; + FastNoise::CellularDistanceFunction _func; + FastNoise::CellularReturnType _ret; + real_t _jitter; + real_t _amp; +}; + +#endif diff --git a/noise.cpp b/noise.cpp index 21f149b..abd16f3 100644 --- a/noise.cpp +++ b/noise.cpp @@ -30,6 +30,11 @@ #include "noise.h" +const String FastNoise::BINDING_STRING_TYPE = "Value,Value Fractal,Perlin,Perlin Fractal,Simplex,Simplex Fractal,Cellular,White Noise,Cubic,Cubic Fractal"; +const String FastNoise::BINDING_STRING_INTERPOLATION = "Linear,Quintic,Hermite"; +const String FastNoise::BINDING_STRING_FRACTAL_TYPE = "FBM,Billow,Rigid Multi"; +const String FastNoise::BINDING_STRING_CELLULAR_DISTANCE_FUNCTION = "Euclidean,Manhattan,Naural"; +const String FastNoise::BINDING_STRING_CELLULAR_RETURN_TYPE = "Cell Value,Noise lookup,Distance,Distance 2,Distance 2 Add,Distance 2 Sub,Distance 2 Mul,Distance 2 Div"; FastNoise::FastNoise() : Reference() { diff --git a/noise.h b/noise.h index e0b9c55..3e81859 100644 --- a/noise.h +++ b/noise.h @@ -4,12 +4,19 @@ #include "core/reference.h" #include "lib/FastNoise.h" +#include "core/ustring.h" + typedef fastnoise::FastNoise _FastNoise; class FastNoise : public Reference { GDCLASS(FastNoise, Reference) public: + static const String BINDING_STRING_TYPE; + static const String BINDING_STRING_INTERPOLATION; + static const String BINDING_STRING_FRACTAL_TYPE; + static const String BINDING_STRING_CELLULAR_DISTANCE_FUNCTION; + static const String BINDING_STRING_CELLULAR_RETURN_TYPE; // Enums Godot-style (same values) diff --git a/register_types.cpp b/register_types.cpp index 3804cad..b9a0e4f 100644 --- a/register_types.cpp +++ b/register_types.cpp @@ -2,11 +2,13 @@ #include "core/class_db.h" #include "noise.h" +#include "fastnoise_noise_params.h" void register_fastnoise_types() { ClassDB::register_class(); + ClassDB::register_class(); }