soft_body_3d.cpp 30 KB

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