gltf_document_extension_physics.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /**************************************************************************/
  2. /* gltf_document_extension_physics.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_document_extension_physics.h"
  31. #include "scene/3d/physics/area_3d.h"
  32. #include "scene/3d/physics/static_body_3d.h"
  33. // Import process.
  34. Error GLTFDocumentExtensionPhysics::import_preflight(Ref<GLTFState> p_state, Vector<String> p_extensions) {
  35. if (!p_extensions.has("OMI_collider") && !p_extensions.has("OMI_physics_body") && !p_extensions.has("OMI_physics_shape")) {
  36. return ERR_SKIP;
  37. }
  38. Dictionary state_json = p_state->get_json();
  39. if (state_json.has("extensions")) {
  40. Dictionary state_extensions = state_json["extensions"];
  41. if (state_extensions.has("OMI_physics_shape")) {
  42. Dictionary omi_physics_shape_ext = state_extensions["OMI_physics_shape"];
  43. if (omi_physics_shape_ext.has("shapes")) {
  44. Array state_shape_dicts = omi_physics_shape_ext["shapes"];
  45. if (state_shape_dicts.size() > 0) {
  46. Array state_shapes;
  47. for (int i = 0; i < state_shape_dicts.size(); i++) {
  48. state_shapes.push_back(GLTFPhysicsShape::from_dictionary(state_shape_dicts[i]));
  49. }
  50. p_state->set_additional_data(StringName("GLTFPhysicsShapes"), state_shapes);
  51. }
  52. }
  53. #ifndef DISABLE_DEPRECATED
  54. } else if (state_extensions.has("OMI_collider")) {
  55. Dictionary omi_collider_ext = state_extensions["OMI_collider"];
  56. if (omi_collider_ext.has("colliders")) {
  57. Array state_collider_dicts = omi_collider_ext["colliders"];
  58. if (state_collider_dicts.size() > 0) {
  59. Array state_colliders;
  60. for (int i = 0; i < state_collider_dicts.size(); i++) {
  61. state_colliders.push_back(GLTFPhysicsShape::from_dictionary(state_collider_dicts[i]));
  62. }
  63. p_state->set_additional_data(StringName("GLTFPhysicsShapes"), state_colliders);
  64. }
  65. }
  66. #endif // DISABLE_DEPRECATED
  67. }
  68. }
  69. return OK;
  70. }
  71. Vector<String> GLTFDocumentExtensionPhysics::get_supported_extensions() {
  72. Vector<String> ret;
  73. ret.push_back("OMI_collider");
  74. ret.push_back("OMI_physics_body");
  75. ret.push_back("OMI_physics_shape");
  76. return ret;
  77. }
  78. Error GLTFDocumentExtensionPhysics::parse_node_extensions(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &p_extensions) {
  79. #ifndef DISABLE_DEPRECATED
  80. if (p_extensions.has("OMI_collider")) {
  81. Dictionary node_collider_ext = p_extensions["OMI_collider"];
  82. if (node_collider_ext.has("collider")) {
  83. // "collider" is the index of the collider in the state colliders array.
  84. int node_collider_index = node_collider_ext["collider"];
  85. Array state_colliders = p_state->get_additional_data(StringName("GLTFPhysicsShapes"));
  86. ERR_FAIL_INDEX_V_MSG(node_collider_index, state_colliders.size(), Error::ERR_FILE_CORRUPT, "GLTF Physics: On node " + p_gltf_node->get_name() + ", the collider index " + itos(node_collider_index) + " is not in the state colliders (size: " + itos(state_colliders.size()) + ").");
  87. p_gltf_node->set_additional_data(StringName("GLTFPhysicsShape"), state_colliders[node_collider_index]);
  88. } else {
  89. p_gltf_node->set_additional_data(StringName("GLTFPhysicsShape"), GLTFPhysicsShape::from_dictionary(node_collider_ext));
  90. }
  91. }
  92. #endif // DISABLE_DEPRECATED
  93. if (p_extensions.has("OMI_physics_body")) {
  94. Dictionary physics_body_ext = p_extensions["OMI_physics_body"];
  95. if (physics_body_ext.has("collider")) {
  96. Dictionary node_collider = physics_body_ext["collider"];
  97. // "shape" is the index of the shape in the state shapes array.
  98. int node_shape_index = node_collider.get("shape", -1);
  99. if (node_shape_index != -1) {
  100. Array state_shapes = p_state->get_additional_data(StringName("GLTFPhysicsShapes"));
  101. ERR_FAIL_INDEX_V_MSG(node_shape_index, state_shapes.size(), Error::ERR_FILE_CORRUPT, "GLTF Physics: On node " + p_gltf_node->get_name() + ", the shape index " + itos(node_shape_index) + " is not in the state shapes (size: " + itos(state_shapes.size()) + ").");
  102. p_gltf_node->set_additional_data(StringName("GLTFPhysicsColliderShape"), state_shapes[node_shape_index]);
  103. } else {
  104. // If this node is a collider but does not have a collider
  105. // shape, then it only serves to combine together shapes.
  106. p_gltf_node->set_additional_data(StringName("GLTFPhysicsCompoundCollider"), true);
  107. }
  108. }
  109. if (physics_body_ext.has("trigger")) {
  110. Dictionary node_trigger = physics_body_ext["trigger"];
  111. // "shape" is the index of the shape in the state shapes array.
  112. int node_shape_index = node_trigger.get("shape", -1);
  113. if (node_shape_index != -1) {
  114. Array state_shapes = p_state->get_additional_data(StringName("GLTFPhysicsShapes"));
  115. ERR_FAIL_INDEX_V_MSG(node_shape_index, state_shapes.size(), Error::ERR_FILE_CORRUPT, "GLTF Physics: On node " + p_gltf_node->get_name() + ", the shape index " + itos(node_shape_index) + " is not in the state shapes (size: " + itos(state_shapes.size()) + ").");
  116. p_gltf_node->set_additional_data(StringName("GLTFPhysicsTriggerShape"), state_shapes[node_shape_index]);
  117. } else {
  118. // If this node is a trigger but does not have a trigger shape,
  119. // then it's a trigger body, what Godot calls an Area3D node.
  120. Ref<GLTFPhysicsBody> trigger_body;
  121. trigger_body.instantiate();
  122. trigger_body->set_body_type("trigger");
  123. p_gltf_node->set_additional_data(StringName("GLTFPhysicsBody"), trigger_body);
  124. }
  125. }
  126. if (physics_body_ext.has("motion") || physics_body_ext.has("type")) {
  127. p_gltf_node->set_additional_data(StringName("GLTFPhysicsBody"), GLTFPhysicsBody::from_dictionary(physics_body_ext));
  128. }
  129. }
  130. return OK;
  131. }
  132. void _setup_shape_mesh_resource_from_index_if_needed(Ref<GLTFState> p_state, Ref<GLTFPhysicsShape> p_gltf_shape) {
  133. GLTFMeshIndex shape_mesh_index = p_gltf_shape->get_mesh_index();
  134. if (shape_mesh_index == -1) {
  135. return; // No mesh for this shape.
  136. }
  137. Ref<ImporterMesh> importer_mesh = p_gltf_shape->get_importer_mesh();
  138. if (importer_mesh.is_valid()) {
  139. return; // The mesh resource is already set up.
  140. }
  141. TypedArray<GLTFMesh> state_meshes = p_state->get_meshes();
  142. ERR_FAIL_INDEX_MSG(shape_mesh_index, state_meshes.size(), "GLTF Physics: When importing '" + p_state->get_scene_name() + "', the shape mesh index " + itos(shape_mesh_index) + " is not in the state meshes (size: " + itos(state_meshes.size()) + ").");
  143. Ref<GLTFMesh> gltf_mesh = state_meshes[shape_mesh_index];
  144. ERR_FAIL_COND(gltf_mesh.is_null());
  145. importer_mesh = gltf_mesh->get_mesh();
  146. ERR_FAIL_COND(importer_mesh.is_null());
  147. p_gltf_shape->set_importer_mesh(importer_mesh);
  148. }
  149. #ifndef DISABLE_DEPRECATED
  150. CollisionObject3D *_generate_shape_with_body(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Ref<GLTFPhysicsShape> p_physics_shape, Ref<GLTFPhysicsBody> p_physics_body) {
  151. print_verbose("glTF: Creating shape with body for: " + p_gltf_node->get_name());
  152. bool is_trigger = p_physics_shape->get_is_trigger();
  153. // This method is used for the case where we must generate a parent body.
  154. // This is can happen for multiple reasons. One possibility is that this
  155. // GLTF file is using OMI_collider but not OMI_physics_body, or at least
  156. // this particular node is not using it. Another possibility is that the
  157. // physics body information is set up on the same GLTF node, not a parent.
  158. CollisionObject3D *body;
  159. if (p_physics_body.is_valid()) {
  160. // This code is run when the physics body is on the same GLTF node.
  161. body = p_physics_body->to_node();
  162. if (is_trigger && (p_physics_body->get_body_type() != "trigger")) {
  163. // Edge case: If the body's trigger and the collider's trigger
  164. // are in disagreement, we need to create another new body.
  165. CollisionObject3D *child = _generate_shape_with_body(p_state, p_gltf_node, p_physics_shape, nullptr);
  166. child->set_name(p_gltf_node->get_name() + (is_trigger ? String("Trigger") : String("Solid")));
  167. body->add_child(child);
  168. return body;
  169. }
  170. } else if (is_trigger) {
  171. body = memnew(Area3D);
  172. } else {
  173. body = memnew(StaticBody3D);
  174. }
  175. CollisionShape3D *shape = p_physics_shape->to_node();
  176. shape->set_name(p_gltf_node->get_name() + "Shape");
  177. body->add_child(shape);
  178. return body;
  179. }
  180. #endif // DISABLE_DEPRECATED
  181. CollisionObject3D *_get_ancestor_collision_object(Node *p_scene_parent) {
  182. // Note: Despite the name of the method, at the moment this only checks
  183. // the direct parent. Only check more later if Godot adds support for it.
  184. if (p_scene_parent) {
  185. CollisionObject3D *co = Object::cast_to<CollisionObject3D>(p_scene_parent);
  186. if (likely(co)) {
  187. return co;
  188. }
  189. }
  190. return nullptr;
  191. }
  192. Node3D *_generate_shape_node_and_body_if_needed(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Ref<GLTFPhysicsShape> p_physics_shape, CollisionObject3D *p_col_object, bool p_is_trigger) {
  193. // If we need to generate a body node, do so.
  194. CollisionObject3D *body_node = nullptr;
  195. if (p_is_trigger || p_physics_shape->get_is_trigger()) {
  196. // If the shape wants to be a trigger but it doesn't
  197. // have an Area3D parent, we need to make one.
  198. if (!Object::cast_to<Area3D>(p_col_object)) {
  199. body_node = memnew(Area3D);
  200. }
  201. } else {
  202. if (!Object::cast_to<PhysicsBody3D>(p_col_object)) {
  203. body_node = memnew(StaticBody3D);
  204. }
  205. }
  206. // Generate the shape node.
  207. _setup_shape_mesh_resource_from_index_if_needed(p_state, p_physics_shape);
  208. CollisionShape3D *shape_node = p_physics_shape->to_node(true);
  209. if (body_node) {
  210. shape_node->set_name(p_gltf_node->get_name() + "Shape");
  211. body_node->add_child(shape_node);
  212. return body_node;
  213. }
  214. return shape_node;
  215. }
  216. // Either add the child to the parent, or return the child if there is no parent.
  217. Node3D *_add_physics_node_to_given_node(Node3D *p_current_node, Node3D *p_child, Ref<GLTFNode> p_gltf_node) {
  218. if (!p_current_node) {
  219. return p_child;
  220. }
  221. String suffix;
  222. if (Object::cast_to<CollisionShape3D>(p_child)) {
  223. suffix = "Shape";
  224. } else if (Object::cast_to<Area3D>(p_child)) {
  225. suffix = "Trigger";
  226. } else {
  227. suffix = "Collider";
  228. }
  229. p_child->set_name(p_gltf_node->get_name() + suffix);
  230. p_current_node->add_child(p_child);
  231. return p_current_node;
  232. }
  233. Node3D *GLTFDocumentExtensionPhysics::generate_scene_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Node *p_scene_parent) {
  234. Ref<GLTFPhysicsBody> gltf_physics_body = p_gltf_node->get_additional_data(StringName("GLTFPhysicsBody"));
  235. #ifndef DISABLE_DEPRECATED
  236. // This deprecated code handles OMI_collider (which we internally name "GLTFPhysicsShape").
  237. Ref<GLTFPhysicsShape> gltf_physics_shape = p_gltf_node->get_additional_data(StringName("GLTFPhysicsShape"));
  238. if (gltf_physics_shape.is_valid()) {
  239. _setup_shape_mesh_resource_from_index_if_needed(p_state, gltf_physics_shape);
  240. // If this GLTF node specifies both a shape and a body, generate both.
  241. if (gltf_physics_body.is_valid()) {
  242. return _generate_shape_with_body(p_state, p_gltf_node, gltf_physics_shape, gltf_physics_body);
  243. }
  244. CollisionObject3D *ancestor_col_obj = _get_ancestor_collision_object(p_scene_parent);
  245. if (gltf_physics_shape->get_is_trigger()) {
  246. // If the shape wants to be a trigger and it already has a
  247. // trigger parent, we only need to make the shape node.
  248. if (Object::cast_to<Area3D>(ancestor_col_obj)) {
  249. return gltf_physics_shape->to_node(true);
  250. }
  251. } else if (ancestor_col_obj != nullptr) {
  252. // If the shape has a valid parent, only make the shape node.
  253. return gltf_physics_shape->to_node(true);
  254. }
  255. // Otherwise, we need to create a new body.
  256. return _generate_shape_with_body(p_state, p_gltf_node, gltf_physics_shape, nullptr);
  257. }
  258. #endif // DISABLE_DEPRECATED
  259. Node3D *ret = nullptr;
  260. CollisionObject3D *ancestor_col_obj = nullptr;
  261. if (gltf_physics_body.is_valid()) {
  262. ancestor_col_obj = gltf_physics_body->to_node();
  263. ret = ancestor_col_obj;
  264. } else {
  265. ancestor_col_obj = _get_ancestor_collision_object(p_scene_parent);
  266. if (!Object::cast_to<PhysicsBody3D>(ancestor_col_obj)) {
  267. if (p_gltf_node->get_additional_data(StringName("GLTFPhysicsCompoundCollider"))) {
  268. // If the GLTF file wants this node to group solid shapes together,
  269. // and there is no parent body, we need to create a static body.
  270. ancestor_col_obj = memnew(StaticBody3D);
  271. ret = ancestor_col_obj;
  272. }
  273. }
  274. }
  275. // Add the shapes to the tree. When an ancestor body is present, use it.
  276. // If an explicit body was specified, it has already been generated and
  277. // set above. If there is no ancestor body, we will either generate an
  278. // Area3D or StaticBody3D implicitly, so prefer an Area3D as the base
  279. // node for best compatibility with signal connections to this node.
  280. Ref<GLTFPhysicsShape> gltf_physics_collider_shape = p_gltf_node->get_additional_data(StringName("GLTFPhysicsColliderShape"));
  281. Ref<GLTFPhysicsShape> gltf_physics_trigger_shape = p_gltf_node->get_additional_data(StringName("GLTFPhysicsTriggerShape"));
  282. bool is_ancestor_col_obj_solid = Object::cast_to<PhysicsBody3D>(ancestor_col_obj);
  283. if (is_ancestor_col_obj_solid && gltf_physics_collider_shape.is_valid()) {
  284. Node3D *child = _generate_shape_node_and_body_if_needed(p_state, p_gltf_node, gltf_physics_collider_shape, ancestor_col_obj, false);
  285. ret = _add_physics_node_to_given_node(ret, child, p_gltf_node);
  286. }
  287. if (gltf_physics_trigger_shape.is_valid()) {
  288. Node3D *child = _generate_shape_node_and_body_if_needed(p_state, p_gltf_node, gltf_physics_trigger_shape, ancestor_col_obj, true);
  289. ret = _add_physics_node_to_given_node(ret, child, p_gltf_node);
  290. }
  291. if (!is_ancestor_col_obj_solid && gltf_physics_collider_shape.is_valid()) {
  292. Node3D *child = _generate_shape_node_and_body_if_needed(p_state, p_gltf_node, gltf_physics_collider_shape, ancestor_col_obj, false);
  293. ret = _add_physics_node_to_given_node(ret, child, p_gltf_node);
  294. }
  295. return ret;
  296. }
  297. // Export process.
  298. bool _are_all_faces_equal(const Vector<Face3> &p_a, const Vector<Face3> &p_b) {
  299. if (p_a.size() != p_b.size()) {
  300. return false;
  301. }
  302. for (int i = 0; i < p_a.size(); i++) {
  303. const Vector3 *a_vertices = p_a[i].vertex;
  304. const Vector3 *b_vertices = p_b[i].vertex;
  305. for (int j = 0; j < 3; j++) {
  306. if (!a_vertices[j].is_equal_approx(b_vertices[j])) {
  307. return false;
  308. }
  309. }
  310. }
  311. return true;
  312. }
  313. GLTFMeshIndex _get_or_insert_mesh_in_state(Ref<GLTFState> p_state, Ref<ImporterMesh> p_mesh) {
  314. ERR_FAIL_COND_V(p_mesh.is_null(), -1);
  315. TypedArray<GLTFMesh> state_meshes = p_state->get_meshes();
  316. Vector<Face3> mesh_faces = p_mesh->get_faces();
  317. // De-duplication: If the state already has the mesh we need, use that one.
  318. for (GLTFMeshIndex i = 0; i < state_meshes.size(); i++) {
  319. Ref<GLTFMesh> state_gltf_mesh = state_meshes[i];
  320. ERR_CONTINUE(state_gltf_mesh.is_null());
  321. Ref<ImporterMesh> state_importer_mesh = state_gltf_mesh->get_mesh();
  322. ERR_CONTINUE(state_importer_mesh.is_null());
  323. if (state_importer_mesh == p_mesh) {
  324. return i;
  325. }
  326. if (_are_all_faces_equal(state_importer_mesh->get_faces(), mesh_faces)) {
  327. return i;
  328. }
  329. }
  330. // After the loop, we have checked that the mesh is not equal to any of the
  331. // meshes in the state. So we insert a new mesh into the state mesh array.
  332. Ref<GLTFMesh> gltf_mesh;
  333. gltf_mesh.instantiate();
  334. gltf_mesh->set_mesh(p_mesh);
  335. GLTFMeshIndex mesh_index = state_meshes.size();
  336. state_meshes.push_back(gltf_mesh);
  337. p_state->set_meshes(state_meshes);
  338. return mesh_index;
  339. }
  340. void GLTFDocumentExtensionPhysics::convert_scene_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Node *p_scene_node) {
  341. if (cast_to<CollisionShape3D>(p_scene_node)) {
  342. CollisionShape3D *godot_shape = Object::cast_to<CollisionShape3D>(p_scene_node);
  343. Ref<GLTFPhysicsShape> gltf_shape = GLTFPhysicsShape::from_node(godot_shape);
  344. {
  345. Ref<ImporterMesh> importer_mesh = gltf_shape->get_importer_mesh();
  346. if (importer_mesh.is_valid()) {
  347. gltf_shape->set_mesh_index(_get_or_insert_mesh_in_state(p_state, importer_mesh));
  348. }
  349. }
  350. if (cast_to<Area3D>(_get_ancestor_collision_object(p_scene_node->get_parent()))) {
  351. p_gltf_node->set_additional_data(StringName("GLTFPhysicsTriggerShape"), gltf_shape);
  352. } else {
  353. p_gltf_node->set_additional_data(StringName("GLTFPhysicsColliderShape"), gltf_shape);
  354. }
  355. } else if (cast_to<CollisionObject3D>(p_scene_node)) {
  356. CollisionObject3D *godot_body = Object::cast_to<CollisionObject3D>(p_scene_node);
  357. p_gltf_node->set_additional_data(StringName("GLTFPhysicsBody"), GLTFPhysicsBody::from_node(godot_body));
  358. }
  359. }
  360. Array _get_or_create_state_shapes_in_state(Ref<GLTFState> p_state) {
  361. Dictionary state_json = p_state->get_json();
  362. Dictionary state_extensions;
  363. if (state_json.has("extensions")) {
  364. state_extensions = state_json["extensions"];
  365. } else {
  366. state_json["extensions"] = state_extensions;
  367. }
  368. Dictionary omi_physics_shape_ext;
  369. if (state_extensions.has("OMI_physics_shape")) {
  370. omi_physics_shape_ext = state_extensions["OMI_physics_shape"];
  371. } else {
  372. state_extensions["OMI_physics_shape"] = omi_physics_shape_ext;
  373. p_state->add_used_extension("OMI_physics_shape");
  374. }
  375. Array state_shapes;
  376. if (omi_physics_shape_ext.has("shapes")) {
  377. state_shapes = omi_physics_shape_ext["shapes"];
  378. } else {
  379. omi_physics_shape_ext["shapes"] = state_shapes;
  380. }
  381. return state_shapes;
  382. }
  383. Dictionary _export_node_shape(Ref<GLTFState> p_state, Ref<GLTFPhysicsShape> p_physics_shape) {
  384. Array state_shapes = _get_or_create_state_shapes_in_state(p_state);
  385. int size = state_shapes.size();
  386. Dictionary shape_property;
  387. Dictionary shape_dict = p_physics_shape->to_dictionary();
  388. for (int i = 0; i < size; i++) {
  389. Dictionary other = state_shapes[i];
  390. if (other == shape_dict) {
  391. // De-duplication: If we already have an identical shape,
  392. // set the shape index to the existing one and return.
  393. shape_property["shape"] = i;
  394. return shape_property;
  395. }
  396. }
  397. // If we don't have an identical shape, add it to the array.
  398. state_shapes.push_back(shape_dict);
  399. shape_property["shape"] = size;
  400. return shape_property;
  401. }
  402. Error GLTFDocumentExtensionPhysics::export_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &r_node_json, Node *p_node) {
  403. Dictionary physics_body_ext;
  404. Ref<GLTFPhysicsBody> physics_body = p_gltf_node->get_additional_data(StringName("GLTFPhysicsBody"));
  405. if (physics_body.is_valid()) {
  406. physics_body_ext = physics_body->to_dictionary();
  407. }
  408. Ref<GLTFPhysicsShape> collider_shape = p_gltf_node->get_additional_data(StringName("GLTFPhysicsColliderShape"));
  409. if (collider_shape.is_valid()) {
  410. physics_body_ext["collider"] = _export_node_shape(p_state, collider_shape);
  411. }
  412. Ref<GLTFPhysicsShape> trigger_shape = p_gltf_node->get_additional_data(StringName("GLTFPhysicsTriggerShape"));
  413. if (trigger_shape.is_valid()) {
  414. physics_body_ext["trigger"] = _export_node_shape(p_state, trigger_shape);
  415. }
  416. if (!physics_body_ext.is_empty()) {
  417. Dictionary node_extensions = r_node_json["extensions"];
  418. node_extensions["OMI_physics_body"] = physics_body_ext;
  419. p_state->add_used_extension("OMI_physics_body");
  420. }
  421. return OK;
  422. }