Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/gltf/tests/test_gltf.h
22517 views
1
/**************************************************************************/
2
/* test_gltf.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 "tests/test_macros.h"
34
35
#ifdef TOOLS_ENABLED
36
37
#include "core/config/project_settings.h"
38
#include "core/io/dir_access.h"
39
#include "core/os/os.h"
40
#include "drivers/png/image_loader_png.h"
41
#include "editor/import/3d/resource_importer_scene.h"
42
#include "editor/import/resource_importer_texture.h"
43
#include "editor/inspector/editor_resource_preview.h"
44
#include "scene/3d/mesh_instance_3d.h"
45
#include "scene/3d/skeleton_3d.h"
46
#include "scene/main/window.h"
47
#include "scene/resources/3d/primitive_meshes.h"
48
#include "scene/resources/compressed_texture.h"
49
#include "scene/resources/material.h"
50
#include "scene/resources/packed_scene.h"
51
#include "tests/test_utils.h"
52
53
#include "modules/gltf/editor/editor_scene_importer_gltf.h"
54
#include "modules/gltf/gltf_document.h"
55
#include "modules/gltf/gltf_state.h"
56
57
namespace TestGltf {
58
59
static Node *gltf_import(const String &p_file) {
60
// Setting up importers.
61
Ref<ResourceImporterScene> import_scene;
62
import_scene.instantiate("PackedScene");
63
ResourceFormatImporter::get_singleton()->add_importer(import_scene);
64
Ref<EditorSceneFormatImporterGLTF> import_gltf;
65
import_gltf.instantiate();
66
ResourceImporterScene::add_scene_importer(import_gltf);
67
68
// Support processing png files in editor import.
69
Ref<ResourceImporterTexture> import_texture;
70
import_texture.instantiate(true);
71
ResourceFormatImporter::get_singleton()->add_importer(import_texture);
72
73
// Once editor import convert pngs to ctex, we will need to load it as ctex resource.
74
Ref<ResourceFormatLoaderCompressedTexture2D> resource_loader_stream_texture;
75
if constexpr (GD_IS_CLASS_ENABLED(CompressedTexture2D)) {
76
resource_loader_stream_texture.instantiate();
77
ResourceLoader::add_resource_format_loader(resource_loader_stream_texture);
78
}
79
80
HashMap<StringName, Variant> options(21);
81
options["nodes/root_type"] = "";
82
options["nodes/root_name"] = "";
83
options["nodes/root_script"] = Variant();
84
options["nodes/apply_root_scale"] = true;
85
options["nodes/root_scale"] = 1.0;
86
options["meshes/ensure_tangents"] = true;
87
options["meshes/generate_lods"] = false;
88
options["meshes/create_shadow_meshes"] = true;
89
options["meshes/light_baking"] = 1;
90
options["meshes/lightmap_texel_size"] = 0.2;
91
options["meshes/force_disable_compression"] = false;
92
options["skins/use_named_skins"] = true;
93
options["animation/import"] = true;
94
options["animation/fps"] = 30;
95
options["animation/trimming"] = false;
96
options["animation/remove_immutable_tracks"] = true;
97
options["import_script/path"] = "";
98
options["extract_path"] = "res://";
99
options["_subresources"] = Dictionary();
100
options["gltf/naming_version"] = 1;
101
102
// Process gltf file, note that this generates `.scn` resource from the 2nd argument.
103
String scene_file = "res://" + p_file.get_file().get_basename();
104
Error err = import_scene->import(0, p_file, scene_file, options, nullptr, nullptr, nullptr);
105
CHECK_MESSAGE(err == OK, "GLTF import failed.");
106
107
Ref<PackedScene> packed_scene = ResourceLoader::load(scene_file + ".scn", "", ResourceFormatLoader::CACHE_MODE_REPLACE, &err);
108
CHECK_MESSAGE(err == OK, "Loading scene failed.");
109
Node *p_scene = packed_scene->instantiate();
110
111
ResourceImporterScene::remove_scene_importer(import_gltf);
112
ResourceFormatImporter::get_singleton()->remove_importer(import_texture);
113
if constexpr (GD_IS_CLASS_ENABLED(CompressedTexture2D)) {
114
ResourceLoader::remove_resource_format_loader(resource_loader_stream_texture);
115
resource_loader_stream_texture.unref();
116
}
117
return p_scene;
118
}
119
120
static Node *gltf_export_then_import(Node *p_root, const String &p_test_name) {
121
String tempfile = TestUtils::get_temp_path(p_test_name);
122
123
Ref<GLTFDocument> doc;
124
doc.instantiate();
125
Ref<GLTFState> state;
126
state.instantiate();
127
Error err = doc->append_from_scene(p_root, state, EditorSceneFormatImporter::IMPORT_USE_NAMED_SKIN_BINDS);
128
CHECK_MESSAGE(err == OK, "GLTF state generation failed.");
129
130
err = doc->write_to_filesystem(state, tempfile + ".gltf");
131
CHECK_MESSAGE(err == OK, "Writing GLTF to cache dir failed.");
132
133
return gltf_import(tempfile + ".gltf");
134
}
135
136
void init(const String &p_test, const String &p_copy_target = String()) {
137
Error err;
138
139
// Setup project settings with `res://` set to a temporary path.
140
String project_folder = TestUtils::get_temp_path(p_test.get_file().get_basename());
141
TestProjectSettingsInternalsAccessor::resource_path() = project_folder;
142
ProjectSettings *ps = ProjectSettings::get_singleton();
143
err = ps->setup(project_folder, String(), true);
144
145
// Create the imported files folder as the editor import process expects it to exist.
146
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
147
da->make_dir_recursive(ps->globalize_path(ps->get_imported_files_path()));
148
149
if (p_copy_target.is_empty()) {
150
return;
151
}
152
153
// Copy all the necessary test data files to the res:// directory.
154
String test_data = String("modules/gltf/tests/data/").path_join(p_test);
155
da = DirAccess::open(test_data);
156
CHECK_MESSAGE(da.is_valid(), "Unable to open folder.");
157
da->list_dir_begin();
158
for (String item = da->get_next(); !item.is_empty(); item = da->get_next()) {
159
if (!FileAccess::exists(test_data.path_join(item))) {
160
continue;
161
}
162
Ref<FileAccess> output = FileAccess::open(p_copy_target.path_join(item), FileAccess::WRITE, &err);
163
CHECK_MESSAGE(err == OK, "Unable to open output file.");
164
output->store_buffer(FileAccess::get_file_as_bytes(test_data.path_join(item)));
165
output->close();
166
}
167
da->list_dir_end();
168
}
169
170
} //namespace TestGltf
171
172
#endif // TOOLS_ENABLED
173
174