From ab0b3f165788c5d79b2d159aeefda8c8947b68ae Mon Sep 17 00:00:00 2001 From: Rosyid Haryadi Date: Mon, 3 Mar 2025 00:15:55 +0700 Subject: refactor material --- src/material.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'src/material.rs') diff --git a/src/material.rs b/src/material.rs index 731eb0c..a654f4d 100644 --- a/src/material.rs +++ b/src/material.rs @@ -5,19 +5,23 @@ use crate::object::HitRecord; #[derive(Clone)] pub enum MaterialType { Diffuse, - Metal, + Metal(f32), // color fuzz } +#[derive(Clone)] pub struct Material { pub material_type: MaterialType, pub albedo: Color, - pub fuzz: f32, } impl Material { - pub fn new(albedo: Color, material_type: MaterialType, fuzz: f32) -> Self { - let fuzz = if fuzz < 1.0 { fuzz } else { 1.0 }; - Self { albedo, material_type, fuzz } + pub fn new(albedo: Color, material_type: MaterialType) -> Self { + let mut material_type = material_type; + if let MaterialType::Metal(fuzz) = material_type { + let fuzz = if fuzz < 1.0 { fuzz } else { fuzz }; + material_type = MaterialType::Metal(fuzz); + } + Self { albedo, material_type } } pub fn scatter(&self, r_in: &Ray, rec: &HitRecord, attenuation: &mut Color, scattered: &mut Ray) -> bool { @@ -30,9 +34,9 @@ impl Material { scatter_direction } } - MaterialType::Metal => { + MaterialType::Metal(fuzz) => { let mut reflected = r_in.reflect(&rec.normal); - reflected.unit().add(&Vec3::random_unit().scalar_mul(self.fuzz)) + reflected.unit().add(&Vec3::random_unit().scalar_mul(fuzz)) } }; -- cgit v1.2.3-70-g09d2