2018-08-29 20:38:13 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* cpu_particles.h */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2022-01-13 08:45:09 +00:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2018-08-29 20:38:13 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
2018-07-06 23:21:13 +00:00
|
|
|
#ifndef CPU_PARTICLES_H
|
|
|
|
#define CPU_PARTICLES_H
|
2018-09-04 09:30:04 +00:00
|
|
|
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/rid.h"
|
2021-01-31 12:34:42 +00:00
|
|
|
#include "core/safe_refcount.h"
|
2018-07-06 23:21:13 +00:00
|
|
|
#include "scene/3d/visual_instance.h"
|
|
|
|
|
|
|
|
class CPUParticles : public GeometryInstance {
|
|
|
|
private:
|
|
|
|
GDCLASS(CPUParticles, GeometryInstance);
|
|
|
|
|
|
|
|
public:
|
|
|
|
enum DrawOrder {
|
|
|
|
DRAW_ORDER_INDEX,
|
|
|
|
DRAW_ORDER_LIFETIME,
|
|
|
|
DRAW_ORDER_VIEW_DEPTH,
|
2021-02-17 14:30:03 +00:00
|
|
|
DRAW_ORDER_MAX
|
2018-07-06 23:21:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum Parameter {
|
|
|
|
|
|
|
|
PARAM_INITIAL_LINEAR_VELOCITY,
|
|
|
|
PARAM_ANGULAR_VELOCITY,
|
2019-06-11 15:47:24 +00:00
|
|
|
PARAM_ORBIT_VELOCITY,
|
2018-07-06 23:21:13 +00:00
|
|
|
PARAM_LINEAR_ACCEL,
|
|
|
|
PARAM_RADIAL_ACCEL,
|
|
|
|
PARAM_TANGENTIAL_ACCEL,
|
|
|
|
PARAM_DAMPING,
|
|
|
|
PARAM_ANGLE,
|
|
|
|
PARAM_SCALE,
|
|
|
|
PARAM_HUE_VARIATION,
|
|
|
|
PARAM_ANIM_SPEED,
|
|
|
|
PARAM_ANIM_OFFSET,
|
|
|
|
PARAM_MAX
|
|
|
|
};
|
|
|
|
|
|
|
|
enum Flags {
|
|
|
|
FLAG_ALIGN_Y_TO_VELOCITY,
|
|
|
|
FLAG_ROTATE_Y,
|
|
|
|
FLAG_DISABLE_Z,
|
|
|
|
FLAG_MAX
|
|
|
|
};
|
|
|
|
|
|
|
|
enum EmissionShape {
|
|
|
|
EMISSION_SHAPE_POINT,
|
|
|
|
EMISSION_SHAPE_SPHERE,
|
|
|
|
EMISSION_SHAPE_BOX,
|
|
|
|
EMISSION_SHAPE_POINTS,
|
|
|
|
EMISSION_SHAPE_DIRECTED_POINTS,
|
2021-04-11 16:08:28 +00:00
|
|
|
EMISSION_SHAPE_RING,
|
2020-01-16 09:59:01 +00:00
|
|
|
EMISSION_SHAPE_MAX
|
2018-07-06 23:21:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool emitting;
|
|
|
|
|
2021-09-15 11:30:45 +00:00
|
|
|
// Previous minimal data for the particle,
|
|
|
|
// for interpolation.
|
|
|
|
struct ParticleBase {
|
|
|
|
void blank() {
|
|
|
|
for (int n = 0; n < 4; n++) {
|
|
|
|
custom[n] = 0.0;
|
|
|
|
}
|
|
|
|
}
|
2018-07-06 23:21:13 +00:00
|
|
|
Transform transform;
|
|
|
|
Color color;
|
|
|
|
float custom[4];
|
2021-09-15 11:30:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Particle : public ParticleBase {
|
|
|
|
void copy_to(ParticleBase &r_o) {
|
|
|
|
r_o.transform = transform;
|
|
|
|
r_o.color = color;
|
|
|
|
memcpy(r_o.custom, custom, sizeof(custom));
|
|
|
|
}
|
2018-07-06 23:21:13 +00:00
|
|
|
Vector3 velocity;
|
|
|
|
bool active;
|
|
|
|
float angle_rand;
|
|
|
|
float scale_rand;
|
|
|
|
float hue_rot_rand;
|
|
|
|
float anim_offset_rand;
|
2021-12-10 15:35:25 +00:00
|
|
|
Color start_color_rand;
|
2018-07-06 23:21:13 +00:00
|
|
|
float time;
|
2019-07-15 04:48:20 +00:00
|
|
|
float lifetime;
|
2018-07-06 23:21:13 +00:00
|
|
|
Color base_color;
|
|
|
|
|
|
|
|
uint32_t seed;
|
|
|
|
};
|
|
|
|
|
|
|
|
float time;
|
|
|
|
float inactive_time;
|
|
|
|
float frame_remainder;
|
|
|
|
int cycle;
|
2019-03-09 01:50:44 +00:00
|
|
|
bool redraw;
|
2018-07-06 23:21:13 +00:00
|
|
|
|
|
|
|
RID multimesh;
|
|
|
|
|
|
|
|
PoolVector<Particle> particles;
|
2021-09-15 11:30:45 +00:00
|
|
|
LocalVector<ParticleBase> particles_prev;
|
2018-07-06 23:21:13 +00:00
|
|
|
PoolVector<float> particle_data;
|
2021-09-15 11:30:45 +00:00
|
|
|
PoolVector<float> particle_data_prev;
|
2018-07-06 23:21:13 +00:00
|
|
|
PoolVector<int> particle_order;
|
|
|
|
|
|
|
|
struct SortLifetime {
|
|
|
|
const Particle *particles;
|
|
|
|
|
|
|
|
bool operator()(int p_a, int p_b) const {
|
2019-06-14 20:22:19 +00:00
|
|
|
return particles[p_a].time > particles[p_b].time;
|
2018-07-06 23:21:13 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SortAxis {
|
|
|
|
const Particle *particles;
|
|
|
|
Vector3 axis;
|
|
|
|
bool operator()(int p_a, int p_b) const {
|
|
|
|
return axis.dot(particles[p_a].transform.origin) < axis.dot(particles[p_b].transform.origin);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
bool one_shot;
|
|
|
|
|
|
|
|
float lifetime;
|
|
|
|
float pre_process_time;
|
|
|
|
float explosiveness_ratio;
|
|
|
|
float randomness_ratio;
|
2019-07-15 04:48:20 +00:00
|
|
|
float lifetime_randomness;
|
2018-07-06 23:21:13 +00:00
|
|
|
float speed_scale;
|
|
|
|
bool local_coords;
|
|
|
|
int fixed_fps;
|
|
|
|
bool fractional_delta;
|
|
|
|
|
2021-01-31 12:34:42 +00:00
|
|
|
SafeFlag can_update;
|
2018-10-08 03:51:48 +00:00
|
|
|
|
2018-07-06 23:21:13 +00:00
|
|
|
DrawOrder draw_order;
|
|
|
|
|
|
|
|
Ref<Mesh> mesh;
|
|
|
|
|
|
|
|
////////
|
|
|
|
|
2019-03-04 22:45:53 +00:00
|
|
|
Vector3 direction;
|
2018-07-06 23:21:13 +00:00
|
|
|
float spread;
|
|
|
|
float flatness;
|
|
|
|
|
|
|
|
float parameters[PARAM_MAX];
|
|
|
|
float randomness[PARAM_MAX];
|
|
|
|
|
|
|
|
Ref<Curve> curve_parameters[PARAM_MAX];
|
|
|
|
Color color;
|
|
|
|
Ref<Gradient> color_ramp;
|
2021-12-10 15:35:25 +00:00
|
|
|
Ref<Gradient> color_initial_ramp;
|
2018-07-06 23:21:13 +00:00
|
|
|
|
|
|
|
bool flags[FLAG_MAX];
|
|
|
|
|
|
|
|
EmissionShape emission_shape;
|
|
|
|
float emission_sphere_radius;
|
|
|
|
Vector3 emission_box_extents;
|
|
|
|
PoolVector<Vector3> emission_points;
|
|
|
|
PoolVector<Vector3> emission_normals;
|
|
|
|
PoolVector<Color> emission_colors;
|
|
|
|
int emission_point_count;
|
2021-04-11 16:08:28 +00:00
|
|
|
float emission_ring_height;
|
|
|
|
float emission_ring_inner_radius;
|
|
|
|
float emission_ring_radius;
|
|
|
|
Vector3 emission_ring_axis;
|
2018-07-06 23:21:13 +00:00
|
|
|
|
|
|
|
Vector3 gravity;
|
|
|
|
|
2021-09-15 11:30:45 +00:00
|
|
|
void _update_internal(bool p_on_physics_tick);
|
2018-07-06 23:21:13 +00:00
|
|
|
void _particles_process(float p_delta);
|
2021-09-15 11:30:45 +00:00
|
|
|
void _particle_process(Particle &r_p, const Transform &p_emission_xform, float p_local_delta, float &r_tv);
|
2018-07-06 23:21:13 +00:00
|
|
|
void _update_particle_data_buffer();
|
|
|
|
|
2021-01-27 09:43:02 +00:00
|
|
|
Mutex update_mutex;
|
2021-09-15 11:30:45 +00:00
|
|
|
bool _interpolated = false;
|
|
|
|
|
|
|
|
// Hard coded to true for now, if we decide after testing to always enable this
|
|
|
|
// when using interpolation we can remove the variable, else we can expose to the UI.
|
|
|
|
bool _streaky = true;
|
2018-07-06 23:21:13 +00:00
|
|
|
|
|
|
|
void _update_render_thread();
|
|
|
|
|
2019-03-09 01:50:44 +00:00
|
|
|
void _set_redraw(bool p_redraw);
|
2021-09-15 11:30:45 +00:00
|
|
|
void _refresh_interpolation_state();
|
|
|
|
|
|
|
|
void _fill_particle_data(const ParticleBase &p_source, float *r_dest, bool p_active) const {
|
|
|
|
const Transform &t = p_source.transform;
|
|
|
|
|
|
|
|
if (p_active) {
|
|
|
|
r_dest[0] = t.basis.elements[0][0];
|
|
|
|
r_dest[1] = t.basis.elements[0][1];
|
|
|
|
r_dest[2] = t.basis.elements[0][2];
|
|
|
|
r_dest[3] = t.origin.x;
|
|
|
|
r_dest[4] = t.basis.elements[1][0];
|
|
|
|
r_dest[5] = t.basis.elements[1][1];
|
|
|
|
r_dest[6] = t.basis.elements[1][2];
|
|
|
|
r_dest[7] = t.origin.y;
|
|
|
|
r_dest[8] = t.basis.elements[2][0];
|
|
|
|
r_dest[9] = t.basis.elements[2][1];
|
|
|
|
r_dest[10] = t.basis.elements[2][2];
|
|
|
|
r_dest[11] = t.origin.z;
|
|
|
|
} else {
|
|
|
|
memset(r_dest, 0, sizeof(float) * 12);
|
|
|
|
}
|
|
|
|
|
|
|
|
Color c = p_source.color;
|
|
|
|
uint8_t *data8 = (uint8_t *)&r_dest[12];
|
|
|
|
data8[0] = CLAMP(c.r * 255.0, 0, 255);
|
|
|
|
data8[1] = CLAMP(c.g * 255.0, 0, 255);
|
|
|
|
data8[2] = CLAMP(c.b * 255.0, 0, 255);
|
|
|
|
data8[3] = CLAMP(c.a * 255.0, 0, 255);
|
|
|
|
|
|
|
|
r_dest[13] = p_source.custom[0];
|
|
|
|
r_dest[14] = p_source.custom[1];
|
|
|
|
r_dest[15] = p_source.custom[2];
|
|
|
|
r_dest[16] = p_source.custom[3];
|
|
|
|
}
|
2019-03-09 01:50:44 +00:00
|
|
|
|
2018-07-06 23:21:13 +00:00
|
|
|
protected:
|
|
|
|
static void _bind_methods();
|
|
|
|
void _notification(int p_what);
|
|
|
|
virtual void _validate_property(PropertyInfo &property) const;
|
|
|
|
|
|
|
|
public:
|
|
|
|
AABB get_aabb() const;
|
|
|
|
PoolVector<Face3> get_faces(uint32_t p_usage_flags) const;
|
|
|
|
|
|
|
|
void set_emitting(bool p_emitting);
|
|
|
|
void set_amount(int p_amount);
|
|
|
|
void set_lifetime(float p_lifetime);
|
|
|
|
void set_one_shot(bool p_one_shot);
|
|
|
|
void set_pre_process_time(float p_time);
|
|
|
|
void set_explosiveness_ratio(float p_ratio);
|
|
|
|
void set_randomness_ratio(float p_ratio);
|
2019-07-15 04:48:20 +00:00
|
|
|
void set_lifetime_randomness(float p_random);
|
2018-07-06 23:21:13 +00:00
|
|
|
void set_use_local_coordinates(bool p_enable);
|
|
|
|
void set_speed_scale(float p_scale);
|
|
|
|
|
|
|
|
bool is_emitting() const;
|
|
|
|
int get_amount() const;
|
|
|
|
float get_lifetime() const;
|
|
|
|
bool get_one_shot() const;
|
|
|
|
float get_pre_process_time() const;
|
|
|
|
float get_explosiveness_ratio() const;
|
|
|
|
float get_randomness_ratio() const;
|
2019-07-15 04:48:20 +00:00
|
|
|
float get_lifetime_randomness() const;
|
2018-07-06 23:21:13 +00:00
|
|
|
bool get_use_local_coordinates() const;
|
|
|
|
float get_speed_scale() const;
|
|
|
|
|
|
|
|
void set_fixed_fps(int p_count);
|
|
|
|
int get_fixed_fps() const;
|
|
|
|
|
|
|
|
void set_fractional_delta(bool p_enable);
|
|
|
|
bool get_fractional_delta() const;
|
|
|
|
|
|
|
|
void set_draw_order(DrawOrder p_order);
|
|
|
|
DrawOrder get_draw_order() const;
|
|
|
|
|
|
|
|
void set_mesh(const Ref<Mesh> &p_mesh);
|
|
|
|
Ref<Mesh> get_mesh() const;
|
|
|
|
|
|
|
|
///////////////////
|
|
|
|
|
2019-03-04 22:45:53 +00:00
|
|
|
void set_direction(Vector3 p_direction);
|
|
|
|
Vector3 get_direction() const;
|
|
|
|
|
2018-07-06 23:21:13 +00:00
|
|
|
void set_spread(float p_spread);
|
|
|
|
float get_spread() const;
|
|
|
|
|
|
|
|
void set_flatness(float p_flatness);
|
|
|
|
float get_flatness() const;
|
|
|
|
|
|
|
|
void set_param(Parameter p_param, float p_value);
|
|
|
|
float get_param(Parameter p_param) const;
|
|
|
|
|
|
|
|
void set_param_randomness(Parameter p_param, float p_value);
|
|
|
|
float get_param_randomness(Parameter p_param) const;
|
|
|
|
|
|
|
|
void set_param_curve(Parameter p_param, const Ref<Curve> &p_curve);
|
|
|
|
Ref<Curve> get_param_curve(Parameter p_param) const;
|
|
|
|
|
|
|
|
void set_color(const Color &p_color);
|
|
|
|
Color get_color() const;
|
|
|
|
|
2019-06-26 13:08:25 +00:00
|
|
|
void set_color_ramp(const Ref<Gradient> &p_ramp);
|
2018-07-06 23:21:13 +00:00
|
|
|
Ref<Gradient> get_color_ramp() const;
|
|
|
|
|
2021-12-10 15:35:25 +00:00
|
|
|
void set_color_initial_ramp(const Ref<Gradient> &p_ramp);
|
|
|
|
Ref<Gradient> get_color_initial_ramp() const;
|
|
|
|
|
2018-07-06 23:21:13 +00:00
|
|
|
void set_particle_flag(Flags p_flag, bool p_enable);
|
|
|
|
bool get_particle_flag(Flags p_flag) const;
|
|
|
|
|
|
|
|
void set_emission_shape(EmissionShape p_shape);
|
|
|
|
void set_emission_sphere_radius(float p_radius);
|
|
|
|
void set_emission_box_extents(Vector3 p_extents);
|
|
|
|
void set_emission_points(const PoolVector<Vector3> &p_points);
|
|
|
|
void set_emission_normals(const PoolVector<Vector3> &p_normals);
|
|
|
|
void set_emission_colors(const PoolVector<Color> &p_colors);
|
2021-04-11 16:08:28 +00:00
|
|
|
void set_emission_ring_height(float p_height);
|
|
|
|
void set_emission_ring_inner_radius(float p_inner_radius);
|
|
|
|
void set_emission_ring_radius(float p_radius);
|
|
|
|
void set_emission_ring_axis(Vector3 p_axis);
|
2018-07-06 23:21:13 +00:00
|
|
|
|
|
|
|
EmissionShape get_emission_shape() const;
|
|
|
|
float get_emission_sphere_radius() const;
|
|
|
|
Vector3 get_emission_box_extents() const;
|
|
|
|
PoolVector<Vector3> get_emission_points() const;
|
|
|
|
PoolVector<Vector3> get_emission_normals() const;
|
|
|
|
PoolVector<Color> get_emission_colors() const;
|
2021-04-11 16:08:28 +00:00
|
|
|
float get_emission_ring_height() const;
|
|
|
|
float get_emission_ring_inner_radius() const;
|
|
|
|
float get_emission_ring_radius() const;
|
|
|
|
Vector3 get_emission_ring_axis() const;
|
2018-07-06 23:21:13 +00:00
|
|
|
|
|
|
|
void set_gravity(const Vector3 &p_gravity);
|
|
|
|
Vector3 get_gravity() const;
|
|
|
|
|
|
|
|
virtual String get_configuration_warning() const;
|
|
|
|
|
|
|
|
void restart();
|
|
|
|
|
2018-07-07 12:04:22 +00:00
|
|
|
void convert_from_particles(Node *p_particles);
|
|
|
|
|
2018-07-06 23:21:13 +00:00
|
|
|
CPUParticles();
|
|
|
|
~CPUParticles();
|
|
|
|
};
|
|
|
|
|
|
|
|
VARIANT_ENUM_CAST(CPUParticles::DrawOrder)
|
|
|
|
VARIANT_ENUM_CAST(CPUParticles::Parameter)
|
|
|
|
VARIANT_ENUM_CAST(CPUParticles::Flags)
|
|
|
|
VARIANT_ENUM_CAST(CPUParticles::EmissionShape)
|
|
|
|
|
|
|
|
#endif // CPU_PARTICLES_H
|