From 18ba1bc824fd4739a34ad78a3c95df1fb552ff1b Mon Sep 17 00:00:00 2001 From: Simon Puchert Date: Sat, 6 Jul 2019 00:13:03 +0200 Subject: [PATCH] cpu_particles: Return uniform density spheres. The current implementation normalizes points from a uniform distribution on a cube. This creates a non-uniform distribution on the sphere. --- scene/2d/cpu_particles_2d.cpp | 5 +++-- scene/3d/cpu_particles.cpp | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/scene/2d/cpu_particles_2d.cpp b/scene/2d/cpu_particles_2d.cpp index 20910854204..591933d9728 100644 --- a/scene/2d/cpu_particles_2d.cpp +++ b/scene/2d/cpu_particles_2d.cpp @@ -661,8 +661,9 @@ void CPUParticles2D::_particles_process(float p_delta) { //do none } break; case EMISSION_SHAPE_SPHERE: { - Vector3 sphere_shape = Vector3(Math::randf() * 2.0 - 1.0, Math::randf() * 2.0 - 1.0, Math::randf() * 2.0 - 1.0).normalized() * emission_sphere_radius; - p.transform[2] = Vector2(sphere_shape.x, sphere_shape.y); + float s = Math::randf(), t = 2.0 * Math_PI * Math::randf(); + float radius = emission_sphere_radius * Math::sqrt(1.0 - s * s); + p.transform[2] = Vector2(Math::cos(t), Math::sin(t)) * radius; } break; case EMISSION_SHAPE_RECTANGLE: { p.transform[2] = Vector2(Math::randf() * 2.0 - 1.0, Math::randf() * 2.0 - 1.0) * emission_rect_extents; diff --git a/scene/3d/cpu_particles.cpp b/scene/3d/cpu_particles.cpp index 358c4bdb442..86b407b9e60 100644 --- a/scene/3d/cpu_particles.cpp +++ b/scene/3d/cpu_particles.cpp @@ -643,7 +643,9 @@ void CPUParticles::_particles_process(float p_delta) { //do none } break; case EMISSION_SHAPE_SPHERE: { - p.transform.origin = Vector3(Math::randf() * 2.0 - 1.0, Math::randf() * 2.0 - 1.0, Math::randf() * 2.0 - 1.0).normalized() * emission_sphere_radius; + float s = 2.0 * Math::randf() - 1.0, t = 2.0 * Math_PI * Math::randf(); + float radius = emission_sphere_radius * Math::sqrt(1.0 - s * s); + p.transform.origin = Vector3(radius * Math::cos(t), radius * Math::sin(t), emission_sphere_radius * s); } break; case EMISSION_SHAPE_BOX: { p.transform.origin = Vector3(Math::randf() * 2.0 - 1.0, Math::randf() * 2.0 - 1.0, Math::randf() * 2.0 - 1.0) * emission_box_extents;