veloren_world/site2/util/
gradient.rs1use vek::*;
2
3#[derive(Clone, Copy)]
5pub enum WrapMode {
6 Clamp,
9 Repeat,
11 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 Plane(Vec3<f32>),
30 Line(Vec3<f32>),
32}
33
34impl Shape {
35 pub fn plane(normal: Vec3<f32>) -> Self { Shape::Plane(normal.normalized()) }
37
38 pub fn radial_line(direction: Vec3<f32>) -> Self { Shape::Line(direction.normalized()) }
40}
41
42#[derive(Clone)]
43pub struct Gradient {
44 pub(super) center: Vec3<f32>,
46 pub(super) size: f32,
48 pub(super) shape: Shape,
50 pub(super) repeat: WrapMode,
53 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 #[must_use]
70 pub fn with_repeat(mut self, repeat: WrapMode) -> Self {
71 self.repeat = repeat;
72 self
73 }
74
75 pub fn sample(&self, pos: Vec3<f32>) -> Rgb<u8> {
78 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 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}