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