0201-金属反射和模糊

环境

  • Time 2022-11-16
  • WSL-Ubuntu 22.04
  • Rust 1.65.0

前言

说明

参考:https://raytracing.github.io/books/RayTracingInOneWeekend.html

目标

新增金属材质,实现反射和模糊函数。

金属材质

pub struct Metal {
    albedo: Color,
    // 模糊属性
    fuzz: f64,
}

impl Metal {
    pub fn new(albedo: Color, fuzz: f64) -> Self {
        Metal { albedo, fuzz }
    }
}

散射

impl Material for Metal {
    fn scatter(&self, ray: &Ray, record: &HitRecord) -> Option<(Color, Ray)> {
        let direction = ray.direction.unit();

        let normal = record.normal;
        let reflected = direction - 2.0 * direction.dot(normal) * normal;
        let ray = reflected + self.fuzz * Vector3::random_unit();
        let scattered = Ray::new(record.point, ray);

        match scattered.direction.dot(normal) > 0.0 {
            true => Some((self.albedo, scattered)),
            false => None,
        }
    }
}

定义球体

let ground = Rc::new(Lambert::new(Color::new(0.8, 0.8, 0.0)));
let center = Rc::new(Lambert::new(Color::new(0.7, 0.3, 0.3)));
let left = Rc::new(Metal::new(Color::new(0.8, 0.8, 0.8), 0.3));
let right = Rc::new(Metal::new(Color::new(0.8, 0.6, 0.2), 1.0));

let world: World = vec![
    Box::new(Sphere::new(Point3::new(0.0, -100.5, -1.0), 100.0, ground)),
    Box::new(Sphere::new(Point3::new(0.0, 0.0, -1.0), 0.5, center)),
    Box::new(Sphere::new(Point3::new(-1.0, 0.0, -1.0), 0.5, left)),
    Box::new(Sphere::new(Point3::new(1.0, 0.0, -1.0), 0.5, right)),
];

效果

金属材质

总结

新增了金属材质,实现了反射功能,增加模糊属性。

附录

posted @ 2024-07-22 09:18  波尔  阅读(10)  评论(0编辑  收藏  举报