aboutsummaryrefslogtreecommitdiff
path: root/src/sdf.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/sdf.rs')
-rw-r--r--src/sdf.rs34
1 files changed, 21 insertions, 13 deletions
diff --git a/src/sdf.rs b/src/sdf.rs
index c46deab..d940f39 100644
--- a/src/sdf.rs
+++ b/src/sdf.rs
@@ -1,10 +1,10 @@
-use std::f32::consts::PI;
-use cgmath::Vector3;
use cgmath::prelude::*;
+use cgmath::Vector3;
+use std::f32::consts::PI;
type Vector = Vector3<f32>;
-pub trait Object {
+pub trait Object: Send + Sync {
fn sdf(&self, point: Vector, time: f32) -> f32;
}
@@ -28,7 +28,7 @@ impl Object for SDBox {
fn sdf(&self, point: Vector, _time: f32) -> f32 {
let diff = self.center - point;
let q = diff.map(|n| n.abs()) - self.size / 2.0;
- return q.map(|n| n.max(0.0)).magnitude() + (q.y.max(q.z).max(q.x)).min(0.0)
+ q.map(|n| n.max(0.0)).magnitude() + (q.y.max(q.z).max(q.x)).min(0.0)
}
}
@@ -48,7 +48,8 @@ impl Object for Gear {
// Ring
{
- let cylinder_dist = (Vector::new(0.0, point.y, point.z) - self.center).magnitude() - (self.radius - thickness_over_4);
+ let cylinder_dist = (Vector::new(0.0, point.y, point.z) - self.center).magnitude()
+ - (self.radius - thickness_over_4);
dist = cylinder_dist.abs() - thickness_over_2; // Make cylinder hollow
}
// Teeth
@@ -60,7 +61,7 @@ impl Object for Gear {
let rotated_point = Vector::new(
point.x,
point.y * angle.cos() - point.z * angle.sin(),
- point.y * angle.sin() + point.z * angle.cos()
+ point.y * angle.sin() + point.z * angle.cos(),
);
// Map all space to the first sector
@@ -70,20 +71,27 @@ impl Object for Gear {
let mapped_point = Vector::new(
rotated_point.x,
(rotated_point.y * angle2.cos() - rotated_point.z * angle2.sin()).abs(),
- rotated_point.y * angle2.sin() + rotated_point.z * angle2.cos()
+ rotated_point.y * angle2.sin() + rotated_point.z * angle2.cos(),
);
// Make teeth smooth by subtracting some amount
- dist = dist.min(SDBox {
- center: Vector { x: 0.0, y: self.radius + thickness_over_2, z: 0.0 },
- size: Vector::new(self.thickness, self.thickness * 2.0, self.thickness),
- }.sdf(mapped_point, time) - thickness_over_4);
+ dist = dist.min(
+ SDBox {
+ center: Vector {
+ x: 0.0,
+ y: self.radius + thickness_over_2,
+ z: 0.0,
+ },
+ size: Vector::new(self.thickness, self.thickness * 2.0, self.thickness),
+ }
+ .sdf(mapped_point, time)
+ - thickness_over_4,
+ );
}
// Take a slice
dist = dist.max(point.x.abs() - thickness_over_2);
- return dist;
+ dist
}
}
-