gltf_node.cpp 9.8 KB

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