summaryrefslogtreecommitdiff
path: root/engine/src/material.rs
blob: 240d7e239995ef1ec7acc873b5d33e5a124d1d27 (plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use crate::color::Color;

#[derive(Debug, Clone)]
pub struct Material
{
    ambient: Color<f32>,
    diffuse: Color<f32>,
    specular: Color<f32>,
    shininess: f32,
}

impl Material
{
    #[must_use]
    pub fn ambient(&self) -> &Color<f32>
    {
        &self.ambient
    }

    #[must_use]
    pub fn diffuse(&self) -> &Color<f32>
    {
        &self.diffuse
    }

    #[must_use]
    pub fn specular(&self) -> &Color<f32>
    {
        &self.specular
    }

    #[must_use]
    pub fn shininess(&self) -> f32
    {
        self.shininess
    }
}

/// [`Material`] builder.
#[derive(Debug, Clone)]
pub struct Builder
{
    ambient: Color<f32>,
    diffuse: Color<f32>,
    specular: Color<f32>,
    shininess: f32,
}

impl Builder
{
    #[must_use]
    pub fn new() -> Self
    {
        Self {
            ambient: 0.2.into(),
            diffuse: 0.5.into(),
            specular: 1.0.into(),
            shininess: 32.0,
        }
    }

    #[must_use]
    pub fn ambient(mut self, ambient: Color<f32>) -> Self
    {
        self.ambient = ambient;

        self
    }

    #[must_use]
    pub fn diffuse(mut self, diffuse: Color<f32>) -> Self
    {
        self.diffuse = diffuse;

        self
    }

    #[must_use]
    pub fn specular(mut self, specular: Color<f32>) -> Self
    {
        self.specular = specular;

        self
    }

    #[must_use]
    pub fn shininess(mut self, shininess: f32) -> Self
    {
        self.shininess = shininess;

        self
    }

    /// Builds a new [`Material`].
    #[must_use]
    pub fn build(&self) -> Material
    {
        Material {
            ambient: self.ambient.clone(),
            diffuse: self.diffuse.clone(),
            specular: self.specular.clone(),
            shininess: self.shininess,
        }
    }
}

impl Default for Builder
{
    fn default() -> Self
    {
        Self::new()
    }
}