summaryrefslogtreecommitdiff
path: root/engine/src
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2024-05-05 22:10:57 +0200
committerHampusM <hampus@hampusmat.com>2024-05-05 22:11:06 +0200
commit5013c63cf727eaa94c30f2680163a9f868aafb6e (patch)
treea882ccf5cb5ee2cc7420182f0f013bfb23ffd57f /engine/src
parent81bba9fb007333c54006be9172e090c38c45d110 (diff)
feat(engine): make material textures optional
Diffstat (limited to 'engine/src')
-rw-r--r--engine/src/data_types/color.rs5
-rw-r--r--engine/src/material.rs41
2 files changed, 42 insertions, 4 deletions
diff --git a/engine/src/data_types/color.rs b/engine/src/data_types/color.rs
index 697ea8b..7bab678 100644
--- a/engine/src/data_types/color.rs
+++ b/engine/src/data_types/color.rs
@@ -12,6 +12,11 @@ impl Color<f32>
pub const WHITE_F32: Self = Self { red: 1.0, green: 1.0, blue: 1.0 };
}
+impl Color<u8>
+{
+ pub const WHITE_U8: Self = Self { red: 255, green: 255, blue: 255 };
+}
+
impl<Value: Clone> From<Value> for Color<Value>
{
fn from(value: Value) -> Self
diff --git a/engine/src/material.rs b/engine/src/material.rs
index 6839fd1..69fe970 100644
--- a/engine/src/material.rs
+++ b/engine/src/material.rs
@@ -1,6 +1,7 @@
use ecs::Component;
use crate::color::Color;
+use crate::data_types::dimens::Dimens;
use crate::texture::{Id as TextureId, Texture};
#[derive(Debug, Clone, Component)]
@@ -167,15 +168,42 @@ impl Builder
/// # Panics
/// Will panic if no ambient map, diffuse map or specular map is set.
#[must_use]
- pub fn build(self) -> Material
+ 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.clone(),
diffuse: self.diffuse.clone(),
specular: self.specular.clone(),
- ambient_map: self.ambient_map.unwrap(),
- diffuse_map: self.diffuse_map.unwrap(),
- specular_map: self.specular_map.unwrap(),
+ ambient_map,
+ diffuse_map,
+ specular_map,
textures: self.textures,
shininess: self.shininess,
}
@@ -189,3 +217,8 @@ impl Default for Builder
Self::new()
}
}
+
+fn create_1x1_white_texture() -> Texture
+{
+ Texture::new_from_color(&Dimens { width: 1, height: 1 }, &Color::WHITE_U8)
+}