1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-22 15:06:45 +00:00

Optimize data format for OpenSimplex images

The previous RGBA format included unused RGB data. Using the LA8 format
removes the need to store the extra data.

The Docs have been updated to reflect the format changes.

(cherry picked from commit 041fe20f64)
This commit is contained in:
nevarek
2021-01-06 19:01:21 -08:00
committed by Rémi Verschelde
parent 64cbff7e7d
commit 2167b977de
3 changed files with 9 additions and 17 deletions

View File

@@ -99,7 +99,7 @@ void OpenSimplexNoise::set_lacunarity(float p_lacunarity) {
Ref<Image> OpenSimplexNoise::get_image(int p_width, int p_height) const {
PoolVector<uint8_t> data;
data.resize(p_width * p_height * 4);
data.resize(p_width * p_height);
PoolVector<uint8_t>::Write wd8 = data.write();
@@ -107,22 +107,18 @@ Ref<Image> OpenSimplexNoise::get_image(int p_width, int p_height) const {
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;
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) const {
PoolVector<uint8_t> data;
data.resize(p_size * p_size * 4);
data.resize(p_size * p_size);
PoolVector<uint8_t>::Write wd8 = data.write();
@@ -144,15 +140,11 @@ Ref<Image> OpenSimplexNoise::get_seamless_image(int p_size) const {
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;
}