Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/gltf/extensions/physics/gltf_document_extension_physics.cpp
10279 views
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
31
#include "gltf_document_extension_physics.h"
32
33
#include "scene/3d/physics/area_3d.h"
34
#include "scene/3d/physics/rigid_body_3d.h"
35
#include "scene/3d/physics/static_body_3d.h"
36
37
using GLTFShapeIndex = int64_t;
38
39
// Import process.
40
Error GLTFDocumentExtensionPhysics::import_preflight(Ref<GLTFState> p_state, Vector<String> p_extensions) {
41
if (!p_extensions.has("OMI_collider") && !p_extensions.has("OMI_physics_body") && !p_extensions.has("OMI_physics_shape")) {
42
return ERR_SKIP;
43
}
44
Dictionary state_json = p_state->get_json();
45
if (state_json.has("extensions")) {
46
Dictionary state_extensions = state_json["extensions"];
47
if (state_extensions.has("OMI_physics_shape")) {
48
Dictionary omi_physics_shape_ext = state_extensions["OMI_physics_shape"];
49
if (omi_physics_shape_ext.has("shapes")) {
50
Array state_shape_dicts = omi_physics_shape_ext["shapes"];
51
if (state_shape_dicts.size() > 0) {
52
Array state_shapes;
53
for (int i = 0; i < state_shape_dicts.size(); i++) {
54
state_shapes.push_back(GLTFPhysicsShape::from_dictionary(state_shape_dicts[i]));
55
}
56
p_state->set_additional_data(StringName("GLTFPhysicsShapes"), state_shapes);
57
}
58
}
59
#ifndef DISABLE_DEPRECATED
60
} else if (state_extensions.has("OMI_collider")) {
61
Dictionary omi_collider_ext = state_extensions["OMI_collider"];
62
if (omi_collider_ext.has("colliders")) {
63
Array state_collider_dicts = omi_collider_ext["colliders"];
64
if (state_collider_dicts.size() > 0) {
65
Array state_colliders;
66
for (int i = 0; i < state_collider_dicts.size(); i++) {
67
state_colliders.push_back(GLTFPhysicsShape::from_dictionary(state_collider_dicts[i]));
68
}
69
p_state->set_additional_data(StringName("GLTFPhysicsShapes"), state_colliders);
70
}
71
}
72
#endif // DISABLE_DEPRECATED
73
}
74
}
75
return OK;
76
}
77
78
Vector<String> GLTFDocumentExtensionPhysics::get_supported_extensions() {
79
Vector<String> ret;
80
ret.push_back("OMI_collider");
81
ret.push_back("OMI_physics_body");
82
ret.push_back("OMI_physics_shape");
83
return ret;
84
}
85
86
Error GLTFDocumentExtensionPhysics::parse_node_extensions(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &p_extensions) {
87
#ifndef DISABLE_DEPRECATED
88
if (p_extensions.has("OMI_collider")) {
89
Dictionary node_collider_ext = p_extensions["OMI_collider"];
90
if (node_collider_ext.has("collider")) {
91
// "collider" is the index of the collider in the state colliders array.
92
int node_collider_index = node_collider_ext["collider"];
93
Array state_colliders = p_state->get_additional_data(StringName("GLTFPhysicsShapes"));
94
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()) + ").");
95
p_gltf_node->set_additional_data(StringName("GLTFPhysicsShape"), state_colliders[node_collider_index]);
96
} else {
97
p_gltf_node->set_additional_data(StringName("GLTFPhysicsShape"), GLTFPhysicsShape::from_dictionary(node_collider_ext));
98
}
99
}
100
#endif // DISABLE_DEPRECATED
101
if (p_extensions.has("OMI_physics_body")) {
102
Dictionary physics_body_ext = p_extensions["OMI_physics_body"];
103
if (physics_body_ext.has("collider")) {
104
Dictionary node_collider = physics_body_ext["collider"];
105
// "shape" is the index of the shape in the state shapes array.
106
int node_shape_index = node_collider.get("shape", -1);
107
if (node_shape_index != -1) {
108
Array state_shapes = p_state->get_additional_data(StringName("GLTFPhysicsShapes"));
109
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()) + ").");
110
p_gltf_node->set_additional_data(StringName("GLTFPhysicsColliderShape"), state_shapes[node_shape_index]);
111
p_gltf_node->set_additional_data(StringName("GLTFPhysicsColliderShapeIndex"), node_shape_index);
112
} else {
113
// If this node is a collider but does not have a collider
114
// shape, then it only serves to combine together shapes.
115
p_gltf_node->set_additional_data(StringName("GLTFPhysicsCompoundCollider"), true);
116
}
117
}
118
if (physics_body_ext.has("trigger")) {
119
Dictionary node_trigger = physics_body_ext["trigger"];
120
// "shape" is the index of the shape in the state shapes array.
121
int node_shape_index = node_trigger.get("shape", -1);
122
if (node_shape_index != -1) {
123
Array state_shapes = p_state->get_additional_data(StringName("GLTFPhysicsShapes"));
124
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()) + ").");
125
p_gltf_node->set_additional_data(StringName("GLTFPhysicsTriggerShape"), state_shapes[node_shape_index]);
126
p_gltf_node->set_additional_data(StringName("GLTFPhysicsTriggerShapeIndex"), node_shape_index);
127
} else {
128
// If this node is a trigger but does not have a trigger shape,
129
// then it's a trigger body, what Godot calls an Area3D node.
130
Ref<GLTFPhysicsBody> trigger_body;
131
trigger_body.instantiate();
132
trigger_body->set_body_type("trigger");
133
p_gltf_node->set_additional_data(StringName("GLTFPhysicsBody"), trigger_body);
134
}
135
// If this node defines explicit member shape nodes, save this information.
136
if (node_trigger.has("nodes")) {
137
Array compound_trigger_nodes = node_trigger["nodes"];
138
p_gltf_node->set_additional_data(StringName("GLTFPhysicsCompoundTriggerNodes"), compound_trigger_nodes);
139
}
140
}
141
if (physics_body_ext.has("motion") || physics_body_ext.has("type")) {
142
p_gltf_node->set_additional_data(StringName("GLTFPhysicsBody"), GLTFPhysicsBody::from_dictionary(physics_body_ext));
143
}
144
}
145
return OK;
146
}
147
148
bool _will_gltf_shape_become_subnode(Ref<GLTFState> p_state, const Ref<GLTFNode> p_gltf_node, GLTFNodeIndex p_gltf_node_index) {
149
if (p_gltf_node->has_additional_data(StringName("GLTFPhysicsBody"))) {
150
return true;
151
}
152
const TypedArray<GLTFNode> state_gltf_nodes = p_state->get_nodes();
153
const GLTFNodeIndex parent_index = p_gltf_node->get_parent();
154
if (parent_index == -1 || parent_index >= state_gltf_nodes.size()) {
155
return true;
156
}
157
const Ref<GLTFNode> parent_gltf_node = state_gltf_nodes[parent_index];
158
const Variant parent_body_maybe = parent_gltf_node->get_additional_data(StringName("GLTFPhysicsBody"));
159
if (parent_body_maybe.get_type() != Variant::NIL) {
160
Ref<GLTFPhysicsBody> parent_body = parent_body_maybe;
161
// If the parent matches the triggerness, then this node will be generated as a shape (CollisionShape3D).
162
// Otherwise, if there is a mismatch, a body will be generated for this node, and a subnode will also be generated for the shape.
163
if (parent_body->get_body_type() == "trigger") {
164
return p_gltf_node->has_additional_data(StringName("GLTFPhysicsColliderShape"));
165
} else {
166
return p_gltf_node->has_additional_data(StringName("GLTFPhysicsTriggerShape"));
167
}
168
}
169
if (parent_gltf_node->has_additional_data(StringName("GLTFPhysicsColliderShape"))) {
170
return false;
171
}
172
if (parent_gltf_node->has_additional_data(StringName("GLTFPhysicsTriggerShape"))) {
173
return false;
174
}
175
Variant compound_trigger_maybe = parent_gltf_node->has_additional_data(StringName("GLTFPhysicsCompoundTriggerNodes"));
176
if (compound_trigger_maybe.get_type() != Variant::NIL) {
177
Array compound_trigger_nodes = compound_trigger_maybe;
178
// Remember, JSON only has numbers, not integers, so must cast to double.
179
return !compound_trigger_nodes.has((double)p_gltf_node_index);
180
}
181
return true;
182
}
183
184
NodePath _get_scene_node_path_for_shape_index(Ref<GLTFState> p_state, const GLTFNodeIndex p_shape_index) {
185
TypedArray<GLTFNode> state_gltf_nodes = p_state->get_nodes();
186
for (GLTFNodeIndex node_index = 0; node_index < state_gltf_nodes.size(); node_index++) {
187
const Ref<GLTFNode> gltf_node = state_gltf_nodes[node_index];
188
ERR_CONTINUE(gltf_node.is_null());
189
// Check if this node has a shape index and if it matches the one we are looking for.
190
Variant shape_index_maybe = gltf_node->get_additional_data(StringName("GLTFPhysicsColliderShapeIndex"));
191
if (shape_index_maybe.get_type() != Variant::INT) {
192
shape_index_maybe = gltf_node->get_additional_data(StringName("GLTFPhysicsTriggerShapeIndex"));
193
if (shape_index_maybe.get_type() != Variant::INT) {
194
continue;
195
}
196
}
197
const GLTFShapeIndex shape_index = shape_index_maybe;
198
if (shape_index != p_shape_index) {
199
continue;
200
}
201
NodePath node_path = gltf_node->get_scene_node_path(p_state);
202
// At this point, we have found a node with the shape index we were looking for.
203
if (_will_gltf_shape_become_subnode(p_state, gltf_node, node_index)) {
204
Vector<StringName> sname_path = node_path.get_names();
205
sname_path.append(gltf_node->get_name() + "Shape");
206
node_path = NodePath(sname_path, false);
207
}
208
return node_path;
209
}
210
return NodePath();
211
}
212
213
Ref<GLTFObjectModelProperty> GLTFDocumentExtensionPhysics::import_object_model_property(Ref<GLTFState> p_state, const PackedStringArray &p_split_json_pointer, const TypedArray<NodePath> &p_partial_paths) {
214
Ref<GLTFObjectModelProperty> ret;
215
if (p_split_json_pointer.size() != 6) {
216
// The only properties this class cares about are exactly 6 levels deep.
217
return ret;
218
}
219
ret.instantiate();
220
const String &prop_name = p_split_json_pointer[5];
221
if (p_split_json_pointer[0] == "extensions" && p_split_json_pointer[2] == "shapes") {
222
if (p_split_json_pointer[1] == "OMI_physics_shape" || p_split_json_pointer[1] == "KHR_collision_shapes") {
223
const GLTFNodeIndex shape_index = p_split_json_pointer[3].to_int();
224
NodePath node_path = _get_scene_node_path_for_shape_index(p_state, shape_index);
225
if (node_path.is_empty()) {
226
return ret;
227
}
228
String godot_prop_name = prop_name;
229
if (prop_name == "size") {
230
ret->set_types(Variant::VECTOR3, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT3);
231
} else if (prop_name == "height" || prop_name == "radius") {
232
ret->set_types(Variant::FLOAT, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT);
233
} else if (prop_name == "radiusBottom" || prop_name == "radiusTop") {
234
godot_prop_name = "radius";
235
ret->set_types(Variant::FLOAT, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT);
236
} else {
237
// Not something we handle, return without appending a NodePath.
238
return ret;
239
}
240
// Example: `A/B/C/CollisionShape3D:shape:radius`.
241
Vector<StringName> subnames;
242
subnames.append("shape");
243
subnames.append(godot_prop_name);
244
node_path = NodePath(node_path.get_names(), subnames, false);
245
ret->append_node_path(node_path);
246
}
247
} else if (p_split_json_pointer[0] == "nodes" && p_split_json_pointer[2] == "extensions" && p_split_json_pointer[4] == "motion") {
248
if (p_split_json_pointer[3] == "OMI_physics_body" || p_split_json_pointer[3] == "KHR_physics_rigid_bodies") {
249
const GLTFNodeIndex node_index = p_split_json_pointer[1].to_int();
250
const TypedArray<GLTFNode> all_gltf_nodes = p_state->get_nodes();
251
ERR_FAIL_INDEX_V_MSG(node_index, all_gltf_nodes.size(), ret, "GLTF Physics: The node index " + itos(node_index) + " is not in the state nodes (size: " + itos(all_gltf_nodes.size()) + ").");
252
const Ref<GLTFNode> gltf_node = all_gltf_nodes[node_index];
253
NodePath node_path;
254
if (p_partial_paths.is_empty()) {
255
node_path = gltf_node->get_scene_node_path(p_state);
256
} else {
257
// The path is already computed for us, just grab it.
258
node_path = p_partial_paths[0];
259
}
260
if (prop_name == "mass") {
261
ret->append_path_to_property(node_path, "mass");
262
ret->set_types(Variant::FLOAT, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT);
263
} else if (prop_name == "linearVelocity") {
264
ret->append_path_to_property(node_path, "linear_velocity");
265
ret->set_types(Variant::VECTOR3, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT3);
266
} else if (prop_name == "angularVelocity") {
267
ret->append_path_to_property(node_path, "angular_velocity");
268
ret->set_types(Variant::VECTOR3, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT3);
269
} else if (prop_name == "centerOfMass") {
270
ret->append_path_to_property(node_path, "center_of_mass");
271
ret->set_types(Variant::VECTOR3, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT3);
272
} else if (prop_name == "inertiaDiagonal") {
273
ret->append_path_to_property(node_path, "inertia");
274
ret->set_types(Variant::VECTOR3, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT3);
275
} else if (prop_name == "inertiaOrientation") {
276
WARN_PRINT("GLTF Physics: The 'inertiaOrientation' property is not supported by Godot.");
277
} else {
278
// Not something we handle, return without appending a NodePath.
279
return ret;
280
}
281
}
282
}
283
return ret;
284
}
285
286
void _setup_shape_mesh_resource_from_index_if_needed(Ref<GLTFState> p_state, Ref<GLTFPhysicsShape> p_gltf_shape) {
287
GLTFMeshIndex shape_mesh_index = p_gltf_shape->get_mesh_index();
288
if (shape_mesh_index == -1) {
289
return; // No mesh for this shape.
290
}
291
Ref<ImporterMesh> importer_mesh = p_gltf_shape->get_importer_mesh();
292
if (importer_mesh.is_valid()) {
293
return; // The mesh resource is already set up.
294
}
295
TypedArray<GLTFMesh> state_meshes = p_state->get_meshes();
296
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()) + ").");
297
Ref<GLTFMesh> gltf_mesh = state_meshes[shape_mesh_index];
298
ERR_FAIL_COND(gltf_mesh.is_null());
299
importer_mesh = gltf_mesh->get_mesh();
300
ERR_FAIL_COND(importer_mesh.is_null());
301
p_gltf_shape->set_importer_mesh(importer_mesh);
302
}
303
304
#ifndef DISABLE_DEPRECATED
305
CollisionObject3D *_generate_shape_with_body(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Ref<GLTFPhysicsShape> p_physics_shape, Ref<GLTFPhysicsBody> p_physics_body) {
306
print_verbose("glTF: Creating shape with body for: " + p_gltf_node->get_name());
307
bool is_trigger = p_physics_shape->get_is_trigger();
308
// This method is used for the case where we must generate a parent body.
309
// This is can happen for multiple reasons. One possibility is that this
310
// glTF file is using OMI_collider but not OMI_physics_body, or at least
311
// this particular node is not using it. Another possibility is that the
312
// physics body information is set up on the same glTF node, not a parent.
313
CollisionObject3D *body;
314
if (p_physics_body.is_valid()) {
315
// This code is run when the physics body is on the same glTF node.
316
body = p_physics_body->to_node();
317
if (is_trigger && (p_physics_body->get_body_type() != "trigger")) {
318
// Edge case: If the body's trigger and the collider's trigger
319
// are in disagreement, we need to create another new body.
320
CollisionObject3D *child = _generate_shape_with_body(p_state, p_gltf_node, p_physics_shape, nullptr);
321
child->set_name(p_gltf_node->get_name() + (is_trigger ? String("Trigger") : String("Solid")));
322
body->add_child(child);
323
return body;
324
}
325
} else if (is_trigger) {
326
body = memnew(Area3D);
327
} else {
328
body = memnew(StaticBody3D);
329
}
330
CollisionShape3D *shape = p_physics_shape->to_node();
331
shape->set_name(p_gltf_node->get_name() + "Shape");
332
body->add_child(shape);
333
return body;
334
}
335
#endif // DISABLE_DEPRECATED
336
337
CollisionObject3D *_get_ancestor_collision_object(Node *p_scene_parent) {
338
// Note: Despite the name of the method, at the moment this only checks
339
// the direct parent. Only check more later if Godot adds support for it.
340
if (p_scene_parent) {
341
CollisionObject3D *co = Object::cast_to<CollisionObject3D>(p_scene_parent);
342
if (likely(co)) {
343
return co;
344
}
345
}
346
return nullptr;
347
}
348
349
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) {
350
// If we need to generate a body node, do so.
351
CollisionObject3D *body_node = nullptr;
352
if (p_is_trigger || p_physics_shape->get_is_trigger()) {
353
// If the shape wants to be a trigger but it doesn't
354
// have an Area3D parent, we need to make one.
355
if (!Object::cast_to<Area3D>(p_col_object)) {
356
body_node = memnew(Area3D);
357
}
358
} else {
359
if (!Object::cast_to<PhysicsBody3D>(p_col_object)) {
360
body_node = memnew(StaticBody3D);
361
}
362
}
363
// Generate the shape node.
364
_setup_shape_mesh_resource_from_index_if_needed(p_state, p_physics_shape);
365
CollisionShape3D *shape_node = p_physics_shape->to_node(true);
366
if (body_node) {
367
shape_node->set_name(p_gltf_node->get_name() + "Shape");
368
body_node->add_child(shape_node);
369
return body_node;
370
}
371
return shape_node;
372
}
373
374
// Either add the child to the parent, or return the child if there is no parent.
375
Node3D *_add_physics_node_to_given_node(Node3D *p_current_node, Node3D *p_child, Ref<GLTFNode> p_gltf_node) {
376
if (!p_current_node) {
377
return p_child;
378
}
379
String suffix;
380
if (Object::cast_to<CollisionShape3D>(p_child)) {
381
suffix = "Shape";
382
} else if (Object::cast_to<Area3D>(p_child)) {
383
suffix = "Trigger";
384
} else {
385
suffix = "Collider";
386
}
387
p_child->set_name(p_gltf_node->get_name() + suffix);
388
p_current_node->add_child(p_child);
389
return p_current_node;
390
}
391
392
Array _get_ancestor_compound_trigger_nodes(Ref<GLTFState> p_state, TypedArray<GLTFNode> p_state_nodes, CollisionObject3D *p_ancestor_col_obj) {
393
GLTFNodeIndex ancestor_index = p_state->get_node_index(p_ancestor_col_obj);
394
ERR_FAIL_INDEX_V(ancestor_index, p_state_nodes.size(), Array());
395
Ref<GLTFNode> ancestor_gltf_node = p_state_nodes[ancestor_index];
396
Variant compound_trigger_nodes = ancestor_gltf_node->get_additional_data(StringName("GLTFPhysicsCompoundTriggerNodes"));
397
if (compound_trigger_nodes.is_array()) {
398
return compound_trigger_nodes;
399
}
400
Array ret;
401
ancestor_gltf_node->set_additional_data(StringName("GLTFPhysicsCompoundTriggerNodes"), ret);
402
return ret;
403
}
404
405
Node3D *GLTFDocumentExtensionPhysics::generate_scene_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Node *p_scene_parent) {
406
Ref<GLTFPhysicsBody> gltf_physics_body = p_gltf_node->get_additional_data(StringName("GLTFPhysicsBody"));
407
#ifndef DISABLE_DEPRECATED
408
// This deprecated code handles OMI_collider (which we internally name "GLTFPhysicsShape").
409
Ref<GLTFPhysicsShape> gltf_physics_shape = p_gltf_node->get_additional_data(StringName("GLTFPhysicsShape"));
410
if (gltf_physics_shape.is_valid()) {
411
_setup_shape_mesh_resource_from_index_if_needed(p_state, gltf_physics_shape);
412
// If this glTF node specifies both a shape and a body, generate both.
413
if (gltf_physics_body.is_valid()) {
414
return _generate_shape_with_body(p_state, p_gltf_node, gltf_physics_shape, gltf_physics_body);
415
}
416
CollisionObject3D *ancestor_col_obj = _get_ancestor_collision_object(p_scene_parent);
417
if (gltf_physics_shape->get_is_trigger()) {
418
// If the shape wants to be a trigger and it already has a
419
// trigger parent, we only need to make the shape node.
420
if (Object::cast_to<Area3D>(ancestor_col_obj)) {
421
return gltf_physics_shape->to_node(true);
422
}
423
} else if (ancestor_col_obj != nullptr) {
424
// If the shape has a valid parent, only make the shape node.
425
return gltf_physics_shape->to_node(true);
426
}
427
// Otherwise, we need to create a new body.
428
return _generate_shape_with_body(p_state, p_gltf_node, gltf_physics_shape, nullptr);
429
}
430
#endif // DISABLE_DEPRECATED
431
Node3D *ret = nullptr;
432
CollisionObject3D *ancestor_col_obj = nullptr;
433
Ref<GLTFPhysicsShape> gltf_physics_collider_shape = p_gltf_node->get_additional_data(StringName("GLTFPhysicsColliderShape"));
434
Ref<GLTFPhysicsShape> gltf_physics_trigger_shape = p_gltf_node->get_additional_data(StringName("GLTFPhysicsTriggerShape"));
435
if (gltf_physics_body.is_valid()) {
436
ancestor_col_obj = gltf_physics_body->to_node();
437
ret = ancestor_col_obj;
438
} else {
439
ancestor_col_obj = _get_ancestor_collision_object(p_scene_parent);
440
if (Object::cast_to<Area3D>(ancestor_col_obj) && gltf_physics_trigger_shape.is_valid()) {
441
// At this point, we found an ancestor Area3D node. But do we want to use it for this trigger shape?
442
TypedArray<GLTFNode> state_nodes = p_state->get_nodes();
443
GLTFNodeIndex self_index = state_nodes.find(p_gltf_node);
444
Array compound_trigger_nodes = _get_ancestor_compound_trigger_nodes(p_state, state_nodes, ancestor_col_obj);
445
// Check if the ancestor specifies compound trigger nodes, and if this node is in there.
446
// Remember that JSON does not have integers, only "number", aka double-precision floats.
447
if (compound_trigger_nodes.size() > 0 && !compound_trigger_nodes.has(double(self_index))) {
448
// If the compound trigger we found is not the intended user of
449
// this shape node, then we need to create a new Area3D node.
450
ancestor_col_obj = memnew(Area3D);
451
ret = ancestor_col_obj;
452
}
453
} else if (!Object::cast_to<PhysicsBody3D>(ancestor_col_obj)) {
454
if (p_gltf_node->get_additional_data(StringName("GLTFPhysicsCompoundCollider"))) {
455
// If the glTF file wants this node to group solid shapes together,
456
// and there is no parent body, we need to create a static body.
457
ancestor_col_obj = memnew(StaticBody3D);
458
ret = ancestor_col_obj;
459
}
460
}
461
}
462
// Add the shapes to the tree. When an ancestor body is present, use it.
463
// If an explicit body was specified, it has already been generated and
464
// set above. If there is no ancestor body, we will either generate an
465
// Area3D or StaticBody3D implicitly, so prefer an Area3D as the base
466
// node for best compatibility with signal connections to this node.
467
bool is_ancestor_col_obj_solid = Object::cast_to<PhysicsBody3D>(ancestor_col_obj);
468
if (is_ancestor_col_obj_solid && gltf_physics_collider_shape.is_valid()) {
469
Node3D *child = _generate_shape_node_and_body_if_needed(p_state, p_gltf_node, gltf_physics_collider_shape, ancestor_col_obj, false);
470
ret = _add_physics_node_to_given_node(ret, child, p_gltf_node);
471
}
472
if (gltf_physics_trigger_shape.is_valid()) {
473
Node3D *child = _generate_shape_node_and_body_if_needed(p_state, p_gltf_node, gltf_physics_trigger_shape, ancestor_col_obj, true);
474
ret = _add_physics_node_to_given_node(ret, child, p_gltf_node);
475
}
476
if (!is_ancestor_col_obj_solid && gltf_physics_collider_shape.is_valid()) {
477
Node3D *child = _generate_shape_node_and_body_if_needed(p_state, p_gltf_node, gltf_physics_collider_shape, ancestor_col_obj, false);
478
ret = _add_physics_node_to_given_node(ret, child, p_gltf_node);
479
}
480
return ret;
481
}
482
483
// Export process.
484
bool _are_all_faces_equal(const Vector<Face3> &p_a, const Vector<Face3> &p_b) {
485
if (p_a.size() != p_b.size()) {
486
return false;
487
}
488
for (int i = 0; i < p_a.size(); i++) {
489
const Vector3 *a_vertices = p_a[i].vertex;
490
const Vector3 *b_vertices = p_b[i].vertex;
491
for (int j = 0; j < 3; j++) {
492
if (!a_vertices[j].is_equal_approx(b_vertices[j])) {
493
return false;
494
}
495
}
496
}
497
return true;
498
}
499
500
GLTFMeshIndex _get_or_insert_mesh_in_state(Ref<GLTFState> p_state, Ref<ImporterMesh> p_mesh) {
501
ERR_FAIL_COND_V(p_mesh.is_null(), -1);
502
TypedArray<GLTFMesh> state_meshes = p_state->get_meshes();
503
Vector<Face3> mesh_faces = p_mesh->get_faces();
504
// De-duplication: If the state already has the mesh we need, use that one.
505
for (GLTFMeshIndex i = 0; i < state_meshes.size(); i++) {
506
Ref<GLTFMesh> state_gltf_mesh = state_meshes[i];
507
ERR_CONTINUE(state_gltf_mesh.is_null());
508
Ref<ImporterMesh> state_importer_mesh = state_gltf_mesh->get_mesh();
509
ERR_CONTINUE(state_importer_mesh.is_null());
510
if (state_importer_mesh == p_mesh) {
511
return i;
512
}
513
if (_are_all_faces_equal(state_importer_mesh->get_faces(), mesh_faces)) {
514
return i;
515
}
516
}
517
// After the loop, we have checked that the mesh is not equal to any of the
518
// meshes in the state. So we insert a new mesh into the state mesh array.
519
Ref<GLTFMesh> gltf_mesh;
520
gltf_mesh.instantiate();
521
gltf_mesh->set_mesh(p_mesh);
522
GLTFMeshIndex mesh_index = state_meshes.size();
523
state_meshes.push_back(gltf_mesh);
524
p_state->set_meshes(state_meshes);
525
return mesh_index;
526
}
527
528
void GLTFDocumentExtensionPhysics::convert_scene_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Node *p_scene_node) {
529
if (cast_to<CollisionShape3D>(p_scene_node)) {
530
CollisionShape3D *godot_shape = Object::cast_to<CollisionShape3D>(p_scene_node);
531
Ref<GLTFPhysicsShape> gltf_shape = GLTFPhysicsShape::from_node(godot_shape);
532
ERR_FAIL_COND_MSG(gltf_shape.is_null(), "glTF Physics: Could not convert CollisionShape3D to GLTFPhysicsShape. Does it have a valid Shape3D?");
533
{
534
Ref<ImporterMesh> importer_mesh = gltf_shape->get_importer_mesh();
535
if (importer_mesh.is_valid()) {
536
gltf_shape->set_mesh_index(_get_or_insert_mesh_in_state(p_state, importer_mesh));
537
}
538
}
539
CollisionObject3D *ancestor_col_obj = _get_ancestor_collision_object(p_scene_node->get_parent());
540
if (cast_to<Area3D>(ancestor_col_obj)) {
541
p_gltf_node->set_additional_data(StringName("GLTFPhysicsTriggerShape"), gltf_shape);
542
// Write explicit member shape nodes to the ancestor compound trigger node.
543
TypedArray<GLTFNode> state_nodes = p_state->get_nodes();
544
GLTFNodeIndex self_index = state_nodes.size(); // The current p_gltf_node will be inserted next.
545
Array compound_trigger_nodes = _get_ancestor_compound_trigger_nodes(p_state, p_state->get_nodes(), ancestor_col_obj);
546
compound_trigger_nodes.push_back(double(self_index));
547
} else {
548
p_gltf_node->set_additional_data(StringName("GLTFPhysicsColliderShape"), gltf_shape);
549
}
550
} else if (cast_to<CollisionObject3D>(p_scene_node)) {
551
CollisionObject3D *godot_body = Object::cast_to<CollisionObject3D>(p_scene_node);
552
p_gltf_node->set_additional_data(StringName("GLTFPhysicsBody"), GLTFPhysicsBody::from_node(godot_body));
553
}
554
}
555
556
Array _get_or_create_state_shapes_in_state(Ref<GLTFState> p_state) {
557
Dictionary state_json = p_state->get_json();
558
Dictionary state_extensions;
559
if (state_json.has("extensions")) {
560
state_extensions = state_json["extensions"];
561
} else {
562
state_json["extensions"] = state_extensions;
563
}
564
Dictionary omi_physics_shape_ext;
565
if (state_extensions.has("OMI_physics_shape")) {
566
omi_physics_shape_ext = state_extensions["OMI_physics_shape"];
567
} else {
568
state_extensions["OMI_physics_shape"] = omi_physics_shape_ext;
569
p_state->add_used_extension("OMI_physics_shape");
570
}
571
Array state_shapes;
572
if (omi_physics_shape_ext.has("shapes")) {
573
state_shapes = omi_physics_shape_ext["shapes"];
574
} else {
575
omi_physics_shape_ext["shapes"] = state_shapes;
576
}
577
return state_shapes;
578
}
579
580
GLTFShapeIndex _export_node_shape(Ref<GLTFState> p_state, Ref<GLTFPhysicsShape> p_physics_shape) {
581
Array state_shapes = _get_or_create_state_shapes_in_state(p_state);
582
GLTFShapeIndex size = state_shapes.size();
583
Dictionary shape_property;
584
Dictionary shape_dict = p_physics_shape->to_dictionary();
585
for (GLTFShapeIndex i = 0; i < size; i++) {
586
Dictionary other = state_shapes[i];
587
if (other == shape_dict) {
588
// De-duplication: If we already have an identical shape,
589
// set the shape index to the existing one and return.
590
return i;
591
}
592
}
593
// If we don't have an identical shape, add it to the array.
594
state_shapes.push_back(shape_dict);
595
return size;
596
}
597
598
Error GLTFDocumentExtensionPhysics::export_preserialize(Ref<GLTFState> p_state) {
599
// Note: Need to do _export_node_shape before exporting animations, so export_node is too late.
600
TypedArray<GLTFNode> state_gltf_nodes = p_state->get_nodes();
601
for (Ref<GLTFNode> gltf_node : state_gltf_nodes) {
602
Ref<GLTFPhysicsShape> collider_shape = gltf_node->get_additional_data(StringName("GLTFPhysicsColliderShape"));
603
if (collider_shape.is_valid()) {
604
GLTFShapeIndex collider_shape_index = _export_node_shape(p_state, collider_shape);
605
gltf_node->set_additional_data(StringName("GLTFPhysicsColliderShapeIndex"), collider_shape_index);
606
}
607
Ref<GLTFPhysicsShape> trigger_shape = gltf_node->get_additional_data(StringName("GLTFPhysicsTriggerShape"));
608
if (trigger_shape.is_valid()) {
609
GLTFShapeIndex trigger_shape_index = _export_node_shape(p_state, trigger_shape);
610
gltf_node->set_additional_data(StringName("GLTFPhysicsTriggerShapeIndex"), trigger_shape_index);
611
}
612
}
613
return OK;
614
}
615
616
Ref<GLTFObjectModelProperty> GLTFDocumentExtensionPhysics::export_object_model_property(Ref<GLTFState> p_state, const NodePath &p_node_path, const Node *p_godot_node, GLTFNodeIndex p_gltf_node_index, const Object *p_target_object, int p_target_depth) {
617
Ref<GLTFObjectModelProperty> ret;
618
const Vector<StringName> &path_subnames = p_node_path.get_subnames();
619
if (path_subnames.is_empty()) {
620
return ret;
621
}
622
ret.instantiate();
623
const StringName &node_prop = path_subnames[0];
624
if (Object::cast_to<RigidBody3D>(p_target_object)) {
625
if (path_subnames.size() != 1) {
626
return ret;
627
}
628
// Example: `/nodes/0/extensions/OMI_physics_body/motion/mass`
629
PackedStringArray split_json_pointer;
630
split_json_pointer.append("nodes");
631
split_json_pointer.append(itos(p_gltf_node_index));
632
split_json_pointer.append("extensions");
633
split_json_pointer.append("OMI_physics_body");
634
split_json_pointer.append("motion");
635
if (node_prop == StringName("mass")) {
636
split_json_pointer.append("mass");
637
ret->set_types(Variant::FLOAT, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT);
638
} else if (node_prop == StringName("linear_velocity")) {
639
split_json_pointer.append("linearVelocity");
640
ret->set_types(Variant::VECTOR3, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT3);
641
} else if (node_prop == StringName("angular_velocity")) {
642
split_json_pointer.append("angularVelocity");
643
ret->set_types(Variant::VECTOR3, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT3);
644
} else if (node_prop == StringName("center_of_mass")) {
645
split_json_pointer.append("centerOfMass");
646
ret->set_types(Variant::VECTOR3, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT3);
647
} else if (node_prop == StringName("inertia")) {
648
split_json_pointer.append("inertiaDiagonal");
649
ret->set_types(Variant::VECTOR3, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT3);
650
} else {
651
// Not something we handle, return without setting the JSON pointer.
652
return ret;
653
}
654
ret->set_json_pointers({ split_json_pointer });
655
} else if (Object::cast_to<CollisionShape3D>(p_godot_node)) {
656
if (path_subnames.size() != 2) {
657
return ret;
658
}
659
// Example: `/extensions/OMI_physics_shape/shapes/0/box/size`
660
PackedStringArray split_json_pointer;
661
split_json_pointer.append("extensions");
662
split_json_pointer.append("OMI_physics_shape");
663
split_json_pointer.append("shapes");
664
TypedArray<GLTFNode> state_gltf_nodes = p_state->get_nodes();
665
ERR_FAIL_INDEX_V(p_gltf_node_index, state_gltf_nodes.size(), ret);
666
Ref<GLTFNode> gltf_node = state_gltf_nodes[p_gltf_node_index];
667
Variant shape_index_maybe = gltf_node->get_additional_data(StringName("GLTFPhysicsColliderShapeIndex"));
668
String shape_type;
669
if (shape_index_maybe.get_type() == Variant::INT) {
670
Ref<GLTFPhysicsShape> collider_shape = gltf_node->get_additional_data(StringName("GLTFPhysicsColliderShape"));
671
shape_type = collider_shape->get_shape_type();
672
} else {
673
shape_index_maybe = gltf_node->get_additional_data(StringName("GLTFPhysicsTriggerShapeIndex"));
674
if (shape_index_maybe.get_type() == Variant::INT) {
675
Ref<GLTFPhysicsShape> trigger_shape = gltf_node->get_additional_data(StringName("GLTFPhysicsTriggerShape"));
676
shape_type = trigger_shape->get_shape_type();
677
}
678
}
679
ERR_FAIL_COND_V(shape_index_maybe.get_type() != Variant::INT, ret);
680
GLTFShapeIndex shape_index = shape_index_maybe;
681
split_json_pointer.append(itos(shape_index));
682
split_json_pointer.append(shape_type);
683
const StringName &shape_prop = path_subnames[1];
684
if (shape_prop == StringName("size")) {
685
split_json_pointer.append("size");
686
ret->set_types(Variant::VECTOR3, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT3);
687
} else if (shape_prop == StringName("radius")) {
688
split_json_pointer.append("radius");
689
ret->set_types(Variant::FLOAT, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT);
690
} else if (shape_prop == StringName("height")) {
691
split_json_pointer.append("height");
692
ret->set_types(Variant::FLOAT, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT);
693
} else {
694
// Not something we handle, return without setting the JSON pointer.
695
return ret;
696
}
697
ret->set_json_pointers({ split_json_pointer });
698
}
699
return ret;
700
}
701
702
Error GLTFDocumentExtensionPhysics::export_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &r_node_json, Node *p_node) {
703
Dictionary physics_body_ext;
704
Ref<GLTFPhysicsBody> physics_body = p_gltf_node->get_additional_data(StringName("GLTFPhysicsBody"));
705
if (physics_body.is_valid()) {
706
physics_body_ext = physics_body->to_dictionary();
707
Variant compound_trigger_nodes = p_gltf_node->get_additional_data(StringName("GLTFPhysicsCompoundTriggerNodes"));
708
if (compound_trigger_nodes.is_array()) {
709
Dictionary trigger_property = physics_body_ext.get_or_add("trigger", {});
710
trigger_property["nodes"] = compound_trigger_nodes;
711
}
712
}
713
Variant collider_shape_index = p_gltf_node->get_additional_data(StringName("GLTFPhysicsColliderShapeIndex"));
714
if (collider_shape_index.get_type() == Variant::INT) {
715
Dictionary collider_dict;
716
collider_dict["shape"] = collider_shape_index;
717
physics_body_ext["collider"] = collider_dict;
718
}
719
Variant trigger_shape_index = p_gltf_node->get_additional_data(StringName("GLTFPhysicsTriggerShapeIndex"));
720
if (trigger_shape_index.get_type() == Variant::INT) {
721
Dictionary trigger_dict = physics_body_ext.get_or_add("trigger", {});
722
trigger_dict["shape"] = trigger_shape_index;
723
}
724
if (!physics_body_ext.is_empty()) {
725
Dictionary node_extensions = r_node_json["extensions"];
726
node_extensions["OMI_physics_body"] = physics_body_ext;
727
p_state->add_used_extension("OMI_physics_body");
728
}
729
return OK;
730
}
731
732