Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/3d/cpu_particles_3d.h
21461 views
1
/**************************************************************************/
2
/* cpu_particles_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
#pragma once
32
33
#include "scene/3d/visual_instance_3d.h"
34
#include "scene/resources/curve.h"
35
#include "scene/resources/gradient.h"
36
37
class RandomNumberGenerator;
38
39
class CPUParticles3D : public GeometryInstance3D {
40
private:
41
GDCLASS(CPUParticles3D, GeometryInstance3D);
42
43
public:
44
enum DrawOrder {
45
DRAW_ORDER_INDEX,
46
DRAW_ORDER_LIFETIME,
47
DRAW_ORDER_VIEW_DEPTH,
48
DRAW_ORDER_MAX
49
};
50
51
enum Parameter {
52
PARAM_INITIAL_LINEAR_VELOCITY,
53
PARAM_ANGULAR_VELOCITY,
54
PARAM_ORBIT_VELOCITY,
55
PARAM_LINEAR_ACCEL,
56
PARAM_RADIAL_ACCEL,
57
PARAM_TANGENTIAL_ACCEL,
58
PARAM_DAMPING,
59
PARAM_ANGLE,
60
PARAM_SCALE,
61
PARAM_HUE_VARIATION,
62
PARAM_ANIM_SPEED,
63
PARAM_ANIM_OFFSET,
64
PARAM_MAX
65
};
66
67
enum ParticleFlags {
68
PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY,
69
PARTICLE_FLAG_ROTATE_Y,
70
PARTICLE_FLAG_DISABLE_Z,
71
PARTICLE_FLAG_MAX
72
};
73
74
enum EmissionShape {
75
EMISSION_SHAPE_POINT,
76
EMISSION_SHAPE_SPHERE,
77
EMISSION_SHAPE_SPHERE_SURFACE,
78
EMISSION_SHAPE_BOX,
79
EMISSION_SHAPE_POINTS,
80
EMISSION_SHAPE_DIRECTED_POINTS,
81
EMISSION_SHAPE_RING,
82
EMISSION_SHAPE_MAX
83
};
84
85
private:
86
bool emitting = false;
87
bool active = false;
88
89
struct Particle {
90
Transform3D transform;
91
Color color;
92
real_t custom[4] = {};
93
Vector3 velocity;
94
bool active = false;
95
real_t angle_rand = 0.0;
96
real_t scale_rand = 0.0;
97
real_t hue_rot_rand = 0.0;
98
real_t anim_offset_rand = 0.0;
99
Color start_color_rand;
100
double time = 0.0;
101
double lifetime = 0.0;
102
Color base_color;
103
104
uint32_t seed = 0;
105
};
106
107
double time = 0.0;
108
double frame_remainder = 0.0;
109
int cycle = 0;
110
bool redraw = false;
111
112
RID multimesh;
113
114
Vector<Particle> particles;
115
Vector<float> particle_data;
116
Vector<int> particle_order;
117
118
struct SortLifetime {
119
const Particle *particles = nullptr;
120
121
bool operator()(int p_a, int p_b) const {
122
return particles[p_a].time > particles[p_b].time;
123
}
124
};
125
126
struct SortAxis {
127
const Particle *particles = nullptr;
128
Vector3 axis;
129
bool operator()(int p_a, int p_b) const {
130
return axis.dot(particles[p_a].transform.origin) < axis.dot(particles[p_b].transform.origin);
131
}
132
};
133
134
//
135
136
bool one_shot = false;
137
138
double lifetime = 1.0;
139
double pre_process_time = 0.0;
140
double _requested_process_time = 0.0;
141
real_t explosiveness_ratio = 0.0;
142
real_t randomness_ratio = 0.0;
143
double lifetime_randomness = 0.0;
144
double speed_scale = 1.0;
145
AABB visibility_aabb;
146
bool local_coords = false;
147
int fixed_fps = 0;
148
bool fractional_delta = true;
149
uint32_t seed = 0;
150
bool use_fixed_seed = false;
151
152
Transform3D inv_emission_transform;
153
154
SafeFlag can_update;
155
156
DrawOrder draw_order = DRAW_ORDER_INDEX;
157
158
Ref<Mesh> mesh;
159
160
////////
161
162
Vector3 direction = Vector3(1, 0, 0);
163
real_t spread = 45.0;
164
real_t flatness = 0.0;
165
166
real_t parameters_min[PARAM_MAX] = {};
167
real_t parameters_max[PARAM_MAX] = {};
168
169
Ref<Curve> curve_parameters[PARAM_MAX];
170
Color color = Color(1, 1, 1, 1);
171
Ref<Gradient> color_ramp;
172
Ref<Gradient> color_initial_ramp;
173
174
bool particle_flags[PARTICLE_FLAG_MAX] = {};
175
176
EmissionShape emission_shape = EMISSION_SHAPE_POINT;
177
real_t emission_sphere_radius = 1.0;
178
Vector3 emission_box_extents = Vector3(1, 1, 1);
179
Vector<Vector3> emission_points;
180
Vector<Vector3> emission_normals;
181
Vector<Color> emission_colors;
182
Vector3 emission_ring_axis;
183
real_t emission_ring_height = 0.0;
184
real_t emission_ring_radius = 0.0;
185
real_t emission_ring_inner_radius = 0.0;
186
real_t emission_ring_cone_angle = 0.0;
187
188
Ref<Curve> scale_curve_x;
189
Ref<Curve> scale_curve_y;
190
Ref<Curve> scale_curve_z;
191
bool split_scale = false;
192
193
Vector3 gravity = Vector3(0, -9.8, 0);
194
195
Ref<RandomNumberGenerator> rng;
196
197
void _update_internal();
198
void _particles_process(double p_delta);
199
void _update_particle_data_buffer();
200
void _set_emitting();
201
202
Mutex update_mutex;
203
204
void _update_render_thread();
205
206
void _set_redraw(bool p_redraw);
207
208
protected:
209
static void _bind_methods();
210
void _notification(int p_what);
211
void _validate_property(PropertyInfo &p_property) const;
212
213
#ifndef DISABLE_DEPRECATED
214
void _restart_bind_compat_92089();
215
static void _bind_compatibility_methods();
216
#endif
217
218
public:
219
AABB get_aabb() const override;
220
221
void set_emitting(bool p_emitting);
222
void set_amount(int p_amount);
223
void set_lifetime(double p_lifetime);
224
void set_one_shot(bool p_one_shot);
225
void set_pre_process_time(double p_time);
226
void set_explosiveness_ratio(real_t p_ratio);
227
void set_randomness_ratio(real_t p_ratio);
228
void set_visibility_aabb(const AABB &p_aabb);
229
void set_lifetime_randomness(double p_random);
230
void set_use_local_coordinates(bool p_enable);
231
void set_speed_scale(double p_scale);
232
233
bool is_emitting() const;
234
int get_amount() const;
235
double get_lifetime() const;
236
bool get_one_shot() const;
237
double get_pre_process_time() const;
238
real_t get_explosiveness_ratio() const;
239
real_t get_randomness_ratio() const;
240
AABB get_visibility_aabb() const;
241
double get_lifetime_randomness() const;
242
bool get_use_local_coordinates() const;
243
double get_speed_scale() const;
244
245
void set_fixed_fps(int p_count);
246
int get_fixed_fps() const;
247
248
void set_fractional_delta(bool p_enable);
249
bool get_fractional_delta() const;
250
251
void set_draw_order(DrawOrder p_order);
252
DrawOrder get_draw_order() const;
253
254
void set_mesh(const Ref<Mesh> &p_mesh);
255
Ref<Mesh> get_mesh() const;
256
257
void set_use_fixed_seed(bool p_use_fixed_seed = false);
258
bool get_use_fixed_seed() const;
259
260
void set_seed(uint32_t p_seed);
261
uint32_t get_seed() const;
262
263
void request_particles_process(real_t p_requested_process_time);
264
265
///////////////////
266
267
void set_direction(Vector3 p_direction);
268
Vector3 get_direction() const;
269
270
void set_spread(real_t p_spread);
271
real_t get_spread() const;
272
273
void set_flatness(real_t p_flatness);
274
real_t get_flatness() const;
275
276
void set_param_min(Parameter p_param, real_t p_value);
277
real_t get_param_min(Parameter p_param) const;
278
279
void set_param_max(Parameter p_param, real_t p_value);
280
real_t get_param_max(Parameter p_param) const;
281
282
void set_param_curve(Parameter p_param, const Ref<Curve> &p_curve);
283
Ref<Curve> get_param_curve(Parameter p_param) const;
284
285
void set_color(const Color &p_color);
286
Color get_color() const;
287
288
void set_color_ramp(const Ref<Gradient> &p_ramp);
289
Ref<Gradient> get_color_ramp() const;
290
291
void set_color_initial_ramp(const Ref<Gradient> &p_ramp);
292
Ref<Gradient> get_color_initial_ramp() const;
293
294
void set_particle_flag(ParticleFlags p_particle_flag, bool p_enable);
295
bool get_particle_flag(ParticleFlags p_particle_flag) const;
296
297
void set_emission_shape(EmissionShape p_shape);
298
void set_emission_sphere_radius(real_t p_radius);
299
void set_emission_box_extents(Vector3 p_extents);
300
void set_emission_points(const Vector<Vector3> &p_points);
301
void set_emission_normals(const Vector<Vector3> &p_normals);
302
void set_emission_colors(const Vector<Color> &p_colors);
303
void set_emission_ring_axis(Vector3 p_axis);
304
void set_emission_ring_height(real_t p_height);
305
void set_emission_ring_radius(real_t p_radius);
306
void set_emission_ring_inner_radius(real_t p_radius);
307
void set_emission_ring_cone_angle(real_t p_angle);
308
void set_scale_curve_x(Ref<Curve> p_scale_curve);
309
void set_scale_curve_y(Ref<Curve> p_scale_curve);
310
void set_scale_curve_z(Ref<Curve> p_scale_curve);
311
void set_split_scale(bool p_split_scale);
312
313
EmissionShape get_emission_shape() const;
314
real_t get_emission_sphere_radius() const;
315
Vector3 get_emission_box_extents() const;
316
Vector<Vector3> get_emission_points() const;
317
Vector<Vector3> get_emission_normals() const;
318
Vector<Color> get_emission_colors() const;
319
Vector3 get_emission_ring_axis() const;
320
real_t get_emission_ring_height() const;
321
real_t get_emission_ring_radius() const;
322
real_t get_emission_ring_inner_radius() const;
323
real_t get_emission_ring_cone_angle() const;
324
Ref<Curve> get_scale_curve_x() const;
325
Ref<Curve> get_scale_curve_y() const;
326
Ref<Curve> get_scale_curve_z() const;
327
bool get_split_scale();
328
329
void set_gravity(const Vector3 &p_gravity);
330
Vector3 get_gravity() const;
331
332
PackedStringArray get_configuration_warnings() const override;
333
334
void restart(bool p_keep_seed = false);
335
336
void convert_from_particles(Node *p_particles);
337
338
AABB capture_aabb() const;
339
340
CPUParticles3D();
341
~CPUParticles3D();
342
};
343
344
VARIANT_ENUM_CAST(CPUParticles3D::DrawOrder)
345
VARIANT_ENUM_CAST(CPUParticles3D::Parameter)
346
VARIANT_ENUM_CAST(CPUParticles3D::ParticleFlags)
347
VARIANT_ENUM_CAST(CPUParticles3D::EmissionShape)
348
349