diff options
author | Rosyid Haryadi <rosyid_haryadi@protonmail.com> | 2025-03-02 21:50:40 +0700 |
---|---|---|
committer | Rosyid Haryadi <rosyid_haryadi@protonmail.com> | 2025-03-02 21:50:40 +0700 |
commit | 7301f5af4fc7cb7bc13a9d559183f2f095de47d0 (patch) | |
tree | 05c54e4ce19b925f2fffacc8158041728e23bbec | |
parent | ece36a7364c3c5e7edd157c389cd8079e21c3423 (diff) |
upd metal fuzz
-rw-r--r-- | src/camera.rs | 3 | ||||
-rw-r--r-- | src/main.rs | 11 | ||||
-rw-r--r-- | src/material.rs | 12 | ||||
-rw-r--r-- | src/object.rs | 10 |
4 files changed, 23 insertions, 13 deletions
diff --git a/src/camera.rs b/src/camera.rs index 83da862..4f2469f 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -89,7 +89,8 @@ impl Camera { // let direction = Vec3::random_on_hemisphere(&rec.normal); let material = Material::new( rec.color.clone(), - rec.material.clone() + rec.material.clone(), + rec.fuzz ); let mut scattered: Ray = Ray { origin: Vec3::random_unit(), diff --git a/src/main.rs b/src/main.rs index 489ef41..fa6c194 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,15 +18,16 @@ fn main() { let mut world = HittableList::new(); world.push( - Sphere::new(Point3::new(0.6, -0.3, -1.0), 0.3, MaterialType::Metal, Color::new(0.8, 0.0, 0.0)) + Sphere::new(Point3::new(0.0, -100.5, -1.0), 100.0, MaterialType::Diffuse, Color::new(0.8, 0.8, 0.0), 0.0) ); - world.push( - Sphere::new(Point3::new(0.0, 0.0, -1.0), 0.5, MaterialType::Diffuse, Color::new(0.7, 0.7, 0.7)) + Sphere::new(Point3::new(0.0, 0.0, -1.2), 0.5, MaterialType::Diffuse, Color::new(0.1, 0.2, 0.5), 0.0) + ); + world.push( + Sphere::new(Point3::new(-1.0, 0.0, -1.0), 0.5, MaterialType::Metal, Color::new(0.8, 0.8, 0.8), 0.1) ); - world.push( - Sphere::new(Point3::new(0.0, -100.5, -1.0), 100.0, MaterialType::Diffuse, Color::new(0.2, 0.2, 0.2)) + Sphere::new(Point3::new(1.0, 0.0, -1.0), 0.5, MaterialType::Metal, Color::new(0.8, 0.6, 0.2), 0.0) ); let camera = Camera::new(); diff --git a/src/material.rs b/src/material.rs index 2b3deae..731eb0c 100644 --- a/src/material.rs +++ b/src/material.rs @@ -10,12 +10,14 @@ pub enum MaterialType { pub struct Material { pub material_type: MaterialType, - pub albedo: Color + pub albedo: Color, + pub fuzz: f32, } impl Material { - pub fn new(albedo: Color, material_type: MaterialType) -> Self { - Self { albedo, material_type } + 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 scatter(&self, r_in: &Ray, rec: &HitRecord, attenuation: &mut Color, scattered: &mut Ray) -> bool { @@ -29,7 +31,9 @@ impl Material { } } MaterialType::Metal => { - r_in.reflect(&rec.normal) + let mut reflected = r_in.reflect(&rec.normal); + reflected.unit().add(&Vec3::random_unit().scalar_mul(self.fuzz)) + } }; scattered.origin = rec.position; diff --git a/src/object.rs b/src/object.rs index d9e2cc4..2192703 100644 --- a/src/object.rs +++ b/src/object.rs @@ -11,6 +11,7 @@ pub struct HitRecord { pub front_face: bool, pub material: MaterialType, pub color: Color, + pub fuzz: f32, } impl Default for HitRecord { @@ -22,6 +23,7 @@ impl Default for HitRecord { front_face: false, material: MaterialType::Diffuse, color:Color::new(0.0, 0.0, 0.0), + fuzz: 1.0, } } } @@ -41,13 +43,14 @@ pub struct Sphere { center: Point3, radius: f32, material: MaterialType, - color: Color + color: Color, + fuzz: f32, } impl Sphere { - pub fn new(center: Vec3, radius: f32, material: MaterialType, color: Color) -> Self { + pub fn new(center: Vec3, radius: f32, material: MaterialType, color: Color, fuzz: f32) -> Self { let radius = f32::max(0.0, radius); - Self { center, radius, material, color } + Self { center, radius, material, color, fuzz } } } @@ -80,6 +83,7 @@ impl Hittable for Sphere { rec.set_face_normal(r, outward_normal); rec.material = self.material.clone(); rec.color = self.color.clone(); + rec.fuzz = self.fuzz; true } |