From 023c3d37a6bbd512d62d7a81cfb91a376a8bfad1 Mon Sep 17 00:00:00 2001 From: Matthew Weatherley Date: Thu, 28 Mar 2024 13:12:17 -0400 Subject: [PATCH 1/3] Translated samples back to the correct base point --- crates/bevy_math/src/shape_sampling.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_math/src/shape_sampling.rs b/crates/bevy_math/src/shape_sampling.rs index 1479aa7209df0..3728034413bef 100644 --- a/crates/bevy_math/src/shape_sampling.rs +++ b/crates/bevy_math/src/shape_sampling.rs @@ -159,9 +159,9 @@ fn sample_triangle_interior( if u + v > 1. { let u1 = 1. - v; let v1 = 1. - u; - ab * u1 + ac * v1 + a + (ab * u1 + ac * v1) } else { - ab * u + ac * v + a + (ab * u + ac * v) } } From 77bf32668df1f7967746f595de477e2788695285 Mon Sep 17 00:00:00 2001 From: Matthew Weatherley Date: Thu, 28 Mar 2024 13:19:14 -0400 Subject: [PATCH 2/3] Reinterpreted random variables as lerp parameters --- crates/bevy_math/src/shape_sampling.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/crates/bevy_math/src/shape_sampling.rs b/crates/bevy_math/src/shape_sampling.rs index 3728034413bef..e3214fd815c7d 100644 --- a/crates/bevy_math/src/shape_sampling.rs +++ b/crates/bevy_math/src/shape_sampling.rs @@ -148,8 +148,6 @@ fn sample_triangle_interior( rng: &mut R, ) -> P { let [a, b, c] = vertices; - let ab = b - a; - let ac = c - a; // Generate random points on a parallelipiped and reflect so that // we can use the points that lie outside the triangle @@ -159,9 +157,9 @@ fn sample_triangle_interior( if u + v > 1. { let u1 = 1. - v; let v1 = 1. - u; - a + (ab * u1 + ac * v1) + a.lerp(b, u1) + a.lerp(c, v1) } else { - a + (ab * u + ac * v) + a.lerp(b, u) + a.lerp(c, v) } } From 9c826f3fe17434942fb8d552f6b8370a8b657fe1 Mon Sep 17 00:00:00 2001 From: Matthew Weatherley Date: Thu, 28 Mar 2024 13:21:09 -0400 Subject: [PATCH 3/3] Came to the realization that the previous refactor was folly --- crates/bevy_math/src/shape_sampling.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/crates/bevy_math/src/shape_sampling.rs b/crates/bevy_math/src/shape_sampling.rs index e3214fd815c7d..3728034413bef 100644 --- a/crates/bevy_math/src/shape_sampling.rs +++ b/crates/bevy_math/src/shape_sampling.rs @@ -148,6 +148,8 @@ fn sample_triangle_interior( rng: &mut R, ) -> P { let [a, b, c] = vertices; + let ab = b - a; + let ac = c - a; // Generate random points on a parallelipiped and reflect so that // we can use the points that lie outside the triangle @@ -157,9 +159,9 @@ fn sample_triangle_interior( if u + v > 1. { let u1 = 1. - v; let v1 = 1. - u; - a.lerp(b, u1) + a.lerp(c, v1) + a + (ab * u1 + ac * v1) } else { - a.lerp(b, u) + a.lerp(c, v) + a + (ab * u + ac * v) } }