Fix color matching function

This commit is contained in:
hal8174 2025-05-25 19:55:38 +02:00
parent e3009563ba
commit 543bf7e1bf
Signed by: hal8174
SSH key fingerprint: SHA256:JwuqS+eVfISfKr+DkDQ6NWAbGd1jFAHkPpCM1yCnlTs
6 changed files with 241 additions and 73 deletions

View file

@ -2,7 +2,7 @@ use core::f32;
use ray_tracing_core::prelude::*;
#[derive(Debug)]
#[derive(Debug, Clone, Copy)]
pub struct Iridescent {
d1: f32,
d2: f32,
@ -49,12 +49,12 @@ impl Iridescent {
let imag = (num_imag * denom_real - num_real * denom_imag)
/ (denom_real * denom_real + denom_imag * denom_imag);
if !real.is_normal() || !imag.is_normal() {
dbg!(
l, theta1, theta2, s_polaized, r12, phi, num_real, num_imag, denom_real,
denom_imag, real, imag
);
}
// if !real.is_normal() || !imag.is_normal() {
// dbg!(
// l, theta1, theta2, s_polaized, r12, phi, num_real, num_imag, denom_real,
// denom_imag, real, imag
// );
// }
real * real + imag * imag
}
@ -87,34 +87,34 @@ impl Iridescent {
let local_c_squred = self.abs_C_squared(l, theta1, theta2, s_polaized);
if !local_c_squred.is_normal() || !local_cos_k_delta.is_normal() {
dbg!((
l,
theta1,
theta2,
local_cos_k_delta,
local_c_squred,
s_polaized
));
}
// if !local_c_squred.is_normal() || !local_cos_k_delta.is_normal() {
// dbg!((
// l,
// theta1,
// theta2,
// local_cos_k_delta,
// local_c_squred,
// s_polaized
// ));
// }
if local_cos_k_delta.abs() < 1.0 {
let k_delta = f32::acos(local_cos_k_delta);
let u = f32::sin(k_delta) / f32::sin(self.n * k_delta);
if !k_delta.is_normal() || !u.is_normal() {
dbg!((
l,
theta1,
theta2,
local_cos_k_delta,
local_c_squred,
s_polaized,
k_delta,
u,
));
}
// if !k_delta.is_normal() || !u.is_normal() {
// dbg!((
// l,
// theta1,
// theta2,
// local_cos_k_delta,
// local_c_squred,
// s_polaized,
// k_delta,
// u,
// ));
// }
local_c_squred / (local_c_squred + u * u)
} else if local_cos_k_delta.abs() > 1.0 {
@ -124,40 +124,40 @@ impl Iridescent {
let u = f32::sinh(imk_delta) / f32::sinh(self.n * imk_delta);
if !imk_delta.is_normal() || !u.is_normal() {
dbg!((
l,
theta1,
theta2,
local_cos_k_delta,
local_c_squred,
s_polaized,
imk_delta,
u
));
}
// if !imk_delta.is_normal() || !u.is_normal() {
// dbg!((
// l,
// theta1,
// theta2,
// local_cos_k_delta,
// local_c_squred,
// s_polaized,
// imk_delta,
// u
// ));
// }
local_c_squred / (local_c_squred + u * u)
} else {
let u = 1.0 / self.n;
if !u.is_normal() {
dbg!((
l,
theta1,
theta2,
local_cos_k_delta,
local_c_squred,
s_polaized,
u
));
}
// if !u.is_normal() {
// dbg!((
// l,
// theta1,
// theta2,
// local_cos_k_delta,
// local_c_squred,
// s_polaized,
// u
// ));
// }
local_c_squred / (local_c_squred + u * u)
}
}
fn averaged_reflectance(&self, l: f32, theta1: f32) -> f32 {
pub fn averaged_reflectance(&self, l: f32, theta1: f32) -> f32 {
0.5 * (self.reflectance(l, theta1, true) + self.reflectance(l, theta1, false))
}
@ -183,25 +183,25 @@ impl Iridescent {
)
}
fn monte_carlo_reflectance<R: Rng>(&self, theta1: f32, rng: &mut R) -> Color {
pub fn monte_carlo_reflectance<R: Rng>(&self, theta1: f32, rng: &mut R) -> Color {
let range = 400.0..800.0;
let size = range.end - range.start;
// let size = range.end - range.start;
let l = rng.gen_range(range);
let r = self.averaged_reflectance(l, theta1);
if !r.is_normal() {
dbg!((theta1, l, r));
}
// if !r.is_normal() {
// dbg!((theta1, l, r));
// }
let color = Self::color_matching_function(l);
if !color.r().is_normal() || !color.g().is_normal() || !color.b().is_normal() {
dbg!(color);
}
// if !color.r().is_normal() || !color.g().is_normal() || !color.b().is_normal() {
// dbg!(color);
// }
color * r
color * r * (1.0 / 0.27)
}
}