Skip to content

Commit

Permalink
Merge pull request #55788 from RPicster/3.x-particles-random-start-color
Browse files Browse the repository at this point in the history
  • Loading branch information
akien-mga committed Dec 10, 2021
2 parents c6a9a38 + 6dd593d commit fc7528b
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 2 deletions.
3 changes: 3 additions & 0 deletions doc/classes/CPUParticles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@
<member name="color" type="Color" setter="set_color" getter="get_color" default="Color( 1, 1, 1, 1 )">
Each particle's initial color. To have particle display color in a [SpatialMaterial] make sure to set [member SpatialMaterial.vertex_color_use_as_albedo] to [code]true[/code].
</member>
<member name="color_initial_ramp" type="Gradient" setter="set_color_initial_ramp" getter="get_color_initial_ramp">
Each particle's initial color will vary along this [GradientTexture] (multiplied with [member color]).
</member>
<member name="color_ramp" type="Gradient" setter="set_color_ramp" getter="get_color_ramp">
Each particle's color will vary along this [GradientTexture] over its lifetime (multiplied with [member color]).
</member>
Expand Down
3 changes: 3 additions & 0 deletions doc/classes/CPUParticles2D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@
<member name="color" type="Color" setter="set_color" getter="get_color" default="Color( 1, 1, 1, 1 )">
Each particle's initial color. If [member texture] is defined, it will be multiplied by this color.
</member>
<member name="color_initial_ramp" type="Gradient" setter="set_color_initial_ramp" getter="get_color_initial_ramp">
Each particle's initial color will vary along this [GradientTexture] (multiplied with [member color]).
</member>
<member name="color_ramp" type="Gradient" setter="set_color_ramp" getter="get_color_ramp">
Each particle's color will vary along this [Gradient] (multiplied with [member color]).
</member>
Expand Down
3 changes: 3 additions & 0 deletions doc/classes/ParticlesMaterial.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@
<member name="color" type="Color" setter="set_color" getter="get_color" default="Color( 1, 1, 1, 1 )">
Each particle's initial color. If the [Particles2D]'s [code]texture[/code] is defined, it will be multiplied by this color. To have particle display color in a [SpatialMaterial] make sure to set [member SpatialMaterial.vertex_color_use_as_albedo] to [code]true[/code].
</member>
<member name="color_initial_ramp" type="Texture" setter="set_color_initial_ramp" getter="get_color_initial_ramp">
Each particle's initial color will vary along this [GradientTexture] (multiplied with [member color]).
</member>
<member name="color_ramp" type="Texture" setter="set_color_ramp" getter="get_color_ramp">
Each particle's color will vary along this [GradientTexture] over its lifetime (multiplied with [member color]).
</member>
Expand Down
25 changes: 24 additions & 1 deletion scene/2d/cpu_particles_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,14 @@ Ref<Gradient> CPUParticles2D::get_color_ramp() const {
return color_ramp;
}

void CPUParticles2D::set_color_initial_ramp(const Ref<Gradient> &p_ramp) {
color_initial_ramp = p_ramp;
}

Ref<Gradient> CPUParticles2D::get_color_initial_ramp() const {
return color_initial_ramp;
}

