浏览代码

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.
nevarek 4 年之前
父节点
当前提交
041fe20f64
共有 2 个文件被更改,包括 8 次插入16 次删除
  1. 2 2
      modules/opensimplex/doc_classes/OpenSimplexNoise.xml
  2. 6 14
      modules/opensimplex/open_simplex_noise.cpp

+ 2 - 2
modules/opensimplex/doc_classes/OpenSimplexNoise.xml

@@ -32,7 +32,7 @@
 			<argument index="1" name="height" type="int">
 			<argument index="1" name="height" type="int">
 			</argument>
 			</argument>
 			<description>
 			<description>
-				Generate a noise image with the requested [code]width[/code] and [code]height[/code], based on the current noise parameters.
+				Generate a noise image in [constant Image.FORMAT_L8] format with the requested [code]width[/code] and [code]height[/code], based on the current noise parameters.
 			</description>
 			</description>
 		</method>
 		</method>
 		<method name="get_noise_1d" qualifiers="const">
 		<method name="get_noise_1d" qualifiers="const">
@@ -108,7 +108,7 @@
 			<argument index="0" name="size" type="int">
 			<argument index="0" name="size" type="int">
 			</argument>
 			</argument>
 			<description>
 			<description>
-				Generate a tileable noise image, based on the current noise parameters. Generated seamless images are always square ([code]size[/code] × [code]size[/code]).
+				Generate a tileable noise image in [constant Image.FORMAT_L8] format, based on the current noise parameters. Generated seamless images are always square ([code]size[/code] × [code]size[/code]).
 			</description>
 			</description>
 		</method>
 		</method>
 	</methods>
 	</methods>

+ 6 - 14
modules/opensimplex/open_simplex_noise.cpp

@@ -104,7 +104,7 @@ void OpenSimplexNoise::set_lacunarity(float p_lacunarity) {
 
 
 Ref<Image> OpenSimplexNoise::get_image(int p_width, int p_height) const {
 Ref<Image> OpenSimplexNoise::get_image(int p_width, int p_height) const {
 	Vector<uint8_t> data;
 	Vector<uint8_t> data;
-	data.resize(p_width * p_height * 4);
+	data.resize(p_width * p_height);
 
 
 	uint8_t *wd8 = data.ptrw();
 	uint8_t *wd8 = data.ptrw();
 
 
@@ -112,21 +112,17 @@ Ref<Image> OpenSimplexNoise::get_image(int p_width, int p_height) const {
 		for (int j = 0; j < p_width; j++) {
 		for (int j = 0; j < p_width; j++) {
 			float v = get_noise_2d(j, i);
 			float v = get_noise_2d(j, i);
 			v = v * 0.5 + 0.5; // Normalize [0..1]
 			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;
 	return image;
 }
 }
 
 
 Ref<Image> OpenSimplexNoise::get_seamless_image(int p_size) const {
 Ref<Image> OpenSimplexNoise::get_seamless_image(int p_size) const {
 	Vector<uint8_t> data;
 	Vector<uint8_t> data;
-	data.resize(p_size * p_size * 4);
+	data.resize(p_size * p_size);
 
 
 	uint8_t *wd8 = data.ptrw();
 	uint8_t *wd8 = data.ptrw();
 
 
@@ -147,15 +143,11 @@ Ref<Image> OpenSimplexNoise::get_seamless_image(int p_size) const {
 			float v = get_noise_4d(x, y, z, w);
 			float v = get_noise_4d(x, y, z, w);
 
 
 			v = v * 0.5 + 0.5; // Normalize [0..1]
 			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;
 	return image;
 }
 }