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