-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathfluid_3d.rs
246 lines (230 loc) · 7.85 KB
/
fluid_3d.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
use godot::classes::notify::Node3DNotification;
use godot::classes::Engine;
use godot::prelude::*;
use super::fluid_impl::FluidImpl;
use crate::servers::rapier_project_settings::RapierProjectSettings;
use crate::servers::RapierPhysicsServer;
use crate::types::*;
#[derive(GodotClass)]
#[class(base=Node3D)]
/// The fluid node. Use this node to simulate fluids in 2D.
pub struct Fluid3D {
#[var(get)]
pub(crate) rid: Rid,
#[var(get)]
pub(crate) radius: real,
#[export]
#[var(get, set = set_debug_draw)]
pub(crate) debug_draw: bool,
#[export]
#[var(get, set = set_density)]
pub(crate) density: real,
#[export]
#[var(get, set = set_lifetime)]
pub(crate) lifetime: real,
#[export]
#[var(get, set = set_effects)]
pub(crate) effects: Array<Option<Gd<Resource>>>,
#[export]
#[var(get = get_points, set = set_points)]
pub(crate) points: PackedVectorArray,
pub(crate) create_times: PackedFloat32Array,
base: Base<Node3D>,
}
#[godot_api]
impl Fluid3D {
#[func]
/// Set the points of the fluid.
fn set_points(&mut self, points: PackedVectorArray) {
FluidImpl::set_points(self, points);
}
#[func]
/// Set the density of the fluid.
fn set_density(&mut self, density: real) {
FluidImpl::set_density(self, density);
}
#[func]
/// Set the lifetime of the fluid.
fn set_lifetime(&mut self, lifetime: real) {
FluidImpl::set_lifetime(self, lifetime);
}
#[func]
/// Set the debug draw of the fluid.
fn set_debug_draw(&mut self, debug_draw: bool) {
FluidImpl::set_debug_draw(self, debug_draw);
}
#[func]
/// Get the accelerations of the fluid.
fn get_accelerations(&self) -> PackedVectorArray {
FluidImpl::get_accelerations(self)
}
#[func]
/// Get the remaining times of the fluid.
fn get_remaining_times(&self) -> PackedFloat32Array {
FluidImpl::get_remaining_times(self)
}
#[func]
/// Get the velocities of the fluid.
fn get_velocities(&self) -> PackedVectorArray {
FluidImpl::get_velocities(self)
}
#[func]
/// Get the points of the fluid.
fn get_points(&self) -> PackedVectorArray {
FluidImpl::get_points(self)
}
#[func]
/// Create the points of the fluid particles inside a box.
fn create_box_points(&self, width: i32, height: i32, depth: i32) -> PackedVectorArray {
let mut new_points = PackedVectorArray::default();
new_points.resize((width * height * depth) as usize);
for i in 0..width {
for j in 0..height {
for k in 0..depth {
new_points[(i + j * width + k * width * height) as usize] = Vector::new(
i as f32 * self.radius * 2.0,
j as f32 * self.radius * 2.0,
k as f32 * self.radius * 2.0,
);
}
}
}
new_points
}
#[func]
/// Create the points of the fluid particles inside a sphere.
fn create_sphere_points(&self, radius: i32) -> PackedVectorArray {
let mut new_points = PackedVectorArray::default();
for i in -radius..radius {
for j in -radius..radius {
for k in -radius..radius {
let x = i as f32 * self.radius * 2.0;
let y = j as f32 * self.radius * 2.0;
let z = k as f32 * self.radius * 2.0;
if i * i + j * j * k * k <= radius * radius {
new_points.push(Vector::new(x, y, z));
}
}
}
}
new_points
}
#[func]
/// Add the points to the fluid particles.
fn add_points_and_velocities(
&mut self,
points: PackedVectorArray,
velocities: PackedVectorArray,
) {
FluidImpl::add_points_and_velocities(self, points, velocities);
}
#[func]
/// Set the points and velocities of the fluid particles.
fn set_points_and_velocities(
&mut self,
points: PackedVectorArray,
velocities: PackedVectorArray,
) {
FluidImpl::set_points_and_velocities(self, points, velocities);
}
#[func]
/// Delete the points of the fluid particles.
fn delete_points(&mut self, indices: PackedInt32Array) {
FluidImpl::delete_points(self, indices);
}
#[func]
/// Set the effects of the fluid particles.
fn set_effects(&mut self, effects: Array<Option<Gd<Resource>>>) {
FluidImpl::set_effects(self, effects);
}
}
#[godot_api]
impl INode3D for Fluid3D {
fn init(base: Base<Node3D>) -> Self {
Self {
rid: RapierPhysicsServer::fluid_create(),
radius: RapierProjectSettings::get_fluid_particle_radius(),
debug_draw: false,
density: 1.0,
lifetime: 0.0,
effects: Array::new(),
points: PackedVectorArray::new(),
create_times: PackedFloat32Array::new(),
base,
}
}
fn on_notification(&mut self, p_what: Node3DNotification) {
match p_what {
Node3DNotification::PROCESS => {
if self.debug_draw {
// TODO
//self.to_gd().queue_redraw();
}
if !Engine::singleton().is_editor_hint() {
FluidImpl::delete_old_particles(self);
}
}
Node3DNotification::ENTER_TREE | Node3DNotification::ENTER_WORLD => {
let mut space_rid = Rid::Invalid;
let fluid_gd = self.to_gd();
if !fluid_gd.is_inside_tree() {
return;
}
if let Some(space) = fluid_gd.get_world_3d() {
space_rid = space.get_space();
}
drop(fluid_gd);
let rid = self.rid;
let guard = self.base_mut();
RapierPhysicsServer::fluid_set_space(rid, space_rid);
drop(guard);
self.set_points(self.points.clone());
let mut fluid_gd = self.to_gd();
fluid_gd.set_notify_transform(self.debug_draw);
}
Node3DNotification::TRANSFORM_CHANGED
| Node3DNotification::LOCAL_TRANSFORM_CHANGED
| Node3DNotification::TRANSLATION_CHANGED => {
if !self.to_gd().is_inside_tree() {
return;
}
self.set_points(self.points.clone());
let mut fluid_gd = self.to_gd();
fluid_gd.set_notify_transform(self.debug_draw);
// TODO
//fluid_gd.queue_redraw();
}
Node3DNotification::EXIT_TREE | Node3DNotification::EXIT_WORLD => {
let rid = self.rid;
let guard = self.base_mut();
RapierPhysicsServer::fluid_set_space(rid, Rid::Invalid);
drop(guard);
}
/*
Node3DNotification::DRAW => {
if self.debug_draw {
self.points = self.get_points();
for point in self.points.as_slice() {
let mut color = Color::WHITE;
color.a = 0.4;
self.to_gd().draw_rect(
Rect2::new(
*point - Vector2::new(self.radius / 2.0, self.radius / 2.0),
Vector2::new(self.radius, self.radius),
),
color,
);
}
}
} */
_ => {}
}
}
}
impl Drop for Fluid3D {
fn drop(&mut self) {
if self.rid != Rid::Invalid {
PhysicsServer::singleton().free_rid(self.rid);
}
}
}