soft_body_3d.cpp 31 KB

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