From 988a418f8907fb5e3abb77222929d6ab7b60c1b6 Mon Sep 17 00:00:00 2001 From: Rosyid Haryadi Date: Mon, 3 Mar 2025 01:55:16 +0700 Subject: upd transparent material --- src/calculus.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'src/calculus.rs') diff --git a/src/calculus.rs b/src/calculus.rs index 568eb60..2e8a1d5 100644 --- a/src/calculus.rs +++ b/src/calculus.rs @@ -119,6 +119,19 @@ pub mod calculus { ) ) } + + pub fn refract(&self, normal: &Self, etai_over_etat: f32) -> Self { + let cos_theta = f32::min( + 1.0, + self.scalar_mul(-1.0).dot_prod(&normal) + ); + let r_out_perp = self + .add(&normal.scalar_mul(cos_theta)) + .scalar_mul(etai_over_etat); + let f = (1.0 - r_out_perp.mag_sqr()).abs().sqrt(); + let r_out_parallel = normal.scalar_mul(-1.0 * f); + r_out_perp.add(&r_out_parallel) + } } pub struct Ray { @@ -133,7 +146,13 @@ pub mod calculus { } pub fn reflect(&self, normal: &Vec3) -> Vec3 { - self.direction.reflect(&normal) + // return unit direction vector + self.direction.reflect(&normal).unit() + } + + pub fn refract(&self, normal: &Vec3, etai_over_etat: f32) -> Vec3 { + // return unit direction vector + self.direction.unit().refract(&normal, etai_over_etat) } } -- cgit v1.2.3-70-g09d2