From 5013c63cf727eaa94c30f2680163a9f868aafb6e Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 5 May 2024 22:10:57 +0200 Subject: feat(engine): make material textures optional --- engine/src/data_types/color.rs | 5 +++++ engine/src/material.rs | 41 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 42 insertions(+), 4 deletions(-) (limited to 'engine/src') 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 pub const WHITE_F32: Self = Self { red: 1.0, green: 1.0, blue: 1.0 }; } +impl Color +{ + pub const WHITE_U8: Self = Self { red: 255, green: 255, blue: 255 }; +} + impl From for Color { 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) +} -- cgit v1.2.3-18-g5258