Skip to content

Commit 7b7966d

Browse files
committed
Improve sdf library.
1 parent d277fbf commit 7b7966d

File tree

1 file changed

+33
-18
lines changed

1 file changed

+33
-18
lines changed

shaders/src/shaders/loading_repeating_circles.rs

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,35 +66,48 @@ impl SDFValue {
6666
return smoothstep(AA_WIDTH, -AA_WIDTH, self.0);
6767
}
6868

69-
/// Creates an outline (hollow shape) from the SDF.
70-
///
71-
/// * `inner_thickness`: how much to expand inwards to form the inner boundary of the outline.
72-
/// * `outer_thickness`: how much to expand outwards to form the outer boundary of the outline.
73-
///
74-
/// The resulting SDF is negative *inside the outline material*.
75-
pub fn to_outline_asym(self, inner_thickness: f32, outer_thickness: f32) -> SDFValue {
76-
return SDFValue((self.0 - outer_thickness).max(-(self.0 + inner_thickness)));
69+
/// Insets the shape by a given thickness.
70+
/// This is done by shrinking the shape and then calculating the difference.
71+
pub fn inset(self, amount: f32) -> Self {
72+
Self(self.0.max(-(self.0 + amount)))
73+
}
74+
75+
/// Expands (if amount > 0) or shrinks (if amount < 0) the shape.
76+
/// This is equivalent to subtracting from the distance value.
77+
pub fn offset(&self, amount: f32) -> Self {
78+
Self(self.0 - amount)
79+
}
80+
81+
/// Takes the absolute value of the SDF, effectively creating an infinitely thin shell
82+
/// on the surface of the original shape.
83+
pub fn shell(self) -> Self {
84+
Self(self.0.abs())
7785
}
7886

7987
/// Creates an outline (hollow shape) from the SDF.
80-
pub fn to_outline(self, thickness: f32) -> SDFValue {
81-
SDFValue(self.0.abs() - thickness)
88+
pub fn to_outline(self, thickness: f32) -> Self {
89+
Self(self.0.abs() - thickness)
8290
}
8391

8492
/// Difference operation (self - other). Result is inside if inside self AND outside other.
8593
/// Equivalent to Intersection(self, Invert(other)).
86-
pub fn difference(self, other: SDFValue) -> SDFValue {
87-
SDFValue(self.0.max(-other.0))
94+
pub fn difference(self, other: Self) -> Self {
95+
Self(self.0.max(-other.0))
8896
}
8997

9098
/// Union operation (self U other). Result is inside if inside self OR inside other.
91-
pub fn union(self, other: SDFValue) -> SDFValue {
92-
SDFValue(self.0.min(other.0))
99+
pub fn union(self, other: Self) -> Self {
100+
Self(self.0.min(other.0))
93101
}
94102

95103
/// Intersection operation (self ∩ other). Result is inside if inside self AND inside other.
96-
pub fn intersection(self, other: SDFValue) -> SDFValue {
97-
SDFValue(self.0.max(other.0))
104+
pub fn intersection(self, other: Self) -> Self {
105+
Self(self.0.max(other.0))
106+
}
107+
108+
/// Inverts the SDF (inside becomes outside and vice-versa).
109+
pub fn invert(self) -> Self {
110+
Self(-self.0)
98111
}
99112
}
100113

@@ -427,7 +440,7 @@ fn sdf_arc_outline(
427440
spine_radius,
428441
outer_radius * 2.0,
429442
);
430-
let sdf_value = sdf_outer.to_outline_asym(outer_radius - inner_radius, 0.0);
443+
let sdf_value = sdf_outer.inset(outer_radius - inner_radius);
431444
let fade_intensity = arc_fade_out(
432445
uv,
433446
center_shape,
@@ -1006,7 +1019,9 @@ impl Inputs {
10061019
-PI / 4.0,
10071020
1.0,
10081021
);
1009-
debug_green_alpha = debug_green_alpha.max(sdf_arc_test.0.to_alpha() * sdf_arc_test.1);
1022+
debug_green_alpha =
1023+
debug_green_alpha.max(sdf_arc_test.0.offset(0.05).to_alpha() * sdf_arc_test.1);
1024+
debug_red_alpha = debug_red_alpha.max(sdf_arc_test.0.to_alpha() * sdf_arc_test.1);
10101025
}
10111026

10121027
if !DEBUG {

0 commit comments

Comments
 (0)