gltf_physics_shape.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /**************************************************************************/
  2. /* gltf_physics_shape.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_physics_shape.h"
  31. #include "../../gltf_state.h"
  32. #include "core/math/convex_hull.h"
  33. #include "scene/3d/physics/area_3d.h"
  34. #include "scene/resources/3d/box_shape_3d.h"
  35. #include "scene/resources/3d/capsule_shape_3d.h"
  36. #include "scene/resources/3d/concave_polygon_shape_3d.h"
  37. #include "scene/resources/3d/convex_polygon_shape_3d.h"
  38. #include "scene/resources/3d/cylinder_shape_3d.h"
  39. #include "scene/resources/3d/importer_mesh.h"
  40. #include "scene/resources/3d/sphere_shape_3d.h"
  41. void GLTFPhysicsShape::_bind_methods() {
  42. ClassDB::bind_static_method("GLTFPhysicsShape", D_METHOD("from_node", "shape_node"), &GLTFPhysicsShape::from_node);
  43. ClassDB::bind_method(D_METHOD("to_node", "cache_shapes"), &GLTFPhysicsShape::to_node, DEFVAL(false));
  44. ClassDB::bind_static_method("GLTFPhysicsShape", D_METHOD("from_resource", "shape_resource"), &GLTFPhysicsShape::from_resource);
  45. ClassDB::bind_method(D_METHOD("to_resource", "cache_shapes"), &GLTFPhysicsShape::to_resource, DEFVAL(false));
  46. ClassDB::bind_static_method("GLTFPhysicsShape", D_METHOD("from_dictionary", "dictionary"), &GLTFPhysicsShape::from_dictionary);
  47. ClassDB::bind_method(D_METHOD("to_dictionary"), &GLTFPhysicsShape::to_dictionary);
  48. ClassDB::bind_method(D_METHOD("get_shape_type"), &GLTFPhysicsShape::get_shape_type);
  49. ClassDB::bind_method(D_METHOD("set_shape_type", "shape_type"), &GLTFPhysicsShape::set_shape_type);
  50. ClassDB::bind_method(D_METHOD("get_size"), &GLTFPhysicsShape::get_size);
  51. ClassDB::bind_method(D_METHOD("set_size", "size"), &GLTFPhysicsShape::set_size);
  52. ClassDB::bind_method(D_METHOD("get_radius"), &GLTFPhysicsShape::get_radius);
  53. ClassDB::bind_method(D_METHOD("set_radius", "radius"), &GLTFPhysicsShape::set_radius);
  54. ClassDB::bind_method(D_METHOD("get_height"), &GLTFPhysicsShape::get_height);
  55. ClassDB::bind_method(D_METHOD("set_height", "height"), &GLTFPhysicsShape::set_height);
  56. ClassDB::bind_method(D_METHOD("get_is_trigger"), &GLTFPhysicsShape::get_is_trigger);
  57. ClassDB::bind_method(D_METHOD("set_is_trigger", "is_trigger"), &GLTFPhysicsShape::set_is_trigger);
  58. ClassDB::bind_method(D_METHOD("get_mesh_index"), &GLTFPhysicsShape::get_mesh_index);
  59. ClassDB::bind_method(D_METHOD("set_mesh_index", "mesh_index"), &GLTFPhysicsShape::set_mesh_index);
  60. ClassDB::bind_method(D_METHOD("get_importer_mesh"), &GLTFPhysicsShape::get_importer_mesh);
  61. ClassDB::bind_method(D_METHOD("set_importer_mesh", "importer_mesh"), &GLTFPhysicsShape::set_importer_mesh);
  62. ADD_PROPERTY(PropertyInfo(Variant::STRING, "shape_type"), "set_shape_type", "get_shape_type");
  63. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "size"), "set_size", "get_size");
  64. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius"), "set_radius", "get_radius");
  65. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height"), "set_height", "get_height");
  66. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "is_trigger"), "set_is_trigger", "get_is_trigger");
  67. ADD_PROPERTY(PropertyInfo(Variant::INT, "mesh_index"), "set_mesh_index", "get_mesh_index");
  68. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "importer_mesh", PROPERTY_HINT_RESOURCE_TYPE, "ImporterMesh"), "set_importer_mesh", "get_importer_mesh");
  69. }
  70. String GLTFPhysicsShape::get_shape_type() const {
  71. return shape_type;
  72. }
  73. void GLTFPhysicsShape::set_shape_type(String p_shape_type) {
  74. shape_type = p_shape_type;
  75. }
  76. Vector3 GLTFPhysicsShape::get_size() const {
  77. return size;
  78. }
  79. void GLTFPhysicsShape::set_size(Vector3 p_size) {
  80. size = p_size;
  81. }
  82. real_t GLTFPhysicsShape::get_radius() const {
  83. return radius;
  84. }
  85. void GLTFPhysicsShape::set_radius(real_t p_radius) {
  86. radius = p_radius;
  87. }
  88. real_t GLTFPhysicsShape::get_height() const {
  89. return height;
  90. }
  91. void GLTFPhysicsShape::set_height(real_t p_height) {
  92. height = p_height;
  93. }
  94. bool GLTFPhysicsShape::get_is_trigger() const {
  95. return is_trigger;
  96. }
  97. void GLTFPhysicsShape::set_is_trigger(bool p_is_trigger) {
  98. is_trigger = p_is_trigger;
  99. }
  100. GLTFMeshIndex GLTFPhysicsShape::get_mesh_index() const {
  101. return mesh_index;
  102. }
  103. void GLTFPhysicsShape::set_mesh_index(GLTFMeshIndex p_mesh_index) {
  104. mesh_index = p_mesh_index;
  105. }
  106. Ref<ImporterMesh> GLTFPhysicsShape::get_importer_mesh() const {
  107. return importer_mesh;
  108. }
  109. void GLTFPhysicsShape::set_importer_mesh(Ref<ImporterMesh> p_importer_mesh) {
  110. importer_mesh = p_importer_mesh;
  111. }
  112. Ref<ImporterMesh> _convert_hull_points_to_mesh(const Vector<Vector3> &p_hull_points) {
  113. Ref<ImporterMesh> importer_mesh;
  114. ERR_FAIL_COND_V_MSG(p_hull_points.size() < 3, importer_mesh, "GLTFPhysicsShape: Convex hull has fewer points (" + itos(p_hull_points.size()) + ") than the minimum of 3. At least 3 points are required in order to save to GLTF, since it uses a mesh to represent convex hulls.");
  115. if (p_hull_points.size() > 255) {
  116. WARN_PRINT("GLTFPhysicsShape: Convex hull has more points (" + itos(p_hull_points.size()) + ") than the recommended maximum of 255. This may not load correctly in other engines.");
  117. }
  118. // Convert the convex hull points into an array of faces.
  119. Geometry3D::MeshData md;
  120. Error err = ConvexHullComputer::convex_hull(p_hull_points, md);
  121. ERR_FAIL_COND_V_MSG(err != OK, importer_mesh, "GLTFPhysicsShape: Failed to compute convex hull.");
  122. Vector<Vector3> face_vertices;
  123. for (uint32_t i = 0; i < md.faces.size(); i++) {
  124. uint32_t index_count = md.faces[i].indices.size();
  125. for (uint32_t j = 1; j < index_count - 1; j++) {
  126. face_vertices.append(p_hull_points[md.faces[i].indices[0]]);
  127. face_vertices.append(p_hull_points[md.faces[i].indices[j]]);
  128. face_vertices.append(p_hull_points[md.faces[i].indices[j + 1]]);
  129. }
  130. }
  131. // Create an ImporterMesh from the faces.
  132. importer_mesh.instantiate();
  133. Array surface_array;
  134. surface_array.resize(Mesh::ArrayType::ARRAY_MAX);
  135. surface_array[Mesh::ArrayType::ARRAY_VERTEX] = face_vertices;
  136. importer_mesh->add_surface(Mesh::PRIMITIVE_TRIANGLES, surface_array);
  137. return importer_mesh;
  138. }
  139. Ref<GLTFPhysicsShape> GLTFPhysicsShape::from_node(const CollisionShape3D *p_godot_shape_node) {
  140. Ref<GLTFPhysicsShape> gltf_shape;
  141. ERR_FAIL_NULL_V_MSG(p_godot_shape_node, gltf_shape, "Tried to create a GLTFPhysicsShape from a CollisionShape3D node, but the given node was null.");
  142. Ref<Shape3D> shape_resource = p_godot_shape_node->get_shape();
  143. ERR_FAIL_COND_V_MSG(shape_resource.is_null(), gltf_shape, "Tried to create a GLTFPhysicsShape from a CollisionShape3D node, but the given node had a null shape.");
  144. gltf_shape = from_resource(shape_resource);
  145. // Check if the shape is part of a trigger.
  146. Node *parent = p_godot_shape_node->get_parent();
  147. if (cast_to<const Area3D>(parent)) {
  148. gltf_shape->set_is_trigger(true);
  149. }
  150. return gltf_shape;
  151. }
  152. CollisionShape3D *GLTFPhysicsShape::to_node(bool p_cache_shapes) {
  153. CollisionShape3D *godot_shape_node = memnew(CollisionShape3D);
  154. to_resource(p_cache_shapes); // Sets `_shape_cache`.
  155. godot_shape_node->set_shape(_shape_cache);
  156. return godot_shape_node;
  157. }
  158. Ref<GLTFPhysicsShape> GLTFPhysicsShape::from_resource(const Ref<Shape3D> &p_shape_resource) {
  159. Ref<GLTFPhysicsShape> gltf_shape;
  160. gltf_shape.instantiate();
  161. ERR_FAIL_COND_V_MSG(p_shape_resource.is_null(), gltf_shape, "Tried to create a GLTFPhysicsShape from a Shape3D resource, but the given resource was null.");
  162. if (cast_to<BoxShape3D>(p_shape_resource.ptr())) {
  163. gltf_shape->shape_type = "box";
  164. Ref<BoxShape3D> box = p_shape_resource;
  165. gltf_shape->set_size(box->get_size());
  166. } else if (cast_to<const CapsuleShape3D>(p_shape_resource.ptr())) {
  167. gltf_shape->shape_type = "capsule";
  168. Ref<CapsuleShape3D> capsule = p_shape_resource;
  169. gltf_shape->set_radius(capsule->get_radius());
  170. gltf_shape->set_height(capsule->get_height());
  171. } else if (cast_to<const CylinderShape3D>(p_shape_resource.ptr())) {
  172. gltf_shape->shape_type = "cylinder";
  173. Ref<CylinderShape3D> cylinder = p_shape_resource;
  174. gltf_shape->set_radius(cylinder->get_radius());
  175. gltf_shape->set_height(cylinder->get_height());
  176. } else if (cast_to<const SphereShape3D>(p_shape_resource.ptr())) {
  177. gltf_shape->shape_type = "sphere";
  178. Ref<SphereShape3D> sphere = p_shape_resource;
  179. gltf_shape->set_radius(sphere->get_radius());
  180. } else if (cast_to<const ConvexPolygonShape3D>(p_shape_resource.ptr())) {
  181. gltf_shape->shape_type = "convex";
  182. Ref<ConvexPolygonShape3D> convex = p_shape_resource;
  183. Vector<Vector3> hull_points = convex->get_points();
  184. Ref<ImporterMesh> importer_mesh = _convert_hull_points_to_mesh(hull_points);
  185. ERR_FAIL_COND_V_MSG(importer_mesh.is_null(), gltf_shape, "GLTFPhysicsShape: Failed to convert convex hull points to a mesh.");
  186. gltf_shape->set_importer_mesh(importer_mesh);
  187. } else if (cast_to<const ConcavePolygonShape3D>(p_shape_resource.ptr())) {
  188. gltf_shape->shape_type = "trimesh";
  189. Ref<ConcavePolygonShape3D> concave = p_shape_resource;
  190. Ref<ImporterMesh> importer_mesh;
  191. importer_mesh.instantiate();
  192. Array surface_array;
  193. surface_array.resize(Mesh::ArrayType::ARRAY_MAX);
  194. surface_array[Mesh::ArrayType::ARRAY_VERTEX] = concave->get_faces();
  195. importer_mesh->add_surface(Mesh::PRIMITIVE_TRIANGLES, surface_array);
  196. gltf_shape->set_importer_mesh(importer_mesh);
  197. } else {
  198. ERR_PRINT("Tried to create a GLTFPhysicsShape from a Shape3D, but the given shape '" + String(Variant(p_shape_resource)) +
  199. "' had an unsupported shape type. Only BoxShape3D, CapsuleShape3D, CylinderShape3D, SphereShape3D, ConcavePolygonShape3D, and ConvexPolygonShape3D are supported.");
  200. }
  201. gltf_shape->_shape_cache = p_shape_resource;
  202. return gltf_shape;
  203. }
  204. Ref<Shape3D> GLTFPhysicsShape::to_resource(bool p_cache_shapes) {
  205. if (!p_cache_shapes || _shape_cache == nullptr) {
  206. if (shape_type == "box") {
  207. Ref<BoxShape3D> box;
  208. box.instantiate();
  209. box->set_size(size);
  210. _shape_cache = box;
  211. } else if (shape_type == "capsule") {
  212. Ref<CapsuleShape3D> capsule;
  213. capsule.instantiate();
  214. capsule->set_radius(radius);
  215. capsule->set_height(height);
  216. _shape_cache = capsule;
  217. } else if (shape_type == "cylinder") {
  218. Ref<CylinderShape3D> cylinder;
  219. cylinder.instantiate();
  220. cylinder->set_radius(radius);
  221. cylinder->set_height(height);
  222. _shape_cache = cylinder;
  223. } else if (shape_type == "sphere") {
  224. Ref<SphereShape3D> sphere;
  225. sphere.instantiate();
  226. sphere->set_radius(radius);
  227. _shape_cache = sphere;
  228. } else if (shape_type == "convex") {
  229. ERR_FAIL_COND_V_MSG(importer_mesh.is_null(), _shape_cache, "GLTFPhysicsShape: Error converting convex hull shape to a shape resource: The mesh resource is null.");
  230. Ref<ConvexPolygonShape3D> convex = importer_mesh->get_mesh()->create_convex_shape();
  231. _shape_cache = convex;
  232. } else if (shape_type == "trimesh") {
  233. ERR_FAIL_COND_V_MSG(importer_mesh.is_null(), _shape_cache, "GLTFPhysicsShape: Error converting concave mesh shape to a shape resource: The mesh resource is null.");
  234. Ref<ConcavePolygonShape3D> concave = importer_mesh->create_trimesh_shape();
  235. _shape_cache = concave;
  236. } else {
  237. ERR_PRINT("GLTFPhysicsShape: Error converting to a shape resource: Shape type '" + shape_type + "' is unknown.");
  238. }
  239. }
  240. return _shape_cache;
  241. }
  242. Ref<GLTFPhysicsShape> GLTFPhysicsShape::from_dictionary(const Dictionary p_dictionary) {
  243. ERR_FAIL_COND_V_MSG(!p_dictionary.has("type"), Ref<GLTFPhysicsShape>(), "Failed to parse GLTFPhysicsShape, missing required field 'type'.");
  244. Ref<GLTFPhysicsShape> gltf_shape;
  245. gltf_shape.instantiate();
  246. String shape_type = p_dictionary["type"];
  247. if (shape_type == "hull") {
  248. shape_type = "convex";
  249. }
  250. gltf_shape->shape_type = shape_type;
  251. if (shape_type != "box" && shape_type != "capsule" && shape_type != "cylinder" && shape_type != "sphere" && shape_type != "convex" && shape_type != "trimesh") {
  252. ERR_PRINT("GLTFPhysicsShape: Error parsing unknown shape type '" + shape_type + "'. Only box, capsule, cylinder, sphere, convex, and trimesh are supported.");
  253. }
  254. Dictionary properties;
  255. if (p_dictionary.has(shape_type)) {
  256. properties = p_dictionary[shape_type];
  257. } else {
  258. properties = p_dictionary;
  259. }
  260. if (properties.has("radius")) {
  261. gltf_shape->set_radius(properties["radius"]);
  262. }
  263. if (properties.has("height")) {
  264. gltf_shape->set_height(properties["height"]);
  265. }
  266. if (properties.has("size")) {
  267. const Array &arr = properties["size"];
  268. if (arr.size() == 3) {
  269. gltf_shape->set_size(Vector3(arr[0], arr[1], arr[2]));
  270. } else {
  271. ERR_PRINT("GLTFPhysicsShape: Error parsing the size, it must have exactly 3 numbers.");
  272. }
  273. }
  274. if (properties.has("isTrigger")) {
  275. gltf_shape->set_is_trigger(properties["isTrigger"]);
  276. }
  277. if (properties.has("mesh")) {
  278. gltf_shape->set_mesh_index(properties["mesh"]);
  279. }
  280. if (unlikely(gltf_shape->get_mesh_index() < 0 && (shape_type == "convex" || shape_type == "trimesh"))) {
  281. ERR_PRINT("Error parsing GLTFPhysicsShape: The mesh-based shape type '" + shape_type + "' does not have a valid mesh index.");
  282. }
  283. return gltf_shape;
  284. }
  285. Dictionary GLTFPhysicsShape::to_dictionary() const {
  286. Dictionary gltf_shape;
  287. gltf_shape["type"] = shape_type;
  288. Dictionary sub;
  289. if (shape_type == "box") {
  290. Array size_array;
  291. size_array.resize(3);
  292. size_array[0] = size.x;
  293. size_array[1] = size.y;
  294. size_array[2] = size.z;
  295. sub["size"] = size_array;
  296. } else if (shape_type == "capsule") {
  297. sub["radius"] = get_radius();
  298. sub["height"] = get_height();
  299. } else if (shape_type == "cylinder") {
  300. sub["radius"] = get_radius();
  301. sub["height"] = get_height();
  302. } else if (shape_type == "sphere") {
  303. sub["radius"] = get_radius();
  304. } else if (shape_type == "trimesh" || shape_type == "convex") {
  305. sub["mesh"] = get_mesh_index();
  306. }
  307. gltf_shape[shape_type] = sub;
  308. return gltf_shape;
  309. }