use Lambertian scattering

This commit is contained in:
numzero 2025-11-16 19:39:36 +03:00
parent 64925f9640
commit 12bc173fb2
2 changed files with 36 additions and 13 deletions

View File

@ -9,7 +9,7 @@ use crate::{
camera::OrbitalCamera, camera::OrbitalCamera,
ray::Ray, ray::Ray,
render::lines::{LookParams, Mesh, Pipeline, Vertex}, render::lines::{LookParams, Mesh, Pipeline, Vertex},
trace::{Scene, Source, Sphere}, trace::{Hit, Scene, Source, Sphere},
}; };
mod camera; mod camera;
@ -212,7 +212,7 @@ impl Core {
let source_rays: Vec<Ray> = (0..10240).map(|_| source.make_ray(&mut prng)).collect(); let source_rays: Vec<Ray> = (0..10240).map(|_| source.make_ray(&mut prng)).collect();
let camera_rays: Vec<Ray> = (0..10240).map(|_| camera.make_ray(&mut prng)).collect(); let camera_rays: Vec<Ray> = (0..10240).map(|_| camera.make_ray(&mut prng)).collect();
let mut source_ray_display: Vec<Vertex> = Vec::with_capacity(source_rays.len()); let mut source_ray_display: Vec<Vertex> = Vec::with_capacity(source_rays.len());
let mut hits: Vec<Ray> = Vec::with_capacity(source_rays.len()); let mut hits: Vec<Hit> = Vec::with_capacity(source_rays.len());
for ray in source_rays { for ray in source_rays {
if let Some(hit) = scene.trace_ray(ray) { if let Some(hit) = scene.trace_ray(ray) {
hits.push(hit); hits.push(hit);
@ -226,11 +226,11 @@ impl Core {
color: vec3(0., 1., 0.), color: vec3(0., 1., 0.),
}, },
Vertex { Vertex {
pos: hit.base - 0.02 * hit.dir, pos: hit.incident.base - 0.02 * hit.incident.dir,
color: vec3(0., 0., 1.), color: vec3(0., 0., 1.),
}, },
Vertex { Vertex {
pos: hit.base, pos: hit.incident.base,
color: vec3(1., 1., 1.), color: vec3(1., 1., 1.),
}, },
]); ]);
@ -256,19 +256,21 @@ impl Core {
}; };
let mut value = 0.0f32; let mut value = 0.0f32;
for light_hit in &hits { for light_hit in &hits {
let d2 = hit.base.distance_squared(light_hit.base); let d2 = hit.incident.base.distance_squared(light_hit.incident.base);
if d2 > 3. * sigma2 { if d2 > 3. * sigma2 {
continue; continue;
} }
assert!(hit.incident.dir.is_normalized());
let radiance = hit.normal.dot(-hit.incident.dir);
let w = (-0.5 * d2 / sigma2).exp(); let w = (-0.5 * d2 / sigma2).exp();
value += w; value += w * radiance;
} }
value *= weight; value *= weight;
value = 3. * (1. - (1. + value).recip()); value = 3. * (1. - (1. + value).recip());
let r = args.accum_sigma; let r = args.accum_sigma;
let color = vec3(value, value - 1., value - 2.).clamp(Vec3::splat(0.), Vec3::splat(1.)); let color = vec3(value, value - 1., value - 2.).clamp(Vec3::splat(0.), Vec3::splat(1.));
let vertex = |off: Vec3| Vertex { let vertex = |off: Vec3| Vertex {
pos: hit.base + r * off, pos: hit.incident.base + r * off,
color, color,
}; };
camera_ray_display.extend([ camera_ray_display.extend([

View File

@ -78,8 +78,14 @@ pub struct Sphere {
pub radius: f32, pub radius: f32,
} }
struct Hit1 {
pos: Vec3,
dist: f32,
normal: Vec3,
}
impl Sphere { impl Sphere {
fn trace_ray(&self, ray: Ray) -> Option<f32> { fn trace_ray(&self, ray: Ray) -> Option<Hit1> {
// let t: f32; // let t: f32;
// let hit = ray.base + t * ray.dir; // let hit = ray.base + t * ray.dir;
// (hit - self.position).length() == self.radius; // (hit - self.position).length() == self.radius;
@ -94,7 +100,10 @@ impl Sphere {
if d4 < 0. { if d4 < 0. {
return None; return None;
} }
Some((-b2 - d4.sqrt()) / a) let dist = (-b2 - d4.sqrt()) / a;
let pos = ray.advance(dist).base;
let normal = (pos - self.position).normalize();
Some(Hit1 { pos, dist, normal })
} }
} }
@ -103,13 +112,25 @@ pub struct Scene {
pub objects: Vec<Sphere>, pub objects: Vec<Sphere>,
} }
#[derive(Debug, Clone, Copy)]
pub struct Hit {
pub incident: Ray,
pub normal: Vec3,
}
impl Scene { impl Scene {
pub fn trace_ray(&self, ray: Ray) -> Option<Ray> { pub fn trace_ray(&self, ray: Ray) -> Option<Hit> {
let dist = self let hit = self
.objects .objects
.iter() .iter()
.filter_map(|obj| obj.trace_ray(ray)) .filter_map(|obj| obj.trace_ray(ray))
.min_by(f32::total_cmp); .min_by(|a, b| f32::total_cmp(&a.dist, &b.dist))?;
Some(ray.advance(dist?)) Some(Hit {
incident: Ray {
base: hit.pos,
dir: ray.dir,
},
normal: hit.normal,
})
} }
} }