veloren_world/site2/util/
gradient.rs

1use vek::*;
2
3/// A wrapping mode, used to determine what to do when sampling outside of 0..=1
4#[derive(Clone, Copy)]
5pub enum WrapMode {
6    /// ..............______
7    /// No repeat ___/
8    Clamp,
9    /// Saw wave repeat / / / /
10    Repeat,
11    /// Triangle wave repeat /\/\/\/\/
12    PingPong,
13}
14
15impl WrapMode {
16    fn sample(&self, t: f32) -> f32 {
17        match self {
18            WrapMode::Clamp => t.clamp(0.0, 1.0),
19            WrapMode::Repeat => (1.0 + t.fract()).fract(),
20            WrapMode::PingPong => 1.0 - 2.0 * ((t / 2.0).fract().abs() - 0.5).abs(),
21        }
22    }
23}
24
25#[derive(Clone, Copy)]
26pub enum Shape {
27    Point,
28    /// Vector should be normalized for Gradient size to work properly
29    Plane(Vec3<f32>),
30    /// Vector should be normalized for Gradient size to work properly
31    Line(Vec3<f32>),
32}
33
34impl Shape {
35    /// Create a new plane shape with the given normal.
36    pub fn plane(normal: Vec3<f32>) -> Self { Shape::Plane(normal.normalized()) }
37
38    /// Create an infinite line shape with the given direction.
39    pub fn radial_line(direction: Vec3<f32>) -> Self { Shape::Line(direction.normalized()) }
40}
41
42#[derive(Clone)]
43pub struct Gradient {
44    /// The center of the gradient shape
45    pub(super) center: Vec3<f32>,
46    /// The distance the gradient is sampled along
47    pub(super) size: f32,
48    /// The shape that the distance is computed to to get the gradient color.
49    pub(super) shape: Shape,
50    /// How the graduint should repeat when the distance from the shape is
51    /// greater than size
52    pub(super) repeat: WrapMode,
53    /// The colors the gradient is lerped between
54    pub(super) colors: (Rgb<u8>, Rgb<u8>),
55}
56
57impl Gradient {
58    pub fn new(center: Vec3<f32>, size: f32, shape: Shape, colors: (Rgb<u8>, Rgb<u8>)) -> Self {
59        Gradient {
60            center,
61            size,
62            shape,
63            repeat: WrapMode::Clamp,
64            colors,
65        }
66    }
67
68    /// Add a repeat mode to the gradient
69    #[must_use]
70    pub fn with_repeat(mut self, repeat: WrapMode) -> Self {
71        self.repeat = repeat;
72        self
73    }
74
75    /// Sample the gradient at a certain point, will always return a color
76    /// that's in the range color.0..=color.1
77    pub fn sample(&self, pos: Vec3<f32>) -> Rgb<u8> {
78        // Calculate t by dividing the distance from the shape divided by size
79        let t = self.repeat.sample(match self.shape {
80            Shape::Point => pos.distance(self.center) / self.size,
81            Shape::Plane(normal) => (pos - self.center).dot(normal) / self.size,
82            Shape::Line(line) => {
83                let u = pos - self.center;
84                (u.dot(line) * line - u).magnitude() / self.size
85            },
86        });
87        // Lerp colors
88        self.colors.0.map2(self.colors.1, |a, b| {
89            (a as f32 * (1.0 - t) + b as f32 * t) as u8
90        })
91    }
92}