You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-06 12:20:30 +00:00
Restructure and refine the noise module
This commit is contained in:
@@ -30,13 +30,13 @@
|
||||
|
||||
#include "noise.h"
|
||||
|
||||
Ref<Image> Noise::get_seamless_image(int p_width, int p_height, bool p_invert, real_t p_blend_skirt) {
|
||||
Ref<Image> Noise::get_seamless_image(int p_width, int p_height, bool p_invert, bool p_in_3d_space, real_t p_blend_skirt) const {
|
||||
int skirt_width = p_width * p_blend_skirt;
|
||||
int skirt_height = p_height * p_blend_skirt;
|
||||
int src_width = p_width + skirt_width;
|
||||
int src_height = p_height + skirt_height;
|
||||
|
||||
Ref<Image> src = get_image(src_width, src_height, p_invert);
|
||||
Ref<Image> src = get_image(src_width, src_height, p_invert, p_in_3d_space);
|
||||
bool grayscale = (src->get_format() == Image::FORMAT_L8);
|
||||
if (grayscale) {
|
||||
return _generate_seamless_image<uint8_t>(src, p_width, p_height, p_invert, p_blend_skirt);
|
||||
@@ -54,6 +54,50 @@ uint8_t Noise::_alpha_blend<uint8_t>(uint8_t p_bg, uint8_t p_fg, int p_alpha) co
|
||||
return (uint8_t)((alpha * p_fg + inv_alpha * p_bg) >> 8);
|
||||
}
|
||||
|
||||
Ref<Image> Noise::get_image(int p_width, int p_height, bool p_invert, bool p_in_3d_space) const {
|
||||
Vector<uint8_t> data;
|
||||
data.resize(p_width * p_height);
|
||||
|
||||
uint8_t *wd8 = data.ptrw();
|
||||
|
||||
// Get all values and identify min/max values.
|
||||
Vector<real_t> values;
|
||||
values.resize(p_width * p_height);
|
||||
real_t min_val = 1000;
|
||||
real_t max_val = -1000;
|
||||
|
||||
for (int y = 0, i = 0; y < p_height; y++) {
|
||||
for (int x = 0; x < p_width; x++, i++) {
|
||||
values.set(i, p_in_3d_space ? get_noise_3d(x, y, 0.0) : get_noise_2d(x, y));
|
||||
if (values[i] > max_val) {
|
||||
max_val = values[i];
|
||||
}
|
||||
if (values[i] < min_val) {
|
||||
min_val = values[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Normalize values and write to texture.
|
||||
uint8_t value;
|
||||
for (int i = 0, x = 0; i < p_height; i++) {
|
||||
for (int j = 0; j < p_width; j++, x++) {
|
||||
if (max_val == min_val) {
|
||||
value = 0;
|
||||
} else {
|
||||
value = uint8_t(CLAMP((values[x] - min_val) / (max_val - min_val) * 255.f, 0, 255));
|
||||
}
|
||||
if (p_invert) {
|
||||
value = 255 - value;
|
||||
}
|
||||
|
||||
wd8[x] = value;
|
||||
}
|
||||
}
|
||||
|
||||
return memnew(Image(p_width, p_height, false, Image::FORMAT_L8, data));
|
||||
}
|
||||
|
||||
void Noise::_bind_methods() {
|
||||
// Noise functions.
|
||||
ClassDB::bind_method(D_METHOD("get_noise_1d", "x"), &Noise::get_noise_1d);
|
||||
@@ -63,6 +107,6 @@ void Noise::_bind_methods() {
|
||||
ClassDB::bind_method(D_METHOD("get_noise_3dv", "v"), &Noise::get_noise_3dv);
|
||||
|
||||
// Textures.
|
||||
ClassDB::bind_method(D_METHOD("get_image", "width", "height", "invert"), &Noise::get_image, DEFVAL(false));
|
||||
ClassDB::bind_method(D_METHOD("get_seamless_image", "width", "height", "invert", "skirt"), &Noise::get_seamless_image, DEFVAL(false), DEFVAL(0.1));
|
||||
ClassDB::bind_method(D_METHOD("get_image", "width", "height", "invert", "in_3d_space"), &Noise::get_image, DEFVAL(false), DEFVAL(false));
|
||||
ClassDB::bind_method(D_METHOD("get_seamless_image", "width", "height", "invert", "in_3d_space", "skirt"), &Noise::get_seamless_image, DEFVAL(false), DEFVAL(false), DEFVAL(0.1));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user