avoid recreating sdf shapes on every sample
@@ -45,9 +45,21 @@ enum Sdf {
impl Sdf {
fn get_sdf(&self) -> Box<dyn Fn(&Point3i) -> f32> {
match self {
- Sdf::Cube => Box::new(|p| sdfu::Box::new(PointN([20.0; 3])).dist(Point3f::from(*p))),
- Sdf::Sphere => Box::new(|p| sdfu::Sphere::new(20.0).dist(Point3f::from(*p))),
- Sdf::Torus => Box::new(|p| sdfu::Torus::new(4.0, 16.0).dist(Point3f::from(p.yzx()))),
+ Sdf::Cube => {
+ let cube = sdfu::Box::new(PointN([20.0; 3]));
+
+ Box::new(move |p| cube.dist(Point3f::from(*p)))
+ }
+ Sdf::Sphere => {
+ let sphere = sdfu::Sphere::new(20.0);
+
+ Box::new(move |p| sphere.dist(Point3f::from(*p)))
+ }
+ Sdf::Torus => {
+ let torus = sdfu::Torus::new(4.0, 16.0);
+
+ Box::new(move |p| torus.dist(Point3f::from(p.yzx())))
+ }
}
}
}
