soft_body.cpp 29 KB

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