Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/gltf/tests/test_gltf_extras.h
10278 views
1
/**************************************************************************/
2
/* test_gltf_extras.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
#include "tests/test_macros.h"
35
36
#ifdef TOOLS_ENABLED
37
38
#include "core/os/os.h"
39
#include "editor/import/3d/resource_importer_scene.h"
40
#include "scene/3d/mesh_instance_3d.h"
41
#include "scene/3d/skeleton_3d.h"
42
#include "scene/main/window.h"
43
#include "scene/resources/3d/primitive_meshes.h"
44
#include "scene/resources/material.h"
45
#include "scene/resources/packed_scene.h"
46
47
#include "modules/gltf/editor/editor_scene_importer_gltf.h"
48
#include "modules/gltf/gltf_document.h"
49
#include "modules/gltf/gltf_state.h"
50
51
namespace TestGltf {
52
53
TEST_CASE("[SceneTree][Node] GLTF test mesh and material meta export and import") {
54
init("gltf_mesh_material_extras");
55
// Setup scene.
56
Ref<StandardMaterial3D> original_material = memnew(StandardMaterial3D);
57
original_material->set_albedo(Color(1.0, .0, .0));
58
original_material->set_name("material");
59
Dictionary material_dict;
60
material_dict["node_type"] = "material";
61
original_material->set_meta("extras", material_dict);
62
63
Ref<PlaneMesh> original_meshdata = memnew(PlaneMesh);
64
original_meshdata->set_name("planemesh");
65
Dictionary meshdata_dict;
66
meshdata_dict["node_type"] = "planemesh";
67
original_meshdata->set_meta("extras", meshdata_dict);
68
original_meshdata->surface_set_material(0, original_material);
69
70
MeshInstance3D *original_mesh_instance = memnew(MeshInstance3D);
71
original_mesh_instance->set_mesh(original_meshdata);
72
original_mesh_instance->set_name("mesh_instance_3d");
73
Dictionary mesh_instance_dict;
74
mesh_instance_dict["node_type"] = "mesh_instance_3d";
75
original_mesh_instance->set_meta("extras", mesh_instance_dict);
76
77
Node3D *original = memnew(Node3D);
78
SceneTree::get_singleton()->get_root()->add_child(original);
79
original->add_child(original_mesh_instance);
80
original->set_name("node3d");
81
Dictionary node_dict;
82
node_dict["node_type"] = "node3d";
83
original->set_meta("extras", node_dict);
84
original->set_meta("meta_not_nested_under_extras", "should not propagate");
85
86
original->set_owner(SceneTree::get_singleton()->get_root());
87
original_mesh_instance->set_owner(SceneTree::get_singleton()->get_root());
88
89
// Convert to GLFT and back.
90
Node *loaded = gltf_export_then_import(original, "gltf_extras");
91
92
// Compare the results.
93
CHECK(loaded->get_name() == "node3d");
94
CHECK(Dictionary(loaded->get_meta("extras")).size() == 1);
95
CHECK(Dictionary(loaded->get_meta("extras"))["node_type"] == "node3d");
96
CHECK_FALSE(loaded->has_meta("meta_not_nested_under_extras"));
97
CHECK_FALSE(Dictionary(loaded->get_meta("extras")).has("meta_not_nested_under_extras"));
98
99
MeshInstance3D *mesh_instance_3d = Object::cast_to<MeshInstance3D>(loaded->find_child("mesh_instance_3d", false, true));
100
CHECK(mesh_instance_3d->get_name() == "mesh_instance_3d");
101
CHECK(Dictionary(mesh_instance_3d->get_meta("extras"))["node_type"] == "mesh_instance_3d");
102
103
Ref<Mesh> mesh = mesh_instance_3d->get_mesh();
104
CHECK(Dictionary(mesh->get_meta("extras"))["node_type"] == "planemesh");
105
106
Ref<Material> material = mesh->surface_get_material(0);
107
CHECK(material->get_name() == "material");
108
CHECK(Dictionary(material->get_meta("extras"))["node_type"] == "material");
109
110
memdelete(original_mesh_instance);
111
memdelete(original);
112
memdelete(loaded);
113
}
114
115
TEST_CASE("[SceneTree][Node] GLTF test skeleton and bone export and import") {
116
init("gltf_skeleton_extras");
117
// Setup scene.
118
Skeleton3D *skeleton = memnew(Skeleton3D);
119
skeleton->set_name("skeleton");
120
Dictionary skeleton_extras;
121
skeleton_extras["node_type"] = "skeleton";
122
skeleton->set_meta("extras", skeleton_extras);
123
124
skeleton->add_bone("parent");
125
skeleton->set_bone_rest(0, Transform3D());
126
Dictionary parent_bone_extras;
127
parent_bone_extras["bone"] = "i_am_parent_bone";
128
skeleton->set_bone_meta(0, "extras", parent_bone_extras);
129
130
skeleton->add_bone("child");
131
skeleton->set_bone_rest(1, Transform3D());
132
skeleton->set_bone_parent(1, 0);
133
Dictionary child_bone_extras;
134
child_bone_extras["bone"] = "i_am_child_bone";
135
skeleton->set_bone_meta(1, "extras", child_bone_extras);
136
137
// We have to have a mesh to link with skeleton or it will not get imported.
138
Ref<PlaneMesh> meshdata = memnew(PlaneMesh);
139
meshdata->set_name("planemesh");
140
141
MeshInstance3D *mesh = memnew(MeshInstance3D);
142
mesh->set_mesh(meshdata);
143
mesh->set_name("mesh_instance_3d");
144
145
Node3D *original = memnew(Node3D);
146
SceneTree::get_singleton()->get_root()->add_child(original);
147
original->add_child(skeleton);
148
original->add_child(mesh);
149
original->set_name("node3d");
150
151
// Now that both skeleton and mesh are part of scene, link them.
152
mesh->set_skeleton_path(mesh->get_path_to(skeleton));
153
154
mesh->set_owner(SceneTree::get_singleton()->get_root());
155
original->set_owner(SceneTree::get_singleton()->get_root());
156
157
// Convert to GLFT and back.
158
Node *loaded = gltf_export_then_import(original, "gltf_bone_extras");
159
160
// Compare the results.
161
CHECK(loaded->get_name() == "node3d");
162
Skeleton3D *result = Object::cast_to<Skeleton3D>(loaded->find_child("Skeleton3D", false, true));
163
CHECK(result->get_bone_name(0) == "parent");
164
CHECK(Dictionary(result->get_bone_meta(0, "extras"))["bone"] == "i_am_parent_bone");
165
CHECK(result->get_bone_name(1) == "child");
166
CHECK(Dictionary(result->get_bone_meta(1, "extras"))["bone"] == "i_am_child_bone");
167
168
memdelete(skeleton);
169
memdelete(mesh);
170
memdelete(original);
171
memdelete(loaded);
172
}
173
} //namespace TestGltf
174
175
#endif // TOOLS_ENABLED
176
177