Skip to content

Commit e8d57af

Browse files
authored
Merge pull request #83831 from QbieShay/qbe/fix-normal-point
Particles: Fix directed points not working, and fix friction formula
2 parents ea0ab44 + 49fee5b commit e8d57af

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

scene/resources/particle_process_material.cpp

+24-1
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,27 @@ void ParticleProcessMaterial::_update_shader() {
850850
code += " }\n";
851851
code += " if (RESTART_VELOCITY) {\n";
852852
code += " VELOCITY = get_random_direction_from_spread(alt_seed, spread) * dynamic_params.initial_velocity_multiplier;\n";
853+
if (emission_shape == EMISSION_SHAPE_DIRECTED_POINTS) {
854+
code += " int point = min(emission_texture_point_count - 1, int(rand_from_seed(alt_seed) * float(emission_texture_point_count)));\n";
855+
code += " ivec2 emission_tex_size = textureSize(emission_texture_points, 0);\n";
856+
code += " ivec2 emission_tex_ofs = ivec2(point % emission_tex_size.x, point / emission_tex_size.x);\n";
857+
if (particle_flags[PARTICLE_FLAG_DISABLE_Z]) {
858+
code += " {\n";
859+
code += " mat2 rotm;";
860+
code += " rotm[0] = texelFetch(emission_texture_normal, emission_tex_ofs, 0).xy;\n";
861+
code += " rotm[1] = rotm[0].yx * vec2(1.0, -1.0);\n";
862+
code += " VELOCITY.xy = rotm * VELOCITY.xy;\n";
863+
code += " }\n";
864+
} else {
865+
code += " {\n";
866+
code += " vec3 normal = texelFetch(emission_texture_normal, emission_tex_ofs, 0).xyz;\n";
867+
code += " vec3 v0 = abs(normal.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(0.0, 1.0, 0.0);\n";
868+
code += " vec3 tangent = normalize(cross(v0, normal));\n";
869+
code += " vec3 bitangent = normalize(cross(tangent, normal));\n";
870+
code += " VELOCITY = mat3(tangent, bitangent, normal) * VELOCITY;\n";
871+
code += " }\n";
872+
}
873+
}
853874
code += " }\n";
854875
code += " process_display_param(params, 0.);\n";
855876
code += "// process_dynamic_parameters(dynamic_params, 0., alt_seed, TRANSFORM, EMISSION_TRANSFORM, DELTA);\n";
@@ -939,7 +960,9 @@ void ParticleProcessMaterial::_update_shader() {
939960
code += " if (physics_params.damping > 0.0) {\n";
940961
if (!particle_flags[PARTICLE_FLAG_DAMPING_AS_FRICTION]) {
941962
code += " float v = length(VELOCITY);\n";
942-
code += " v -= physics_params.damping * DELTA;\n";
963+
code += " // Realistic friction formula. We assume the mass of a particle to be 0.05kg.\n";
964+
code += " float damp = v * v * physics_params.damping * 0.05 * DELTA;\n";
965+
code += " v -= damp;\n";
943966
code += " if (v < 0.0) {\n";
944967
code += " VELOCITY = vec3(0.0);\n";
945968
code += " } else {\n";

0 commit comments

Comments
 (0)