Skip to content
0.2.0-dev Download ZIP
251 lines ·10.4 KB Raw
1/**************************************************************************/
2/* gltf_node.cpp */
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#include "gltf_node.h"
32
33#include "../gltf_state.h"
34
35#include "core/object/class_db.h"
36
37void GLTFNode::_bind_methods() {
38 ClassDB::bind_method(D_METHOD("get_original_name"), &GLTFNode::get_original_name);
39 ClassDB::bind_method(D_METHOD("set_original_name", "original_name"), &GLTFNode::set_original_name);
40 ClassDB::bind_method(D_METHOD("get_parent"), &GLTFNode::get_parent);
41 ClassDB::bind_method(D_METHOD("set_parent", "parent"), &GLTFNode::set_parent);
42 ClassDB::bind_method(D_METHOD("get_height"), &GLTFNode::get_height);
43 ClassDB::bind_method(D_METHOD("set_height", "height"), &GLTFNode::set_height);
44 ClassDB::bind_method(D_METHOD("get_xform"), &GLTFNode::get_xform);
45 ClassDB::bind_method(D_METHOD("set_xform", "xform"), &GLTFNode::set_xform);
46 ClassDB::bind_method(D_METHOD("get_mesh"), &GLTFNode::get_mesh);
47 ClassDB::bind_method(D_METHOD("set_mesh", "mesh"), &GLTFNode::set_mesh);
48 ClassDB::bind_method(D_METHOD("get_camera"), &GLTFNode::get_camera);
49 ClassDB::bind_method(D_METHOD("set_camera", "camera"), &GLTFNode::set_camera);
50 ClassDB::bind_method(D_METHOD("get_skin"), &GLTFNode::get_skin);
51 ClassDB::bind_method(D_METHOD("set_skin", "skin"), &GLTFNode::set_skin);
52 ClassDB::bind_method(D_METHOD("get_skeleton"), &GLTFNode::get_skeleton);
53 ClassDB::bind_method(D_METHOD("set_skeleton", "skeleton"), &GLTFNode::set_skeleton);
54 ClassDB::bind_method(D_METHOD("get_position"), &GLTFNode::get_position);
55 ClassDB::bind_method(D_METHOD("set_position", "position"), &GLTFNode::set_position);
56 ClassDB::bind_method(D_METHOD("get_rotation"), &GLTFNode::get_rotation);
57 ClassDB::bind_method(D_METHOD("set_rotation", "rotation"), &GLTFNode::set_rotation);
58 ClassDB::bind_method(D_METHOD("get_scale"), &GLTFNode::get_scale);
59 ClassDB::bind_method(D_METHOD("set_scale", "scale"), &GLTFNode::set_scale);
60 ClassDB::bind_method(D_METHOD("get_children"), &GLTFNode::get_children);
61 ClassDB::bind_method(D_METHOD("set_children", "children"), &GLTFNode::set_children);
62 ClassDB::bind_method(D_METHOD("append_child_index", "child_index"), &GLTFNode::append_child_index);
63 ClassDB::bind_method(D_METHOD("get_light"), &GLTFNode::get_light);
64 ClassDB::bind_method(D_METHOD("set_light", "light"), &GLTFNode::set_light);
65 ClassDB::bind_method(D_METHOD("get_visible"), &GLTFNode::get_visible);
66 ClassDB::bind_method(D_METHOD("set_visible", "visible"), &GLTFNode::set_visible);
67 ClassDB::bind_method(D_METHOD("get_additional_data", "extension_name"), &GLTFNode::get_additional_data);
68 ClassDB::bind_method(D_METHOD("set_additional_data", "extension_name", "additional_data"), &GLTFNode::set_additional_data);
69 ClassDB::bind_method(D_METHOD("get_scene_node_path", "gltf_state", "handle_skeletons"), &GLTFNode::get_scene_node_path, DEFVAL(true));
70
71 ADD_PROPERTY(PropertyInfo(Variant::STRING, "original_name"), "set_original_name", "get_original_name"); // String
72 ADD_PROPERTY(PropertyInfo(Variant::INT, "parent"), "set_parent", "get_parent"); // GLTFNodeIndex
73 ADD_PROPERTY(PropertyInfo(Variant::INT, "height"), "set_height", "get_height"); // int
74 ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM3D, "xform"), "set_xform", "get_xform"); // Transform3D
75 ADD_PROPERTY(PropertyInfo(Variant::INT, "mesh"), "set_mesh", "get_mesh"); // GLTFMeshIndex
76 ADD_PROPERTY(PropertyInfo(Variant::INT, "camera"), "set_camera", "get_camera"); // GLTFCameraIndex
77 ADD_PROPERTY(PropertyInfo(Variant::INT, "skin"), "set_skin", "get_skin"); // GLTFSkinIndex
78 ADD_PROPERTY(PropertyInfo(Variant::INT, "skeleton"), "set_skeleton", "get_skeleton"); // GLTFSkeletonIndex
79 ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "position"), "set_position", "get_position"); // Vector3
80 ADD_PROPERTY(PropertyInfo(Variant::QUATERNION, "rotation"), "set_rotation", "get_rotation"); // Quaternion
81 ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "scale"), "set_scale", "get_scale"); // Vector3
82 ADD_PROPERTY(PropertyInfo(Variant::PACKED_INT32_ARRAY, "children"), "set_children", "get_children"); // Vector<int>
83 ADD_PROPERTY(PropertyInfo(Variant::INT, "light"), "set_light", "get_light"); // GLTFLightIndex
84 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "visible"), "set_visible", "get_visible"); // bool
85}
86
87String GLTFNode::get_original_name() {
88 return original_name;
89}
90void GLTFNode::set_original_name(const String &p_name) {
91 original_name = p_name;
92}
93
94GLTFNodeIndex GLTFNode::get_parent() {
95 return parent;
96}
97
98void GLTFNode::set_parent(GLTFNodeIndex p_parent) {
99 parent = p_parent;
100}
101
102int GLTFNode::get_height() {
103 return height;
104}
105
106void GLTFNode::set_height(int p_height) {
107 height = p_height;
108}
109
110Transform3D GLTFNode::get_xform() {
111 return transform;
112}
113
114void GLTFNode::set_xform(const Transform3D &p_xform) {
115 transform = p_xform;
116}
117
118GLTFMeshIndex GLTFNode::get_mesh() {
119 return mesh;
120}
121
122void GLTFNode::set_mesh(GLTFMeshIndex p_mesh) {
123 mesh = p_mesh;
124}
125
126GLTFCameraIndex GLTFNode::get_camera() {
127 return camera;
128}
129
130void GLTFNode::set_camera(GLTFCameraIndex p_camera) {
131 camera = p_camera;
132}
133
134GLTFSkinIndex GLTFNode::get_skin() {
135 return skin;
136}
137
138void GLTFNode::set_skin(GLTFSkinIndex p_skin) {
139 skin = p_skin;
140}
141
142GLTFSkeletonIndex GLTFNode::get_skeleton() {
143 return skeleton;
144}
145
146void GLTFNode::set_skeleton(GLTFSkeletonIndex p_skeleton) {
147 skeleton = p_skeleton;
148}
149
150Vector3 GLTFNode::get_position() {
151 return transform.origin;
152}
153
154void GLTFNode::set_position(const Vector3 &p_position) {
155 transform.origin = p_position;
156}
157
158Quaternion GLTFNode::get_rotation() {
159 return transform.basis.get_rotation_quaternion();
160}
161
162void GLTFNode::set_rotation(const Quaternion &p_rotation) {
163 transform.basis.set_quaternion_scale(p_rotation, transform.basis.get_scale());
164}
165
166Vector3 GLTFNode::get_scale() {
167 return transform.basis.get_scale();
168}
169
170void GLTFNode::set_scale(const Vector3 &p_scale) {
171 transform.basis = transform.basis.orthonormalized() * Basis::from_scale(p_scale);
172}
173
174Vector<int> GLTFNode::get_children() {
175 return Vector<int>(children);
176}
177
178void GLTFNode::set_children(const Vector<int> &p_children) {
179 children = Vector<int>(p_children);
180}
181
182void GLTFNode::append_child_index(int p_child_index) {
183 children.append(p_child_index);
184}
185
186GLTFLightIndex GLTFNode::get_light() {
187 return light;
188}
189
190void GLTFNode::set_light(GLTFLightIndex p_light) {
191 light = p_light;
192}
193
194bool GLTFNode::get_visible() {
195 return visible;
196}
197
198void GLTFNode::set_visible(bool p_visible) {
199 visible = p_visible;
200}
201
202Variant GLTFNode::get_additional_data(const StringName &p_extension_name) {
203 return additional_data.get(p_extension_name, Variant());
204}
205
206bool GLTFNode::has_additional_data(const StringName &p_extension_name) {
207 return additional_data.has(p_extension_name);
208}
209
210void GLTFNode::set_additional_data(const StringName &p_extension_name, Variant p_additional_data) {
211 additional_data[p_extension_name] = p_additional_data;
212}
213
214NodePath GLTFNode::get_scene_node_path(Ref<GLTFState> p_state, bool p_handle_skeletons) {
215 ERR_FAIL_COND_V_MSG(p_state.is_null(), NodePath(), "Cannot get scene node path because GLTFState is null.");
216 Vector<StringName> path;
217 Vector<StringName> subpath;
218 Ref<GLTFNode> current_gltf_node = this;
219 const int gltf_node_count = p_state->nodes.size();
220 if (p_handle_skeletons && skeleton != -1) {
221 // Special case for skeleton nodes, skip all bones so that the path is to the Skeleton3D node.
222 // A path that would otherwise be `A/B/C/Bone1/Bone2/Bone3` becomes `A/B/C/Skeleton3D:Bone3`.
223 subpath.append(get_name());
224 // The generated Skeleton3D node will be named Skeleton3D, so add it to the path.
225 path.append("Skeleton3D");
226 do {
227 const int parent_index = current_gltf_node->get_parent();
228 ERR_FAIL_INDEX_V(parent_index, gltf_node_count, NodePath());
229 current_gltf_node = p_state->nodes[parent_index];
230 } while (current_gltf_node->skeleton != -1);
231 }
232 const bool is_godot_single_root = p_state->extensions_used.has("GODOT_single_root");
233 while (true) {
234 const int parent_index = current_gltf_node->get_parent();
235 if (is_godot_single_root && parent_index == -1) {
236 // For GODOT_single_root scenes, the root glTF node becomes the Godot scene root, so it
237 // should not be included in the path. Ex: A/B/C, A is single root, we want B/C only.
238 break;
239 }
240 path.insert(0, current_gltf_node->get_name());
241 if (!is_godot_single_root && parent_index == -1) {
242 break;
243 }
244 ERR_FAIL_INDEX_V(parent_index, gltf_node_count, NodePath());
245 current_gltf_node = p_state->nodes[parent_index];
246 }
247 if (unlikely(path.is_empty())) {
248 path.append(".");
249 }
250 return NodePath(path, subpath, false);
251}