soft_body_3d.cpp 30 KB

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