Allow negative scale in Particles and CPUParticles

This can be used to make 2D particles look more 3D-y with thoughtful use
of scale curve animation.
This commit is contained in:
Hugo Locurcio 2021-10-15 18:35:33 +02:00
parent c15143e316
commit c4a3560884
No known key found for this signature in database
GPG Key ID: 39E8F8BE30B0A49C
6 changed files with 12 additions and 15 deletions

View File

@ -269,7 +269,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].

View File

@ -249,7 +249,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].

View File

@ -224,7 +224,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].

View File

@ -910,9 +910,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;
@ -1322,7 +1321,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", "");

View File

@ -1010,9 +1010,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);
@ -1463,7 +1462,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", "");

View File

@ -644,9 +644,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";
@ -1278,7 +1277,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", "");