Path: blob/master/modules/noise/tests/test_noise_texture_3d.h
10278 views
/**************************************************************************/1/* test_noise_texture_3d.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_3d.h"3334#include "tests/test_macros.h"3536namespace TestNoiseTexture3D {3738class NoiseTexture3DTester : public RefCounted {39GDCLASS(NoiseTexture3DTester, RefCounted);4041const NoiseTexture3D *const texture;4243public:44NoiseTexture3DTester(const NoiseTexture3D *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 Vector<Ref<Image>> noise_data = texture->get_data();6465for (int i = 0; i < noise_data.size(); i++) {66const Ref<Image> noise_image = noise_data[i];6768CHECK(noise_image.is_valid());69CHECK(noise_image->get_width() == texture->get_width());70CHECK(noise_image->get_height() == texture->get_height());7172CHECK(noise_image->get_format() == Image::FORMAT_RGBA8);73CHECK(!noise_image->has_mipmaps());7475Color avg_color = compute_average_color(noise_image);7677// Check that the noise texture is modulated correctly by the color ramp (Gradient).78CHECK_FALSE_MESSAGE((avg_color.r + avg_color.g + avg_color.b) == doctest::Approx(0.0), "The noise texture should not be all black");79CHECK_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");80CHECK_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}8384void check_seamless_texture_grayscale() {85const Vector<Ref<Image>> noise_data = texture->get_data();8687for (int i = 0; i < noise_data.size(); i++) {88const Ref<Image> noise_image = noise_data[i];8990CHECK(noise_image.is_valid());91CHECK(noise_image->get_width() == texture->get_width());92CHECK(noise_image->get_height() == texture->get_height());9394CHECK(noise_image->get_format() == Image::FORMAT_L8);9596Color avg_color = compute_average_color(noise_image);9798// Since it's a grayscale image and every channel except the alpha channel has the99// same values (conversion happens in Image::get_pixel) we only need to test one channel.100CHECK(avg_color.r == doctest::Approx(0.5).epsilon(0.05));101}102}103104void check_seamless_texture_rgba() {105const Vector<Ref<Image>> noise_data = texture->get_data();106107for (int i = 0; i < noise_data.size(); i++) {108const Ref<Image> noise_image = noise_data[i];109110CHECK(noise_image.is_valid());111CHECK(noise_image->get_width() == texture->get_width());112CHECK(noise_image->get_height() == texture->get_height());113114CHECK(noise_image->get_format() == Image::FORMAT_RGBA8);115116// Check that the noise texture is modulated correctly by the color ramp (Gradient).117Color avg_color = compute_average_color(noise_image);118119// We use a default (black to white) gradient, so the average of the red, green and blue channels should be the same.120CHECK(avg_color.r == doctest::Approx(0.5).epsilon(0.05));121CHECK(avg_color.g == doctest::Approx(0.5).epsilon(0.05));122CHECK(avg_color.b == doctest::Approx(0.5).epsilon(0.05));123}124}125};126127TEST_CASE("[NoiseTexture][SceneTree] Getter and setter") {128Ref<NoiseTexture3D> noise_texture = memnew(NoiseTexture3D);129130Ref<FastNoiseLite> noise = memnew(FastNoiseLite);131noise_texture->set_noise(noise);132CHECK(noise_texture->get_noise() == noise);133noise_texture->set_noise(nullptr);134CHECK(noise_texture->get_noise().is_null());135136noise_texture->set_width(8);137noise_texture->set_height(4);138noise_texture->set_depth(2);139CHECK(noise_texture->get_width() == 8);140CHECK(noise_texture->get_height() == 4);141CHECK(noise_texture->get_depth() == 2);142143ERR_PRINT_OFF;144noise_texture->set_width(-1);145noise_texture->set_height(-1);146noise_texture->set_depth(-1);147ERR_PRINT_ON;148CHECK(noise_texture->get_width() == 8);149CHECK(noise_texture->get_height() == 4);150CHECK(noise_texture->get_depth() == 2);151152noise_texture->set_invert(true);153CHECK(noise_texture->get_invert() == true);154noise_texture->set_invert(false);155CHECK(noise_texture->get_invert() == false);156157noise_texture->set_seamless(true);158CHECK(noise_texture->get_seamless() == true);159noise_texture->set_seamless(false);160CHECK(noise_texture->get_seamless() == false);161162noise_texture->set_seamless_blend_skirt(0.45);163CHECK(noise_texture->get_seamless_blend_skirt() == doctest::Approx(0.45));164165ERR_PRINT_OFF;166noise_texture->set_seamless_blend_skirt(-1.0);167noise_texture->set_seamless_blend_skirt(2.0);168CHECK(noise_texture->get_seamless_blend_skirt() == doctest::Approx(0.45));169ERR_PRINT_ON;170171Ref<Gradient> gradient = memnew(Gradient);172noise_texture->set_color_ramp(gradient);173CHECK(noise_texture->get_color_ramp() == gradient);174noise_texture->set_color_ramp(nullptr);175CHECK(noise_texture->get_color_ramp().is_null());176}177178TEST_CASE("[NoiseTexture3D][SceneTree] Generating a basic noise texture with mipmaps and color ramp modulation") {179Ref<NoiseTexture3D> noise_texture = memnew(NoiseTexture3D);180181Ref<FastNoiseLite> noise = memnew(FastNoiseLite);182noise_texture->set_noise(noise);183184Ref<Gradient> gradient = memnew(Gradient);185Vector<float> offsets = { 0.0, 1.0 };186Vector<Color> colors = { Color(1, 0, 0), Color(0, 0, 1) };187gradient->set_offsets(offsets);188gradient->set_colors(colors);189190noise_texture->set_color_ramp(gradient);191noise_texture->set_width(16);192noise_texture->set_height(16);193noise_texture->set_depth(16);194195Ref<NoiseTexture3DTester> tester = memnew(NoiseTexture3DTester(noise_texture.ptr()));196noise_texture->connect_changed(callable_mp(tester.ptr(), &NoiseTexture3DTester::check_mip_and_color_ramp));197MessageQueue::get_singleton()->flush();198}199200TEST_CASE("[NoiseTexture3D][SceneTree] Generating a seamless noise texture") {201Ref<NoiseTexture3D> noise_texture = memnew(NoiseTexture3D);202203Ref<FastNoiseLite> noise = memnew(FastNoiseLite);204noise->set_frequency(0.5);205noise_texture->set_noise(noise);206noise_texture->set_width(16);207noise_texture->set_height(16);208noise_texture->set_depth(16);209noise_texture->set_seamless(true);210211Ref<NoiseTexture3DTester> tester = memnew(NoiseTexture3DTester(noise_texture.ptr()));212213SUBCASE("Grayscale(L8) 16x16x16, with seamless blend skirt of 0.05") {214noise_texture->set_seamless_blend_skirt(0.05);215noise_texture->connect_changed(callable_mp(tester.ptr(), &NoiseTexture3DTester::check_seamless_texture_grayscale));216MessageQueue::get_singleton()->flush();217}218219SUBCASE("16x16x16 modulated with default (transparent)black and white gradient (RGBA8), with seamless blend skirt of 1.0") {220Ref<Gradient> gradient = memnew(Gradient);221222Vector<float> offsets = { 0.0, 1.0 };223Vector<Color> colors = { Color(0, 0, 0, 0), Color(1, 1, 1, 1) };224gradient->set_offsets(offsets);225gradient->set_colors(colors);226227noise_texture->set_color_ramp(gradient);228noise_texture->set_seamless_blend_skirt(1.0);229noise_texture->connect_changed(callable_mp(tester.ptr(), &NoiseTexture3DTester::check_seamless_texture_rgba));230MessageQueue::get_singleton()->flush();231}232}233234} //namespace TestNoiseTexture3D235236237