summaryrefslogtreecommitdiff
path: root/modules/opensimplex/open_simplex_noise.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'modules/opensimplex/open_simplex_noise.cpp')
-rw-r--r--modules/opensimplex/open_simplex_noise.cpp70
1 files changed, 30 insertions, 40 deletions
diff --git a/modules/opensimplex/open_simplex_noise.cpp b/modules/opensimplex/open_simplex_noise.cpp
index 238faa4130..e4e2e0613a 100644
--- a/modules/opensimplex/open_simplex_noise.cpp
+++ b/modules/opensimplex/open_simplex_noise.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -33,7 +33,6 @@
#include "core/core_string_names.h"
OpenSimplexNoise::OpenSimplexNoise() {
-
seed = 0;
persistence = 0.5;
octaves = 3;
@@ -53,9 +52,9 @@ void OpenSimplexNoise::_init_seeds() {
}
void OpenSimplexNoise::set_seed(int p_seed) {
-
- if (seed == p_seed)
+ if (seed == p_seed) {
return;
+ }
seed = p_seed;
@@ -64,13 +63,14 @@ void OpenSimplexNoise::set_seed(int p_seed) {
emit_changed();
}
-int OpenSimplexNoise::get_seed() {
-
+int OpenSimplexNoise::get_seed() const {
return seed;
}
void OpenSimplexNoise::set_octaves(int p_octaves) {
- if (p_octaves == octaves) return;
+ if (p_octaves == octaves) {
+ return;
+ }
ERR_FAIL_COND_MSG(p_octaves > MAX_OCTAVES, vformat("The number of OpenSimplexNoise octaves is limited to %d; ignoring the new value.", MAX_OCTAVES));
@@ -79,56 +79,55 @@ void OpenSimplexNoise::set_octaves(int p_octaves) {
}
void OpenSimplexNoise::set_period(float p_period) {
- if (p_period == period) return;
+ if (p_period == period) {
+ return;
+ }
period = p_period;
emit_changed();
}
void OpenSimplexNoise::set_persistence(float p_persistence) {
- if (p_persistence == persistence) return;
+ if (p_persistence == persistence) {
+ return;
+ }
persistence = p_persistence;
emit_changed();
}
void OpenSimplexNoise::set_lacunarity(float p_lacunarity) {
- if (p_lacunarity == lacunarity) return;
+ if (p_lacunarity == lacunarity) {
+ return;
+ }
lacunarity = p_lacunarity;
emit_changed();
}
-Ref<Image> OpenSimplexNoise::get_image(int p_width, int p_height) {
-
+Ref<Image> OpenSimplexNoise::get_image(int p_width, int p_height) const {
Vector<uint8_t> data;
- data.resize(p_width * p_height * 4);
+ data.resize(p_width * p_height);
uint8_t *wd8 = data.ptrw();
for (int i = 0; i < p_height; i++) {
for (int j = 0; j < p_width; j++) {
- float v = get_noise_2d(i, j);
+ float v = get_noise_2d(j, i);
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;
+ wd8[(i * p_width + j)] = uint8_t(CLAMP(v * 255.0, 0, 255));
}
}
- Ref<Image> image = memnew(Image(p_width, p_height, false, Image::FORMAT_RGBA8, data));
+ Ref<Image> image = memnew(Image(p_width, p_height, false, Image::FORMAT_L8, data));
return image;
}
-Ref<Image> OpenSimplexNoise::get_seamless_image(int p_size) {
-
+Ref<Image> OpenSimplexNoise::get_seamless_image(int p_size) const {
Vector<uint8_t> data;
- data.resize(p_size * p_size * 4);
+ data.resize(p_size * p_size);
uint8_t *wd8 = data.ptrw();
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;
@@ -144,20 +143,15 @@ Ref<Image> OpenSimplexNoise::get_seamless_image(int p_size) {
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;
+ wd8[(i * p_size + j)] = uint8_t(CLAMP(v * 255.0, 0, 255));
}
}
- Ref<Image> image = memnew(Image(p_size, p_size, false, Image::FORMAT_RGBA8, data));
+ Ref<Image> image = memnew(Image(p_size, p_size, false, Image::FORMAT_L8, 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);
@@ -191,13 +185,11 @@ void OpenSimplexNoise::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "lacunarity", PROPERTY_HINT_RANGE, "0.1,4.0,0.01"), "set_lacunarity", "get_lacunarity");
}
-float OpenSimplexNoise::get_noise_1d(float x) {
-
+float OpenSimplexNoise::get_noise_1d(float x) const {
return get_noise_2d(x, 1.0);
}
-float OpenSimplexNoise::get_noise_2d(float x, float y) {
-
+float OpenSimplexNoise::get_noise_2d(float x, float y) const {
x /= period;
y /= period;
@@ -217,8 +209,7 @@ float OpenSimplexNoise::get_noise_2d(float x, float y) {
return sum / max;
}
-float OpenSimplexNoise::get_noise_3d(float x, float y, float z) {
-
+float OpenSimplexNoise::get_noise_3d(float x, float y, float z) const {
x /= period;
y /= period;
z /= period;
@@ -240,8 +231,7 @@ float OpenSimplexNoise::get_noise_3d(float x, float y, float z) {
return sum / max;
}
-float OpenSimplexNoise::get_noise_4d(float x, float y, float z, float w) {
-
+float OpenSimplexNoise::get_noise_4d(float x, float y, float z, float w) const {
x /= period;
y /= period;
z /= period;