Merge pull request #47310 from mortarroad/3.x-fix-particles-material-spread

[3.x] Fix ParticlesMaterial spread.
This commit is contained in:
Rémi Verschelde 2021-05-11 09:10:31 +02:00 committed by GitHub
commit 39826d3a94
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 7 deletions

View file

@ -180,7 +180,7 @@
If [code]true[/code], particles rotate around Y axis by [member angle].
</member>
<member name="flatness" type="float" setter="set_flatness" getter="get_flatness" default="0.0">
Amount of [member spread] in Y/Z plane. A value of [code]1[/code] restricts particles to X/Z plane.
Amount of [member spread] along the Y axis.
</member>
<member name="gravity" type="Vector3" setter="set_gravity" getter="get_gravity" default="Vector3( 0, -9.8, 0 )">
Gravity applied to every particle.
@ -241,7 +241,7 @@
Scale randomness ratio.
</member>
<member name="spread" type="float" setter="set_spread" getter="get_spread" default="45.0">
Each particle's initial direction range from [code]+spread[/code] to [code]-spread[/code] degrees. Applied to X/Z plane and Y/Z planes.
Each particle's initial direction range from [code]+spread[/code] to [code]-spread[/code] degrees.
</member>
<member name="tangential_accel" type="float" setter="set_param" getter="get_param" default="0.0">
Tangential acceleration applied to each particle. Tangential acceleration is perpendicular to the particle's velocity giving the particles a swirling motion.

View file

@ -331,14 +331,21 @@ void ParticlesMaterial::_update_shader() {
//initiate velocity spread in 3D
code += " float angle1_rad = rand_from_seed_m1_p1(alt_seed) * spread_rad;\n";
code += " float angle2_rad = rand_from_seed_m1_p1(alt_seed) * spread_rad * (1.0 - flatness);\n";
code += " angle1_rad += direction.z != 0.0 ? atan(direction.x, direction.z) : sign(direction.x) * (pi / 2.0);\n";
code += " angle2_rad += direction.z != 0.0 ? atan(direction.y, abs(direction.z)) : (direction.x != 0.0 ? atan(direction.y, abs(direction.x)) : sign(direction.y) * (pi / 2.0));\n";
code += " vec3 direction_xz = vec3(sin(angle1_rad), 0.0, cos(angle1_rad));\n";
code += " vec3 direction_yz = vec3(0.0, sin(angle2_rad), cos(angle2_rad));\n";
code += " direction_yz.z = direction_yz.z / max(0.0001,sqrt(abs(direction_yz.z))); // better uniform distribution\n";
code += " vec3 vec_direction = vec3(direction_xz.x * direction_yz.z, direction_yz.y, direction_xz.z * direction_yz.z);\n";
code += " vec_direction = normalize(vec_direction);\n";
code += " VELOCITY = vec_direction * initial_linear_velocity * mix(1.0, rand_from_seed(alt_seed), initial_linear_velocity_random);\n";
code += " vec3 spread_direction = vec3(direction_xz.x * direction_yz.z, direction_yz.y, direction_xz.z * direction_yz.z);\n";
code += " vec3 direction_nrm = normalize(direction);\n";
code += " // rotate spread to direction\n";
code += " vec3 binormal = cross(vec3(0.0, 1.0, 0.0), direction_nrm);\n";
code += " if (length(binormal) < 0.0001) {\n";
code += " // direction is parallel to Y. Choose Z as the binormal.\n";
code += " binormal = vec3(0.0, 0.0, 1.0);\n";
code += " }\n";
code += " binormal = normalize(binormal);\n";
code += " vec3 normal = cross(binormal, direction_nrm);\n";
code += " spread_direction = binormal * spread_direction.x + normal * spread_direction.y + direction_nrm * spread_direction.z;\n";
code += " VELOCITY = spread_direction * initial_linear_velocity * mix(1.0, rand_from_seed(alt_seed), initial_linear_velocity_random);\n";
}
code += " float base_angle = (initial_angle + tex_angle) * mix(1.0, angle_rand, initial_angle_random);\n";