Path: blob/master/modules/gltf/tests/test_gltf_images.h
10279 views
/**************************************************************************/1/* test_gltf_images.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 "test_gltf.h"3334#ifdef TOOLS_ENABLED3536#include "editor/file_system/editor_file_system.h"37#include "editor/file_system/editor_paths.h"38#include "scene/resources/image_texture.h"3940namespace TestGltf {41Ref<Texture2D> _check_texture(Node *p_node) {42MeshInstance3D *mesh_instance_3d = Object::cast_to<MeshInstance3D>(p_node->find_child("mesh_instance_3d", true, true));43Ref<StandardMaterial3D> material = mesh_instance_3d->get_active_material(0);44Ref<Texture2D> texture = material->get_texture(StandardMaterial3D::TextureParam::TEXTURE_ALBEDO);4546CHECK_MESSAGE(texture->get_size().x == 2, "Texture width not correct.");47CHECK_MESSAGE(texture->get_size().y == 2, "Texture height not correct.");4849// Check if the loaded texture pixels are exactly as we expect.50for (int x = 0; x < 2; ++x) {51for (int y = 0; y < 2; ++y) {52Color c = texture->get_image()->get_pixel(x, y);53CHECK_MESSAGE(c == Color(x, y, y), "Texture content is incorrect.");54}55}56return texture;57}5859TEST_CASE("[SceneTree][Node] Export GLTF with external texture and import") {60init("gltf_images_external_export_import");61// Setup scene.62Ref<ImageTexture> original_texture;63original_texture.instantiate();64Ref<Image> image;65image.instantiate();66image->initialize_data(2, 2, false, Image::FORMAT_RGBA8);67for (int x = 0; x < 2; ++x) {68for (int y = 0; y < 2; ++y) {69image->set_pixel(x, y, Color(x, y, y));70}71}7273original_texture->set_image(image);7475Ref<StandardMaterial3D> original_material;76original_material.instantiate();77original_material->set_texture(StandardMaterial3D::TextureParam::TEXTURE_ALBEDO, original_texture);78original_material->set_name("material");7980Ref<PlaneMesh> original_meshdata;81original_meshdata.instantiate();82original_meshdata->set_name("planemesh");83original_meshdata->surface_set_material(0, original_material);8485MeshInstance3D *original_mesh_instance = memnew(MeshInstance3D);86original_mesh_instance->set_mesh(original_meshdata);87original_mesh_instance->set_name("mesh_instance_3d");8889Node3D *original = memnew(Node3D);90SceneTree::get_singleton()->get_root()->add_child(original);91original->add_child(original_mesh_instance);92original->set_owner(SceneTree::get_singleton()->get_root());93original_mesh_instance->set_owner(SceneTree::get_singleton()->get_root());9495// Convert to GLFT and back.96Node *loaded = gltf_export_then_import(original, "gltf_images");97_check_texture(loaded);9899memdelete(original_mesh_instance);100memdelete(original);101memdelete(loaded);102}103104TEST_CASE("[SceneTree][Node][Editor] Import GLTF from .godot/imported folder with external texture") {105init("gltf_placed_in_dot_godot_imported", "res://.godot/imported");106107EditorFileSystem *efs = memnew(EditorFileSystem);108EditorResourcePreview *erp = memnew(EditorResourcePreview);109110ERR_PRINT_OFF111Node *loaded = gltf_import("res://.godot/imported/gltf_placed_in_dot_godot_imported.gltf");112Ref<Texture2D> texture = _check_texture(loaded);113ERR_PRINT_ON114115// In-editor imports of gltf and texture from .godot/imported folder should end up in res:// if extract_path is defined.116CHECK_MESSAGE(texture->get_path() == "res://gltf_placed_in_dot_godot_imported_material_albedo000.png", "Texture not parsed as resource.");117118memdelete(loaded);119memdelete(erp);120memdelete(efs);121}122123TEST_CASE("[SceneTree][Node][Editor] Import GLTF with texture outside of res:// directory") {124init("gltf_pointing_to_texture_outside_of_res_folder", "res://");125126EditorFileSystem *efs = memnew(EditorFileSystem);127EditorResourcePreview *erp = memnew(EditorResourcePreview);128129// Copy texture to the parent folder of res:// - i.e. to res://.. where we can't import from.130String oneup = TestUtils::get_temp_path("texture.png");131Error err;132Ref<FileAccess> output = FileAccess::open(oneup, FileAccess::WRITE, &err);133CHECK_MESSAGE(err == OK, "Unable to open texture file.");134output->store_buffer(FileAccess::get_file_as_bytes("res://texture_source.png"));135output->close();136137ERR_PRINT_OFF138Node *loaded = gltf_import("res://gltf_pointing_to_texture_outside_of_res_folder.gltf");139Ref<Texture2D> texture = _check_texture(loaded);140ERR_PRINT_ON141142// Imports of gltf with texture from outside of res:// folder should end up being copied to res://143CHECK_MESSAGE(texture->get_path() == "res://gltf_pointing_to_texture_outside_of_res_folder_material_albedo000.png", "Texture not parsed as resource.");144145memdelete(loaded);146memdelete(erp);147memdelete(efs);148}149150TEST_CASE("[SceneTree][Node][Editor] Import GLTF with embedded texture, check how it got extracted") {151init("gltf_embedded_texture", "res://");152153EditorFileSystem *efs = memnew(EditorFileSystem);154EditorResourcePreview *erp = memnew(EditorResourcePreview);155156ERR_PRINT_OFF157Node *loaded = gltf_import("res://embedded_texture.gltf");158Ref<Texture2D> texture = _check_texture(loaded);159ERR_PRINT_ON160161// In-editor imports of texture embedded in file should end up with a resource.162CHECK_MESSAGE(texture->get_path() == "res://embedded_texture_material_albedo000.png", "Texture not parsed as resource.");163164memdelete(loaded);165memdelete(erp);166memdelete(efs);167}168169} //namespace TestGltf170171#endif // TOOLS_ENABLED172173174