Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/gltf/tests/test_gltf_images.h
10279 views
1
/**************************************************************************/
2
/* test_gltf_images.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 "test_gltf.h"
34
35
#ifdef TOOLS_ENABLED
36
37
#include "editor/file_system/editor_file_system.h"
38
#include "editor/file_system/editor_paths.h"
39
#include "scene/resources/image_texture.h"
40
41
namespace TestGltf {
42
Ref<Texture2D> _check_texture(Node *p_node) {
43
MeshInstance3D *mesh_instance_3d = Object::cast_to<MeshInstance3D>(p_node->find_child("mesh_instance_3d", true, true));
44
Ref<StandardMaterial3D> material = mesh_instance_3d->get_active_material(0);
45
Ref<Texture2D> texture = material->get_texture(StandardMaterial3D::TextureParam::TEXTURE_ALBEDO);
46
47
CHECK_MESSAGE(texture->get_size().x == 2, "Texture width not correct.");
48
CHECK_MESSAGE(texture->get_size().y == 2, "Texture height not correct.");
49
50
// Check if the loaded texture pixels are exactly as we expect.
51
for (int x = 0; x < 2; ++x) {
52
for (int y = 0; y < 2; ++y) {
53
Color c = texture->get_image()->get_pixel(x, y);
54
CHECK_MESSAGE(c == Color(x, y, y), "Texture content is incorrect.");
55
}
56
}
57
return texture;
58
}
59
60
TEST_CASE("[SceneTree][Node] Export GLTF with external texture and import") {
61
init("gltf_images_external_export_import");
62
// Setup scene.
63
Ref<ImageTexture> original_texture;
64
original_texture.instantiate();
65
Ref<Image> image;
66
image.instantiate();
67
image->initialize_data(2, 2, false, Image::FORMAT_RGBA8);
68
for (int x = 0; x < 2; ++x) {
69
for (int y = 0; y < 2; ++y) {
70
image->set_pixel(x, y, Color(x, y, y));
71
}
72
}
73
74
original_texture->set_image(image);
75
76
Ref<StandardMaterial3D> original_material;
77
original_material.instantiate();
78
original_material->set_texture(StandardMaterial3D::TextureParam::TEXTURE_ALBEDO, original_texture);
79
original_material->set_name("material");
80
81
Ref<PlaneMesh> original_meshdata;
82
original_meshdata.instantiate();
83
original_meshdata->set_name("planemesh");
84
original_meshdata->surface_set_material(0, original_material);
85
86
MeshInstance3D *original_mesh_instance = memnew(MeshInstance3D);
87
original_mesh_instance->set_mesh(original_meshdata);
88
original_mesh_instance->set_name("mesh_instance_3d");
89
90
Node3D *original = memnew(Node3D);
91
SceneTree::get_singleton()->get_root()->add_child(original);
92
original->add_child(original_mesh_instance);
93
original->set_owner(SceneTree::get_singleton()->get_root());
94
original_mesh_instance->set_owner(SceneTree::get_singleton()->get_root());
95
96
// Convert to GLFT and back.
97
Node *loaded = gltf_export_then_import(original, "gltf_images");
98
_check_texture(loaded);
99
100
memdelete(original_mesh_instance);
101
memdelete(original);
102
memdelete(loaded);
103
}
104
105
TEST_CASE("[SceneTree][Node][Editor] Import GLTF from .godot/imported folder with external texture") {
106
init("gltf_placed_in_dot_godot_imported", "res://.godot/imported");
107
108
EditorFileSystem *efs = memnew(EditorFileSystem);
109
EditorResourcePreview *erp = memnew(EditorResourcePreview);
110
111
ERR_PRINT_OFF
112
Node *loaded = gltf_import("res://.godot/imported/gltf_placed_in_dot_godot_imported.gltf");
113
Ref<Texture2D> texture = _check_texture(loaded);
114
ERR_PRINT_ON
115
116
// In-editor imports of gltf and texture from .godot/imported folder should end up in res:// if extract_path is defined.
117
CHECK_MESSAGE(texture->get_path() == "res://gltf_placed_in_dot_godot_imported_material_albedo000.png", "Texture not parsed as resource.");
118
119
memdelete(loaded);
120
memdelete(erp);
121
memdelete(efs);
122
}
123
124
TEST_CASE("[SceneTree][Node][Editor] Import GLTF with texture outside of res:// directory") {
125
init("gltf_pointing_to_texture_outside_of_res_folder", "res://");
126
127
EditorFileSystem *efs = memnew(EditorFileSystem);
128
EditorResourcePreview *erp = memnew(EditorResourcePreview);
129
130
// Copy texture to the parent folder of res:// - i.e. to res://.. where we can't import from.
131
String oneup = TestUtils::get_temp_path("texture.png");
132
Error err;
133
Ref<FileAccess> output = FileAccess::open(oneup, FileAccess::WRITE, &err);
134
CHECK_MESSAGE(err == OK, "Unable to open texture file.");
135
output->store_buffer(FileAccess::get_file_as_bytes("res://texture_source.png"));
136
output->close();
137
138
ERR_PRINT_OFF
139
Node *loaded = gltf_import("res://gltf_pointing_to_texture_outside_of_res_folder.gltf");
140
Ref<Texture2D> texture = _check_texture(loaded);
141
ERR_PRINT_ON
142
143
// Imports of gltf with texture from outside of res:// folder should end up being copied to res://
144
CHECK_MESSAGE(texture->get_path() == "res://gltf_pointing_to_texture_outside_of_res_folder_material_albedo000.png", "Texture not parsed as resource.");
145
146
memdelete(loaded);
147
memdelete(erp);
148
memdelete(efs);
149
}
150
151
TEST_CASE("[SceneTree][Node][Editor] Import GLTF with embedded texture, check how it got extracted") {
152
init("gltf_embedded_texture", "res://");
153
154
EditorFileSystem *efs = memnew(EditorFileSystem);
155
EditorResourcePreview *erp = memnew(EditorResourcePreview);
156
157
ERR_PRINT_OFF
158
Node *loaded = gltf_import("res://embedded_texture.gltf");
159
Ref<Texture2D> texture = _check_texture(loaded);
160
ERR_PRINT_ON
161
162
// In-editor imports of texture embedded in file should end up with a resource.
163
CHECK_MESSAGE(texture->get_path() == "res://embedded_texture_material_albedo000.png", "Texture not parsed as resource.");
164
165
memdelete(loaded);
166
memdelete(erp);
167
memdelete(efs);
168
}
169
170
} //namespace TestGltf
171
172
#endif // TOOLS_ENABLED
173
174