void CPUParticles2D::set_particle_flag(Flags p_flag, bool p_enable) {
ERR_FAIL_INDEX(p_flag, FLAG_MAX);
flags[p_flag] = p_enable;
Expand Down Expand Up @@ -690,6 +698,12 @@ void CPUParticles2D::_particles_process(float p_delta) {
p.hue_rot_rand = Math::randf();
p.anim_offset_rand = Math::randf();

if (color_initial_ramp.is_valid()) {
p.start_color_rand = color_initial_ramp->get_color_at_offset(Math::randf());
} else {
p.start_color_rand = Color(1, 1, 1, 1);
}

float angle1_rad = Math::atan2(direction.y, direction.x) + (Math::randf() * 2.0 - 1.0) * Math_PI * spread / 180.0;
Vector2 rot = Vector2(Math::cos(angle1_rad), Math::sin(angle1_rad));
p.velocity = rot * parameters[PARAM_INITIAL_LINEAR_VELOCITY] * Math::lerp(1.0f, float(Math::randf()), randomness[PARAM_INITIAL_LINEAR_VELOCITY]);
Expand Down Expand Up @@ -895,7 +909,7 @@ void CPUParticles2D::_particles_process(float p_delta) {
p.color.g = color_rgb.y;
p.color.b = color_rgb.z;

p.color *= p.base_color;
p.color *= p.base_color * p.start_color_rand;

if (flags[FLAG_ALIGN_Y_TO_VELOCITY]) {
if (p.velocity.length() > 0.0) {
Expand Down Expand Up @@ -1127,6 +1141,11 @@ void CPUParticles2D::convert_from_particles(Node *p_particles) {
set_color_ramp(gt->get_gradient());
}

Ref<GradientTexture> gti = material->get_color_initial_ramp();
if (gti.is_valid()) {
set_color_initial_ramp(gti->get_gradient());
}

set_particle_flag(FLAG_ALIGN_Y_TO_VELOCITY, material->get_flag(ParticlesMaterial::FLAG_ALIGN_Y_TO_VELOCITY));

set_emission_shape(EmissionShape(material->get_emission_shape()));
Expand Down Expand Up @@ -1247,6 +1266,9 @@ void CPUParticles2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_color_ramp", "ramp"), &CPUParticles2D::set_color_ramp);
ClassDB::bind_method(D_METHOD("get_color_ramp"), &CPUParticles2D::get_color_ramp);

ClassDB::bind_method(D_METHOD("set_color_initial_ramp", "ramp"), &CPUParticles2D::set_color_initial_ramp);
ClassDB::bind_method(D_METHOD("get_color_initial_ramp"), &CPUParticles2D::get_color_initial_ramp);

ClassDB::bind_method(D_METHOD("set_particle_flag", "flag", "enable"), &CPUParticles2D::set_particle_flag);
ClassDB::bind_method(D_METHOD("get_particle_flag", "flag"), &CPUParticles2D::get_particle_flag);

Expand Down Expand Up @@ -1328,6 +1350,7 @@ void CPUParticles2D::_bind_methods() {
ADD_GROUP("Color", "");
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_ramp", PROPERTY_HINT_RESOURCE_TYPE, "Gradient"), "set_color_ramp", "get_color_ramp");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_initial_ramp", PROPERTY_HINT_RESOURCE_TYPE, "Gradient"), "set_color_initial_ramp", "get_color_initial_ramp");

ADD_GROUP("Hue Variation", "hue_");
ADD_PROPERTYI(PropertyInfo(Variant::REAL, "hue_variation", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_param", "get_param", PARAM_HUE_VARIATION);
Expand Down
5 changes: 5 additions & 0 deletions scene/2d/cpu_particles_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class CPUParticles2D : public Node2D {
float scale_rand;
float hue_rot_rand;
float anim_offset_rand;
Color start_color_rand;
float time;
float lifetime;
Color base_color;
Expand Down Expand Up @@ -162,6 +163,7 @@ class CPUParticles2D : public Node2D {
Ref<Curve> curve_parameters[PARAM_MAX];
Color color;
Ref<Gradient> color_ramp;
Ref<Gradient> color_initial_ramp;

bool flags[FLAG_MAX];

Expand Down Expand Up @@ -255,6 +257,9 @@ class CPUParticles2D : public Node2D {
void set_color_ramp(const Ref<Gradient> &p_ramp);
Ref<Gradient> get_color_ramp() const;

void set_color_initial_ramp(const Ref<Gradient> &p_ramp);
Ref<Gradient> get_color_initial_ramp() const;

void set_particle_flag(Flags p_flag, bool p_enable);
bool get_particle_flag(Flags p_flag) const;

Expand Down
25 changes: 24 additions & 1 deletion scene/3d/cpu_particles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,14 @@ Ref<Gradient> CPUParticles::get_color_ramp() const {
return color_ramp;
}

void CPUParticles::set_color_initial_ramp(const Ref<Gradient> &p_ramp) {
color_initial_ramp = p_ramp;
}

Ref<Gradient> CPUParticles::get_color_initial_ramp() const {
return color_initial_ramp;
}

void CPUParticles::set_particle_flag(Flags p_flag, bool p_enable) {
ERR_FAIL_INDEX(p_flag, FLAG_MAX);
flags[p_flag] = p_enable;
Expand Down Expand Up @@ -687,6 +695,12 @@ void CPUParticles::_particles_process(float p_delta) {
p.hue_rot_rand = Math::randf();
p.anim_offset_rand = Math::randf();

if (color_initial_ramp.is_valid()) {
p.start_color_rand = color_initial_ramp->get_color_at_offset(Math::randf());
} else {
p.start_color_rand = Color(1, 1, 1, 1);
}

if (flags[FLAG_DISABLE_Z]) {
float angle1_rad = Math::atan2(direction.y, direction.x) + (Math::randf() * 2.0 - 1.0) * Math_PI * spread / 180.0;
Vector3 rot = Vector3(Math::cos(angle1_rad), Math::sin(angle1_rad), 0.0);
Expand Down Expand Up @@ -963,7 +977,7 @@ void CPUParticles::_particles_process(float p_delta) {
p.color.g = color_rgb.y;
p.color.b = color_rgb.z;

p.color *= p.base_color;
p.color *= p.base_color * p.start_color_rand;

if (flags[FLAG_DISABLE_Z]) {
if (flags[FLAG_ALIGN_Y_TO_VELOCITY]) {
Expand Down Expand Up @@ -1245,6 +1259,11 @@ void CPUParticles::convert_from_particles(Node *p_particles) {
set_color_ramp(gt->get_gradient());
}

Ref<GradientTexture> gti = material->get_color_initial_ramp();
if (gti.is_valid()) {
set_color_initial_ramp(gti->get_gradient());
}

set_particle_flag(FLAG_ALIGN_Y_TO_VELOCITY, material->get_flag(ParticlesMaterial::FLAG_ALIGN_Y_TO_VELOCITY));
set_particle_flag(FLAG_ROTATE_Y, material->get_flag(ParticlesMaterial::FLAG_ROTATE_Y));
set_particle_flag(FLAG_DISABLE_Z, material->get_flag(ParticlesMaterial::FLAG_DISABLE_Z));
Expand Down Expand Up @@ -1368,6 +1387,9 @@ void CPUParticles::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_color_ramp", "ramp"), &CPUParticles::set_color_ramp);
ClassDB::bind_method(D_METHOD("get_color_ramp"), &CPUParticles::get_color_ramp);

ClassDB::bind_method(D_METHOD("set_color_initial_ramp", "ramp"), &CPUParticles::set_color_initial_ramp);
ClassDB::bind_method(D_METHOD("get_color_initial_ramp"), &CPUParticles::get_color_initial_ramp);

ClassDB::bind_method(D_METHOD("set_particle_flag", "flag", "enable"), &CPUParticles::set_particle_flag);
ClassDB::bind_method(D_METHOD("get_particle_flag", "flag"), &CPUParticles::get_particle_flag);

Expand Down Expand Up @@ -1468,6 +1490,7 @@ void CPUParticles::_bind_methods() {
ADD_GROUP("Color", "");
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_ramp", PROPERTY_HINT_RESOURCE_TYPE, "Gradient"), "set_color_ramp", "get_color_ramp");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_initial_ramp", PROPERTY_HINT_RESOURCE_TYPE, "Gradient"), "set_color_initial_ramp", "get_color_initial_ramp");

ADD_GROUP("Hue Variation", "hue_");
ADD_PROPERTYI(PropertyInfo(Variant::REAL, "hue_variation", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_param", "get_param", PARAM_HUE_VARIATION);
Expand Down
5 changes: 5 additions & 0 deletions scene/3d/cpu_particles.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class CPUParticles : public GeometryInstance {
float scale_rand;
float hue_rot_rand;
float anim_offset_rand;
Color start_color_rand;
float time;
float lifetime;
Color base_color;
Expand Down Expand Up @@ -163,6 +164,7 @@ class CPUParticles : public GeometryInstance {
Ref<Curve> curve_parameters[PARAM_MAX];
Color color;
Ref<Gradient> color_ramp;
Ref<Gradient> color_initial_ramp;

bool flags[FLAG_MAX];

Expand Down Expand Up @@ -259,6 +261,9 @@ class CPUParticles : public GeometryInstance {
void set_color_ramp(const Ref<Gradient> &p_ramp);
Ref<Gradient> get_color_ramp() const;

void set_color_initial_ramp(const Ref<Gradient> &p_ramp);
Ref<Gradient> get_color_initial_ramp() const;

void set_particle_flag(Flags p_flag, bool p_enable);
bool get_particle_flag(Flags p_flag) const;

Expand Down
29 changes: 29 additions & 0 deletions scene/resources/particles_material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ void ParticlesMaterial::init_shaders() {

shader_names->color = "color_value";
shader_names->color_ramp = "color_ramp";
shader_names->color_initial_ramp = "color_initial_ramp";

shader_names->emission_sphere_radius = "emission_sphere_radius";
shader_names->emission_box_extents = "emission_box_extents";
Expand Down Expand Up @@ -217,6 +218,10 @@ void ParticlesMaterial::_update_shader() {
code += "uniform sampler2D color_ramp;\n";
}

if (color_initial_ramp.is_valid()) {
code += "uniform sampler2D color_initial_ramp;\n";
}

if (tex_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid()) {
code += "uniform sampler2D linear_velocity_texture;\n";
}
Expand Down Expand Up @@ -299,6 +304,9 @@ void ParticlesMaterial::_update_shader() {
code += " float scale_rand = rand_from_seed(alt_seed);\n";
code += " float hue_rot_rand = rand_from_seed(alt_seed);\n";
code += " float anim_offset_rand = rand_from_seed(alt_seed);\n";
if (color_initial_ramp.is_valid()) {
code += " float color_initial_rand = rand_from_seed(alt_seed);\n";
}
code += " float pi = 3.14159;\n";
code += " float degree_to_rad = pi / 180.0;\n";
code += "\n";
Expand Down Expand Up @@ -593,6 +601,12 @@ void ParticlesMaterial::_update_shader() {
} else {
code += " COLOR = hue_rot_mat * color_value;\n";
}

if (color_initial_ramp.is_valid()) {
code += " vec4 start_color = textureLod(color_initial_ramp, vec2(color_initial_rand, 0.0), 0.0);\n";
code += " COLOR *= start_color;\n";
}

if (emission_color_texture.is_valid() && (emission_shape == EMISSION_SHAPE_POINTS || emission_shape == EMISSION_SHAPE_DIRECTED_POINTS)) {
code += " COLOR *= texelFetch(emission_texture_color, emission_tex_ofs, 0);\n";
}
Expand Down Expand Up @@ -933,6 +947,17 @@ Ref<Texture> ParticlesMaterial::get_color_ramp() const {
return color_ramp;
}

void ParticlesMaterial::set_color_initial_ramp(const Ref<Texture> &p_texture) {
color_initial_ramp = p_texture;
VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->color_initial_ramp, p_texture);
_queue_shader_change();
_change_notify();
}

Ref<Texture> ParticlesMaterial::get_color_initial_ramp() const {
return color_initial_ramp;
}

void ParticlesMaterial::set_flag(Flags p_flag, bool p_enable) {
ERR_FAIL_INDEX(p_flag, FLAG_MAX);
flags[p_flag] = p_enable;
Expand Down Expand Up @@ -1167,6 +1192,9 @@ void ParticlesMaterial::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_color_ramp", "ramp"), &ParticlesMaterial::set_color_ramp);
ClassDB::bind_method(D_METHOD("get_color_ramp"), &ParticlesMaterial::get_color_ramp);

ClassDB::bind_method(D_METHOD("set_color_initial_ramp", "ramp"), &ParticlesMaterial::set_color_initial_ramp);
ClassDB::bind_method(D_METHOD("get_color_initial_ramp"), &ParticlesMaterial::get_color_initial_ramp);

ClassDB::bind_method(D_METHOD("set_flag", "flag", "enable"), &ParticlesMaterial::set_flag);
ClassDB::bind_method(D_METHOD("get_flag", "flag"), &ParticlesMaterial::get_flag);

Expand Down Expand Up @@ -1285,6 +1313,7 @@ void ParticlesMaterial::_bind_methods() {
ADD_GROUP("Color", "");
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_ramp", PROPERTY_HINT_RESOURCE_TYPE, "GradientTexture"), "set_color_ramp", "get_color_ramp");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_initial_ramp", PROPERTY_HINT_RESOURCE_TYPE, "GradientTexture"), "set_color_initial_ramp", "get_color_initial_ramp");

ADD_GROUP("Hue Variation", "hue_");
ADD_PROPERTYI(PropertyInfo(Variant::REAL, "hue_variation", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_param", "get_param", PARAM_HUE_VARIATION);
Expand Down
7 changes: 7 additions & 0 deletions scene/resources/particles_material.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class ParticlesMaterial : public Material {
struct {
uint32_t texture_mask : 16;
uint32_t texture_color : 1;
uint32_t texture_initial_color : 1;
uint32_t flags : 4;
uint32_t emission_shape : 3;
uint32_t trail_size_texture : 1;
Expand Down Expand Up @@ -119,6 +120,7 @@ class ParticlesMaterial : public Material {
}

mk.texture_color = color_ramp.is_valid() ? 1 : 0;
mk.texture_initial_color = color_initial_ramp.is_valid() ? 1 : 0;
mk.emission_shape = emission_shape;
mk.trail_color_texture = trail_color_modifier.is_valid() ? 1 : 0;
mk.trail_size_texture = trail_size_modifier.is_valid() ? 1 : 0;
Expand Down Expand Up @@ -174,6 +176,7 @@ class ParticlesMaterial : public Material {

StringName color;
StringName color_ramp;
StringName color_initial_ramp;

StringName emission_sphere_radius;
StringName emission_box_extents;
Expand Down Expand Up @@ -214,6 +217,7 @@ class ParticlesMaterial : public Material {
Ref<Texture> tex_parameters[PARAM_MAX];
Color color;
Ref<Texture> color_ramp;
Ref<Texture> color_initial_ramp;

bool flags[FLAG_MAX];

Expand Down Expand Up @@ -271,6 +275,9 @@ class ParticlesMaterial : public Material {
void set_color_ramp(const Ref<Texture> &p_texture);
Ref<Texture> get_color_ramp() const;

void set_color_initial_ramp(const Ref<Texture> &p_texture);
Ref<Texture> get_color_initial_ramp() const;

void set_flag(Flags p_flag, bool p_enable);
bool get_flag(Flags p_flag) const;

Expand Down

0 comments on commit fc7528b

Please sign in to comment.