Path: blob/master/modules/noise/tests/test_noise_texture_2d.h
10278 views
/**************************************************************************/1/* test_noise_texture_2d.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "../noise_texture_2d.h"3334#include "tests/test_macros.h"3536namespace TestNoiseTexture2D {3738class NoiseTextureTester : public RefCounted {39GDCLASS(NoiseTextureTester, RefCounted);4041const NoiseTexture2D *const texture;4243public:44NoiseTextureTester(const NoiseTexture2D *const p_texture) :45texture{ p_texture } {}4647Color compute_average_color(const Ref<Image> &p_noise_image) {48Color r_avg_color{};4950for (int i = 0; i < p_noise_image->get_width(); ++i) {51for (int j = 0; j < p_noise_image->get_height(); ++j) {52const Color pixel = p_noise_image->get_pixel(i, j);53r_avg_color += pixel;54}55}5657int pixel_count = p_noise_image->get_width() * p_noise_image->get_height();58r_avg_color /= pixel_count;59return r_avg_color;60}6162void check_mip_and_color_ramp() {63const Ref<Image> noise_image = texture->get_image();64CHECK(noise_image.is_valid());65CHECK(noise_image->get_width() == texture->get_width());66CHECK(noise_image->get_height() == texture->get_height());6768CHECK(noise_image->get_format() == Image::FORMAT_RGBA8);69CHECK(noise_image->has_mipmaps());7071Color avg_color = compute_average_color(noise_image);7273// Check that the noise texture is modulated correctly by the color ramp (Gradient).74CHECK_FALSE_MESSAGE((avg_color.r + avg_color.g + avg_color.b) == doctest::Approx(0.0), "The noise texture should not be all black");75CHECK_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");76CHECK_MESSAGE(avg_color.g == doctest::Approx(0.0), "The noise texture should not have any green when modulated correctly by the color ramp");77}7879void check_normal_map() {80const Ref<Image> noise_image = texture->get_image();81CHECK(noise_image.is_valid());82CHECK(noise_image->get_width() == texture->get_width());83CHECK(noise_image->get_height() == texture->get_height());8485CHECK(noise_image->get_format() == Image::FORMAT_RGBA8);86CHECK_FALSE(noise_image->has_mipmaps());8788Color avg_color = compute_average_color(noise_image);8990// Check for the characteristic color distribution (for tangent space) of a normal map.91CHECK(avg_color.r == doctest::Approx(0.5).epsilon(0.05));92CHECK(avg_color.g == doctest::Approx(0.5).epsilon(0.05));93CHECK(avg_color.b == doctest::Approx(1.0).epsilon(0.05));94}9596void check_seamless_texture_grayscale() {97const Ref<Image> noise_image = texture->get_image();98CHECK(noise_image.is_valid());99CHECK(noise_image->get_width() == texture->get_width());100CHECK(noise_image->get_height() == texture->get_height());101102CHECK(noise_image->get_format() == Image::FORMAT_L8);103104Color avg_color = compute_average_color(noise_image);105106// Since it's a grayscale image and every channel except the alpha channel has the107// same values (conversion happens in Image::get_pixel) we only need to test one channel.108CHECK(avg_color.r == doctest::Approx(0.5).epsilon(0.05));109}110111void check_seamless_texture_rgba() {112const Ref<Image> noise_image = texture->get_image();113CHECK(noise_image.is_valid());114CHECK(noise_image->get_width() == texture->get_width());115CHECK(noise_image->get_height() == texture->get_height());116117CHECK(noise_image->get_format() == Image::FORMAT_RGBA8);118119// Check that the noise texture is modulated correctly by the color ramp (Gradient).120Color avg_color = compute_average_color(noise_image);121122// We use a default (black to white) gradient, so the average of the red, green and blue channels should be the same.123CHECK(avg_color.r == doctest::Approx(0.5).epsilon(0.05));124CHECK(avg_color.g == doctest::Approx(0.5).epsilon(0.05));125CHECK(avg_color.b == doctest::Approx(0.5).epsilon(0.05));126}127};128129TEST_CASE("[NoiseTexture][SceneTree] Getter and setter") {130Ref<NoiseTexture2D> noise_texture = memnew(NoiseTexture2D);131132Ref<FastNoiseLite> noise = memnew(FastNoiseLite);133noise_texture->set_noise(noise);134CHECK(noise_texture->get_noise() == noise);135noise_texture->set_noise(nullptr);136CHECK(noise_texture->get_noise().is_null());137138noise_texture->set_width(8);139noise_texture->set_height(4);140CHECK(noise_texture->get_width() == 8);141CHECK(noise_texture->get_height() == 4);142143ERR_PRINT_OFF;144noise_texture->set_width(-1);145noise_texture->set_height(-1);146ERR_PRINT_ON;147CHECK(noise_texture->get_width() == 8);148CHECK(noise_texture->get_height() == 4);149150noise_texture->set_invert(true);151CHECK(noise_texture->get_invert() == true);152noise_texture->set_invert(false);153CHECK(noise_texture->get_invert() == false);154155noise_texture->set_in_3d_space(true);156CHECK(noise_texture->is_in_3d_space() == true);157noise_texture->set_in_3d_space(false);158CHECK(noise_texture->is_in_3d_space() == false);159160noise_texture->set_generate_mipmaps(true);161CHECK(noise_texture->is_generating_mipmaps() == true);162noise_texture->set_generate_mipmaps(false);163CHECK(noise_texture->is_generating_mipmaps() == false);164165noise_texture->set_seamless(true);166CHECK(noise_texture->get_seamless() == true);167noise_texture->set_seamless(false);168CHECK(noise_texture->get_seamless() == false);169170noise_texture->set_seamless_blend_skirt(0.45);171CHECK(noise_texture->get_seamless_blend_skirt() == doctest::Approx(0.45));172173ERR_PRINT_OFF;174noise_texture->set_seamless_blend_skirt(-1.0);175noise_texture->set_seamless_blend_skirt(2.0);176CHECK(noise_texture->get_seamless_blend_skirt() == doctest::Approx(0.45));177ERR_PRINT_ON;178179noise_texture->set_as_normal_map(true);180CHECK(noise_texture->is_normal_map() == true);181noise_texture->set_as_normal_map(false);182CHECK(noise_texture->is_normal_map() == false);183184noise_texture->set_bump_strength(0.168);185CHECK(noise_texture->get_bump_strength() == doctest::Approx(0.168));186187Ref<Gradient> gradient = memnew(Gradient);188noise_texture->set_color_ramp(gradient);189CHECK(noise_texture->get_color_ramp() == gradient);190noise_texture->set_color_ramp(nullptr);191CHECK(noise_texture->get_color_ramp().is_null());192}193194TEST_CASE("[NoiseTexture2D][SceneTree] Generating a basic noise texture with mipmaps and color ramp modulation") {195Ref<NoiseTexture2D> noise_texture = memnew(NoiseTexture2D);196197Ref<FastNoiseLite> noise = memnew(FastNoiseLite);198noise_texture->set_noise(noise);199200Ref<Gradient> gradient = memnew(Gradient);201Vector<float> offsets = { 0.0, 1.0 };202Vector<Color> colors = { Color(1, 0, 0), Color(0, 0, 1) };203gradient->set_offsets(offsets);204gradient->set_colors(colors);205206noise_texture->set_color_ramp(gradient);207noise_texture->set_width(16);208noise_texture->set_height(16);209noise_texture->set_generate_mipmaps(true);210211Ref<NoiseTextureTester> tester = memnew(NoiseTextureTester(noise_texture.ptr()));212noise_texture->connect_changed(callable_mp(tester.ptr(), &NoiseTextureTester::check_mip_and_color_ramp));213MessageQueue::get_singleton()->flush();214}215216TEST_CASE("[NoiseTexture2D][SceneTree] Generating a normal map without mipmaps") {217Ref<NoiseTexture2D> noise_texture = memnew(NoiseTexture2D);218219Ref<FastNoiseLite> noise = memnew(FastNoiseLite);220noise->set_frequency(0.5);221noise_texture->set_noise(noise);222noise_texture->set_width(16);223noise_texture->set_height(16);224noise_texture->set_as_normal_map(true);225noise_texture->set_bump_strength(0.5);226noise_texture->set_generate_mipmaps(false);227228Ref<NoiseTextureTester> tester = memnew(NoiseTextureTester(noise_texture.ptr()));229noise_texture->connect_changed(callable_mp(tester.ptr(), &NoiseTextureTester::check_normal_map));230MessageQueue::get_singleton()->flush();231}232233TEST_CASE("[NoiseTexture2D][SceneTree] Generating a seamless noise texture") {234Ref<NoiseTexture2D> noise_texture = memnew(NoiseTexture2D);235236Ref<FastNoiseLite> noise = memnew(FastNoiseLite);237noise->set_frequency(0.5);238noise_texture->set_noise(noise);239noise_texture->set_width(16);240noise_texture->set_height(16);241noise_texture->set_seamless(true);242243Ref<NoiseTextureTester> tester = memnew(NoiseTextureTester(noise_texture.ptr()));244245SUBCASE("Grayscale(L8) 16x16, with seamless blend skirt of 0.05") {246noise_texture->set_seamless_blend_skirt(0.05);247noise_texture->connect_changed(callable_mp(tester.ptr(), &NoiseTextureTester::check_seamless_texture_grayscale));248MessageQueue::get_singleton()->flush();249}250251SUBCASE("16x16 modulated with default (transparent)black and white gradient (RGBA8), with seamless blend skirt of 1.0") {252Ref<Gradient> gradient = memnew(Gradient);253254Vector<float> offsets = { 0.0, 1.0 };255Vector<Color> colors = { Color(0, 0, 0, 0), Color(1, 1, 1, 1) };256gradient->set_offsets(offsets);257gradient->set_colors(colors);258259noise_texture->set_color_ramp(gradient);260noise_texture->set_seamless_blend_skirt(1.0);261noise_texture->connect_changed(callable_mp(tester.ptr(), &NoiseTextureTester::check_seamless_texture_rgba));262MessageQueue::get_singleton()->flush();263}264}265266} //namespace TestNoiseTexture2D267268269