1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/// Extensions to floating-point primitives.
///
/// Adds additional math-related functionality to floats
pub trait FloatExt {
/// Returns `0.0` if `value < self` and 1.0 otherwise.
///
/// Similar to glsl's step(edge, x), which translates into edge.step(x)
#[must_use]
fn step(self, value: Self) -> Self;
/// Selects between `less` and `greater_or_equal` based on the result of `value < self`
#[must_use]
fn step_select(self, value: Self, less: Self, greater_or_equal: Self) -> Self;
/// Performs a linear interpolation between `self` and `other` using `a` to weight between them.
/// The return value is computed as `self * (1−a) + other * a`.
#[must_use]
fn lerp(self, other: Self, a: Self) -> Self;
/// Clamp `self` within the range `[0.0, 1.0]`
#[must_use]
fn saturate(self) -> Self;
}
impl FloatExt for f32 {
fn step(self, value: Self) -> Self {
if value < self {
0.0
} else {
1.0
}
}
fn step_select(self, value: Self, less: Self, greater_or_equal: Self) -> Self {
if value < self {
less
} else {
greater_or_equal
}
}
fn lerp(self, other: Self, a: Self) -> Self {
self + (other - self) * a
}
fn saturate(self) -> Self {
self.clamp(0.0, 1.0)
}
}