首页 > 其他分享 >0203-透明材质的反射

0203-透明材质的反射

时间:2024-07-22 09:32:00浏览次数:7  
标签:反射 1.0 0.0 let 0203 new 材质 f64 ray

环境

  • 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)
}

效果

反射

总结

新增了反射功能。

附录

标签:反射,1.0,0.0,let,0203,new,材质,f64,ray
From: https://www.cnblogs.com/jiangbo4444/p/18315382

相关文章

  • 0201-金属反射和模糊
    环境Time2022-11-16WSL-Ubuntu22.04Rust1.65.0前言说明参考:https://raytracing.github.io/books/RayTracingInOneWeekend.html目标新增金属材质,实现反射和模糊函数。金属材质pubstructMetal{albedo:Color,//模糊属性fuzz:f64,}implMetal......
  • 深入探究 Golang 反射:功能与原理及应用
    Hi亲爱的朋友们,我是k哥。今天,咱们来一同探讨下Golang反射。Go出于通用性的考量,提供了反射这一功能。借助反射功能,我们可以实现通用性更强的函数,传入任意的参数,在函数内通过反射动态调用参数对象的方法并访问它的属性。举例来说,下面的bridge接口为了支持灵活调用任意函数,在......
  • Java 语言及其常用集合类的操作,以及反射机制与注解
    目录一、Java语言概述二、Java集合框架ArrayList操作示例:HashMap操作示例:三、反射机制1.反射的示例五、总结Java是一种广泛使用的高级编程语言,因其平台独立性、简洁性及丰富的API而备受开发者青睐。一、Java语言概述 Java语言由JamesGosling等人......
  • Java反射
    java是面向对象的编程语言,对象,是一种具体的概念类:类是对具有相同特征或属性,具有相同行为能力的一类事物的描述或称呼对象:对象是这一类事物带有属性值,具有具体行为的个体或实例面向对象编程的语言packagetest;//定义一个类publicclassPhone{//添加一些属性......
  • 0200-材质和散射
    环境Time2022-11-16WSL-Ubuntu22.04Rust1.65.0前言说明参考:https://raytracing.github.io/books/RayTracingInOneWeekend.html目标不同的材质有不同的散射光线和衰减,这一节将不同的材质进行抽象。向量的乘法implMulforVector3{typeOutput=Self;f......
  • 0199-漫反射和伽马校正
    环境Time2022-11-16WSL-Ubuntu22.04Rust1.65.0前言说明参考:https://raytracing.github.io/books/RayTracingInOneWeekend.html目标对物体上的光线进行漫反射,然后增加伽马校正。颜色显示函数pubfnformat_str(&self,samples:f64)->String{letir=(256.......
  • 为什么反射慢?
    反射机制就是通过字节码文件对象获取成员变量、成员方法和构造方法,然后进一步获取它们的具体信息,如名字、修饰符、类型等。反射机制的性能较低有很多原因,这里详细总结以下4点原因:(1)JIT优化受限:JIT编译器的优化是基于静态分析和预测的。反射是一种在运行时动态解析类型信息的机......
  • 反射快速入门
    反射就是通过字节码文件获取类的成员变量、构造方法和成员方法的所有信息。利用反射,我们可以获取成员变量的修饰符、名字、类型、取值。我们可以获取构造方法的名字、形参,并利用通过反射获取的构造方法创建对象。我们可以获取成员方法的修饰符、名字、形参、返回值、抛出的异常、......
  • 反射工具类
    InvokeUtils代码如下:packagecom.ksource.utils;importorg.apache.commons.collections4.MultiValuedMap;importjava.lang.reflect.Method;importjava.util.Collection;/***@Authordxy*@Date2024/7/1617:54*@Description*/publicclassInvokeUtils{......
  • UE4材质笔记
    常用节点:添加贴图:TextureSample(贴图采样)常量数字:Constant(数字)Vector或长按数字并点击左键乘法:Multiply或长按M并点击左键常用插值:Lerp或长按L并点击左键加法:Add或者长按A键并点击左键减法:Subtract除法:Divide或者长按D键并点击左键1-x:搜索1-或者oneminus或者长按O键并点击左键绝......