soft_body.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. /*************************************************************************/
  2. /* soft_body.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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.h"
  31. #include "os/os.h"
  32. #include "scene/3d/collision_object.h"
  33. #include "scene/3d/skeleton.h"
  34. #include "servers/physics_server.h"
  35. SoftBodyVisualServerHandler::SoftBodyVisualServerHandler() {}
  36. void SoftBodyVisualServerHandler::prepare(RID p_mesh, int p_surface) {
  37. clear();
  38. ERR_FAIL_COND(!p_mesh.is_valid());
  39. mesh = p_mesh;
  40. surface = p_surface;
  41. const uint32_t surface_format = VS::get_singleton()->mesh_surface_get_format(mesh, surface);
  42. const int surface_vertex_len = VS::get_singleton()->mesh_surface_get_array_len(mesh, p_surface);
  43. const int surface_index_len = VS::get_singleton()->mesh_surface_get_array_index_len(mesh, p_surface);
  44. uint32_t surface_offsets[VS::ARRAY_MAX];
  45. buffer = VS::get_singleton()->mesh_surface_get_array(mesh, surface);
  46. stride = VS::get_singleton()->mesh_surface_make_offsets_from_format(surface_format, surface_vertex_len, surface_index_len, surface_offsets);
  47. offset_vertices = surface_offsets[VS::ARRAY_VERTEX];
  48. offset_normal = surface_offsets[VS::ARRAY_NORMAL];
  49. }
  50. void SoftBodyVisualServerHandler::clear() {
  51. if (mesh.is_valid()) {
  52. buffer.resize(0);
  53. }
  54. mesh = RID();
  55. }
  56. void SoftBodyVisualServerHandler::open() {
  57. write_buffer = buffer.write();
  58. }
  59. void SoftBodyVisualServerHandler::close() {
  60. write_buffer = PoolVector<uint8_t>::Write();
  61. }
  62. void SoftBodyVisualServerHandler::commit_changes() {
  63. VS::get_singleton()->mesh_surface_update_region(mesh, surface, 0, buffer);
  64. }
  65. void SoftBodyVisualServerHandler::set_vertex(int p_vertex_id, const void *p_vector3) {
  66. copymem(&write_buffer[p_vertex_id * stride + offset_vertices], p_vector3, sizeof(float) * 3);
  67. }
  68. void SoftBodyVisualServerHandler::set_normal(int p_vertex_id, const void *p_vector3) {
  69. copymem(&write_buffer[p_vertex_id * stride + offset_normal], p_vector3, sizeof(float) * 3);
  70. }
  71. void SoftBodyVisualServerHandler::set_aabb(const AABB &p_aabb) {
  72. VS::get_singleton()->mesh_set_custom_aabb(mesh, p_aabb);
  73. }
  74. SoftBody::PinnedPoint::PinnedPoint() :
  75. point_index(-1),
  76. spatial_attachment(NULL) {
  77. }
  78. SoftBody::PinnedPoint::PinnedPoint(const PinnedPoint &obj_tocopy) {
  79. point_index = obj_tocopy.point_index;
  80. spatial_attachment_path = obj_tocopy.spatial_attachment_path;
  81. spatial_attachment = obj_tocopy.spatial_attachment;
  82. offset = obj_tocopy.offset;
  83. }
  84. void SoftBody::_update_pickable() {
  85. if (!is_inside_tree())
  86. return;
  87. bool pickable = ray_pickable && is_inside_tree() && is_visible_in_tree();
  88. PhysicsServer::get_singleton()->soft_body_set_ray_pickable(physics_rid, pickable);
  89. }
  90. bool SoftBody::_set(const StringName &p_name, const Variant &p_value) {
  91. String name = p_name;
  92. String which = name.get_slicec('/', 0);
  93. if ("pinned_points" == which) {
  94. return _set_property_pinned_points_indices(p_value);
  95. } else if ("attachments" == which) {
  96. int idx = name.get_slicec('/', 1).to_int();
  97. String what = name.get_slicec('/', 2);
  98. return _set_property_pinned_points_attachment(idx, what, p_value);
  99. }
  100. return false;
  101. }
  102. bool SoftBody::_get(const StringName &p_name, Variant &r_ret) const {
  103. String name = p_name;
  104. String which = name.get_slicec('/', 0);
  105. if ("pinned_points" == which) {
  106. Array arr_ret;
  107. const int pinned_points_indices_size = pinned_points.size();
  108. PoolVector<PinnedPoint>::Read r = pinned_points.read();
  109. arr_ret.resize(pinned_points_indices_size);
  110. for (int i = 0; i < pinned_points_indices_size; ++i) {
  111. arr_ret[i] = r[i].point_index;
  112. }
  113. r_ret = arr_ret;
  114. return true;
  115. } else if ("attachments" == which) {
  116. int idx = name.get_slicec('/', 1).to_int();
  117. String what = name.get_slicec('/', 2);
  118. return _get_property_pinned_points(idx, what, r_ret);
  119. }
  120. return false;
  121. }
  122. void SoftBody::_get_property_list(List<PropertyInfo> *p_list) const {
  123. const int pinned_points_indices_size = pinned_points.size();
  124. p_list->push_back(PropertyInfo(Variant::POOL_INT_ARRAY, "pinned_points"));
  125. for (int i = 0; i < pinned_points_indices_size; ++i) {
  126. p_list->push_back(PropertyInfo(Variant::INT, "attachments/" + itos(i) + "/point_index"));
  127. p_list->push_back(PropertyInfo(Variant::NODE_PATH, "attachments/" + itos(i) + "/spatial_attachment_path"));
  128. p_list->push_back(PropertyInfo(Variant::VECTOR3, "attachments/" + itos(i) + "/offset"));
  129. }
  130. }
  131. bool SoftBody::_set_property_pinned_points_indices(const Array &p_indices) {
  132. const int p_indices_size = p_indices.size();
  133. { // Remove the pined points on physics server that will be removed by resize
  134. PoolVector<PinnedPoint>::Read r = pinned_points.read();
  135. if (p_indices_size < pinned_points.size()) {
  136. for (int i = pinned_points.size() - 1; i >= p_indices_size; --i) {
  137. pin_point(r[i].point_index, false);
  138. }
  139. }
  140. }
  141. pinned_points.resize(p_indices_size);
  142. PoolVector<PinnedPoint>::Write w = pinned_points.write();
  143. int point_index;
  144. for (int i = 0; i < p_indices_size; ++i) {
  145. point_index = p_indices.get(i);
  146. if (w[i].point_index != point_index) {
  147. if (-1 != w[i].point_index)
  148. pin_point(w[i].point_index, false);
  149. w[i].point_index = point_index;
  150. pin_point(w[i].point_index, true);
  151. }
  152. }
  153. return true;
  154. }
  155. bool SoftBody::_set_property_pinned_points_attachment(int p_item, const String &p_what, const Variant &p_value) {
  156. if (pinned_points.size() <= p_item) {
  157. return false;
  158. }
  159. if ("spatial_attachment_path" == p_what) {
  160. PoolVector<PinnedPoint>::Write w = pinned_points.write();
  161. pin_point(w[p_item].point_index, true, p_value);
  162. _make_cache_dirty();
  163. } else if ("offset" == p_what) {
  164. PoolVector<PinnedPoint>::Write w = pinned_points.write();
  165. w[p_item].offset = p_value;
  166. } else {
  167. return false;
  168. }
  169. return true;
  170. }
  171. bool SoftBody::_get_property_pinned_points(int p_item, const String &p_what, Variant &r_ret) const {
  172. if (pinned_points.size() <= p_item) {
  173. return false;
  174. }
  175. PoolVector<PinnedPoint>::Read r = pinned_points.read();
  176. if ("point_index" == p_what) {
  177. r_ret = r[p_item].point_index;
  178. } else if ("spatial_attachment_path" == p_what) {
  179. r_ret = r[p_item].spatial_attachment_path;
  180. } else if ("offset" == p_what) {
  181. r_ret = r[p_item].offset;
  182. } else {
  183. return false;
  184. }
  185. return true;
  186. }
  187. void SoftBody::_changed_callback(Object *p_changed, const char *p_prop) {
  188. update_physics_server();
  189. _reset_points_offsets();
  190. #ifdef TOOLS_ENABLED
  191. if (p_changed == this) {
  192. update_configuration_warning();
  193. }
  194. #endif
  195. }
  196. void SoftBody::_notification(int p_what) {
  197. switch (p_what) {
  198. case NOTIFICATION_ENTER_WORLD: {
  199. if (Engine::get_singleton()->is_editor_hint()) {
  200. add_change_receptor(this);
  201. }
  202. RID space = get_world()->get_space();
  203. PhysicsServer::get_singleton()->soft_body_set_space(physics_rid, space);
  204. update_physics_server();
  205. } break;
  206. case NOTIFICATION_READY: {
  207. if (!parent_collision_ignore.is_empty())
  208. add_collision_exception_with(get_node(parent_collision_ignore));
  209. } break;
  210. case NOTIFICATION_TRANSFORM_CHANGED: {
  211. if (Engine::get_singleton()->is_editor_hint()) {
  212. _reset_points_offsets();
  213. return;
  214. }
  215. PhysicsServer::get_singleton()->soft_body_set_transform(physics_rid, get_global_transform());
  216. set_notify_transform(false);
  217. // Required to be top level with Transform at center of world in order to modify VisualServer only to support custom Transform
  218. set_as_toplevel(true);
  219. set_transform(Transform());
  220. set_notify_transform(true);
  221. } break;
  222. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  223. if (!simulation_started)
  224. return;
  225. _update_cache_pin_points_datas();
  226. // Submit bone attachment
  227. const int pinned_points_indices_size = pinned_points.size();
  228. PoolVector<PinnedPoint>::Read r = pinned_points.read();
  229. for (int i = 0; i < pinned_points_indices_size; ++i) {
  230. if (r[i].spatial_attachment) {
  231. PhysicsServer::get_singleton()->soft_body_move_point(physics_rid, r[i].point_index, r[i].spatial_attachment->get_global_transform().xform(r[i].offset));
  232. }
  233. }
  234. } break;
  235. case NOTIFICATION_VISIBILITY_CHANGED: {
  236. _update_pickable();
  237. } break;
  238. case NOTIFICATION_EXIT_WORLD: {
  239. PhysicsServer::get_singleton()->soft_body_set_space(physics_rid, RID());
  240. } break;
  241. }
  242. #ifdef TOOLS_ENABLED
  243. if (p_what == NOTIFICATION_LOCAL_TRANSFORM_CHANGED) {
  244. if (Engine::get_singleton()->is_editor_hint()) {
  245. update_configuration_warning();
  246. }
  247. }
  248. #endif
  249. }
  250. void SoftBody::_bind_methods() {
  251. ClassDB::bind_method(D_METHOD("_draw_soft_mesh"), &SoftBody::_draw_soft_mesh);
  252. ClassDB::bind_method(D_METHOD("set_collision_mask", "collision_mask"), &SoftBody::set_collision_mask);
  253. ClassDB::bind_method(D_METHOD("get_collision_mask"), &SoftBody::get_collision_mask);
  254. ClassDB::bind_method(D_METHOD("set_collision_layer", "collision_layer"), &SoftBody::set_collision_layer);
  255. ClassDB::bind_method(D_METHOD("get_collision_layer"), &SoftBody::get_collision_layer);
  256. ClassDB::bind_method(D_METHOD("set_collision_mask_bit", "bit", "value"), &SoftBody::set_collision_mask_bit);
  257. ClassDB::bind_method(D_METHOD("get_collision_mask_bit", "bit"), &SoftBody::get_collision_mask_bit);
  258. ClassDB::bind_method(D_METHOD("set_collision_layer_bit", "bit", "value"), &SoftBody::set_collision_layer_bit);
  259. ClassDB::bind_method(D_METHOD("get_collision_layer_bit", "bit"), &SoftBody::get_collision_layer_bit);
  260. ClassDB::bind_method(D_METHOD("set_parent_collision_ignore", "parent_collision_ignore"), &SoftBody::set_parent_collision_ignore);
  261. ClassDB::bind_method(D_METHOD("get_parent_collision_ignore"), &SoftBody::get_parent_collision_ignore);
  262. ClassDB::bind_method(D_METHOD("add_collision_exception_with", "body"), &SoftBody::add_collision_exception_with);
  263. ClassDB::bind_method(D_METHOD("remove_collision_exception_with", "body"), &SoftBody::remove_collision_exception_with);
  264. ClassDB::bind_method(D_METHOD("set_simulation_precision", "simulation_precision"), &SoftBody::set_simulation_precision);
  265. ClassDB::bind_method(D_METHOD("get_simulation_precision"), &SoftBody::get_simulation_precision);
  266. ClassDB::bind_method(D_METHOD("set_total_mass", "mass"), &SoftBody::set_total_mass);
  267. ClassDB::bind_method(D_METHOD("get_total_mass"), &SoftBody::get_total_mass);
  268. ClassDB::bind_method(D_METHOD("set_linear_stiffness", "linear_stiffness"), &SoftBody::set_linear_stiffness);
  269. ClassDB::bind_method(D_METHOD("get_linear_stiffness"), &SoftBody::get_linear_stiffness);
  270. ClassDB::bind_method(D_METHOD("set_areaAngular_stiffness", "areaAngular_stiffness"), &SoftBody::set_areaAngular_stiffness);
  271. ClassDB::bind_method(D_METHOD("get_areaAngular_stiffness"), &SoftBody::get_areaAngular_stiffness);
  272. ClassDB::bind_method(D_METHOD("set_volume_stiffness", "volume_stiffness"), &SoftBody::set_volume_stiffness);
  273. ClassDB::bind_method(D_METHOD("get_volume_stiffness"), &SoftBody::get_volume_stiffness);
  274. ClassDB::bind_method(D_METHOD("set_pressure_coefficient", "pressure_coefficient"), &SoftBody::set_pressure_coefficient);
  275. ClassDB::bind_method(D_METHOD("get_pressure_coefficient"), &SoftBody::get_pressure_coefficient);
  276. ClassDB::bind_method(D_METHOD("set_pose_matching_coefficient", "pose_matching_coefficient"), &SoftBody::set_pose_matching_coefficient);
  277. ClassDB::bind_method(D_METHOD("get_pose_matching_coefficient"), &SoftBody::get_pose_matching_coefficient);
  278. ClassDB::bind_method(D_METHOD("set_damping_coefficient", "damping_coefficient"), &SoftBody::set_damping_coefficient);
  279. ClassDB::bind_method(D_METHOD("get_damping_coefficient"), &SoftBody::get_damping_coefficient);
  280. ClassDB::bind_method(D_METHOD("set_drag_coefficient", "drag_coefficient"), &SoftBody::set_drag_coefficient);
  281. ClassDB::bind_method(D_METHOD("get_drag_coefficient"), &SoftBody::get_drag_coefficient);
  282. ClassDB::bind_method(D_METHOD("set_ray_pickable", "ray_pickable"), &SoftBody::set_ray_pickable);
  283. ClassDB::bind_method(D_METHOD("is_ray_pickable"), &SoftBody::is_ray_pickable);
  284. ADD_GROUP("Collision", "collision_");
  285. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_layer", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_layer", "get_collision_layer");
  286. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
  287. 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");
  288. ADD_PROPERTY(PropertyInfo(Variant::INT, "simulation_precision", PROPERTY_HINT_RANGE, "1,100,1"), "set_simulation_precision", "get_simulation_precision");
  289. ADD_PROPERTY(PropertyInfo(Variant::REAL, "total_mass", PROPERTY_HINT_RANGE, "0.01,10000,1"), "set_total_mass", "get_total_mass");
  290. ADD_PROPERTY(PropertyInfo(Variant::REAL, "linear_stiffness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_linear_stiffness", "get_linear_stiffness");
  291. ADD_PROPERTY(PropertyInfo(Variant::REAL, "areaAngular_stiffness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_areaAngular_stiffness", "get_areaAngular_stiffness");
  292. ADD_PROPERTY(PropertyInfo(Variant::REAL, "volume_stiffness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_volume_stiffness", "get_volume_stiffness");
  293. ADD_PROPERTY(PropertyInfo(Variant::REAL, "pressure_coefficient"), "set_pressure_coefficient", "get_pressure_coefficient");
  294. ADD_PROPERTY(PropertyInfo(Variant::REAL, "damping_coefficient", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_damping_coefficient", "get_damping_coefficient");
  295. ADD_PROPERTY(PropertyInfo(Variant::REAL, "drag_coefficient", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_drag_coefficient", "get_drag_coefficient");
  296. ADD_PROPERTY(PropertyInfo(Variant::REAL, "pose_matching_coefficient", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_pose_matching_coefficient", "get_pose_matching_coefficient");
  297. }
  298. String SoftBody::get_configuration_warning() const {
  299. String warning = MeshInstance::get_configuration_warning();
  300. if (get_mesh().is_null()) {
  301. if (!warning.empty())
  302. warning += "\n\n";
  303. warning += TTR("This body will be ignored until you set a mesh");
  304. }
  305. Transform t = get_transform();
  306. if ((ABS(t.basis.get_axis(0).length() - 1.0) > 0.05 || ABS(t.basis.get_axis(1).length() - 1.0) > 0.05 || ABS(t.basis.get_axis(0).length() - 1.0) > 0.05)) {
  307. if (!warning.empty())
  308. warning += "\n\n";
  309. warning += TTR("Size changes to SoftBody will be overridden by the physics engine when running.\nChange the size in children collision shapes instead.");
  310. }
  311. return warning;
  312. }
  313. void SoftBody::_draw_soft_mesh() {
  314. if (get_mesh().is_null())
  315. return;
  316. if (!visual_server_handler.is_ready()) {
  317. visual_server_handler.prepare(get_mesh()->get_rid(), 0);
  318. /// Necessary in order to render the mesh correctly (Soft body nodes are in global space)
  319. simulation_started = true;
  320. call_deferred("set_as_toplevel", true);
  321. call_deferred("set_transform", Transform());
  322. }
  323. visual_server_handler.open();
  324. PhysicsServer::get_singleton()->soft_body_update_visual_server(physics_rid, &visual_server_handler);
  325. visual_server_handler.close();
  326. visual_server_handler.commit_changes();
  327. }
  328. void SoftBody::update_physics_server() {
  329. if (Engine::get_singleton()->is_editor_hint()) {
  330. if (get_mesh().is_valid())
  331. PhysicsServer::get_singleton()->soft_body_set_mesh(physics_rid, get_mesh());
  332. else
  333. PhysicsServer::get_singleton()->soft_body_set_mesh(physics_rid, NULL);
  334. return;
  335. }
  336. if (get_mesh().is_valid()) {
  337. become_mesh_owner();
  338. PhysicsServer::get_singleton()->soft_body_set_mesh(physics_rid, get_mesh());
  339. VS::get_singleton()->connect("frame_pre_draw", this, "_draw_soft_mesh");
  340. } else {
  341. PhysicsServer::get_singleton()->soft_body_set_mesh(physics_rid, NULL);
  342. VS::get_singleton()->disconnect("frame_pre_draw", this, "_draw_soft_mesh");
  343. }
  344. }
  345. void SoftBody::become_mesh_owner() {
  346. if (mesh.is_null())
  347. return;
  348. if (!mesh_owner) {
  349. mesh_owner = true;
  350. Vector<Ref<Material> > copy_materials;
  351. copy_materials.append_array(materials);
  352. ERR_FAIL_COND(!mesh->get_surface_count());
  353. // Get current mesh array and create new mesh array with necessary flag for softbody
  354. Array surface_arrays = mesh->surface_get_arrays(0);
  355. Array surface_blend_arrays = mesh->surface_get_blend_shape_arrays(0);
  356. uint32_t surface_format = mesh->surface_get_format(0);
  357. surface_format &= ~(Mesh::ARRAY_COMPRESS_VERTEX | Mesh::ARRAY_COMPRESS_NORMAL);
  358. surface_format |= Mesh::ARRAY_FLAG_USE_DYNAMIC_UPDATE;
  359. Ref<ArrayMesh> soft_mesh;
  360. soft_mesh.instance();
  361. soft_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, surface_arrays, surface_blend_arrays, surface_format);
  362. soft_mesh->surface_set_material(0, mesh->surface_get_material(0));
  363. set_mesh(soft_mesh);
  364. for (int i = copy_materials.size() - 1; 0 <= i; --i) {
  365. set_surface_material(i, copy_materials[i]);
  366. }
  367. }
  368. }
  369. void SoftBody::set_collision_mask(uint32_t p_mask) {
  370. collision_mask = p_mask;
  371. PhysicsServer::get_singleton()->soft_body_set_collision_mask(physics_rid, p_mask);
  372. }
  373. uint32_t SoftBody::get_collision_mask() const {
  374. return collision_mask;
  375. }
  376. void SoftBody::set_collision_layer(uint32_t p_layer) {
  377. collision_layer = p_layer;
  378. PhysicsServer::get_singleton()->soft_body_set_collision_layer(physics_rid, p_layer);
  379. }
  380. uint32_t SoftBody::get_collision_layer() const {
  381. return collision_layer;
  382. }
  383. void SoftBody::set_collision_mask_bit(int p_bit, bool p_value) {
  384. uint32_t mask = get_collision_mask();
  385. if (p_value)
  386. mask |= 1 << p_bit;
  387. else
  388. mask &= ~(1 << p_bit);
  389. set_collision_mask(mask);
  390. }
  391. bool SoftBody::get_collision_mask_bit(int p_bit) const {
  392. return get_collision_mask() & (1 << p_bit);
  393. }
  394. void SoftBody::set_collision_layer_bit(int p_bit, bool p_value) {
  395. uint32_t layer = get_collision_layer();
  396. if (p_value)
  397. layer |= 1 << p_bit;
  398. else
  399. layer &= ~(1 << p_bit);
  400. set_collision_layer(layer);
  401. }
  402. bool SoftBody::get_collision_layer_bit(int p_bit) const {
  403. return get_collision_layer() & (1 << p_bit);
  404. }
  405. void SoftBody::set_parent_collision_ignore(const NodePath &p_parent_collision_ignore) {
  406. parent_collision_ignore = p_parent_collision_ignore;
  407. }
  408. const NodePath &SoftBody::get_parent_collision_ignore() const {
  409. return parent_collision_ignore;
  410. }
  411. void SoftBody::set_pinned_points_indices(PoolVector<SoftBody::PinnedPoint> p_pinned_points_indices) {
  412. pinned_points = p_pinned_points_indices;
  413. PoolVector<PinnedPoint>::Read w = pinned_points.read();
  414. for (int i = pinned_points.size() - 1; 0 <= i; --i) {
  415. pin_point(p_pinned_points_indices[i].point_index, true);
  416. }
  417. }
  418. PoolVector<SoftBody::PinnedPoint> SoftBody::get_pinned_points_indices() {
  419. return pinned_points;
  420. }
  421. void SoftBody::add_collision_exception_with(Node *p_node) {
  422. ERR_FAIL_NULL(p_node);
  423. CollisionObject *collision_object = Object::cast_to<CollisionObject>(p_node);
  424. if (!collision_object) {
  425. ERR_EXPLAIN("Collision exception only works between two CollisionObject");
  426. }
  427. ERR_FAIL_COND(!collision_object);
  428. PhysicsServer::get_singleton()->soft_body_add_collision_exception(physics_rid, collision_object->get_rid());
  429. }
  430. void SoftBody::remove_collision_exception_with(Node *p_node) {
  431. ERR_FAIL_NULL(p_node);
  432. CollisionObject *collision_object = Object::cast_to<CollisionObject>(p_node);
  433. if (!collision_object) {
  434. ERR_EXPLAIN("Collision exception only works between two CollisionObject");
  435. }
  436. ERR_FAIL_COND(!collision_object);
  437. PhysicsServer::get_singleton()->soft_body_remove_collision_exception(physics_rid, collision_object->get_rid());
  438. }
  439. int SoftBody::get_simulation_precision() {
  440. return PhysicsServer::get_singleton()->soft_body_get_simulation_precision(physics_rid);
  441. }
  442. void SoftBody::set_simulation_precision(int p_simulation_precision) {
  443. PhysicsServer::get_singleton()->soft_body_set_simulation_precision(physics_rid, p_simulation_precision);
  444. }
  445. real_t SoftBody::get_total_mass() {
  446. return PhysicsServer::get_singleton()->soft_body_get_total_mass(physics_rid);
  447. }
  448. void SoftBody::set_total_mass(real_t p_total_mass) {
  449. PhysicsServer::get_singleton()->soft_body_set_total_mass(physics_rid, p_total_mass);
  450. }
  451. void SoftBody::set_linear_stiffness(real_t p_linear_stiffness) {
  452. PhysicsServer::get_singleton()->soft_body_set_linear_stiffness(physics_rid, p_linear_stiffness);
  453. }
  454. real_t SoftBody::get_linear_stiffness() {
  455. return PhysicsServer::get_singleton()->soft_body_get_linear_stiffness(physics_rid);
  456. }
  457. void SoftBody::set_areaAngular_stiffness(real_t p_areaAngular_stiffness) {
  458. PhysicsServer::get_singleton()->soft_body_set_areaAngular_stiffness(physics_rid, p_areaAngular_stiffness);
  459. }
  460. real_t SoftBody::get_areaAngular_stiffness() {
  461. return PhysicsServer::get_singleton()->soft_body_get_areaAngular_stiffness(physics_rid);
  462. }
  463. void SoftBody::set_volume_stiffness(real_t p_volume_stiffness) {
  464. PhysicsServer::get_singleton()->soft_body_set_volume_stiffness(physics_rid, p_volume_stiffness);
  465. }
  466. real_t SoftBody::get_volume_stiffness() {
  467. return PhysicsServer::get_singleton()->soft_body_get_volume_stiffness(physics_rid);
  468. }
  469. real_t SoftBody::get_pressure_coefficient() {
  470. return PhysicsServer::get_singleton()->soft_body_get_pressure_coefficient(physics_rid);
  471. }
  472. void SoftBody::set_pose_matching_coefficient(real_t p_pose_matching_coefficient) {
  473. PhysicsServer::get_singleton()->soft_body_set_pose_matching_coefficient(physics_rid, p_pose_matching_coefficient);
  474. }
  475. real_t SoftBody::get_pose_matching_coefficient() {
  476. return PhysicsServer::get_singleton()->soft_body_get_pose_matching_coefficient(physics_rid);
  477. }
  478. void SoftBody::set_pressure_coefficient(real_t p_pressure_coefficient) {
  479. PhysicsServer::get_singleton()->soft_body_set_pressure_coefficient(physics_rid, p_pressure_coefficient);
  480. }
  481. real_t SoftBody::get_damping_coefficient() {
  482. return PhysicsServer::get_singleton()->soft_body_get_damping_coefficient(physics_rid);
  483. }
  484. void SoftBody::set_damping_coefficient(real_t p_damping_coefficient) {
  485. PhysicsServer::get_singleton()->soft_body_set_damping_coefficient(physics_rid, p_damping_coefficient);
  486. }
  487. real_t SoftBody::get_drag_coefficient() {
  488. return PhysicsServer::get_singleton()->soft_body_get_drag_coefficient(physics_rid);
  489. }
  490. void SoftBody::set_drag_coefficient(real_t p_drag_coefficient) {
  491. PhysicsServer::get_singleton()->soft_body_set_drag_coefficient(physics_rid, p_drag_coefficient);
  492. }
  493. Vector3 SoftBody::get_point_transform(int p_point_index) {
  494. return PhysicsServer::get_singleton()->soft_body_get_point_global_position(physics_rid, p_point_index);
  495. }
  496. void SoftBody::pin_point_toggle(int p_point_index) {
  497. pin_point(p_point_index, !(-1 != _has_pinned_point(p_point_index)));
  498. }
  499. void SoftBody::pin_point(int p_point_index, bool pin, const NodePath &p_spatial_attachment_path) {
  500. _pin_point_on_physics_server(p_point_index, pin);
  501. if (pin) {
  502. _add_pinned_point(p_point_index, p_spatial_attachment_path);
  503. } else {
  504. _remove_pinned_point(p_point_index);
  505. }
  506. }
  507. bool SoftBody::is_point_pinned(int p_point_index) const {
  508. return -1 != _has_pinned_point(p_point_index);
  509. }
  510. void SoftBody::set_ray_pickable(bool p_ray_pickable) {
  511. ray_pickable = p_ray_pickable;
  512. _update_pickable();
  513. }
  514. bool SoftBody::is_ray_pickable() const {
  515. return ray_pickable;
  516. }
  517. SoftBody::SoftBody() :
  518. MeshInstance(),
  519. physics_rid(PhysicsServer::get_singleton()->soft_body_create()),
  520. mesh_owner(false),
  521. collision_mask(1),
  522. collision_layer(1),
  523. simulation_started(false),
  524. pinned_points_cache_dirty(true) {
  525. PhysicsServer::get_singleton()->body_attach_object_instance_id(physics_rid, get_instance_id());
  526. //set_notify_transform(true);
  527. set_physics_process_internal(true);
  528. }
  529. SoftBody::~SoftBody() {
  530. }
  531. void SoftBody::reset_softbody_pin() {
  532. PhysicsServer::get_singleton()->soft_body_remove_all_pinned_points(physics_rid);
  533. PoolVector<PinnedPoint>::Read pps = pinned_points.read();
  534. for (int i = pinned_points.size() - 1; 0 < i; --i) {
  535. PhysicsServer::get_singleton()->soft_body_pin_point(physics_rid, pps[i].point_index, true);
  536. }
  537. }
  538. void SoftBody::_make_cache_dirty() {
  539. pinned_points_cache_dirty = true;
  540. }
  541. void SoftBody::_update_cache_pin_points_datas() {
  542. if (!pinned_points_cache_dirty)
  543. return;
  544. pinned_points_cache_dirty = false;
  545. PoolVector<PinnedPoint>::Write w = pinned_points.write();
  546. for (int i = pinned_points.size() - 1; 0 <= i; --i) {
  547. if (!w[i].spatial_attachment_path.is_empty()) {
  548. w[i].spatial_attachment = Object::cast_to<Spatial>(get_node(w[i].spatial_attachment_path));
  549. }
  550. if (!w[i].spatial_attachment) {
  551. ERR_PRINT("Spatial node not defined in the pinned point, Softbody undefined behaviour!");
  552. }
  553. }
  554. }
  555. void SoftBody::_pin_point_on_physics_server(int p_point_index, bool pin) {
  556. PhysicsServer::get_singleton()->soft_body_pin_point(physics_rid, p_point_index, pin);
  557. }
  558. void SoftBody::_add_pinned_point(int p_point_index, const NodePath &p_spatial_attachment_path) {
  559. SoftBody::PinnedPoint *pinned_point;
  560. if (-1 == _get_pinned_point(p_point_index, pinned_point)) {
  561. // Create new
  562. PinnedPoint pp;
  563. pp.point_index = p_point_index;
  564. pp.spatial_attachment_path = p_spatial_attachment_path;
  565. if (!p_spatial_attachment_path.is_empty() && has_node(p_spatial_attachment_path)) {
  566. pp.spatial_attachment = Object::cast_to<Spatial>(get_node(p_spatial_attachment_path));
  567. pp.offset = (pp.spatial_attachment->get_global_transform().affine_inverse() * get_global_transform()).xform(PhysicsServer::get_singleton()->soft_body_get_point_global_position(physics_rid, pp.point_index));
  568. }
  569. pinned_points.push_back(pp);
  570. } else {
  571. pinned_point->point_index = p_point_index;
  572. pinned_point->spatial_attachment_path = p_spatial_attachment_path;
  573. if (!p_spatial_attachment_path.is_empty() && has_node(p_spatial_attachment_path)) {
  574. pinned_point->spatial_attachment = Object::cast_to<Spatial>(get_node(p_spatial_attachment_path));
  575. pinned_point->offset = (pinned_point->spatial_attachment->get_global_transform().affine_inverse() * get_global_transform()).xform(PhysicsServer::get_singleton()->soft_body_get_point_global_position(physics_rid, pinned_point->point_index));
  576. }
  577. }
  578. }
  579. void SoftBody::_reset_points_offsets() {
  580. if (!Engine::get_singleton()->is_editor_hint())
  581. return;
  582. PoolVector<PinnedPoint>::Read r = pinned_points.read();
  583. PoolVector<PinnedPoint>::Write w = pinned_points.write();
  584. for (int i = pinned_points.size() - 1; 0 <= i; --i) {
  585. if (!r[i].spatial_attachment)
  586. w[i].spatial_attachment = Object::cast_to<Spatial>(get_node(r[i].spatial_attachment_path));
  587. if (!r[i].spatial_attachment)
  588. continue;
  589. w[i].offset = (r[i].spatial_attachment->get_global_transform().affine_inverse() * get_global_transform()).xform(PhysicsServer::get_singleton()->soft_body_get_point_global_position(physics_rid, r[i].point_index));
  590. }
  591. }
  592. void SoftBody::_remove_pinned_point(int p_point_index) {
  593. const int id(_has_pinned_point(p_point_index));
  594. if (-1 != id) {
  595. pinned_points.remove(id);
  596. }
  597. }
  598. int SoftBody::_get_pinned_point(int p_point_index, SoftBody::PinnedPoint *&r_point) const {
  599. const int id = _has_pinned_point(p_point_index);
  600. if (-1 == id) {
  601. r_point = NULL;
  602. return -1;
  603. } else {
  604. r_point = const_cast<SoftBody::PinnedPoint *>(&pinned_points.read()[id]);
  605. return id;
  606. }
  607. }
  608. int SoftBody::_has_pinned_point(int p_point_index) const {
  609. PoolVector<PinnedPoint>::Read r = pinned_points.read();
  610. for (int i = pinned_points.size() - 1; 0 <= i; --i) {
  611. if (p_point_index == r[i].point_index) {
  612. return i;
  613. }
  614. }
  615. return -1;
  616. }