Debian Codegraph @

tests/scene/test_gltf_document.h

raw symbols SCIP
1 /**************************************************************************/
2 /* test_gltf_document.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 "modules/gltf/extensions/gltf_document_extension_convert_importer_mesh.h"
34 #include "modules/gltf/gltf_document.h"
35
36 #include "tests/test_macros.h"
37 #include "tests/test_utils.h"
38
39 namespace TestGLTFDocument {
40
41 struct GLTFArraySize {
42 String key;
43 int val;
44 };
45
46 struct GLTFKeyValue {
47 String key;
48 Variant val;
49 };
50
51 struct GLTFTestCase {
52 String filename;
53 String copyright;
54 String generator;
55 String version;
56 Vector<GLTFArraySize> array_sizes;
57 Vector<GLTFArraySize> json_array_sizes;
58 Vector<GLTFKeyValue> keyvalues;
59 };
60
61 const GLTFTestCase glTF_test_cases[] = {
62 { "models/cube.gltf",
63 "",
64 "Khronos glTF Blender I/O v4.3.47",
65 "2.0",
66 // Here are the array sizes.
67 {
68 { "nodes", 1 },
69 { "buffers", 1 },
70 { "buffer_views", 13 },
71 { "accessors", 13 },
72 { "meshes", 1 },
73 { "materials", 2 },
74 { "root_nodes", 1 },
75 { "textures", 0 },
76 { "texture_samplers", 0 },
77 { "images", 0 },
78 { "skins", 0 },
79 { "cameras", 0 },
80 { "lights", 0 },
81 { "skeletons", 0 },
82 { "animations", 1 },
83 },
84 // Here are the json array sizes.
85 {
86 { "scenes", 1 },
87 { "nodes", 1 },
88 { "animations", 1 },
89 { "meshes", 1 },
90 { "accessors", 13 },
91 { "bufferViews", 13 },
92 { "buffers", 1 },
93 },
94 // Here are the key-value pairs.
95 {
96 { "major_version", 2 },
97 { "minor_version", 0 },
98 { "scene_name", "cube" },
99 { "filename", "cube" } } },
100 { "models/suzanne.glb",
101 "this is example text",
102 "Khronos glTF Blender I/O v4.3.47",
103 "2.0",
104 // Here are the array sizes.
105 {
106 { "glb_data", 68908 },
107 { "nodes", 2 },
108 { "buffers", 1 },
109 { "buffer_views", 5 },
110 { "accessors", 4 },
111 { "meshes", 1 },
112 { "materials", 1 },
113 { "root_nodes", 2 },
114 { "textures", 1 },
115 { "texture_samplers", 1 },
116 { "images", 1 },
117 { "skins", 0 },
118 { "cameras", 1 },
119 { "lights", 0 },
120 { "unique_names", 4 },
121 { "skeletons", 0 },
122 { "animations", 0 },
123 },
124 // Here are the json array sizes.
125 {
126 { "scenes", 1 },
127 { "nodes", 2 },
128 { "cameras", 1 },
129 { "materials", 1 },
130 { "meshes", 1 },
131 { "textures", 1 },
132 { "images", 1 },
133 { "accessors", 4 },
134 { "bufferViews", 5 },
135 { "buffers", 1 },
136 },
137 // Here are the key-value pairs.
138 {
139 { "major_version", 2 },
140 { "minor_version", 0 },
141 { "scene_name", "suzanne" },
142 { "filename", "suzanne" } } },
143 };
144
145 void register_gltf_extension() {
146 GLTFDocument::unregister_all_gltf_document_extensions();
147
148 // Ensures meshes become a MeshInstance3D and not an ImporterMeshInstance3D.
149 Ref<GLTFDocumentExtensionConvertImporterMesh> extension_GLTFDocumentExtensionConvertImporterMesh;
150 extension_GLTFDocumentExtensionConvertImporterMesh.instantiate();
151 GLTFDocument::register_gltf_document_extension(extension_GLTFDocumentExtensionConvertImporterMesh);
152 }
153
154 void test_gltf_document_values(Ref<GLTFDocument> &p_gltf_document, Ref<GLTFState> &p_gltf_state, const GLTFTestCase &p_test_case) {
155 const Error err = p_gltf_document->append_from_file(TestUtils::get_data_path(p_test_case.filename), p_gltf_state);
156 REQUIRE(err == OK);
157
158 for (GLTFArraySize array_size : p_test_case.array_sizes) {
159 CHECK_MESSAGE(((Array)(p_gltf_state->getvar(array_size.key))).size() == array_size.val, "Expected \"", array_size.key, "\" to have ", array_size.val, " elements.");
160 }
161
162 for (GLTFArraySize array_size : p_test_case.json_array_sizes) {
163 CHECK(p_gltf_state->get_json().has(array_size.key));
164 CHECK_MESSAGE(((Array)(p_gltf_state->get_json()[array_size.key])).size() == array_size.val, "Expected \"", array_size.key, "\" to have ", array_size.val, " elements.");
165 }
166
167 for (GLTFKeyValue key_value : p_test_case.keyvalues) {
168 CHECK_MESSAGE(p_gltf_state->getvar(key_value.key) == key_value.val, "Expected \"", key_value.key, "\" to be \"", key_value.val, "\".");
169 }
170
171 CHECK(p_gltf_state->get_copyright() == p_test_case.copyright);
172 CHECK(((Dictionary)p_gltf_state->get_json()["asset"])["generator"] == p_test_case.generator);
173 CHECK(((Dictionary)p_gltf_state->get_json()["asset"])["version"] == p_test_case.version);
174 }
175
176 void test_gltf_save(Node *p_node) {
177 Ref<GLTFDocument> gltf_document_save;
178 gltf_document_save.instantiate();
179 Ref<GLTFState> gltf_state_save;
180 gltf_state_save.instantiate();
181
182 gltf_document_save->append_from_scene(p_node, gltf_state_save);
183
184 // Check saving the scene to gltf and glb.
185 const Error err_save_gltf = gltf_document_save->write_to_filesystem(gltf_state_save, TestUtils::get_temp_path("cube.gltf"));
186 const Error err_save_glb = gltf_document_save->write_to_filesystem(gltf_state_save, TestUtils::get_temp_path("cube.glb"));
187 CHECK(err_save_gltf == OK);
188 CHECK(err_save_glb == OK);
189 }
190
191 TEST_CASE("[SceneTree][GLTFDocument] Load cube.gltf") {
192 register_gltf_extension();
193
194 Ref<GLTFDocument> gltf_document;
195 gltf_document.instantiate();
196 Ref<GLTFState> gltf_state;
197 gltf_state.instantiate();
198
199 test_gltf_document_values(gltf_document, gltf_state, glTF_test_cases[0]);
200
201 Node *node = gltf_document->generate_scene(gltf_state);
202
203 // Check the loaded scene.
204 CHECK(node->is_class("Node3D"));
205 CHECK(node->get_name() == "cube");
206
207 CHECK(node->get_child(0)->is_class("MeshInstance3D"));
208 CHECK(node->get_child(0)->get_name() == "Cube");
209
210 CHECK(node->get_child(1)->is_class("AnimationPlayer"));
211 CHECK(node->get_child(1)->get_name() == "AnimationPlayer");
212
213 test_gltf_save(node);
214
215 // Clean up the node.
216 memdelete(node);
217 }
218
219 TEST_CASE("[SceneTree][GLTFDocument] Load suzanne.glb") {
220 register_gltf_extension();
221
222 Ref<GLTFDocument> gltf_document;
223 gltf_document.instantiate();
224 Ref<GLTFState> gltf_state;
225 gltf_state.instantiate();
226
227 test_gltf_document_values(gltf_document, gltf_state, glTF_test_cases[1]);
228
229 Node *node = gltf_document->generate_scene(gltf_state);
230
231 // Check the loaded scene.
232 CHECK(node->is_class("Node3D"));
233 CHECK(node->get_name() == "suzanne");
234
235 CHECK(node->get_child(0)->is_class("MeshInstance3D"));
236 CHECK(node->get_child(0)->get_name() == "Suzanne");
237
238 CHECK(node->get_child(1)->is_class("Camera3D"));
239 CHECK(node->get_child(1)->get_name() == "Camera");
240
241 test_gltf_save(node);
242
243 // Clean up the node.
244 memdelete(node);
245 }
246
247 } // namespace TestGLTFDocument