You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rust版《Ray Tracing in One Weekend》玻璃表面折射失效问题排查

玻璃材质折射效果异常排查求助

我正在用Rust实现《Ray Tracing in One Weekend》教程,添加Dielectric(玻璃)材质后,玻璃球体的折射效果出现严重异常。对比Rust原版及C++基础版本代码后,数学逻辑看似无误,但生成效果与预期差距明显。

预期效果:
Expected

实际生成效果:
Actual

我的项目代码托管在GitHub上。以下是添加的折射函数及玻璃材质代码:

折射函数代码

pub fn refract(self, n: Vec3, etai_over_etat: f64) -> Vec3 {
   let cos_theta = (-1.0 * self).dot(n).min(1.0);
   let r_out_perp = etai_over_etat * (self + cos_theta * n);
   let r_out_parallel = -(1.0 - r_out_perp.length().powi(2)).abs().sqrt() * n;
   r_out_perp + r_out_parallel
}

玻璃材质代码

pub struct Dielectric {
 ir: f64,
}

impl Dielectric {
  pub fn new(index_of_refraction: f64) -> Dielectric {
    Dielectric {
        ir: index_of_refraction,
    }
}


impl Scatter for Dielectric {
  fn scatter(&self, r_in: &Ray, rec: &HitRecord) -> Option<(Color, Ray)> {
     let refraction_ratio = if rec.front_face {
         1.0 / self.ir
     } else {
        self.ir
     };

     let unit_direction = r_in.direction();
     let refracted = unit_direction.refract(rec.normal, refraction_ratio);
     let scattered = Ray::new(rec.p, refracted);

     Some((Color::new(1.0, 1.0, 1.0), scattered))
}

我回溯了教程多个章节的代码,未发现明显遗漏,对照Snell定律也未找到数学错误。不确定是折射逻辑本身存在问题,还是前期代码的隐藏bug在添加折射功能后暴露,恳请提供排查方向。

内容的提问来源于stack exchange,提问作者Jack Jutzi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 12:01:20