0.2.0-dev Download ZIP
270 lines ·10.9 KB Raw
| 1 | /**************************************************************************/ |
| 2 | /* test_noise_texture_2d.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 | |
| 31 | #pragma once |
| 32 | |
| 33 | #include "../fastnoise_lite.h" |
| 34 | #include "../noise_texture_2d.h" |
| 35 | |
| 36 | #include "core/object/callable_mp.h" |
| 37 | #include "core/object/message_queue.h" |
| 38 | #include "tests/test_macros.h" |
| 39 | |
| 40 | namespace TestNoiseTexture2D { |
| 41 | |
| 42 | class NoiseTextureTester : public RefCounted { |
| 43 | GDCLASS(NoiseTextureTester, RefCounted); |
| 44 | |
| 45 | const NoiseTexture2D *const texture; |
| 46 | |
| 47 | public: |
| 48 | NoiseTextureTester(const NoiseTexture2D *const p_texture) : |
| 49 | texture{ p_texture } {} |
| 50 | |
| 51 | Color compute_average_color(const Ref<Image> &p_noise_image) { |
| 52 | Color r_avg_color{}; |
| 53 | |
| 54 | for (int i = 0; i < p_noise_image->get_width(); ++i) { |
| 55 | for (int j = 0; j < p_noise_image->get_height(); ++j) { |
| 56 | const Color pixel = p_noise_image->get_pixel(i, j); |
| 57 | r_avg_color += pixel; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | int pixel_count = p_noise_image->get_width() * p_noise_image->get_height(); |
| 62 | r_avg_color /= pixel_count; |
| 63 | return r_avg_color; |
| 64 | } |
| 65 | |
| 66 | void check_mip_and_color_ramp() { |
| 67 | const Ref<Image> noise_image = texture->get_image(); |
| 68 | CHECK(noise_image.is_valid()); |
| 69 | CHECK(noise_image->get_width() == texture->get_width()); |
| 70 | CHECK(noise_image->get_height() == texture->get_height()); |
| 71 | |
| 72 | CHECK(noise_image->get_format() == Image::FORMAT_RGBA8); |
| 73 | CHECK(noise_image->has_mipmaps()); |
| 74 | |
| 75 | Color avg_color = compute_average_color(noise_image); |
| 76 | |
| 77 | // Check that the noise texture is modulated correctly by the color ramp (Gradient). |
| 78 | CHECK_FALSE_MESSAGE((avg_color.r + avg_color.g + avg_color.b) == doctest::Approx(0.0), "The noise texture should not be all black"); |
| 79 | 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"); |
| 80 | CHECK_MESSAGE(avg_color.g == doctest::Approx(0.0), "The noise texture should not have any green when modulated correctly by the color ramp"); |
| 81 | } |
| 82 | |
| 83 | void check_normal_map() { |
| 84 | const Ref<Image> noise_image = texture->get_image(); |
| 85 | CHECK(noise_image.is_valid()); |
| 86 | CHECK(noise_image->get_width() == texture->get_width()); |
| 87 | CHECK(noise_image->get_height() == texture->get_height()); |
| 88 | |
| 89 | CHECK(noise_image->get_format() == Image::FORMAT_RGBA8); |
| 90 | CHECK_FALSE(noise_image->has_mipmaps()); |
| 91 | |
| 92 | Color avg_color = compute_average_color(noise_image); |
| 93 | |
| 94 | // Check for the characteristic color distribution (for tangent space) of a normal map. |
| 95 | CHECK(avg_color.r == doctest::Approx(0.5).epsilon(0.05)); |
| 96 | CHECK(avg_color.g == doctest::Approx(0.5).epsilon(0.05)); |
| 97 | CHECK(avg_color.b == doctest::Approx(1.0).epsilon(0.05)); |
| 98 | } |
| 99 | |
| 100 | void check_seamless_texture_grayscale() { |
| 101 | const Ref<Image> noise_image = texture->get_image(); |
| 102 | CHECK(noise_image.is_valid()); |
| 103 | CHECK(noise_image->get_width() == texture->get_width()); |
| 104 | CHECK(noise_image->get_height() == texture->get_height()); |
| 105 | |
| 106 | CHECK(noise_image->get_format() == Image::FORMAT_L8); |
| 107 | |
| 108 | Color avg_color = compute_average_color(noise_image); |
| 109 | |
| 110 | // Since it's a grayscale image and every channel except the alpha channel has the |
| 111 | // same values (conversion happens in Image::get_pixel) we only need to test one channel. |
| 112 | CHECK(avg_color.r == doctest::Approx(0.5).epsilon(0.05)); |
| 113 | } |
| 114 | |
| 115 | void check_seamless_texture_rgba() { |
| 116 | const Ref<Image> noise_image = texture->get_image(); |
| 117 | CHECK(noise_image.is_valid()); |
| 118 | CHECK(noise_image->get_width() == texture->get_width()); |
| 119 | CHECK(noise_image->get_height() == texture->get_height()); |
| 120 | |
| 121 | CHECK(noise_image->get_format() == Image::FORMAT_RGBA8); |
| 122 | |
| 123 | // Check that the noise texture is modulated correctly by the color ramp (Gradient). |
| 124 | Color avg_color = compute_average_color(noise_image); |
| 125 | |
| 126 | // We use a default (black to white) gradient, so the average of the red, green and blue channels should be the same. |
| 127 | CHECK(avg_color.r == doctest::Approx(0.5).epsilon(0.05)); |
| 128 | CHECK(avg_color.g == doctest::Approx(0.5).epsilon(0.05)); |
| 129 | CHECK(avg_color.b == doctest::Approx(0.5).epsilon(0.05)); |
| 130 | } |
| 131 | }; |
| 132 | |
| 133 | TEST_CASE("[NoiseTexture][SceneTree] Getter and setter") { |
| 134 | Ref<NoiseTexture2D> noise_texture = memnew(NoiseTexture2D); |
| 135 | |
| 136 | Ref<FastNoiseLite> noise = memnew(FastNoiseLite); |
| 137 | noise_texture->set_noise(noise); |
| 138 | CHECK(noise_texture->get_noise() == noise); |
| 139 | noise_texture->set_noise(nullptr); |
| 140 | CHECK(noise_texture->get_noise().is_null()); |
| 141 | |
| 142 | noise_texture->set_width(8); |
| 143 | noise_texture->set_height(4); |
| 144 | CHECK(noise_texture->get_width() == 8); |
| 145 | CHECK(noise_texture->get_height() == 4); |
| 146 | |
| 147 | ERR_PRINT_OFF; |
| 148 | noise_texture->set_width(-1); |
| 149 | noise_texture->set_height(-1); |
| 150 | ERR_PRINT_ON; |
| 151 | CHECK(noise_texture->get_width() == 8); |
| 152 | CHECK(noise_texture->get_height() == 4); |
| 153 | |
| 154 | noise_texture->set_invert(true); |
| 155 | CHECK(noise_texture->get_invert() == true); |
| 156 | noise_texture->set_invert(false); |
| 157 | CHECK(noise_texture->get_invert() == false); |
| 158 | |
| 159 | noise_texture->set_in_3d_space(true); |
| 160 | CHECK(noise_texture->is_in_3d_space() == true); |
| 161 | noise_texture->set_in_3d_space(false); |
| 162 | CHECK(noise_texture->is_in_3d_space() == false); |
| 163 | |
| 164 | noise_texture->set_generate_mipmaps(true); |
| 165 | CHECK(noise_texture->is_generating_mipmaps() == true); |
| 166 | noise_texture->set_generate_mipmaps(false); |
| 167 | CHECK(noise_texture->is_generating_mipmaps() == false); |
| 168 | |
| 169 | noise_texture->set_seamless(true); |
| 170 | CHECK(noise_texture->get_seamless() == true); |
| 171 | noise_texture->set_seamless(false); |
| 172 | CHECK(noise_texture->get_seamless() == false); |
| 173 | |
| 174 | noise_texture->set_seamless_blend_skirt(0.45); |
| 175 | CHECK(noise_texture->get_seamless_blend_skirt() == doctest::Approx(0.45)); |
| 176 | |
| 177 | ERR_PRINT_OFF; |
| 178 | noise_texture->set_seamless_blend_skirt(-1.0); |
| 179 | noise_texture->set_seamless_blend_skirt(2.0); |
| 180 | CHECK(noise_texture->get_seamless_blend_skirt() == doctest::Approx(0.45)); |
| 181 | ERR_PRINT_ON; |
| 182 | |
| 183 | noise_texture->set_as_normal_map(true); |
| 184 | CHECK(noise_texture->is_normal_map() == true); |
| 185 | noise_texture->set_as_normal_map(false); |
| 186 | CHECK(noise_texture->is_normal_map() == false); |
| 187 | |
| 188 | noise_texture->set_bump_strength(0.168); |
| 189 | CHECK(noise_texture->get_bump_strength() == doctest::Approx(0.168)); |
| 190 | |
| 191 | Ref<Gradient> gradient = memnew(Gradient); |
| 192 | noise_texture->set_color_ramp(gradient); |
| 193 | CHECK(noise_texture->get_color_ramp() == gradient); |
| 194 | noise_texture->set_color_ramp(nullptr); |
| 195 | CHECK(noise_texture->get_color_ramp().is_null()); |
| 196 | } |
| 197 | |
| 198 | TEST_CASE("[NoiseTexture2D][SceneTree] Generating a basic noise texture with mipmaps and color ramp modulation") { |
| 199 | Ref<NoiseTexture2D> noise_texture = memnew(NoiseTexture2D); |
| 200 | |
| 201 | Ref<FastNoiseLite> noise = memnew(FastNoiseLite); |
| 202 | noise_texture->set_noise(noise); |
| 203 | |
| 204 | Ref<Gradient> gradient = memnew(Gradient); |
| 205 | Vector<float> offsets = { 0.0, 1.0 }; |
| 206 | Vector<Color> colors = { Color(1, 0, 0), Color(0, 0, 1) }; |
| 207 | gradient->set_offsets(offsets); |
| 208 | gradient->set_colors(colors); |
| 209 | |
| 210 | noise_texture->set_color_ramp(gradient); |
| 211 | noise_texture->set_width(16); |
| 212 | noise_texture->set_height(16); |
| 213 | noise_texture->set_generate_mipmaps(true); |
| 214 | |
| 215 | Ref<NoiseTextureTester> tester = memnew(NoiseTextureTester(noise_texture.ptr())); |
| 216 | noise_texture->connect_changed(callable_mp(tester.ptr(), &NoiseTextureTester::check_mip_and_color_ramp)); |
| 217 | MessageQueue::get_singleton()->flush(); |
| 218 | } |
| 219 | |
| 220 | TEST_CASE("[NoiseTexture2D][SceneTree] Generating a normal map without mipmaps") { |
| 221 | Ref<NoiseTexture2D> noise_texture = memnew(NoiseTexture2D); |
| 222 | |
| 223 | Ref<FastNoiseLite> noise = memnew(FastNoiseLite); |
| 224 | noise->set_frequency(0.5); |
| 225 | noise_texture->set_noise(noise); |
| 226 | noise_texture->set_width(16); |
| 227 | noise_texture->set_height(16); |
| 228 | noise_texture->set_as_normal_map(true); |
| 229 | noise_texture->set_bump_strength(0.5); |
| 230 | noise_texture->set_generate_mipmaps(false); |
| 231 | |
| 232 | Ref<NoiseTextureTester> tester = memnew(NoiseTextureTester(noise_texture.ptr())); |
| 233 | noise_texture->connect_changed(callable_mp(tester.ptr(), &NoiseTextureTester::check_normal_map)); |
| 234 | MessageQueue::get_singleton()->flush(); |
| 235 | } |
| 236 | |
| 237 | TEST_CASE("[NoiseTexture2D][SceneTree] Generating a seamless noise texture") { |
| 238 | Ref<NoiseTexture2D> noise_texture = memnew(NoiseTexture2D); |
| 239 | |
| 240 | Ref<FastNoiseLite> noise = memnew(FastNoiseLite); |
| 241 | noise->set_frequency(0.5); |
| 242 | noise_texture->set_noise(noise); |
| 243 | noise_texture->set_width(16); |
| 244 | noise_texture->set_height(16); |
| 245 | noise_texture->set_seamless(true); |
| 246 | |
| 247 | Ref<NoiseTextureTester> tester = memnew(NoiseTextureTester(noise_texture.ptr())); |
| 248 | |
| 249 | SUBCASE("Grayscale(L8) 16x16, with seamless blend skirt of 0.05") { |
| 250 | noise_texture->set_seamless_blend_skirt(0.05); |
| 251 | noise_texture->connect_changed(callable_mp(tester.ptr(), &NoiseTextureTester::check_seamless_texture_grayscale)); |
| 252 | MessageQueue::get_singleton()->flush(); |
| 253 | } |
| 254 | |
| 255 | SUBCASE("16x16 modulated with default (transparent)black and white gradient (RGBA8), with seamless blend skirt of 1.0") { |
| 256 | Ref<Gradient> gradient = memnew(Gradient); |
| 257 | |
| 258 | Vector<float> offsets = { 0.0, 1.0 }; |
| 259 | Vector<Color> colors = { Color(0, 0, 0, 0), Color(1, 1, 1, 1) }; |
| 260 | gradient->set_offsets(offsets); |
| 261 | gradient->set_colors(colors); |
| 262 | |
| 263 | noise_texture->set_color_ramp(gradient); |
| 264 | noise_texture->set_seamless_blend_skirt(1.0); |
| 265 | noise_texture->connect_changed(callable_mp(tester.ptr(), &NoiseTextureTester::check_seamless_texture_rgba)); |
| 266 | MessageQueue::get_singleton()->flush(); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | } //namespace TestNoiseTexture2D |