test_noise_texture_3d.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /**************************************************************************/
  2. /* test_noise_texture_3d.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef TEST_NOISE_TEXTURE_3D_H
  31. #define TEST_NOISE_TEXTURE_3D_H
  32. #include "../noise_texture_3d.h"
  33. #include "tests/test_macros.h"
  34. namespace TestNoiseTexture3D {
  35. class NoiseTexture3DTester : public RefCounted {
  36. GDCLASS(NoiseTexture3DTester, RefCounted);
  37. const NoiseTexture3D *const texture;
  38. public:
  39. NoiseTexture3DTester(const NoiseTexture3D *const p_texture) :
  40. texture{ p_texture } {}
  41. Color compute_average_color(const Ref<Image> &p_noise_image) {
  42. Color r_avg_color{};
  43. for (int i = 0; i < p_noise_image->get_width(); ++i) {
  44. for (int j = 0; j < p_noise_image->get_height(); ++j) {
  45. const Color pixel = p_noise_image->get_pixel(i, j);
  46. r_avg_color += pixel;
  47. }
  48. }
  49. int pixel_count = p_noise_image->get_width() * p_noise_image->get_height();
  50. r_avg_color /= pixel_count;
  51. return r_avg_color;
  52. }
  53. void check_mip_and_color_ramp() {
  54. const Vector<Ref<Image>> noise_data = texture->get_data();
  55. for (int i = 0; i < noise_data.size(); i++) {
  56. const Ref<Image> noise_image = noise_data[i];
  57. CHECK(noise_image.is_valid());
  58. CHECK(noise_image->get_width() == texture->get_width());
  59. CHECK(noise_image->get_height() == texture->get_height());
  60. CHECK(noise_image->get_format() == Image::FORMAT_RGBA8);
  61. CHECK(!noise_image->has_mipmaps());
  62. Color avg_color = compute_average_color(noise_image);
  63. // Check that the noise texture is modulated correctly by the color ramp (Gradient).
  64. CHECK_FALSE_MESSAGE((avg_color.r + avg_color.g + avg_color.b) == doctest::Approx(0.0), "The noise texture should not be all black");
  65. CHECK_FALSE_MESSAGE((avg_color.r + avg_color.g + avg_color.b) == doctest::Approx(noise_image->get_width() * noise_image->get_height() * 3.0), "The noise texture should not be all white");
  66. CHECK_MESSAGE(avg_color.g == doctest::Approx(0.0), "The noise texture should not have any green when modulated correctly by the color ramp");
  67. }
  68. }
  69. void check_seamless_texture_grayscale() {
  70. const Vector<Ref<Image>> noise_data = texture->get_data();
  71. for (int i = 0; i < noise_data.size(); i++) {
  72. const Ref<Image> noise_image = noise_data[i];
  73. CHECK(noise_image.is_valid());
  74. CHECK(noise_image->get_width() == texture->get_width());
  75. CHECK(noise_image->get_height() == texture->get_height());
  76. CHECK(noise_image->get_format() == Image::FORMAT_L8);
  77. Color avg_color = compute_average_color(noise_image);
  78. // Since it's a grayscale image and every channel except the alpha channel has the
  79. // same values (conversion happens in Image::get_pixel) we only need to test one channel.
  80. CHECK(avg_color.r == doctest::Approx(0.5).epsilon(0.05));
  81. }
  82. }
  83. void check_seamless_texture_rgba() {
  84. const Vector<Ref<Image>> noise_data = texture->get_data();
  85. for (int i = 0; i < noise_data.size(); i++) {
  86. const Ref<Image> noise_image = noise_data[i];
  87. CHECK(noise_image.is_valid());
  88. CHECK(noise_image->get_width() == texture->get_width());
  89. CHECK(noise_image->get_height() == texture->get_height());
  90. CHECK(noise_image->get_format() == Image::FORMAT_RGBA8);
  91. // Check that the noise texture is modulated correctly by the color ramp (Gradient).
  92. Color avg_color = compute_average_color(noise_image);
  93. // We use a default (black to white) gradient, so the average of the red, green and blue channels should be the same.
  94. CHECK(avg_color.r == doctest::Approx(0.5).epsilon(0.05));
  95. CHECK(avg_color.g == doctest::Approx(0.5).epsilon(0.05));
  96. CHECK(avg_color.b == doctest::Approx(0.5).epsilon(0.05));
  97. }
  98. }
  99. };
  100. TEST_CASE("[NoiseTexture][SceneTree] Getter and setter") {
  101. Ref<NoiseTexture3D> noise_texture = memnew(NoiseTexture3D);
  102. Ref<FastNoiseLite> noise = memnew(FastNoiseLite);
  103. noise_texture->set_noise(noise);
  104. CHECK(noise_texture->get_noise() == noise);
  105. noise_texture->set_noise(nullptr);
  106. CHECK(noise_texture->get_noise().is_null());
  107. noise_texture->set_width(8);
  108. noise_texture->set_height(4);
  109. noise_texture->set_depth(2);
  110. CHECK(noise_texture->get_width() == 8);
  111. CHECK(noise_texture->get_height() == 4);
  112. CHECK(noise_texture->get_depth() == 2);
  113. ERR_PRINT_OFF;
  114. noise_texture->set_width(-1);
  115. noise_texture->set_height(-1);
  116. noise_texture->set_depth(-1);
  117. ERR_PRINT_ON;
  118. CHECK(noise_texture->get_width() == 8);
  119. CHECK(noise_texture->get_height() == 4);
  120. CHECK(noise_texture->get_depth() == 2);
  121. noise_texture->set_invert(true);
  122. CHECK(noise_texture->get_invert() == true);
  123. noise_texture->set_invert(false);
  124. CHECK(noise_texture->get_invert() == false);
  125. noise_texture->set_seamless(true);
  126. CHECK(noise_texture->get_seamless() == true);
  127. noise_texture->set_seamless(false);
  128. CHECK(noise_texture->get_seamless() == false);
  129. noise_texture->set_seamless_blend_skirt(0.45);
  130. CHECK(noise_texture->get_seamless_blend_skirt() == doctest::Approx(0.45));
  131. ERR_PRINT_OFF;
  132. noise_texture->set_seamless_blend_skirt(-1.0);
  133. noise_texture->set_seamless_blend_skirt(2.0);
  134. CHECK(noise_texture->get_seamless_blend_skirt() == doctest::Approx(0.45));
  135. ERR_PRINT_ON;
  136. Ref<Gradient> gradient = memnew(Gradient);
  137. noise_texture->set_color_ramp(gradient);
  138. CHECK(noise_texture->get_color_ramp() == gradient);
  139. noise_texture->set_color_ramp(nullptr);
  140. CHECK(noise_texture->get_color_ramp().is_null());
  141. }
  142. TEST_CASE("[NoiseTexture3D][SceneTree] Generating a basic noise texture with mipmaps and color ramp modulation") {
  143. Ref<NoiseTexture3D> noise_texture = memnew(NoiseTexture3D);
  144. Ref<FastNoiseLite> noise = memnew(FastNoiseLite);
  145. noise_texture->set_noise(noise);
  146. Ref<Gradient> gradient = memnew(Gradient);
  147. Vector<float> offsets = { 0.0, 1.0 };
  148. Vector<Color> colors = { Color(1, 0, 0), Color(0, 0, 1) };
  149. gradient->set_offsets(offsets);
  150. gradient->set_colors(colors);
  151. noise_texture->set_color_ramp(gradient);
  152. noise_texture->set_width(16);
  153. noise_texture->set_height(16);
  154. noise_texture->set_depth(16);
  155. Ref<NoiseTexture3DTester> tester = memnew(NoiseTexture3DTester(noise_texture.ptr()));
  156. noise_texture->connect_changed(callable_mp(tester.ptr(), &NoiseTexture3DTester::check_mip_and_color_ramp));
  157. MessageQueue::get_singleton()->flush();
  158. }
  159. TEST_CASE("[NoiseTexture3D][SceneTree] Generating a seamless noise texture") {
  160. Ref<NoiseTexture3D> noise_texture = memnew(NoiseTexture3D);
  161. Ref<FastNoiseLite> noise = memnew(FastNoiseLite);
  162. noise->set_frequency(0.5);
  163. noise_texture->set_noise(noise);
  164. noise_texture->set_width(16);
  165. noise_texture->set_height(16);
  166. noise_texture->set_depth(16);
  167. noise_texture->set_seamless(true);
  168. Ref<NoiseTexture3DTester> tester = memnew(NoiseTexture3DTester(noise_texture.ptr()));
  169. SUBCASE("Grayscale(L8) 16x16x16, with seamless blend skirt of 0.05") {
  170. noise_texture->set_seamless_blend_skirt(0.05);
  171. noise_texture->connect_changed(callable_mp(tester.ptr(), &NoiseTexture3DTester::check_seamless_texture_grayscale));
  172. MessageQueue::get_singleton()->flush();
  173. }
  174. SUBCASE("16x16x16 modulated with default (transparent)black and white gradient (RGBA8), with seamless blend skirt of 1.0") {
  175. Ref<Gradient> gradient = memnew(Gradient);
  176. Vector<float> offsets = { 0.0, 1.0 };
  177. Vector<Color> colors = { Color(0, 0, 0, 0), Color(1, 1, 1, 1) };
  178. gradient->set_offsets(offsets);
  179. gradient->set_colors(colors);
  180. noise_texture->set_color_ramp(gradient);
  181. noise_texture->set_seamless_blend_skirt(1.0);
  182. noise_texture->connect_changed(callable_mp(tester.ptr(), &NoiseTexture3DTester::check_seamless_texture_rgba));
  183. MessageQueue::get_singleton()->flush();
  184. }
  185. }
  186. } //namespace TestNoiseTexture3D
  187. #endif // TEST_NOISE_TEXTURE_3D_H