soft_body_3d.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**************************************************************************/
  2. /* soft_body_3d.h */
  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. #ifndef SOFT_BODY_3D_H
  31. #define SOFT_BODY_3D_H
  32. #include "scene/3d/mesh_instance_3d.h"
  33. #include "servers/physics_server_3d.h"
  34. class PhysicsBody3D;
  35. class SoftBody3D;
  36. class SoftBodyRenderingServerHandler : public PhysicsServer3DRenderingServerHandler {
  37. friend class SoftBody3D;
  38. RID mesh;
  39. int surface = 0;
  40. Vector<uint8_t> buffer;
  41. uint32_t stride = 0;
  42. uint32_t normal_stride = 0;
  43. uint32_t offset_vertices = 0;
  44. uint32_t offset_normal = 0;
  45. uint8_t *write_buffer = nullptr;
  46. private:
  47. SoftBodyRenderingServerHandler();
  48. bool is_ready(RID p_mesh_rid) const { return mesh.is_valid() && mesh == p_mesh_rid; }
  49. void prepare(RID p_mesh_rid, int p_surface);
  50. void clear();
  51. void open();
  52. void close();
  53. void commit_changes();
  54. public:
  55. void set_vertex(int p_vertex_id, const Vector3 &p_vertex) override;
  56. void set_normal(int p_vertex_id, const Vector3 &p_normal) override;
  57. void set_aabb(const AABB &p_aabb) override;
  58. };
  59. class SoftBody3D : public MeshInstance3D {
  60. GDCLASS(SoftBody3D, MeshInstance3D);
  61. public:
  62. enum DisableMode {
  63. DISABLE_MODE_REMOVE,
  64. DISABLE_MODE_KEEP_ACTIVE,
  65. };
  66. struct PinnedPoint {
  67. int point_index = -1;
  68. NodePath spatial_attachment_path;
  69. Node3D *spatial_attachment = nullptr; // Cache
  70. Vector3 offset;
  71. PinnedPoint();
  72. PinnedPoint(const PinnedPoint &obj_tocopy);
  73. void operator=(const PinnedPoint &obj);
  74. };
  75. private:
  76. SoftBodyRenderingServerHandler *rendering_server_handler = nullptr;
  77. RID physics_rid;
  78. DisableMode disable_mode = DISABLE_MODE_REMOVE;
  79. RID owned_mesh;
  80. uint32_t collision_mask = 1;
  81. uint32_t collision_layer = 1;
  82. NodePath parent_collision_ignore;
  83. Vector<PinnedPoint> pinned_points;
  84. bool simulation_started = false;
  85. bool pinned_points_cache_dirty = true;
  86. Ref<ArrayMesh> debug_mesh_cache;
  87. class MeshInstance3D *debug_mesh = nullptr;
  88. bool capture_input_on_drag = false;
  89. bool ray_pickable = true;
  90. void _update_pickable();
  91. void _update_physics_server();
  92. void _draw_soft_mesh();
  93. void _prepare_physics_server();
  94. void _become_mesh_owner();
  95. protected:
  96. bool _set(const StringName &p_name, const Variant &p_value);
  97. bool _get(const StringName &p_name, Variant &r_ret) const;
  98. void _get_property_list(List<PropertyInfo> *p_list) const;
  99. bool _set_property_pinned_points_indices(const Array &p_indices);
  100. bool _set_property_pinned_points_attachment(int p_item, const String &p_what, const Variant &p_value);
  101. bool _get_property_pinned_points(int p_item, const String &p_what, Variant &r_ret) const;
  102. void _notification(int p_what);
  103. static void _bind_methods();
  104. PackedStringArray get_configuration_warnings() const override;
  105. public:
  106. RID get_physics_rid() const { return physics_rid; }
  107. void set_collision_mask(uint32_t p_mask);
  108. uint32_t get_collision_mask() const;
  109. void set_collision_layer(uint32_t p_layer);
  110. uint32_t get_collision_layer() const;
  111. void set_collision_layer_value(int p_layer_number, bool p_value);
  112. bool get_collision_layer_value(int p_layer_number) const;
  113. void set_collision_mask_value(int p_layer_number, bool p_value);
  114. bool get_collision_mask_value(int p_layer_number) const;
  115. void set_disable_mode(DisableMode p_mode);
  116. DisableMode get_disable_mode() const;
  117. void set_parent_collision_ignore(const NodePath &p_parent_collision_ignore);
  118. const NodePath &get_parent_collision_ignore() const;
  119. void set_pinned_points_indices(Vector<PinnedPoint> p_pinned_points_indices);
  120. Vector<PinnedPoint> get_pinned_points_indices();
  121. void set_simulation_precision(int p_simulation_precision);
  122. int get_simulation_precision();
  123. void set_total_mass(real_t p_total_mass);
  124. real_t get_total_mass();
  125. void set_linear_stiffness(real_t p_linear_stiffness);
  126. real_t get_linear_stiffness();
  127. void set_pressure_coefficient(real_t p_pressure_coefficient);
  128. real_t get_pressure_coefficient();
  129. void set_damping_coefficient(real_t p_damping_coefficient);
  130. real_t get_damping_coefficient();
  131. void set_drag_coefficient(real_t p_drag_coefficient);
  132. real_t get_drag_coefficient();
  133. TypedArray<PhysicsBody3D> get_collision_exceptions();
  134. void add_collision_exception_with(Node *p_node);
  135. void remove_collision_exception_with(Node *p_node);
  136. Vector3 get_point_transform(int p_point_index);
  137. void pin_point_toggle(int p_point_index);
  138. void pin_point(int p_point_index, bool pin, const NodePath &p_spatial_attachment_path = NodePath());
  139. bool is_point_pinned(int p_point_index) const;
  140. void _pin_point_deferred(int p_point_index, bool pin, const NodePath p_spatial_attachment_path);
  141. void set_ray_pickable(bool p_ray_pickable);
  142. bool is_ray_pickable() const;
  143. SoftBody3D();
  144. ~SoftBody3D();
  145. private:
  146. void _make_cache_dirty();
  147. void _update_cache_pin_points_datas();
  148. void _pin_point_on_physics_server(int p_point_index, bool pin);
  149. void _add_pinned_point(int p_point_index, const NodePath &p_spatial_attachment_path);
  150. void _reset_points_offsets();
  151. void _remove_pinned_point(int p_point_index);
  152. int _get_pinned_point(int p_point_index, PinnedPoint *&r_point) const;
  153. int _has_pinned_point(int p_point_index) const;
  154. };
  155. VARIANT_ENUM_CAST(SoftBody3D::DisableMode);
  156. #endif // SOFT_BODY_3D_H