summaryrefslogtreecommitdiff
path: root/engine/src/material.rs
blob: e3685192fe163d58e604337208cdaaf4d9f984e0 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
use ecs::Component;

use crate::color::Color;
use crate::data_types::dimens::Dimens;
use crate::texture::{Id as TextureId, Texture};
use crate::util::builder;

#[derive(Debug, Clone, Component)]
#[non_exhaustive]
pub struct Material
{
    pub ambient: Color<f32>,
    pub diffuse: Color<f32>,
    pub specular: Color<f32>,
    pub ambient_map: TextureId,
    pub diffuse_map: TextureId,
    pub specular_map: TextureId,
    pub textures: Vec<Texture>,
    pub shininess: f32,
}

impl Material
{
    pub fn builder() -> Builder
    {
        Builder::default()
    }
}

/// [`Material`] builder.
#[derive(Debug, Clone)]
pub struct Builder
{
    ambient: Option<Color<f32>>,
    diffuse: Option<Color<f32>>,
    specular: Option<Color<f32>>,
    ambient_map: Option<TextureId>,
    diffuse_map: Option<TextureId>,
    specular_map: Option<TextureId>,
    textures: Vec<Texture>,
    shininess: f32,
}

impl Builder
{
    #[must_use]
    pub fn new() -> Self
    {
        Self {
            ambient: None,
            diffuse: None,
            specular: None,
            ambient_map: None,
            diffuse_map: None,
            specular_map: None,
            textures: Vec::new(),
            shininess: 32.0,
        }
    }

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

        self
    }

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

        self
    }

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

        self
    }

    #[must_use]
    pub fn ambient_map(mut self, ambient_map: TextureId) -> Self
    {
        self.ambient_map = Some(ambient_map);

        self
    }

    #[must_use]
    pub fn diffuse_map(mut self, diffuse_map: TextureId) -> Self
    {
        self.diffuse_map = Some(diffuse_map);

        self
    }

    #[must_use]
    pub fn specular_map(mut self, specular_map: TextureId) -> Self
    {
        self.specular_map = Some(specular_map);

        self
    }

    #[must_use]
    pub fn textures(mut self, textures: impl IntoIterator<Item = Texture>) -> Self
    {
        self.textures = textures.into_iter().collect();

        self
    }

    #[must_use]
    pub fn texture(mut self, texture: Texture) -> Self
    {
        self.textures.push(texture);

        self
    }

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

        self
    }

    /// Builds a new [`Material`].
    ///
    /// # Panics
    /// Will panic if no ambient map, diffuse map or specular map is set.
    #[must_use]
    pub fn build(mut self) -> Material
    {
        let ambient_map = self.ambient_map.unwrap_or_else(|| {
            let texture = create_1x1_white_texture();
            let texture_id = texture.id();

            self.textures.push(texture);

            texture_id
        });

        let diffuse_map = self.diffuse_map.unwrap_or_else(|| {
            let texture = create_1x1_white_texture();
            let texture_id = texture.id();

            self.textures.push(texture);

            texture_id
        });

        let specular_map = self.specular_map.unwrap_or_else(|| {
            let texture = create_1x1_white_texture();
            let texture_id = texture.id();

            self.textures.push(texture);

            texture_id
        });

        Material {
            ambient: self.ambient.unwrap_or(Color::WHITE_F32),
            diffuse: self.diffuse.unwrap_or(Color::WHITE_F32),
            specular: self.specular.unwrap_or(Color::WHITE_F32),
            ambient_map,
            diffuse_map,
            specular_map,
            textures: self.textures,
            shininess: self.shininess,
        }
    }
}

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

builder! {
/// Material flags.
#[builder(name = FlagsBuilder, derives = (Debug, Default, Clone))]
#[derive(Debug, Default, Clone, Component)]
#[non_exhaustive]
pub struct Flags
{
    /// Whether to use material's ambient color instead of the global ambient color.
    /// Default is `false`
    pub use_ambient_color: bool,
}
}

impl Flags
{
    #[must_use]
    pub fn builder() -> FlagsBuilder
    {
        FlagsBuilder::default()
    }
}

fn create_1x1_white_texture() -> Texture
{
    Texture::new_from_color(&Dimens { width: 1, height: 1 }, &Color::WHITE_U8)
}