0205-增加景深
环境
- Time 2022-11-17
- WSL-Ubuntu 22.04
- Rust 1.65.0
前言
说明
参考:https://raytracing.github.io/books/RayTracingInOneWeekend.html
目标
在相机不对焦的地方,会出现模糊效果,这一节实现这种效果。
vector3.rs
pub fn random_in_unit_disk() -> Vector3 {
let mut rand = rand::thread_rng();
loop {
let p = Vector3::new(rand.gen_range(-1.0..1.0), rand.gen_range(-1.0..1.0), 0.0);
if p.length() < 1.0 {
return p;
}
}
}
camera.rs
use super::ray::Ray;
use super::vector3::{Point3, Vector3};
pub struct Camera {
origin: Point3,
corner: Point3,
horizontal: Vector3,
vertical: Vector3,
cu: Vector3,
cv: Vector3,
radius: f64,
}
pub const RATIO: f64 = 16.0 / 9.0;
impl Camera {
pub fn new(
origin: Point3,
at: Point3,
vup: Vector3,
fov: f64,
ratio: f64,
aperture: f64,
focus: f64,
) -> Camera {
let theta = std::f64::consts::PI / 180.0 * fov;
let viewport_height = 2.0 * (theta / 2.0).tan();
let viewport_width = ratio * viewport_height;
let cw = (origin - at).unit();
let cu = vup.cross(cw);
let cv = cw.cross(cu);
let horizontal = focus * viewport_width * cu;
let vertical = focus * viewport_height * cv;
let corner = origin - horizontal / 2.0 - vertical / 2.0 - focus * cw;
Camera {
origin,
horizontal,
vertical,
corner,
cu,
cv,
radius: aperture / 2.0,
}
}
pub fn get_ray(&self, u: f64, v: f64) -> Ray {
let rd = self.radius * Vector3::random_in_unit_disk();
let offset = self.cu * rd.x + self.cv * rd.y;
let vector3 = self.corner + u * self.horizontal + v * self.vertical;
Ray {
origin: self.origin + offset,
direction: vector3 - self.origin - offset,
}
}
}
相机
// 相机
let from = Point3::new(3.0, 3.0, 2.0);
let at = Point3::new(0.0, 0.0, -1.0);
let camera = Camera::new(
from,
at,
Vector3::new(0.0, 1.0, 0.0),
20.0,
camera::RATIO,
2.0,
(from - at).length(),
);
效果
总结
实现了景深效果。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
· 凌晨三点救火实录:Java内存泄漏的七个神坑,你至少踩过三个!
2020-07-22 【JavaScript】对象构建实践