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