summaryrefslogtreecommitdiff
path: root/src/material.rs
diff options
context:
space:
mode:
authorRosyid Haryadi <rosyid_haryadi@protonmail.com>2025-03-03 00:15:55 +0700
committerRosyid Haryadi <rosyid_haryadi@protonmail.com>2025-03-03 00:15:55 +0700
commitab0b3f165788c5d79b2d159aeefda8c8947b68ae (patch)
treefc19c8105c401685f7668ed663050366338a2d27 /src/material.rs
parent7301f5af4fc7cb7bc13a9d559183f2f095de47d0 (diff)
refactor material
Diffstat (limited to 'src/material.rs')
-rw-r--r--src/material.rs18
1 files changed, 11 insertions, 7 deletions
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))
}
};