Skip to content

Commit

Permalink
Merge pull request #53852 from Calinou/particles-allow-negative-scale
Browse files Browse the repository at this point in the history
Allow negative scale in Particles and CPUParticles
  • Loading branch information
akien-mga authored Aug 5, 2022
2 parents 144f3ec + c4a3560 commit 906a693
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 15 deletions.
2 changes: 1 addition & 1 deletion doc/classes/CPUParticles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@
Emission lifetime randomness ratio.
</member>
<member name="scale_amount" type="float" setter="set_param" getter="get_param" default="1.0">
Initial scale applied to each particle.
Initial scale applied to each particle. This can be set to a negative value to flip the particle on all axes.
</member>
<member name="scale_amount_curve" type="Curve" setter="set_param_curve" getter="get_param_curve">
Each particle's scale will vary along this [Curve].
Expand Down
2 changes: 1 addition & 1 deletion doc/classes/CPUParticles2D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@
Emission lifetime randomness ratio.
</member>
<member name="scale_amount" type="float" setter="set_param" getter="get_param" default="1.0">
Initial scale applied to each particle.
Initial scale applied to each particle. This can be set to a negative value to flip the particle on both axes.
</member>
<member name="scale_amount_curve" type="Curve" setter="set_param_curve" getter="get_param_curve">
Each particle's scale will vary along this [Curve].
Expand Down
2 changes: 1 addition & 1 deletion doc/classes/ParticlesMaterial.xml
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@
Radial acceleration randomness ratio.
</member>
<member name="scale" type="float" setter="set_param" getter="get_param" default="1.0">
Initial scale applied to each particle.
Initial scale applied to each particle. This can be set to a negative value to flip the particle on all axes.
</member>
<member name="scale_curve" type="Texture" setter="set_param_texture" getter="get_param_texture">
Each particle's scale will vary along this [CurveTexture].
Expand Down
7 changes: 3 additions & 4 deletions scene/2d/cpu_particles_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -925,9 +925,8 @@ void CPUParticles2D::_particles_process(float p_delta) {

//scale by scale
float base_scale = tex_scale * Math::lerp(parameters[PARAM_SCALE], 1.0f, p.scale_rand * randomness[PARAM_SCALE]);
if (base_scale < 0.000001) {
base_scale = 0.000001;
}
// Prevent zero scale (which can cause rendering issues).
base_scale = SGN(base_scale) * MAX(Math::abs(base_scale), 0.000001);

p.transform.elements[0] *= base_scale;
p.transform.elements[1] *= base_scale;
Expand Down Expand Up @@ -1347,7 +1346,7 @@ void CPUParticles2D::_bind_methods() {
ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angle_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_ANGLE);
ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "angle_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANGLE);
ADD_GROUP("Scale", "");
ADD_PROPERTYI(PropertyInfo(Variant::REAL, "scale_amount", PROPERTY_HINT_RANGE, "0,1000,0.01,or_greater"), "set_param", "get_param", PARAM_SCALE);
ADD_PROPERTYI(PropertyInfo(Variant::REAL, "scale_amount", PROPERTY_HINT_RANGE, "-1000,1000,0.01,or_greater"), "set_param", "get_param", PARAM_SCALE);
ADD_PROPERTYI(PropertyInfo(Variant::REAL, "scale_amount_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_SCALE);
ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "scale_amount_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_SCALE);
ADD_GROUP("Color", "");
Expand Down
7 changes: 3 additions & 4 deletions scene/3d/cpu_particles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -994,9 +994,8 @@ void CPUParticles::_particles_process(float p_delta) {

//scale by scale
float base_scale = tex_scale * Math::lerp(parameters[PARAM_SCALE], 1.0f, p.scale_rand * randomness[PARAM_SCALE]);
if (base_scale < 0.000001) {
base_scale = 0.000001;
}
// Prevent zero scale (which can cause rendering issues).
base_scale = SGN(base_scale) * MAX(Math::abs(base_scale), 0.000001);

p.transform.basis.scale(Vector3(1, 1, 1) * base_scale);

Expand Down Expand Up @@ -1560,7 +1559,7 @@ void CPUParticles::_bind_methods() {
ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angle_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_ANGLE);
ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "angle_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANGLE);
ADD_GROUP("Scale", "");
ADD_PROPERTYI(PropertyInfo(Variant::REAL, "scale_amount", PROPERTY_HINT_RANGE, "0,1000,0.01,or_greater"), "set_param", "get_param", PARAM_SCALE);
ADD_PROPERTYI(PropertyInfo(Variant::REAL, "scale_amount", PROPERTY_HINT_RANGE, "-1000,1000,0.01,or_greater"), "set_param", "get_param", PARAM_SCALE);
ADD_PROPERTYI(PropertyInfo(Variant::REAL, "scale_amount_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_SCALE);
ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "scale_amount_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_SCALE);
ADD_GROUP("Color", "");
Expand Down
7 changes: 3 additions & 4 deletions scene/resources/particles_material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -659,9 +659,8 @@ void ParticlesMaterial::_update_shader() {
}
//scale by scale
code += " float base_scale = tex_scale * mix(scale, 1.0, scale_random * scale_rand);\n";
code += " if (base_scale < 0.000001) {\n";
code += " base_scale = 0.000001;\n";
code += " }\n";
// Prevent zero scale (which can cause rendering issues).
code += " base_scale = sign(base_scale) * max(abs(base_scale), 0.000001);\n";
if (trail_size_modifier.is_valid()) {
code += " if (trail_divisor > 1) {\n";
code += " base_scale *= textureLod(trail_size_modifier, vec2(float(int(NUMBER) % trail_divisor) / float(trail_divisor - 1), 0.0), 0.0).r;\n";
Expand Down Expand Up @@ -1307,7 +1306,7 @@ void ParticlesMaterial::_bind_methods() {
ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angle_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_ANGLE);
ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "angle_curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture"), "set_param_texture", "get_param_texture", PARAM_ANGLE);
ADD_GROUP("Scale", "");
ADD_PROPERTYI(PropertyInfo(Variant::REAL, "scale", PROPERTY_HINT_RANGE, "0,1000,0.01,or_greater"), "set_param", "get_param", PARAM_SCALE);
ADD_PROPERTYI(PropertyInfo(Variant::REAL, "scale", PROPERTY_HINT_RANGE, "-1000,1000,0.01,or_greater"), "set_param", "get_param", PARAM_SCALE);
ADD_PROPERTYI(PropertyInfo(Variant::REAL, "scale_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_SCALE);
ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "scale_curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture"), "set_param_texture", "get_param_texture", PARAM_SCALE);
ADD_GROUP("Color", "");
Expand Down

0 comments on commit 906a693

Please sign in to comment.