From f8d08a83760d2b847de271c80c13d800b4a97aab Mon Sep 17 00:00:00 2001 From: Kasper Frandsen Date: Wed, 17 Apr 2024 17:33:30 +0100 Subject: [PATCH] fix: avoid shader crash on null vector and negative x vector --- scene/3d/cpu_particles_3d.cpp | 4 ++-- scene/resources/particle_process_material.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/scene/3d/cpu_particles_3d.cpp b/scene/3d/cpu_particles_3d.cpp index e7b6aa1dba3..0dc9834539d 100644 --- a/scene/3d/cpu_particles_3d.cpp +++ b/scene/3d/cpu_particles_3d.cpp @@ -881,9 +881,9 @@ void CPUParticles3D::_particles_process(double p_delta) { case EMISSION_SHAPE_RING: { real_t ring_random_angle = Math::randf() * Math_TAU; real_t ring_random_radius = Math::randf() * (emission_ring_radius - emission_ring_inner_radius) + emission_ring_inner_radius; - Vector3 axis = emission_ring_axis.normalized(); + Vector3 axis = emission_ring_axis == Vector3(0.0, 0.0, 0.0) ? Vector3(0.0, 0.0, 1.0) : emission_ring_axis.normalized(); Vector3 ortho_axis; - if (axis == Vector3(1.0, 0.0, 0.0)) { + if (axis.abs() == Vector3(1.0, 0.0, 0.0)) { ortho_axis = Vector3(0.0, 1.0, 0.0).cross(axis); } else { ortho_axis = Vector3(1.0, 0.0, 0.0).cross(axis); diff --git a/scene/resources/particle_process_material.cpp b/scene/resources/particle_process_material.cpp index 5acb08de14d..30b90841e3b 100644 --- a/scene/resources/particle_process_material.cpp +++ b/scene/resources/particle_process_material.cpp @@ -635,9 +635,9 @@ void ParticleProcessMaterial::_update_shader() { code += " \n"; code += " float ring_spawn_angle = rand_from_seed(alt_seed) * 2.0 * pi;\n"; code += " float ring_random_radius = rand_from_seed(alt_seed) * (emission_ring_radius - emission_ring_inner_radius) + emission_ring_inner_radius;\n"; - code += " vec3 axis = normalize(emission_ring_axis);\n"; + code += " vec3 axis = emission_ring_axis == vec3(0.0) ? vec3(0.0, 0.0, 1.0) : normalize(emission_ring_axis);\n"; code += " vec3 ortho_axis = vec3(0.0);\n"; - code += " if (axis == vec3(1.0, 0.0, 0.0)) {\n"; + code += " if (abs(axis) == vec3(1.0, 0.0, 0.0)) {\n"; code += " ortho_axis = cross(axis, vec3(0.0, 1.0, 0.0));\n"; code += " } else {\n"; code += " ortho_axis = cross(axis, vec3(1.0, 0.0, 0.0));\n";