From 32a44e14e6c86f5f7e83b8d79230f7c07050314a Mon Sep 17 00:00:00 2001 From: Lynn <62256001+solis-lumine-vorago@users.noreply.github.com> Date: Thu, 30 May 2024 14:08:28 +0200 Subject: [PATCH] Implement `ShapeSample` for `Extrusion` (#13567) # Objective - Implement `ShapeSample` for `Extrusion` --- .../bevy_math/src/sampling/shape_sampling.rs | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/crates/bevy_math/src/sampling/shape_sampling.rs b/crates/bevy_math/src/sampling/shape_sampling.rs index 95dfe677fd20c..50c9b90c64328 100644 --- a/crates/bevy_math/src/sampling/shape_sampling.rs +++ b/crates/bevy_math/src/sampling/shape_sampling.rs @@ -542,6 +542,34 @@ impl ShapeSample for Capsule3d { } } +impl> ShapeSample for Extrusion

{ + type Output = Vec3; + + fn sample_interior(&self, rng: &mut R) -> Self::Output { + let base_point = self.base_shape.sample_interior(rng); + let depth = rng.gen_range(-self.half_depth..self.half_depth); + base_point.extend(depth) + } + + fn sample_boundary(&self, rng: &mut R) -> Self::Output { + let base_area = self.base_shape.area(); + let total_area = self.area(); + + let random = rng.gen_range(0.0..total_area); + match random { + x if x < base_area => self.base_shape.sample_interior(rng).extend(self.half_depth), + x if x < 2. * base_area => self + .base_shape + .sample_interior(rng) + .extend(-self.half_depth), + _ => self + .base_shape + .sample_boundary(rng) + .extend(rng.gen_range(-self.half_depth..self.half_depth)), + } + } +} + #[cfg(test)] mod tests { use super::*;