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