From 2694053be34f64d15181114df9042c1f3a3ffc39 Mon Sep 17 00:00:00 2001 From: santouits Date: Fri, 28 Sep 2018 14:30:07 +0300 Subject: Renaming SimplexNoise refs to OpenSimplexNoise Because I think github doesn't show history of renamed files - The original work is done in commit f12a1b8 by JFonS - Improved Documentation in commit a386af6 - Last change on the files was in commit 463af5b - and Fixed compiler warnings in commit e5bbcb8bcf8180a54fdcc5c4291b153246d861ce --- modules/opensimplex/SCsub | 2 +- modules/opensimplex/doc_classes/NoiseTexture.xml | 8 +- .../opensimplex/doc_classes/OpenSimplexNoise.xml | 126 ++++++++++ modules/opensimplex/doc_classes/SimplexNoise.xml | 126 ---------- modules/opensimplex/noise_texture.cpp | 8 +- modules/opensimplex/noise_texture.h | 8 +- modules/opensimplex/open_simplex_noise.cpp | 257 +++++++++++++++++++++ modules/opensimplex/open_simplex_noise.h | 93 ++++++++ modules/opensimplex/register_types.cpp | 4 +- modules/opensimplex/simplex_noise.cpp | 257 --------------------- modules/opensimplex/simplex_noise.h | 93 -------- 11 files changed, 491 insertions(+), 491 deletions(-) create mode 100644 modules/opensimplex/doc_classes/OpenSimplexNoise.xml delete mode 100644 modules/opensimplex/doc_classes/SimplexNoise.xml create mode 100644 modules/opensimplex/open_simplex_noise.cpp create mode 100644 modules/opensimplex/open_simplex_noise.h delete mode 100644 modules/opensimplex/simplex_noise.cpp delete mode 100644 modules/opensimplex/simplex_noise.h diff --git a/modules/opensimplex/SCsub b/modules/opensimplex/SCsub index be9c8b091f..e6b2f41162 100644 --- a/modules/opensimplex/SCsub +++ b/modules/opensimplex/SCsub @@ -1,4 +1,4 @@ #!/usr/bin/env python Import('env') -env.add_source_files(env.modules_sources, ["register_types.cpp", "simplex_noise.cpp", "noise_texture.cpp", "#thirdparty/misc/open-simplex-noise.c"]) +env.add_source_files(env.modules_sources, ["register_types.cpp", "open_simplex_noise.cpp", "noise_texture.cpp", "#thirdparty/misc/open-simplex-noise.c"]) diff --git a/modules/opensimplex/doc_classes/NoiseTexture.xml b/modules/opensimplex/doc_classes/NoiseTexture.xml index c7dc373f59..6af58e7a6b 100644 --- a/modules/opensimplex/doc_classes/NoiseTexture.xml +++ b/modules/opensimplex/doc_classes/NoiseTexture.xml @@ -1,10 +1,10 @@ - [SimplexNoise] filled texture. + [OpenSimplexNoise] filled texture. - Uses a [SimplexNoise] to fill the texture data. You can specify the texture size but keep in mind that larger textures will take longer to generate and seamless noise only works with square sized textures. + Uses an [OpenSimplexNoise] to fill the texture data. You can specify the texture size but keep in mind that larger textures will take longer to generate and seamless noise only works with square sized textures. NoiseTexture can also generate normalmap textures. @@ -35,8 +35,8 @@ If true, the resulting texture contains a normal map created from the original noise interpreted as a bump map. - - The [SimplexNoise] instance used to generate the noise. + + The [OpenSimplexNoise] instance used to generate the noise. Whether the texture can be tiled without visible seams or not. Seamless textures take longer to generate. diff --git a/modules/opensimplex/doc_classes/OpenSimplexNoise.xml b/modules/opensimplex/doc_classes/OpenSimplexNoise.xml new file mode 100644 index 0000000000..31f13f341c --- /dev/null +++ b/modules/opensimplex/doc_classes/OpenSimplexNoise.xml @@ -0,0 +1,126 @@ + + + + Noise generator based on Open Simplex. + + + This resource allows you to configure and sample a fractal noise space. Here is a brief usage example that configures an OpenSimplexNoise and gets samples at various positions and dimensions: + [codeblock] + var noise = OpenSimplexNoise.new() + + # Configure + noise.seed = randi() + noise.octaves = 4 + noise.period = 20.0 + noise.persistence = 0.8 + + # Sample + print("Values:") + print(noise.get_noise_2d(1.0, 1.0)) + print(noise.get_noise_3d(0.5, 3.0, 15.0)) + print(noise.get_noise_4d(0.5, 1.9, 4.7, 0.0)) + [/codeblock] + + + + + + + + + + + + + + + Generate a noise image with the requested [code]width[/code] and [code]height[/code], based on the current noise parameters. + + + + + + + + + + + Returns the 2D noise value [code][-1,1][/code] at the given position. + + + + + + + + + Returns the 2D noise value [code][-1,1][/code] at the given position. + + + + + + + + + + + + + Returns the 3D noise value [code][-1,1][/code] at the given position. + + + + + + + + + Returns the 3D noise value [code][-1,1][/code] at the given position. + + + + + + + + + + + + + + + Returns the 4D noise value [code][-1,1][/code] at the given position. + + + + + + + + + Generate a tileable noise image, based on the current noise parameters. Generated seamless images are always square ([code]size[/code] x [code]size[/code]). + + + + + + Difference in period between [member octaves]. + + + Number of OpenSimplex noise layers that are sampled to get the fractal noise. + + + Period of the base octave. A lower period results in a higher-frequency noise (more value changes across the same distance). + + + Contribution factor of the different octaves. A [code]persistence[/code] value of 1 means all the octaves have the same contribution, a value of 0.5 means each octave contributes half as much as the previous one. + + + Seed used to generate random values, different seeds will generate different noise maps. + + + + + diff --git a/modules/opensimplex/doc_classes/SimplexNoise.xml b/modules/opensimplex/doc_classes/SimplexNoise.xml deleted file mode 100644 index e5e0c15b3c..0000000000 --- a/modules/opensimplex/doc_classes/SimplexNoise.xml +++ /dev/null @@ -1,126 +0,0 @@ - - - - Noise generator based on Open Simplex. - - - This resource allows you to configure and sample a fractal noise space. Here is a brief usage example that configures a SimplexNoise and gets samples at various positions and dimensions: - [codeblock] - var noise = SimplexNoise.new() - - # Configure - noise.seed = randi() - noise.octaves = 4 - noise.period = 20.0 - noise.persistence = 0.8 - - # Sample - print("Values:") - print(noise.get_noise_2d(1.0, 1.0)) - print(noise.get_noise_3d(0.5, 3.0, 15.0)) - print(noise.get_noise_4d(0.5, 1.9, 4.7, 0.0)) - [/codeblock] - - - - - - - - - - - - - - - Generate a noise image with the requested [code]width[/code] and [code]height[/code], based on the current noise parameters. - - - - - - - - - - - Returns the 2D noise value [code][-1,1][/code] at the given position. - - - - - - - - - Returns the 2D noise value [code][-1,1][/code] at the given position. - - - - - - - - - - - - - Returns the 3D noise value [code][-1,1][/code] at the given position. - - - - - - - - - Returns the 3D noise value [code][-1,1][/code] at the given position. - - - - - - - - - - - - - - - Returns the 4D noise value [code][-1,1][/code] at the given position. - - - - - - - - - Generate a tileable noise image, based on the current noise parameters. Generated seamless images are always square ([code]size[/code] x [code]size[/code]). - - - - - - Difference in period between [member octaves]. - - - Number of Simplex noise layers that are sampled to get the fractal noise. - - - Period of the base octave. A lower period results in a higher-frequency noise (more value changes across the same distance). - - - Contribution factor of the different octaves. A [code]persistence[/code] value of 1 means all the octaves have the same contribution, a value of 0.5 means each octave contributes half as much as the previous one. - - - Seed used to generate random values, different seeds will generate different noise maps. - - - - - diff --git a/modules/opensimplex/noise_texture.cpp b/modules/opensimplex/noise_texture.cpp index b82b0b453f..6f2723e43b 100644 --- a/modules/opensimplex/noise_texture.cpp +++ b/modules/opensimplex/noise_texture.cpp @@ -43,7 +43,7 @@ NoiseTexture::NoiseTexture() { as_normalmap = false; flags = FLAGS_DEFAULT; - noise = Ref(); + noise = Ref(); texture = VS::get_singleton()->texture_create(); @@ -76,7 +76,7 @@ void NoiseTexture::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "size"), "set_size", "get_size"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "seamless"), "set_seamless", "get_seamless"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "as_normalmap"), "set_as_normalmap", "is_normalmap"); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "noise", PROPERTY_HINT_RESOURCE_TYPE, "SimplexNoise"), "set_noise", "get_noise"); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "noise", PROPERTY_HINT_RESOURCE_TYPE, "OpenSimplexNoise"), "set_noise", "get_noise"); } void NoiseTexture::_set_texture_data(const Ref &p_image) { @@ -159,7 +159,7 @@ void NoiseTexture::_update_texture() { } } -void NoiseTexture::set_noise(Ref p_noise) { +void NoiseTexture::set_noise(Ref p_noise) { if (p_noise == noise) return; if (noise.is_valid()) { @@ -172,7 +172,7 @@ void NoiseTexture::set_noise(Ref p_noise) { _queue_update(); } -Ref NoiseTexture::get_noise() { +Ref NoiseTexture::get_noise() { return noise; } diff --git a/modules/opensimplex/noise_texture.h b/modules/opensimplex/noise_texture.h index 108e471fc1..78a02cda9f 100644 --- a/modules/opensimplex/noise_texture.h +++ b/modules/opensimplex/noise_texture.h @@ -31,7 +31,7 @@ #ifndef NOISE_TEXTURE_H #define NOISE_TEXTURE_H -#include "simplex_noise.h" +#include "open_simplex_noise.h" #include "core/image.h" #include "core/reference.h" @@ -54,7 +54,7 @@ private: RID texture; uint32_t flags; - Ref noise; + Ref noise; Vector2i size; bool seamless; bool as_normalmap; @@ -71,8 +71,8 @@ protected: static void _bind_methods(); public: - void set_noise(Ref p_noise); - Ref get_noise(); + void set_noise(Ref p_noise); + Ref get_noise(); void set_width(int p_width); void set_height(int p_hieght); diff --git a/modules/opensimplex/open_simplex_noise.cpp b/modules/opensimplex/open_simplex_noise.cpp new file mode 100644 index 0000000000..bfc2732ff4 --- /dev/null +++ b/modules/opensimplex/open_simplex_noise.cpp @@ -0,0 +1,257 @@ +/*************************************************************************/ +/* open_simplex_noise.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "open_simplex_noise.h" + +#include "core/core_string_names.h" + +OpenSimplexNoise::OpenSimplexNoise() { + + seed = 0; + persistence = 0.5; + octaves = 3; + period = 64; + lacunarity = 2.0; + + _init_seeds(); +} + +OpenSimplexNoise::~OpenSimplexNoise() { +} + +void OpenSimplexNoise::_init_seeds() { + for (int i = 0; i < 6; ++i) { + open_simplex_noise(seed + i * 2, &(contexts[i])); + } +} + +void OpenSimplexNoise::set_seed(int p_seed) { + + if (seed == p_seed) + return; + + seed = p_seed; + + _init_seeds(); + + emit_changed(); +} + +int OpenSimplexNoise::get_seed() { + + return seed; +} + +void OpenSimplexNoise::set_octaves(int p_octaves) { + if (p_octaves == octaves) return; + octaves = CLAMP(p_octaves, 1, 6); + emit_changed(); +} + +void OpenSimplexNoise::set_period(float p_period) { + if (p_period == period) return; + period = p_period; + emit_changed(); +} + +void OpenSimplexNoise::set_persistence(float p_persistence) { + if (p_persistence == persistence) return; + persistence = p_persistence; + emit_changed(); +} + +void OpenSimplexNoise::set_lacunarity(float p_lacunarity) { + if (p_lacunarity == lacunarity) return; + lacunarity = p_lacunarity; + emit_changed(); +} + +Ref OpenSimplexNoise::get_image(int p_width, int p_height) { + + PoolVector data; + data.resize(p_width * p_height * 4); + + PoolVector::Write wd8 = data.write(); + + for (int i = 0; i < p_height; i++) { + for (int j = 0; j < p_width; j++) { + float v = get_noise_2d(i, j); + v = v * 0.5 + 0.5; // Normalize [0..1] + uint8_t value = uint8_t(CLAMP(v * 255.0, 0, 255)); + wd8[(i * p_width + j) * 4 + 0] = value; + wd8[(i * p_width + j) * 4 + 1] = value; + wd8[(i * p_width + j) * 4 + 2] = value; + wd8[(i * p_width + j) * 4 + 3] = 255; + } + } + + Ref image = memnew(Image(p_width, p_height, false, Image::FORMAT_RGBA8, data)); + return image; +} + +Ref OpenSimplexNoise::get_seamless_image(int p_size) { + + PoolVector data; + data.resize(p_size * p_size * 4); + + PoolVector::Write wd8 = data.write(); + + for (int i = 0; i < p_size; i++) { + for (int j = 0; j < p_size; j++) { + + float ii = (float)i / (float)p_size; + float jj = (float)j / (float)p_size; + + ii *= 2.0 * Math_PI; + jj *= 2.0 * Math_PI; + + float radius = p_size / (2.0 * Math_PI); + + float x = radius * Math::sin(jj); + float y = radius * Math::cos(jj); + float z = radius * Math::sin(ii); + float w = radius * Math::cos(ii); + float v = get_noise_4d(x, y, z, w); + + v = v * 0.5 + 0.5; // Normalize [0..1] + uint8_t value = uint8_t(CLAMP(v * 255.0, 0, 255)); + wd8[(i * p_size + j) * 4 + 0] = value; + wd8[(i * p_size + j) * 4 + 1] = value; + wd8[(i * p_size + j) * 4 + 2] = value; + wd8[(i * p_size + j) * 4 + 3] = 255; + } + } + + Ref image = memnew(Image(p_size, p_size, false, Image::FORMAT_RGBA8, data)); + return image; +} + +void OpenSimplexNoise::_bind_methods() { + + ClassDB::bind_method(D_METHOD("get_seed"), &OpenSimplexNoise::get_seed); + ClassDB::bind_method(D_METHOD("set_seed", "seed"), &OpenSimplexNoise::set_seed); + + ClassDB::bind_method(D_METHOD("set_octaves", "octave_count"), &OpenSimplexNoise::set_octaves); + ClassDB::bind_method(D_METHOD("get_octaves"), &OpenSimplexNoise::get_octaves); + + ClassDB::bind_method(D_METHOD("set_period", "period"), &OpenSimplexNoise::set_period); + ClassDB::bind_method(D_METHOD("get_period"), &OpenSimplexNoise::get_period); + + ClassDB::bind_method(D_METHOD("set_persistence", "persistence"), &OpenSimplexNoise::set_persistence); + ClassDB::bind_method(D_METHOD("get_persistence"), &OpenSimplexNoise::get_persistence); + + ClassDB::bind_method(D_METHOD("set_lacunarity", "lacunarity"), &OpenSimplexNoise::set_lacunarity); + ClassDB::bind_method(D_METHOD("get_lacunarity"), &OpenSimplexNoise::get_lacunarity); + + ClassDB::bind_method(D_METHOD("get_image", "width", "height"), &OpenSimplexNoise::get_image); + ClassDB::bind_method(D_METHOD("get_seamless_image", "size"), &OpenSimplexNoise::get_seamless_image); + + ClassDB::bind_method(D_METHOD("get_noise_2d", "x", "y"), &OpenSimplexNoise::get_noise_2d); + ClassDB::bind_method(D_METHOD("get_noise_3d", "x", "y", "z"), &OpenSimplexNoise::get_noise_3d); + ClassDB::bind_method(D_METHOD("get_noise_4d", "x", "y", "z", "w"), &OpenSimplexNoise::get_noise_4d); + + ClassDB::bind_method(D_METHOD("get_noise_2dv", "pos"), &OpenSimplexNoise::get_noise_2dv); + ClassDB::bind_method(D_METHOD("get_noise_3dv", "pos"), &OpenSimplexNoise::get_noise_3dv); + + ADD_PROPERTY(PropertyInfo(Variant::INT, "seed"), "set_seed", "get_seed"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "octaves", PROPERTY_HINT_RANGE, "1,6,1"), "set_octaves", "get_octaves"); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "period", PROPERTY_HINT_RANGE, "0.1,256.0,0.1"), "set_period", "get_period"); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "persistence", PROPERTY_HINT_RANGE, "0.0,1.0,0.001"), "set_persistence", "get_persistence"); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "lacunarity", PROPERTY_HINT_RANGE, "0.1,4.0,0.01"), "set_lacunarity", "get_lacunarity"); +} + +float OpenSimplexNoise::get_noise_2d(float x, float y) { + + x /= period; + y /= period; + + float amp = 1.0; + float max = 1.0; + float sum = _get_octave_noise_2d(0, x, y); + + int i = 0; + while (++i < octaves) { + x *= lacunarity; + y *= lacunarity; + amp *= persistence; + max += amp; + sum += _get_octave_noise_2d(i, x, y) * amp; + } + + return sum / max; +} + +float OpenSimplexNoise::get_noise_3d(float x, float y, float z) { + + x /= period; + y /= period; + z /= period; + + float amp = 1.0; + float max = 1.0; + float sum = _get_octave_noise_3d(0, x, y, z); + + int i = 0; + while (++i < octaves) { + x *= lacunarity; + y *= lacunarity; + z *= lacunarity; + amp *= persistence; + max += amp; + sum += _get_octave_noise_3d(i, x, y, z) * amp; + } + + return sum / max; +} + +float OpenSimplexNoise::get_noise_4d(float x, float y, float z, float w) { + + x /= period; + y /= period; + z /= period; + w /= period; + + float amp = 1.0; + float max = 1.0; + float sum = _get_octave_noise_4d(0, x, y, z, w); + + int i = 0; + while (++i < octaves) { + x *= lacunarity; + y *= lacunarity; + z *= lacunarity; + w *= lacunarity; + amp *= persistence; + max += amp; + sum += _get_octave_noise_4d(i, x, y, z, w) * amp; + } + + return sum / max; +} diff --git a/modules/opensimplex/open_simplex_noise.h b/modules/opensimplex/open_simplex_noise.h new file mode 100644 index 0000000000..a9bee266e8 --- /dev/null +++ b/modules/opensimplex/open_simplex_noise.h @@ -0,0 +1,93 @@ +/*************************************************************************/ +/* open_simplex_noise.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef OPEN_SIMPLEX_NOISE_H +#define OPEN_SIMPLEX_NOISE_H + +#include "core/image.h" +#include "core/reference.h" +#include "scene/resources/texture.h" + +#include "thirdparty/misc/open-simplex-noise.h" + +class OpenSimplexNoise : public Resource { + GDCLASS(OpenSimplexNoise, Resource) + OBJ_SAVE_TYPE(OpenSimplexNoise); + + osn_context contexts[6]; + + int seed; + float persistence; // Controls details, value in [0,1]. Higher increases grain, lower increases smoothness. + int octaves; // Number of noise layers + float period; // Distance above which we start to see similarities. The higher, the longer "hills" will be on a terrain. + float lacunarity; // Controls period change across octaves. 2 is usually a good value to address all detail levels. + +public: + OpenSimplexNoise(); + ~OpenSimplexNoise(); + + void _init_seeds(); + + void set_seed(int seed); + int get_seed(); + + void set_octaves(int p_octaves); + int get_octaves() const { return octaves; } + + void set_period(float p_period); + float get_period() const { return period; } + + void set_persistence(float p_persistence); + float get_persistence() const { return persistence; } + + void set_lacunarity(float p_lacunarity); + float get_lacunarity() const { return lacunarity; } + + Ref get_image(int p_width, int p_height); + Ref get_seamless_image(int p_size); + + float get_noise_2d(float x, float y); + float get_noise_3d(float x, float y, float z); + float get_noise_4d(float x, float y, float z, float w); + + _FORCE_INLINE_ float _get_octave_noise_2d(int octave, float x, float y) { return open_simplex_noise2(&(contexts[octave]), x, y); } + _FORCE_INLINE_ float _get_octave_noise_3d(int octave, float x, float y, float z) { return open_simplex_noise3(&(contexts[octave]), x, y, z); } + _FORCE_INLINE_ float _get_octave_noise_4d(int octave, float x, float y, float z, float w) { return open_simplex_noise4(&(contexts[octave]), x, y, z, w); } + + // Convenience + + _FORCE_INLINE_ float get_noise_2dv(Vector2 v) { return get_noise_2d(v.x, v.y); } + _FORCE_INLINE_ float get_noise_3dv(Vector3 v) { return get_noise_3d(v.x, v.y, v.z); } + +protected: + static void _bind_methods(); +}; + +#endif // OPEN_SIMPLEX_NOISE_H diff --git a/modules/opensimplex/register_types.cpp b/modules/opensimplex/register_types.cpp index 9e4af99651..d1c77da257 100644 --- a/modules/opensimplex/register_types.cpp +++ b/modules/opensimplex/register_types.cpp @@ -30,11 +30,11 @@ #include "register_types.h" #include "noise_texture.h" -#include "simplex_noise.h" +#include "open_simplex_noise.h" void register_opensimplex_types() { - ClassDB::register_class(); + ClassDB::register_class(); ClassDB::register_class(); } diff --git a/modules/opensimplex/simplex_noise.cpp b/modules/opensimplex/simplex_noise.cpp deleted file mode 100644 index c99fd7b123..0000000000 --- a/modules/opensimplex/simplex_noise.cpp +++ /dev/null @@ -1,257 +0,0 @@ -/*************************************************************************/ -/* simplex_noise.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "simplex_noise.h" - -#include "core/core_string_names.h" - -SimplexNoise::SimplexNoise() { - - seed = 0; - persistence = 0.5; - octaves = 3; - period = 64; - lacunarity = 2.0; - - _init_seeds(); -} - -SimplexNoise::~SimplexNoise() { -} - -void SimplexNoise::_init_seeds() { - for (int i = 0; i < 6; ++i) { - open_simplex_noise(seed + i * 2, &(contexts[i])); - } -} - -void SimplexNoise::set_seed(int p_seed) { - - if (seed == p_seed) - return; - - seed = p_seed; - - _init_seeds(); - - emit_changed(); -} - -int SimplexNoise::get_seed() { - - return seed; -} - -void SimplexNoise::set_octaves(int p_octaves) { - if (p_octaves == octaves) return; - octaves = CLAMP(p_octaves, 1, 6); - emit_changed(); -} - -void SimplexNoise::set_period(float p_period) { - if (p_period == period) return; - period = p_period; - emit_changed(); -} - -void SimplexNoise::set_persistence(float p_persistence) { - if (p_persistence == persistence) return; - persistence = p_persistence; - emit_changed(); -} - -void SimplexNoise::set_lacunarity(float p_lacunarity) { - if (p_lacunarity == lacunarity) return; - lacunarity = p_lacunarity; - emit_changed(); -} - -Ref SimplexNoise::get_image(int p_width, int p_height) { - - PoolVector data; - data.resize(p_width * p_height * 4); - - PoolVector::Write wd8 = data.write(); - - for (int i = 0; i < p_height; i++) { - for (int j = 0; j < p_width; j++) { - float v = get_noise_2d(i, j); - v = v * 0.5 + 0.5; // Normalize [0..1] - uint8_t value = uint8_t(CLAMP(v * 255.0, 0, 255)); - wd8[(i * p_width + j) * 4 + 0] = value; - wd8[(i * p_width + j) * 4 + 1] = value; - wd8[(i * p_width + j) * 4 + 2] = value; - wd8[(i * p_width + j) * 4 + 3] = 255; - } - } - - Ref image = memnew(Image(p_width, p_height, false, Image::FORMAT_RGBA8, data)); - return image; -} - -Ref SimplexNoise::get_seamless_image(int p_size) { - - PoolVector data; - data.resize(p_size * p_size * 4); - - PoolVector::Write wd8 = data.write(); - - for (int i = 0; i < p_size; i++) { - for (int j = 0; j < p_size; j++) { - - float ii = (float)i / (float)p_size; - float jj = (float)j / (float)p_size; - - ii *= 2.0 * Math_PI; - jj *= 2.0 * Math_PI; - - float radius = p_size / (2.0 * Math_PI); - - float x = radius * Math::sin(jj); - float y = radius * Math::cos(jj); - float z = radius * Math::sin(ii); - float w = radius * Math::cos(ii); - float v = get_noise_4d(x, y, z, w); - - v = v * 0.5 + 0.5; // Normalize [0..1] - uint8_t value = uint8_t(CLAMP(v * 255.0, 0, 255)); - wd8[(i * p_size + j) * 4 + 0] = value; - wd8[(i * p_size + j) * 4 + 1] = value; - wd8[(i * p_size + j) * 4 + 2] = value; - wd8[(i * p_size + j) * 4 + 3] = 255; - } - } - - Ref image = memnew(Image(p_size, p_size, false, Image::FORMAT_RGBA8, data)); - return image; -} - -void SimplexNoise::_bind_methods() { - - ClassDB::bind_method(D_METHOD("get_seed"), &SimplexNoise::get_seed); - ClassDB::bind_method(D_METHOD("set_seed", "seed"), &SimplexNoise::set_seed); - - ClassDB::bind_method(D_METHOD("set_octaves", "octave_count"), &SimplexNoise::set_octaves); - ClassDB::bind_method(D_METHOD("get_octaves"), &SimplexNoise::get_octaves); - - ClassDB::bind_method(D_METHOD("set_period", "period"), &SimplexNoise::set_period); - ClassDB::bind_method(D_METHOD("get_period"), &SimplexNoise::get_period); - - ClassDB::bind_method(D_METHOD("set_persistence", "persistence"), &SimplexNoise::set_persistence); - ClassDB::bind_method(D_METHOD("get_persistence"), &SimplexNoise::get_persistence); - - ClassDB::bind_method(D_METHOD("set_lacunarity", "lacunarity"), &SimplexNoise::set_lacunarity); - ClassDB::bind_method(D_METHOD("get_lacunarity"), &SimplexNoise::get_lacunarity); - - ClassDB::bind_method(D_METHOD("get_image", "width", "height"), &SimplexNoise::get_image); - ClassDB::bind_method(D_METHOD("get_seamless_image", "size"), &SimplexNoise::get_seamless_image); - - ClassDB::bind_method(D_METHOD("get_noise_2d", "x", "y"), &SimplexNoise::get_noise_2d); - ClassDB::bind_method(D_METHOD("get_noise_3d", "x", "y", "z"), &SimplexNoise::get_noise_3d); - ClassDB::bind_method(D_METHOD("get_noise_4d", "x", "y", "z", "w"), &SimplexNoise::get_noise_4d); - - ClassDB::bind_method(D_METHOD("get_noise_2dv", "pos"), &SimplexNoise::get_noise_2dv); - ClassDB::bind_method(D_METHOD("get_noise_3dv", "pos"), &SimplexNoise::get_noise_3dv); - - ADD_PROPERTY(PropertyInfo(Variant::INT, "seed"), "set_seed", "get_seed"); - ADD_PROPERTY(PropertyInfo(Variant::INT, "octaves", PROPERTY_HINT_RANGE, "1,6,1"), "set_octaves", "get_octaves"); - ADD_PROPERTY(PropertyInfo(Variant::REAL, "period", PROPERTY_HINT_RANGE, "0.1,256.0,0.1"), "set_period", "get_period"); - ADD_PROPERTY(PropertyInfo(Variant::REAL, "persistence", PROPERTY_HINT_RANGE, "0.0,1.0,0.001"), "set_persistence", "get_persistence"); - ADD_PROPERTY(PropertyInfo(Variant::REAL, "lacunarity", PROPERTY_HINT_RANGE, "0.1,4.0,0.01"), "set_lacunarity", "get_lacunarity"); -} - -float SimplexNoise::get_noise_2d(float x, float y) { - - x /= period; - y /= period; - - float amp = 1.0; - float max = 1.0; - float sum = _get_octave_noise_2d(0, x, y); - - int i = 0; - while (++i < octaves) { - x *= lacunarity; - y *= lacunarity; - amp *= persistence; - max += amp; - sum += _get_octave_noise_2d(i, x, y) * amp; - } - - return sum / max; -} - -float SimplexNoise::get_noise_3d(float x, float y, float z) { - - x /= period; - y /= period; - z /= period; - - float amp = 1.0; - float max = 1.0; - float sum = _get_octave_noise_3d(0, x, y, z); - - int i = 0; - while (++i < octaves) { - x *= lacunarity; - y *= lacunarity; - z *= lacunarity; - amp *= persistence; - max += amp; - sum += _get_octave_noise_3d(i, x, y, z) * amp; - } - - return sum / max; -} - -float SimplexNoise::get_noise_4d(float x, float y, float z, float w) { - - x /= period; - y /= period; - z /= period; - w /= period; - - float amp = 1.0; - float max = 1.0; - float sum = _get_octave_noise_4d(0, x, y, z, w); - - int i = 0; - while (++i < octaves) { - x *= lacunarity; - y *= lacunarity; - z *= lacunarity; - w *= lacunarity; - amp *= persistence; - max += amp; - sum += _get_octave_noise_4d(i, x, y, z, w) * amp; - } - - return sum / max; -} diff --git a/modules/opensimplex/simplex_noise.h b/modules/opensimplex/simplex_noise.h deleted file mode 100644 index 9a48dbf809..0000000000 --- a/modules/opensimplex/simplex_noise.h +++ /dev/null @@ -1,93 +0,0 @@ -/*************************************************************************/ -/* simplex_noise.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#ifndef SIMPLEX_NOISE_H -#define SIMPLEX_NOISE_H - -#include "core/image.h" -#include "core/reference.h" -#include "scene/resources/texture.h" - -#include "thirdparty/misc/open-simplex-noise.h" - -class SimplexNoise : public Resource { - GDCLASS(SimplexNoise, Resource) - OBJ_SAVE_TYPE(SimplexNoise); - - osn_context contexts[6]; - - int seed; - float persistence; // Controls details, value in [0,1]. Higher increases grain, lower increases smoothness. - int octaves; // Number of noise layers - float period; // Distance above which we start to see similarities. The higher, the longer "hills" will be on a terrain. - float lacunarity; // Controls period change across octaves. 2 is usually a good value to address all detail levels. - -public: - SimplexNoise(); - ~SimplexNoise(); - - void _init_seeds(); - - void set_seed(int seed); - int get_seed(); - - void set_octaves(int p_octaves); - int get_octaves() const { return octaves; } - - void set_period(float p_period); - float get_period() const { return period; } - - void set_persistence(float p_persistence); - float get_persistence() const { return persistence; } - - void set_lacunarity(float p_lacunarity); - float get_lacunarity() const { return lacunarity; } - - Ref get_image(int p_width, int p_height); - Ref get_seamless_image(int p_size); - - float get_noise_2d(float x, float y); - float get_noise_3d(float x, float y, float z); - float get_noise_4d(float x, float y, float z, float w); - - _FORCE_INLINE_ float _get_octave_noise_2d(int octave, float x, float y) { return open_simplex_noise2(&(contexts[octave]), x, y); } - _FORCE_INLINE_ float _get_octave_noise_3d(int octave, float x, float y, float z) { return open_simplex_noise3(&(contexts[octave]), x, y, z); } - _FORCE_INLINE_ float _get_octave_noise_4d(int octave, float x, float y, float z, float w) { return open_simplex_noise4(&(contexts[octave]), x, y, z, w); } - - // Convenience - - _FORCE_INLINE_ float get_noise_2dv(Vector2 v) { return get_noise_2d(v.x, v.y); } - _FORCE_INLINE_ float get_noise_3dv(Vector3 v) { return get_noise_3d(v.x, v.y, v.z); } - -protected: - static void _bind_methods(); -}; - -#endif // OPENSIMPLEX_NOISE_H -- cgit v1.2.3