Debian Codegraph @

scene/3d/physics/soft_body_3d.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* soft_body_3d.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 "soft_body_3d.h"
32 #include "soft_body_3d.compat.inc"
33
34 #include "scene/3d/physics/physics_body_3d.h"
35
36 SoftBodyRenderingServerHandler::SoftBodyRenderingServerHandler() {}
37
38 void SoftBodyRenderingServerHandler::prepare(RID p_mesh, int p_surface) {
39 clear();
40
41 ERR_FAIL_COND(!p_mesh.is_valid());
42
43 mesh = p_mesh;
44 surface = p_surface;
45
46 RS::SurfaceData surface_data = RS::get_singleton()->mesh_get_surface(mesh, surface);
47
48 uint32_t surface_offsets[RS::ARRAY_MAX];
49 uint32_t vertex_stride;
50 uint32_t normal_tangent_stride;
51 uint32_t attrib_stride;
52 uint32_t skin_stride;
53 RS::get_singleton()->mesh_surface_make_offsets_from_format(surface_data.format, surface_data.vertex_count, surface_data.index_count, surface_offsets, vertex_stride, normal_tangent_stride, attrib_stride, skin_stride);
54
55 buffer = surface_data.vertex_data;
56 stride = vertex_stride;
57 normal_stride = normal_tangent_stride;
58 offset_vertices = surface_offsets[RS::ARRAY_VERTEX];
59 offset_normal = surface_offsets[RS::ARRAY_NORMAL];
60 }
61
62 void SoftBodyRenderingServerHandler::clear() {
63 buffer.resize(0);
64 stride = 0;
65 normal_stride = 0;
66 offset_vertices = 0;
67 offset_normal = 0;
68
69 surface = 0;
70 mesh = RID();
71 }
72
73 void SoftBodyRenderingServerHandler::open() {
74 write_buffer = buffer.ptrw();
75 }
76
77 void SoftBodyRenderingServerHandler::close() {
78 write_buffer = nullptr;
79 }
80
81 void SoftBodyRenderingServerHandler::commit_changes() {
82 RS::get_singleton()->mesh_surface_update_vertex_region(mesh, surface, 0, buffer);
83 }
84
85 void SoftBodyRenderingServerHandler::set_vertex(int p_vertex_id, const Vector3 &p_vertex) {
86 float *vertex_buffer = reinterpret_cast<float *>(write_buffer + p_vertex_id * stride + offset_vertices);
87 *vertex_buffer++ = (float)p_vertex.x;
88 *vertex_buffer++ = (float)p_vertex.y;
89 *vertex_buffer++ = (float)p_vertex.z;
90 }
91
92 void SoftBodyRenderingServerHandler::set_normal(int p_vertex_id, const Vector3 &p_normal) {
93 Vector2 res = p_normal.octahedron_encode();
94 uint32_t value = 0;
95 value |= (uint16_t)CLAMP(res.x * 65535, 0, 65535);
96 value |= (uint16_t)CLAMP(res.y * 65535, 0, 65535) << 16;
97 memcpy(&write_buffer[p_vertex_id * normal_stride + offset_normal], &value, sizeof(uint32_t));
98 }
99
100 void SoftBodyRenderingServerHandler::set_aabb(const AABB &p_aabb) {
101 RS::get_singleton()->mesh_set_custom_aabb(mesh, p_aabb);
102 }
103
104 SoftBody3D::PinnedPoint::PinnedPoint() {
105 }
106
107 SoftBody3D::PinnedPoint::PinnedPoint(const PinnedPoint &obj_tocopy) {
108 point_index = obj_tocopy.point_index;
109 spatial_attachment_path = obj_tocopy.spatial_attachment_path;
110 spatial_attachment = obj_tocopy.spatial_attachment;
111 offset = obj_tocopy.offset;
112 }
113
114 void SoftBody3D::PinnedPoint::operator=(const PinnedPoint &obj) {
115 point_index = obj.point_index;
116 spatial_attachment_path = obj.spatial_attachment_path;
117 spatial_attachment = obj.spatial_attachment;
118 offset = obj.offset;
119 }
120
121 void SoftBody3D::_update_pickable() {
122 if (!is_inside_tree()) {
123 return;
124 }
125 bool pickable = ray_pickable && is_visible_in_tree();
126 PhysicsServer3D::get_singleton()->soft_body_set_ray_pickable(physics_rid, pickable);
127 }
128
129 bool SoftBody3D::_set(const StringName &p_name, const Variant &p_value) {
130 String name = p_name;
131 String which = name.get_slicec('/', 0);
132
133 if ("pinned_points" == which) {
134 return _set_property_pinned_points_indices(p_value);
135
136 } else if ("attachments" == which) {
137 int idx = name.get_slicec('/', 1).to_int();
138 String what = name.get_slicec('/', 2);
139
140 return _set_property_pinned_points_attachment(idx, what, p_value);
141 }
142
143 return false;
144 }
145
146 bool SoftBody3D::_get(const StringName &p_name, Variant &r_ret) const {
147 String name = p_name;
148 String which = name.get_slicec('/', 0);
149
150 if ("pinned_points" == which) {
151 Array arr_ret;
152 const int pinned_points_indices_size = pinned_points.size();
153 const PinnedPoint *r = pinned_points.ptr();
154 arr_ret.resize(pinned_points_indices_size);
155
156 for (int i = 0; i < pinned_points_indices_size; ++i) {
157 arr_ret[i] = r[i].point_index;
158 }
159
160 r_ret = arr_ret;
161 return true;
162
163 } else if ("attachments" == which) {
164 int idx = name.get_slicec('/', 1).to_int();
165 String what = name.get_slicec('/', 2);
166
167 return _get_property_pinned_points(idx, what, r_ret);
168 }
169
170 return false;
171 }
172
173 void SoftBody3D::_get_property_list(List<PropertyInfo> *p_list) const {
174 const int pinned_points_indices_size = pinned_points.size();
175
176 p_list->push_back(PropertyInfo(Variant::PACKED_INT32_ARRAY, PNAME("pinned_points")));
177
178 for (int i = 0; i < pinned_points_indices_size; ++i) {
179 const String prefix = vformat("%s/%d/", PNAME("attachments"), i);
180 p_list->push_back(PropertyInfo(Variant::INT, prefix + PNAME("point_index")));
181 p_list->push_back(PropertyInfo(Variant::NODE_PATH, prefix + PNAME("spatial_attachment_path")));
182 p_list->push_back(PropertyInfo(Variant::VECTOR3, prefix + PNAME("offset")));
183 }
184 }
185
186 bool SoftBody3D::_set_property_pinned_points_indices(const Array &p_indices) {
187 const int p_indices_size = p_indices.size();
188
189 { // Remove the pined points on physics server that will be removed by resize
190 const PinnedPoint *r = pinned_points.ptr();
191 if (p_indices_size < pinned_points.size()) {
192 for (int i = pinned_points.size() - 1; i >= p_indices_size; --i) {
193 pin_point(r[i].point_index, false);
194 }
195 }
196 }
197
198 pinned_points.resize(p_indices_size);
199
200 PinnedPoint *w = pinned_points.ptrw();
201 int point_index;
202 for (int i = 0; i < p_indices_size; ++i) {
203 point_index = p_indices.get(i);
204 if (w[i].point_index != point_index || pinned_points.size() < p_indices_size) {
205 bool insert = false;
206 if (w[i].point_index != -1 && p_indices.find(w[i].point_index) == -1) {
207 pin_point(w[i].point_index, false);
208 insert = true;
209 }
210 w[i].point_index = point_index;
211 if (insert) {
212 pin_point(w[i].point_index, true, NodePath(), i);
213 } else {
214 pin_point(w[i].point_index, true);
215 }
216 }
217 }
218 return true;
219 }
220
221 bool SoftBody3D::_set_property_pinned_points_attachment(int p_item, const String &p_what, const Variant &p_value) {
222 if (pinned_points.size() <= p_item) {
223 return false;
224 }
225
226 if ("spatial_attachment_path" == p_what) {
227 PinnedPoint *w = pinned_points.ptrw();
228
229 if (is_inside_tree()) {
230 callable_mp(this, &SoftBody3D::_pin_point_deferred).call_deferred(Variant(w[p_item].point_index), true, p_value);
231 } else {
232 pin_point(w[p_item].point_index, true, p_value);
233 _make_cache_dirty();
234 }
235 } else if ("offset" == p_what) {
236 PinnedPoint *w = pinned_points.ptrw();
237 w[p_item].offset = p_value;
238 } else {
239 return false;
240 }
241
242 return true;
243 }
244
245 bool SoftBody3D::_get_property_pinned_points(int p_item, const String &p_what, Variant &r_ret) const {
246 if (pinned_points.size() <= p_item) {
247 return false;
248 }
249 const PinnedPoint *r = pinned_points.ptr();
250
251 if ("point_index" == p_what) {
252 r_ret = r[p_item].point_index;
253 } else if ("spatial_attachment_path" == p_what) {
254 r_ret = r[p_item].spatial_attachment_path;
255 } else if ("offset" == p_what) {
256 r_ret = r[p_item].offset;
257 } else {
258 return false;
259 }
260
261 return true;
262 }
263
264 void SoftBody3D::_notification(int p_what) {
265 switch (p_what) {
266 case NOTIFICATION_ENTER_WORLD: {
267 if (Engine::get_singleton()->is_editor_hint()) {
268 // I have no idea what this is supposed to do, it's really weird
269 // leaving for upcoming PK work on physics
270 //add_change_receptor(this);
271 }
272
273 RID space = get_world_3d()->get_space();
274 PhysicsServer3D::get_singleton()->soft_body_set_space(physics_rid, space);
275 _prepare_physics_server();
276 } break;
277
278 case NOTIFICATION_READY: {
279 if (!parent_collision_ignore.is_empty()) {
280 add_collision_exception_with(get_node(parent_collision_ignore));
281 }
282 } break;
283
284 case NOTIFICATION_TRANSFORM_CHANGED: {
285 if (Engine::get_singleton()->is_editor_hint()) {
286 _reset_points_offsets();
287 return;
288 }
289
290 PhysicsServer3D::get_singleton()->soft_body_set_transform(physics_rid, get_global_transform());
291
292 set_notify_transform(false);
293 // Required to be top level with Transform at center of world in order to modify RenderingServer only to support custom Transform
294 set_as_top_level(true);
295 set_transform(Transform3D());
296 set_notify_transform(true);
297 } break;
298
299 case NOTIFICATION_VISIBILITY_CHANGED: {
300 _update_pickable();
301 } break;
302
303 case NOTIFICATION_EXIT_WORLD: {
304 PhysicsServer3D::get_singleton()->soft_body_set_space(physics_rid, RID());
305 } break;
306
307 case NOTIFICATION_DISABLED: {
308 if (is_inside_tree() && (disable_mode == DISABLE_MODE_REMOVE)) {
309 _prepare_physics_server();
310 }
311 } break;
312
313 case NOTIFICATION_ENABLED: {
314 if (is_inside_tree() && (disable_mode == DISABLE_MODE_REMOVE)) {
315 _prepare_physics_server();
316 }
317 } break;
318 }
319 }
320
321 void SoftBody3D::_bind_methods() {
322 ClassDB::bind_method(D_METHOD("get_physics_rid"), &SoftBody3D::get_physics_rid);
323
324 ClassDB::bind_method(D_METHOD("set_collision_mask", "collision_mask"), &SoftBody3D::set_collision_mask);
325 ClassDB::bind_method(D_METHOD("get_collision_mask"), &SoftBody3D::get_collision_mask);
326
327 ClassDB::bind_method(D_METHOD("set_collision_layer", "collision_layer"), &SoftBody3D::set_collision_layer);
328 ClassDB::bind_method(D_METHOD("get_collision_layer"), &SoftBody3D::get_collision_layer);
329
330 ClassDB::bind_method(D_METHOD("set_collision_mask_value", "layer_number", "value"), &SoftBody3D::set_collision_mask_value);
331 ClassDB::bind_method(D_METHOD("get_collision_mask_value", "layer_number"), &SoftBody3D::get_collision_mask_value);
332
333 ClassDB::bind_method(D_METHOD("set_collision_layer_value", "layer_number", "value"), &SoftBody3D::set_collision_layer_value);
334 ClassDB::bind_method(D_METHOD("get_collision_layer_value", "layer_number"), &SoftBody3D::get_collision_layer_value);
335
336 ClassDB::bind_method(D_METHOD("set_parent_collision_ignore", "parent_collision_ignore"), &SoftBody3D::set_parent_collision_ignore);
337 ClassDB::bind_method(D_METHOD("get_parent_collision_ignore"), &SoftBody3D::get_parent_collision_ignore);
338
339 ClassDB::bind_method(D_METHOD("set_disable_mode", "mode"), &SoftBody3D::set_disable_mode);
340 ClassDB::bind_method(D_METHOD("get_disable_mode"), &SoftBody3D::get_disable_mode);
341
342 ClassDB::bind_method(D_METHOD("get_collision_exceptions"), &SoftBody3D::get_collision_exceptions);
343 ClassDB::bind_method(D_METHOD("add_collision_exception_with", "body"), &SoftBody3D::add_collision_exception_with);
344 ClassDB::bind_method(D_METHOD("remove_collision_exception_with", "body"), &SoftBody3D::remove_collision_exception_with);
345
346 ClassDB::bind_method(D_METHOD("set_simulation_precision", "simulation_precision"), &SoftBody3D::set_simulation_precision);
347 ClassDB::bind_method(D_METHOD("get_simulation_precision"), &SoftBody3D::get_simulation_precision);
348
349 ClassDB::bind_method(D_METHOD("set_total_mass", "mass"), &SoftBody3D::set_total_mass);
350 ClassDB::bind_method(D_METHOD("get_total_mass"), &SoftBody3D::get_total_mass);
351
352 ClassDB::bind_method(D_METHOD("set_linear_stiffness", "linear_stiffness"), &SoftBody3D::set_linear_stiffness);
353 ClassDB::bind_method(D_METHOD("get_linear_stiffness"), &SoftBody3D::get_linear_stiffness);
354
355 ClassDB::bind_method(D_METHOD("set_shrinking_factor", "shrinking_factor"), &SoftBody3D::set_shrinking_factor);
356 ClassDB::bind_method(D_METHOD("get_shrinking_factor"), &SoftBody3D::get_shrinking_factor);
357
358 ClassDB::bind_method(D_METHOD("set_pressure_coefficient", "pressure_coefficient"), &SoftBody3D::set_pressure_coefficient);
359 ClassDB::bind_method(D_METHOD("get_pressure_coefficient"), &SoftBody3D::get_pressure_coefficient);
360
361 ClassDB::bind_method(D_METHOD("set_damping_coefficient", "damping_coefficient"), &SoftBody3D::set_damping_coefficient);
362 ClassDB::bind_method(D_METHOD("get_damping_coefficient"), &SoftBody3D::get_damping_coefficient);
363
364 ClassDB::bind_method(D_METHOD("set_drag_coefficient", "drag_coefficient"), &SoftBody3D::set_drag_coefficient);
365 ClassDB::bind_method(D_METHOD("get_drag_coefficient"), &SoftBody3D::get_drag_coefficient);
366
367 ClassDB::bind_method(D_METHOD("get_point_transform", "point_index"), &SoftBody3D::get_point_transform);
368
369 ClassDB::bind_method(D_METHOD("apply_impulse", "point_index", "impulse"), &SoftBody3D::apply_impulse);
370 ClassDB::bind_method(D_METHOD("apply_force", "point_index", "force"), &SoftBody3D::apply_force);
371 ClassDB::bind_method(D_METHOD("apply_central_impulse", "impulse"), &SoftBody3D::apply_central_impulse);
372 ClassDB::bind_method(D_METHOD("apply_central_force", "force"), &SoftBody3D::apply_central_force);
373
374 ClassDB::bind_method(D_METHOD("set_point_pinned", "point_index", "pinned", "attachment_path", "insert_at"), &SoftBody3D::pin_point, DEFVAL(NodePath()), DEFVAL(-1));
375 ClassDB::bind_method(D_METHOD("is_point_pinned", "point_index"), &SoftBody3D::is_point_pinned);
376
377 ClassDB::bind_method(D_METHOD("set_ray_pickable", "ray_pickable"), &SoftBody3D::set_ray_pickable);
378 ClassDB::bind_method(D_METHOD("is_ray_pickable"), &SoftBody3D::is_ray_pickable);
379
380 ADD_GROUP("Collision", "collision_");
381 ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_layer", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_layer", "get_collision_layer");
382 ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
383
384 ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "parent_collision_ignore", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "CollisionObject3D"), "set_parent_collision_ignore", "get_parent_collision_ignore");
385 ADD_PROPERTY(PropertyInfo(Variant::INT, "simulation_precision", PROPERTY_HINT_RANGE, "1,100,1"), "set_simulation_precision", "get_simulation_precision");
386 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "total_mass", PROPERTY_HINT_RANGE, "0,1000,0.001,or_greater,exp,suffix:kg"), "set_total_mass", "get_total_mass");
387 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "linear_stiffness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_linear_stiffness", "get_linear_stiffness");
388 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "shrinking_factor", PROPERTY_HINT_RANGE, "-1,1,0.01,or_less,or_greater"), "set_shrinking_factor", "get_shrinking_factor");
389 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "pressure_coefficient"), "set_pressure_coefficient", "get_pressure_coefficient");
390 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "damping_coefficient", PROPERTY_HINT_RANGE, "0,1,0.01,or_greater"), "set_damping_coefficient", "get_damping_coefficient");
391 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "drag_coefficient", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_drag_coefficient", "get_drag_coefficient");
392
393 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ray_pickable"), "set_ray_pickable", "is_ray_pickable");
394
395 ADD_PROPERTY(PropertyInfo(Variant::INT, "disable_mode", PROPERTY_HINT_ENUM, "Remove,KeepActive"), "set_disable_mode", "get_disable_mode");
396
397 BIND_ENUM_CONSTANT(DISABLE_MODE_REMOVE);
398 BIND_ENUM_CONSTANT(DISABLE_MODE_KEEP_ACTIVE);
399 }
400
401 PackedStringArray SoftBody3D::get_configuration_warnings() const {
402 PackedStringArray warnings = MeshInstance3D::get_configuration_warnings();
403
404 if (mesh.is_null()) {
405 warnings.push_back(RTR("This body will be ignored until you set a mesh."));
406 }
407
408 return warnings;
409 }
410
411 void SoftBody3D::_update_physics_server() {
412 if (!simulation_started) {
413 return;
414 }
415
416 _update_cache_pin_points_datas();
417 // Submit bone attachment
418 const int pinned_points_indices_size = pinned_points.size();
419 const PinnedPoint *r = pinned_points.ptr();
420 for (int i = 0; i < pinned_points_indices_size; ++i) {
421 if (r[i].spatial_attachment) {
422 PhysicsServer3D::get_singleton()->soft_body_move_point(physics_rid, r[i].point_index, r[i].spatial_attachment->get_global_transform().xform(r[i].offset));
423 }
424 }
425 }
426
427 void SoftBody3D::_draw_soft_mesh() {
428 if (mesh.is_null()) {
429 return;
430 }
431
432 RID mesh_rid = mesh->get_rid();
433 if (owned_mesh != mesh_rid) {
434 _become_mesh_owner();
435 mesh_rid = mesh->get_rid();
436 PhysicsServer3D::get_singleton()->soft_body_set_mesh(physics_rid, mesh_rid);
437 }
438
439 if (!rendering_server_handler->is_ready(mesh_rid)) {
440 rendering_server_handler->prepare(mesh_rid, 0);
441
442 /// Necessary in order to render the mesh correctly (Soft body nodes are in global space)
443 simulation_started = true;
444 callable_mp((Node3D *)this, &Node3D::set_as_top_level).call_deferred(true);
445 callable_mp((Node3D *)this, &Node3D::set_transform).call_deferred(Transform3D());
446 }
447
448 _update_physics_server();
449
450 rendering_server_handler->open();
451 PhysicsServer3D::get_singleton()->soft_body_update_rendering_server(physics_rid, rendering_server_handler);
452 rendering_server_handler->close();
453
454 rendering_server_handler->commit_changes();
455 }
456
457 void SoftBody3D::_prepare_physics_server() {
458 #ifdef TOOLS_ENABLED
459 if (Engine::get_singleton()->is_editor_hint()) {
460 if (mesh.is_valid()) {
461 PhysicsServer3D::get_singleton()->soft_body_set_mesh(physics_rid, mesh->get_rid());
462 } else {
463 PhysicsServer3D::get_singleton()->soft_body_set_mesh(physics_rid, RID());
464 }
465
466 return;
467 }
468 #endif
469
470 if (mesh.is_valid() && (is_enabled() || (disable_mode != DISABLE_MODE_REMOVE))) {
471 RID mesh_rid = mesh->get_rid();
472 if (owned_mesh != mesh_rid) {
473 _become_mesh_owner();
474 mesh_rid = mesh->get_rid();
475 }
476 PhysicsServer3D::get_singleton()->soft_body_set_mesh(physics_rid, mesh_rid);
477 RS::get_singleton()->connect("frame_pre_draw", callable_mp(this, &SoftBody3D::_draw_soft_mesh));
478 } else {
479 PhysicsServer3D::get_singleton()->soft_body_set_mesh(physics_rid, RID());
480 if (RS::get_singleton()->is_connected("frame_pre_draw", callable_mp(this, &SoftBody3D::_draw_soft_mesh))) {
481 RS::get_singleton()->disconnect("frame_pre_draw", callable_mp(this, &SoftBody3D::_draw_soft_mesh));
482 }
483 }
484 }
485
486 void SoftBody3D::_become_mesh_owner() {
487 Vector<Ref<Material>> copy_materials;
488 copy_materials.append_array(surface_override_materials);
489
490 ERR_FAIL_COND(!mesh->get_surface_count());
491
492 // Get current mesh array and create new mesh array with necessary flag for SoftBody
493 Array surface_arrays = mesh->surface_get_arrays(0);
494 Array surface_blend_arrays = mesh->surface_get_blend_shape_arrays(0);
495 Dictionary surface_lods = mesh->surface_get_lods(0);
496 uint32_t surface_format = mesh->surface_get_format(0);
497
498 surface_format |= Mesh::ARRAY_FLAG_USE_DYNAMIC_UPDATE;
499 surface_format &= ~Mesh::ARRAY_FLAG_COMPRESS_ATTRIBUTES;
500
501 Ref<ArrayMesh> soft_mesh;
502 soft_mesh.instantiate();
503 soft_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, surface_arrays, surface_blend_arrays, surface_lods, surface_format);
504 soft_mesh->surface_set_material(0, mesh->surface_get_material(0));
505
506 set_mesh(soft_mesh);
507
508 for (int i = copy_materials.size() - 1; 0 <= i; --i) {
509 set_surface_override_material(i, copy_materials[i]);
510 }
511
512 owned_mesh = soft_mesh->get_rid();
513 }
514
515 void SoftBody3D::set_collision_mask(uint32_t p_mask) {
516 collision_mask = p_mask;
517 PhysicsServer3D::get_singleton()->soft_body_set_collision_mask(physics_rid, p_mask);
518 }
519
520 uint32_t SoftBody3D::get_collision_mask() const {
521 return collision_mask;
522 }
523
524 void SoftBody3D::set_collision_layer(uint32_t p_layer) {
525 collision_layer = p_layer;
526 PhysicsServer3D::get_singleton()->soft_body_set_collision_layer(physics_rid, p_layer);
527 }
528
529 uint32_t SoftBody3D::get_collision_layer() const {
530 return collision_layer;
531 }
532
533 void SoftBody3D::set_collision_layer_value(int p_layer_number, bool p_value) {
534 ERR_FAIL_COND_MSG(p_layer_number < 1, "Collision layer number must be between 1 and 32 inclusive.");
535 ERR_FAIL_COND_MSG(p_layer_number > 32, "Collision layer number must be between 1 and 32 inclusive.");
536 uint32_t collision_layer_new = get_collision_layer();
537 if (p_value) {
538 collision_layer_new |= 1 << (p_layer_number - 1);
539 } else {
540 collision_layer_new &= ~(1 << (p_layer_number - 1));
541 }
542 set_collision_layer(collision_layer_new);
543 }
544
545 bool SoftBody3D::get_collision_layer_value(int p_layer_number) const {
546 ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Collision layer number must be between 1 and 32 inclusive.");
547 ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Collision layer number must be between 1 and 32 inclusive.");
548 return get_collision_layer() & (1 << (p_layer_number - 1));
549 }
550
551 void SoftBody3D::set_collision_mask_value(int p_layer_number, bool p_value) {
552 ERR_FAIL_COND_MSG(p_layer_number < 1, "Collision layer number must be between 1 and 32 inclusive.");
553 ERR_FAIL_COND_MSG(p_layer_number > 32, "Collision layer number must be between 1 and 32 inclusive.");
554 uint32_t mask = get_collision_mask();
555 if (p_value) {
556 mask |= 1 << (p_layer_number - 1);
557 } else {
558 mask &= ~(1 << (p_layer_number - 1));
559 }
560 set_collision_mask(mask);
561 }
562
563 bool SoftBody3D::get_collision_mask_value(int p_layer_number) const {
564 ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Collision layer number must be between 1 and 32 inclusive.");
565 ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Collision layer number must be between 1 and 32 inclusive.");
566 return get_collision_mask() & (1 << (p_layer_number - 1));
567 }
568
569 void SoftBody3D::set_disable_mode(DisableMode p_mode) {
570 if (disable_mode == p_mode) {
571 return;
572 }
573
574 disable_mode = p_mode;
575
576 if (mesh.is_valid() && is_inside_tree() && !is_enabled()) {
577 _prepare_physics_server();
578 }
579 }
580
581 SoftBody3D::DisableMode SoftBody3D::get_disable_mode() const {
582 return disable_mode;
583 }
584
585 void SoftBody3D::set_parent_collision_ignore(const NodePath &p_parent_collision_ignore) {
586 parent_collision_ignore = p_parent_collision_ignore;
587 }
588
589 const NodePath &SoftBody3D::get_parent_collision_ignore() const {
590 return parent_collision_ignore;
591 }
592
593 void SoftBody3D::set_pinned_points_indices(Vector<SoftBody3D::PinnedPoint> p_pinned_points_indices) {
594 pinned_points = p_pinned_points_indices;
595 for (int i = pinned_points.size() - 1; 0 <= i; --i) {
596 pin_point(p_pinned_points_indices[i].point_index, true);
597 }
598 }
599
600 Vector<SoftBody3D::PinnedPoint> SoftBody3D::get_pinned_points_indices() {
601 return pinned_points;
602 }
603
604 TypedArray<PhysicsBody3D> SoftBody3D::get_collision_exceptions() {
605 List<RID> exceptions;
606 PhysicsServer3D::get_singleton()->soft_body_get_collision_exceptions(physics_rid, &exceptions);
607 TypedArray<PhysicsBody3D> ret;
608 for (const RID &body : exceptions) {
609 ObjectID instance_id = PhysicsServer3D::get_singleton()->body_get_object_instance_id(body);
610 Object *obj = ObjectDB::get_instance(instance_id);
611 PhysicsBody3D *physics_body = Object::cast_to<PhysicsBody3D>(obj);
612 ret.append(physics_body);
613 }
614 return ret;
615 }
616
617 void SoftBody3D::add_collision_exception_with(RequiredParam<Node> rp_node) {
618 EXTRACT_PARAM_OR_FAIL(p_node, rp_node);
619 CollisionObject3D *collision_object = Object::cast_to<CollisionObject3D>(p_node);
620 ERR_FAIL_NULL_MSG(collision_object, "Collision exception only works between two nodes that inherit from CollisionObject3D (such as Area3D or PhysicsBody3D).");
621 PhysicsServer3D::get_singleton()->soft_body_add_collision_exception(physics_rid, collision_object->get_rid());
622 }
623
624 void SoftBody3D::remove_collision_exception_with(RequiredParam<Node> rp_node) {
625 EXTRACT_PARAM_OR_FAIL(p_node, rp_node);
626 CollisionObject3D *collision_object = Object::cast_to<CollisionObject3D>(p_node);
627 ERR_FAIL_NULL_MSG(collision_object, "Collision exception only works between two nodes that inherit from CollisionObject3D (such as Area3D or PhysicsBody3D).");
628 PhysicsServer3D::get_singleton()->soft_body_remove_collision_exception(physics_rid, collision_object->get_rid());
629 }
630
631 int SoftBody3D::get_simulation_precision() {
632 return PhysicsServer3D::get_singleton()->soft_body_get_simulation_precision(physics_rid);
633 }
634
635 void SoftBody3D::set_simulation_precision(int p_simulation_precision) {
636 PhysicsServer3D::get_singleton()->soft_body_set_simulation_precision(physics_rid, p_simulation_precision);
637 }
638
639 real_t SoftBody3D::get_total_mass() {
640 return PhysicsServer3D::get_singleton()->soft_body_get_total_mass(physics_rid);
641 }
642
643 void SoftBody3D::set_total_mass(real_t p_total_mass) {
644 PhysicsServer3D::get_singleton()->soft_body_set_total_mass(physics_rid, p_total_mass);
645 }
646
647 void SoftBody3D::set_linear_stiffness(real_t p_linear_stiffness) {
648 PhysicsServer3D::get_singleton()->soft_body_set_linear_stiffness(physics_rid, p_linear_stiffness);
649 }
650
651 real_t SoftBody3D::get_linear_stiffness() {
652 return PhysicsServer3D::get_singleton()->soft_body_get_linear_stiffness(physics_rid);
653 }
654
655 void SoftBody3D::set_shrinking_factor(real_t p_shrinking_factor) {
656 PhysicsServer3D::get_singleton()->soft_body_set_shrinking_factor(physics_rid, p_shrinking_factor);
657 }
658
659 real_t SoftBody3D::get_shrinking_factor() {
660 return PhysicsServer3D::get_singleton()->soft_body_get_shrinking_factor(physics_rid);
661 }
662
663 real_t SoftBody3D::get_pressure_coefficient() {
664 return PhysicsServer3D::get_singleton()->soft_body_get_pressure_coefficient(physics_rid);
665 }
666
667 void SoftBody3D::set_pressure_coefficient(real_t p_pressure_coefficient) {
668 PhysicsServer3D::get_singleton()->soft_body_set_pressure_coefficient(physics_rid, p_pressure_coefficient);
669 }
670
671 real_t SoftBody3D::get_damping_coefficient() {
672 return PhysicsServer3D::get_singleton()->soft_body_get_damping_coefficient(physics_rid);
673 }
674
675 void SoftBody3D::set_damping_coefficient(real_t p_damping_coefficient) {
676 PhysicsServer3D::get_singleton()->soft_body_set_damping_coefficient(physics_rid, p_damping_coefficient);
677 }
678
679 real_t SoftBody3D::get_drag_coefficient() {
680 return PhysicsServer3D::get_singleton()->soft_body_get_drag_coefficient(physics_rid);
681 }
682
683 void SoftBody3D::set_drag_coefficient(real_t p_drag_coefficient) {
684 PhysicsServer3D::get_singleton()->soft_body_set_drag_coefficient(physics_rid, p_drag_coefficient);
685 }
686
687 Vector3 SoftBody3D::get_point_transform(int p_point_index) {
688 return PhysicsServer3D::get_singleton()->soft_body_get_point_global_position(physics_rid, p_point_index);
689 }
690
691 void SoftBody3D::apply_impulse(int p_point_index, const Vector3 &p_impulse) {
692 PhysicsServer3D::get_singleton()->soft_body_apply_point_impulse(physics_rid, p_point_index, p_impulse);
693 }
694
695 void SoftBody3D::apply_force(int p_point_index, const Vector3 &p_force) {
696 PhysicsServer3D::get_singleton()->soft_body_apply_point_force(physics_rid, p_point_index, p_force);
697 }
698
699 void SoftBody3D::apply_central_impulse(const Vector3 &p_impulse) {
700 PhysicsServer3D::get_singleton()->soft_body_apply_central_impulse(physics_rid, p_impulse);
701 }
702
703 void SoftBody3D::apply_central_force(const Vector3 &p_force) {
704 PhysicsServer3D::get_singleton()->soft_body_apply_central_force(physics_rid, p_force);
705 }
706
707 void SoftBody3D::pin_point(int p_point_index, bool pin, const NodePath &p_spatial_attachment_path, int p_insert_at) {
708 ERR_FAIL_COND_MSG(p_insert_at < -1 || p_insert_at >= pinned_points.size(), "Invalid index for pin point insertion position.");
709 _pin_point_on_physics_server(p_point_index, pin);
710 if (pin) {
711 _add_pinned_point(p_point_index, p_spatial_attachment_path, p_insert_at);
712 } else {
713 _remove_pinned_point(p_point_index);
714 }
715 }
716
717 void SoftBody3D::_pin_point_deferred(int p_point_index, bool pin, const NodePath p_spatial_attachment_path) {
718 pin_point(p_point_index, pin, p_spatial_attachment_path);
719 _make_cache_dirty();
720 }
721
722 bool SoftBody3D::is_point_pinned(int p_point_index) const {
723 return -1 != _has_pinned_point(p_point_index);
724 }
725
726 void SoftBody3D::set_ray_pickable(bool p_ray_pickable) {
727 ray_pickable = p_ray_pickable;
728 _update_pickable();
729 }
730
731 bool SoftBody3D::is_ray_pickable() const {
732 return ray_pickable;
733 }
734
735 SoftBody3D::SoftBody3D() :
736 physics_rid(PhysicsServer3D::get_singleton()->soft_body_create()) {
737 rendering_server_handler = memnew(SoftBodyRenderingServerHandler);
738 PhysicsServer3D::get_singleton()->body_attach_object_instance_id(physics_rid, get_instance_id());
739 }
740
741 SoftBody3D::~SoftBody3D() {
742 memdelete(rendering_server_handler);
743 ERR_FAIL_NULL(PhysicsServer3D::get_singleton());
744 PhysicsServer3D::get_singleton()->free_rid(physics_rid);
745 }
746
747 void SoftBody3D::_make_cache_dirty() {
748 pinned_points_cache_dirty = true;
749 }
750
751 void SoftBody3D::_update_cache_pin_points_datas() {
752 if (!pinned_points_cache_dirty) {
753 return;
754 }
755
756 pinned_points_cache_dirty = false;
757
758 PinnedPoint *w = pinned_points.ptrw();
759 for (int i = pinned_points.size() - 1; 0 <= i; --i) {
760 if (!w[i].spatial_attachment_path.is_empty()) {
761 w[i].spatial_attachment = Object::cast_to<Node3D>(get_node(w[i].spatial_attachment_path));
762 }
763 }
764 }
765
766 void SoftBody3D::_pin_point_on_physics_server(int p_point_index, bool pin) {
767 PhysicsServer3D::get_singleton()->soft_body_pin_point(physics_rid, p_point_index, pin);
768 }
769
770 void SoftBody3D::_add_pinned_point(int p_point_index, const NodePath &p_spatial_attachment_path, int p_insert_at) {
771 SoftBody3D::PinnedPoint *pinned_point;
772 if (-1 == _get_pinned_point(p_point_index, pinned_point)) {
773 // Create new
774 PinnedPoint pp;
775 pp.point_index = p_point_index;
776 pp.spatial_attachment_path = p_spatial_attachment_path;
777
778 if (!p_spatial_attachment_path.is_empty() && has_node(p_spatial_attachment_path)) {
779 pp.spatial_attachment = Object::cast_to<Node3D>(get_node(p_spatial_attachment_path));
780 pp.offset = (pp.spatial_attachment->get_global_transform().affine_inverse() * get_global_transform()).xform(PhysicsServer3D::get_singleton()->soft_body_get_point_global_position(physics_rid, pp.point_index));
781 }
782
783 if (p_insert_at != -1) {
784 pinned_points.insert(p_insert_at, pp);
785 } else {
786 pinned_points.push_back(pp);
787 }
788
789 } else {
790 pinned_point->point_index = p_point_index;
791 pinned_point->spatial_attachment_path = p_spatial_attachment_path;
792
793 if (!p_spatial_attachment_path.is_empty() && has_node(p_spatial_attachment_path)) {
794 Node3D *attachment_node = Object::cast_to<Node3D>(get_node(p_spatial_attachment_path));
795
796 ERR_FAIL_NULL_MSG(attachment_node, "Attachment node path is invalid.");
797
798 pinned_point->spatial_attachment = attachment_node;
799 pinned_point->offset = (pinned_point->spatial_attachment->get_global_transform().affine_inverse() * get_global_transform()).xform(PhysicsServer3D::get_singleton()->soft_body_get_point_global_position(physics_rid, pinned_point->point_index));
800 }
801 }
802 }
803
804 void SoftBody3D::_reset_points_offsets() {
805 if (!Engine::get_singleton()->is_editor_hint()) {
806 return;
807 }
808
809 const PinnedPoint *r = pinned_points.ptr();
810 PinnedPoint *w = pinned_points.ptrw();
811 for (int i = pinned_points.size() - 1; 0 <= i; --i) {
812 if (!r[i].spatial_attachment) {
813 if (!r[i].spatial_attachment_path.is_empty() && has_node(r[i].spatial_attachment_path)) {
814 w[i].spatial_attachment = Object::cast_to<Node3D>(get_node(r[i].spatial_attachment_path));
815 }
816 }
817
818 if (!r[i].spatial_attachment) {
819 continue;
820 }
821
822 w[i].offset = (r[i].spatial_attachment->get_global_transform().affine_inverse() * get_global_transform()).xform(PhysicsServer3D::get_singleton()->soft_body_get_point_global_position(physics_rid, r[i].point_index));
823 }
824 }
825
826 void SoftBody3D::_remove_pinned_point(int p_point_index) {
827 const int id(_has_pinned_point(p_point_index));
828 if (-1 != id) {
829 pinned_points.remove_at(id);
830 }
831 }
832
833 int SoftBody3D::_get_pinned_point(int p_point_index, SoftBody3D::PinnedPoint *&r_point) const {
834 const int id = _has_pinned_point(p_point_index);
835 if (-1 == id) {
836 r_point = nullptr;
837 return -1;
838 } else {
839 r_point = const_cast<SoftBody3D::PinnedPoint *>(&pinned_points.ptr()[id]);
840 return id;
841 }
842 }
843
844 int SoftBody3D::_has_pinned_point(int p_point_index) const {
845 const PinnedPoint *r = pinned_points.ptr();
846 for (int i = pinned_points.size() - 1; 0 <= i; --i) {
847 if (p_point_index == r[i].point_index) {
848 return i;
849 }
850 }
851 return -1;
852 }