Path: blob/master/modules/gltf/extensions/physics/gltf_physics_shape.cpp
10279 views
/**************************************************************************/1/* gltf_physics_shape.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "gltf_physics_shape.h"3132#include "../../gltf_state.h"3334#include "core/math/convex_hull.h"35#include "scene/3d/physics/area_3d.h"36#include "scene/resources/3d/box_shape_3d.h"37#include "scene/resources/3d/capsule_shape_3d.h"38#include "scene/resources/3d/concave_polygon_shape_3d.h"39#include "scene/resources/3d/convex_polygon_shape_3d.h"40#include "scene/resources/3d/cylinder_shape_3d.h"41#include "scene/resources/3d/importer_mesh.h"42#include "scene/resources/3d/sphere_shape_3d.h"4344void GLTFPhysicsShape::_bind_methods() {45ClassDB::bind_static_method("GLTFPhysicsShape", D_METHOD("from_node", "shape_node"), &GLTFPhysicsShape::from_node);46ClassDB::bind_method(D_METHOD("to_node", "cache_shapes"), &GLTFPhysicsShape::to_node, DEFVAL(false));4748ClassDB::bind_static_method("GLTFPhysicsShape", D_METHOD("from_resource", "shape_resource"), &GLTFPhysicsShape::from_resource);49ClassDB::bind_method(D_METHOD("to_resource", "cache_shapes"), &GLTFPhysicsShape::to_resource, DEFVAL(false));5051ClassDB::bind_static_method("GLTFPhysicsShape", D_METHOD("from_dictionary", "dictionary"), &GLTFPhysicsShape::from_dictionary);52ClassDB::bind_method(D_METHOD("to_dictionary"), &GLTFPhysicsShape::to_dictionary);5354ClassDB::bind_method(D_METHOD("get_shape_type"), &GLTFPhysicsShape::get_shape_type);55ClassDB::bind_method(D_METHOD("set_shape_type", "shape_type"), &GLTFPhysicsShape::set_shape_type);56ClassDB::bind_method(D_METHOD("get_size"), &GLTFPhysicsShape::get_size);57ClassDB::bind_method(D_METHOD("set_size", "size"), &GLTFPhysicsShape::set_size);58ClassDB::bind_method(D_METHOD("get_radius"), &GLTFPhysicsShape::get_radius);59ClassDB::bind_method(D_METHOD("set_radius", "radius"), &GLTFPhysicsShape::set_radius);60ClassDB::bind_method(D_METHOD("get_height"), &GLTFPhysicsShape::get_height);61ClassDB::bind_method(D_METHOD("set_height", "height"), &GLTFPhysicsShape::set_height);62ClassDB::bind_method(D_METHOD("get_is_trigger"), &GLTFPhysicsShape::get_is_trigger);63ClassDB::bind_method(D_METHOD("set_is_trigger", "is_trigger"), &GLTFPhysicsShape::set_is_trigger);64ClassDB::bind_method(D_METHOD("get_mesh_index"), &GLTFPhysicsShape::get_mesh_index);65ClassDB::bind_method(D_METHOD("set_mesh_index", "mesh_index"), &GLTFPhysicsShape::set_mesh_index);66ClassDB::bind_method(D_METHOD("get_importer_mesh"), &GLTFPhysicsShape::get_importer_mesh);67ClassDB::bind_method(D_METHOD("set_importer_mesh", "importer_mesh"), &GLTFPhysicsShape::set_importer_mesh);6869ADD_PROPERTY(PropertyInfo(Variant::STRING, "shape_type"), "set_shape_type", "get_shape_type");70ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "size"), "set_size", "get_size");71ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius"), "set_radius", "get_radius");72ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height"), "set_height", "get_height");73ADD_PROPERTY(PropertyInfo(Variant::BOOL, "is_trigger"), "set_is_trigger", "get_is_trigger");74ADD_PROPERTY(PropertyInfo(Variant::INT, "mesh_index"), "set_mesh_index", "get_mesh_index");75ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "importer_mesh", PROPERTY_HINT_RESOURCE_TYPE, "ImporterMesh"), "set_importer_mesh", "get_importer_mesh");76}7778String GLTFPhysicsShape::get_shape_type() const {79return shape_type;80}8182void GLTFPhysicsShape::set_shape_type(String p_shape_type) {83shape_type = p_shape_type;84}8586Vector3 GLTFPhysicsShape::get_size() const {87return size;88}8990void GLTFPhysicsShape::set_size(Vector3 p_size) {91size = p_size;92}9394real_t GLTFPhysicsShape::get_radius() const {95return radius;96}9798void GLTFPhysicsShape::set_radius(real_t p_radius) {99radius = p_radius;100}101102real_t GLTFPhysicsShape::get_height() const {103return height;104}105106void GLTFPhysicsShape::set_height(real_t p_height) {107height = p_height;108}109110bool GLTFPhysicsShape::get_is_trigger() const {111return is_trigger;112}113114void GLTFPhysicsShape::set_is_trigger(bool p_is_trigger) {115is_trigger = p_is_trigger;116}117118GLTFMeshIndex GLTFPhysicsShape::get_mesh_index() const {119return mesh_index;120}121122void GLTFPhysicsShape::set_mesh_index(GLTFMeshIndex p_mesh_index) {123mesh_index = p_mesh_index;124}125126Ref<ImporterMesh> GLTFPhysicsShape::get_importer_mesh() const {127return importer_mesh;128}129130void GLTFPhysicsShape::set_importer_mesh(Ref<ImporterMesh> p_importer_mesh) {131importer_mesh = p_importer_mesh;132}133134Ref<ImporterMesh> _convert_hull_points_to_mesh(const Vector<Vector3> &p_hull_points) {135Ref<ImporterMesh> importer_mesh;136ERR_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.");137if (p_hull_points.size() > 255) {138WARN_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.");139}140// Convert the convex hull points into an array of faces.141Geometry3D::MeshData md;142Error err = ConvexHullComputer::convex_hull(p_hull_points, md);143ERR_FAIL_COND_V_MSG(err != OK, importer_mesh, "GLTFPhysicsShape: Failed to compute convex hull.");144Vector<Vector3> face_vertices;145for (uint32_t i = 0; i < md.faces.size(); i++) {146uint32_t index_count = md.faces[i].indices.size();147for (uint32_t j = 1; j < index_count - 1; j++) {148face_vertices.append(p_hull_points[md.faces[i].indices[0]]);149face_vertices.append(p_hull_points[md.faces[i].indices[j]]);150face_vertices.append(p_hull_points[md.faces[i].indices[j + 1]]);151}152}153// Create an ImporterMesh from the faces.154importer_mesh.instantiate();155Array surface_array;156surface_array.resize(Mesh::ArrayType::ARRAY_MAX);157surface_array[Mesh::ArrayType::ARRAY_VERTEX] = face_vertices;158importer_mesh->add_surface(Mesh::PRIMITIVE_TRIANGLES, surface_array);159return importer_mesh;160}161162Ref<GLTFPhysicsShape> GLTFPhysicsShape::from_node(const CollisionShape3D *p_godot_shape_node) {163Ref<GLTFPhysicsShape> gltf_shape;164ERR_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.");165Ref<Shape3D> shape_resource = p_godot_shape_node->get_shape();166ERR_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.");167gltf_shape = from_resource(shape_resource);168// Check if the shape is part of a trigger.169Node *parent = p_godot_shape_node->get_parent();170if (cast_to<const Area3D>(parent)) {171gltf_shape->set_is_trigger(true);172}173return gltf_shape;174}175176CollisionShape3D *GLTFPhysicsShape::to_node(bool p_cache_shapes) {177CollisionShape3D *godot_shape_node = memnew(CollisionShape3D);178to_resource(p_cache_shapes); // Sets `_shape_cache`.179godot_shape_node->set_shape(_shape_cache);180return godot_shape_node;181}182183Ref<GLTFPhysicsShape> GLTFPhysicsShape::from_resource(const Ref<Shape3D> &p_shape_resource) {184Ref<GLTFPhysicsShape> gltf_shape;185gltf_shape.instantiate();186ERR_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.");187if (cast_to<BoxShape3D>(p_shape_resource.ptr())) {188gltf_shape->shape_type = "box";189Ref<BoxShape3D> box = p_shape_resource;190gltf_shape->set_size(box->get_size());191} else if (cast_to<const CapsuleShape3D>(p_shape_resource.ptr())) {192gltf_shape->shape_type = "capsule";193Ref<CapsuleShape3D> capsule = p_shape_resource;194gltf_shape->set_radius(capsule->get_radius());195gltf_shape->set_height(capsule->get_height());196} else if (cast_to<const CylinderShape3D>(p_shape_resource.ptr())) {197gltf_shape->shape_type = "cylinder";198Ref<CylinderShape3D> cylinder = p_shape_resource;199gltf_shape->set_radius(cylinder->get_radius());200gltf_shape->set_height(cylinder->get_height());201} else if (cast_to<const SphereShape3D>(p_shape_resource.ptr())) {202gltf_shape->shape_type = "sphere";203Ref<SphereShape3D> sphere = p_shape_resource;204gltf_shape->set_radius(sphere->get_radius());205} else if (cast_to<const ConvexPolygonShape3D>(p_shape_resource.ptr())) {206gltf_shape->shape_type = "convex";207Ref<ConvexPolygonShape3D> convex = p_shape_resource;208Vector<Vector3> hull_points = convex->get_points();209Ref<ImporterMesh> importer_mesh = _convert_hull_points_to_mesh(hull_points);210ERR_FAIL_COND_V_MSG(importer_mesh.is_null(), gltf_shape, "GLTFPhysicsShape: Failed to convert convex hull points to a mesh.");211gltf_shape->set_importer_mesh(importer_mesh);212} else if (cast_to<const ConcavePolygonShape3D>(p_shape_resource.ptr())) {213gltf_shape->shape_type = "trimesh";214Ref<ConcavePolygonShape3D> concave = p_shape_resource;215Ref<ImporterMesh> importer_mesh;216importer_mesh.instantiate();217Array surface_array;218surface_array.resize(Mesh::ArrayType::ARRAY_MAX);219surface_array[Mesh::ArrayType::ARRAY_VERTEX] = concave->get_faces();220importer_mesh->add_surface(Mesh::PRIMITIVE_TRIANGLES, surface_array);221gltf_shape->set_importer_mesh(importer_mesh);222} else {223ERR_PRINT("Tried to create a GLTFPhysicsShape from a Shape3D, but the given shape '" + String(Variant(p_shape_resource)) +224"' had an unsupported shape type. Only BoxShape3D, CapsuleShape3D, CylinderShape3D, SphereShape3D, ConcavePolygonShape3D, and ConvexPolygonShape3D are supported.");225}226gltf_shape->_shape_cache = p_shape_resource;227return gltf_shape;228}229230Ref<Shape3D> GLTFPhysicsShape::to_resource(bool p_cache_shapes) {231if (!p_cache_shapes || _shape_cache.is_null()) {232if (shape_type == "box") {233Ref<BoxShape3D> box;234box.instantiate();235box->set_size(size);236_shape_cache = box;237} else if (shape_type == "capsule") {238Ref<CapsuleShape3D> capsule;239capsule.instantiate();240capsule->set_radius(radius);241capsule->set_height(height);242_shape_cache = capsule;243} else if (shape_type == "cylinder") {244Ref<CylinderShape3D> cylinder;245cylinder.instantiate();246cylinder->set_radius(radius);247cylinder->set_height(height);248_shape_cache = cylinder;249} else if (shape_type == "sphere") {250Ref<SphereShape3D> sphere;251sphere.instantiate();252sphere->set_radius(radius);253_shape_cache = sphere;254} else if (shape_type == "convex") {255ERR_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.");256Ref<ConvexPolygonShape3D> convex = importer_mesh->get_mesh()->create_convex_shape();257_shape_cache = convex;258} else if (shape_type == "trimesh") {259ERR_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.");260Ref<ConcavePolygonShape3D> concave = importer_mesh->create_trimesh_shape();261_shape_cache = concave;262} else {263ERR_PRINT("GLTFPhysicsShape: Error converting to a shape resource: Shape type '" + shape_type + "' is unknown.");264}265}266return _shape_cache;267}268269Ref<GLTFPhysicsShape> GLTFPhysicsShape::from_dictionary(const Dictionary p_dictionary) {270ERR_FAIL_COND_V_MSG(!p_dictionary.has("type"), Ref<GLTFPhysicsShape>(), "Failed to parse GLTFPhysicsShape, missing required field 'type'.");271Ref<GLTFPhysicsShape> gltf_shape;272gltf_shape.instantiate();273String shape_type = p_dictionary["type"];274if (shape_type == "hull") {275shape_type = "convex";276}277gltf_shape->shape_type = shape_type;278if (shape_type != "box" && shape_type != "capsule" && shape_type != "cylinder" && shape_type != "sphere" && shape_type != "convex" && shape_type != "trimesh") {279ERR_PRINT("GLTFPhysicsShape: Error parsing unknown shape type '" + shape_type + "'. Only box, capsule, cylinder, sphere, convex, and trimesh are supported.");280}281Dictionary properties;282if (p_dictionary.has(shape_type)) {283properties = p_dictionary[shape_type];284} else {285properties = p_dictionary;286}287if (properties.has("radius")) {288gltf_shape->set_radius(properties["radius"]);289}290if (properties.has("height")) {291gltf_shape->set_height(properties["height"]);292}293if (properties.has("size")) {294const Array &arr = properties["size"];295if (arr.size() == 3) {296gltf_shape->set_size(Vector3(arr[0], arr[1], arr[2]));297} else {298ERR_PRINT("GLTFPhysicsShape: Error parsing the size, it must have exactly 3 numbers.");299}300}301if (properties.has("isTrigger")) {302gltf_shape->set_is_trigger(properties["isTrigger"]);303}304if (properties.has("mesh")) {305gltf_shape->set_mesh_index(properties["mesh"]);306}307if (unlikely(gltf_shape->get_mesh_index() < 0 && (shape_type == "convex" || shape_type == "trimesh"))) {308ERR_PRINT("Error parsing GLTFPhysicsShape: The mesh-based shape type '" + shape_type + "' does not have a valid mesh index.");309}310return gltf_shape;311}312313Dictionary GLTFPhysicsShape::to_dictionary() const {314Dictionary gltf_shape;315gltf_shape["type"] = shape_type;316Dictionary sub;317if (shape_type == "box") {318Array size_array;319size_array.resize(3);320size_array[0] = size.x;321size_array[1] = size.y;322size_array[2] = size.z;323sub["size"] = size_array;324} else if (shape_type == "capsule") {325sub["radius"] = get_radius();326sub["height"] = get_height();327} else if (shape_type == "cylinder") {328sub["radius"] = get_radius();329sub["height"] = get_height();330} else if (shape_type == "sphere") {331sub["radius"] = get_radius();332} else if (shape_type == "trimesh" || shape_type == "convex") {333sub["mesh"] = get_mesh_index();334}335gltf_shape[shape_type] = sub;336return gltf_shape;337}338339340