soft_body_3d.cpp 30 KB

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