环境
- Time 2022-11-17
- WSL-Ubuntu 22.04
- Rust 1.65.0
前言
说明
参考:https://raytracing.github.io/books/RayTracingInOneWeekend.html
目标
实现透明材质的反射功能。
Dielectric
impl Dielectric {
pub fn new(refraction: f64) -> Dielectric {
Dielectric { refraction }
}
// 折射
fn refract(uv: Vector3, n: Vector3, f: f64) -> Vector3 {
let cos_theta = (-1.0 * uv).dot(n).min(1.0);
let r = f * (uv + cos_theta * n);
r - (1.0 - r.dot(r)).abs().sqrt() * n
}
fn reflectance(cosine: f64, ref_idx: f64) -> f64 {
let r0 = ((1.0 - ref_idx) / (1.0 + ref_idx)).powi(2);
r0 + (1.0 - r0) * (1.0 - cosine).powi(5)
}
}
Material
impl Material for Dielectric {
fn scatter(&self, ray: &Ray, record: &HitRecord) -> Option<(Color, Ray)> {
let ratio = match record.face {
true => 1.0 / self.refraction,
false => self.refraction,
};
let normal = record.normal;
let direction = ray.direction().unit();
let cos = (-1.0 * direction).dot(normal).min(1.0);
let sin = (1.0 - cos.powi(2)).sqrt();
let mut rand = rand::thread_rng();
let cannot_refract = ratio * sin > 1.0;
let will_reflect = Self::reflectance(cos, ratio) > rand.gen();
let direction = match cannot_refract || will_reflect {
true => direction - 2.0 * direction.dot(normal) * normal,
false => Self::refract(direction, normal, ratio),
};
let scattered = Ray::new(record.point, direction);
Some((Color::new(1.0, 1.0, 1.0), scattered))
}
}
main.rs
use std::rc::Rc;
use camera::Camera;
use hittable::{Hit, World};
use material::{Lambert, Metal};
use rand::Rng;
use ray::Ray;
use sphere::Sphere;
use vector3::{Color, Point3};
use crate::material::Dielectric;
mod camera;
mod hittable;
mod material;
mod ray;
mod sphere;
mod vector3;
fn main() {
// 图片的比例,和宽高
const WIDTH: u64 = 400;
const HEIGHT: u64 = (WIDTH as f64 / camera::RATIO) as u64;
const SAMPLES_PER_PIXEL: u64 = 100;
const MAX_DEPTH: u64 = 50;
// 相机
let camera = Camera::new();
// 输出图片,第一行输出 P3,表示像素图
let mut content = String::from("P3");
// 输出宽和高,和最大颜色值
content.push_str(&format!("\n{WIDTH} {HEIGHT}\n255\n"));
let ground = Rc::new(Lambert::new(Color::new(0.8, 0.8, 0.0)));
let center = Rc::new(Lambert::new(Color::new(0.1, 0.2, 0.5)));
let left = Rc::new(Dielectric::new(1.5));
let left2 = Rc::new(Dielectric::new(1.5));
let right = Rc::new(Metal::new(Color::new(0.8, 0.6, 0.2), 0.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.4, left2)),
Box::new(Sphere::new(Point3::new(1.0, 0.0, -1.0), 0.5, right)),
];
let mut rng = rand::thread_rng();
for j in (0..HEIGHT).rev() {
// 进度
eprintln!("Scan lines remaining: {j}");
for i in 0..WIDTH {
let mut color = Color::default();
for _ in 0..SAMPLES_PER_PIXEL {
let random_u: f64 = rng.gen();
let random_v: f64 = rng.gen();
let u = ((i as f64) + random_u) / ((WIDTH - 1) as f64);
let v = ((j as f64) + random_v) / ((HEIGHT - 1) as f64);
color += ray_color(&camera.get_ray(u, v), &world, MAX_DEPTH);
}
content.push_str(&color.format_str(SAMPLES_PER_PIXEL as f64));
}
}
println!("{}", content);
eprintln!("Done.");
}
// 光线的颜色计算
fn ray_color(ray: &Ray, hittable: &dyn Hit, depth: u64) -> Color {
// 超过最大深度,直接变成黑色
if depth == 0 {
return Color::new(0.0, 0.0, 0.0);
}
// 射线命中物体
if let Some(record) = hittable.hit(ray, 0.001, f64::INFINITY) {
// 命中物体根据材料散射光线
return match record.material.scatter(ray, &record) {
Some((attenuation, scattered)) => {
attenuation * ray_color(&scattered, hittable, depth - 1)
}
None => Color::new(0.0, 0.0, 0.0),
};
}
// 射线未命中,射线的单位向量
let unit = ray.direction().unit();
// 因为需要得到上下渐变的背景图,所以需要对 y 进行插值。
let t = 0.5 * (unit.y + 1.0);
// 线性插值,根据不同的光线得到在下面这个范围里的不同的颜色,并且是渐变色。
(1.0 - t) * Color::new(1.0, 1.0, 1.0) + t * Color::new(0.5, 0.7, 1.0)
}
效果
总结
新增了反射功能。