Compare commits

..

No commits in common. "59bc5b640f515318c0af780921bcf9eeb2dac0e8" and "295037edc6a22774ae76cb3a9b26d97766e0db5d" have entirely different histories.

View File

@ -249,6 +249,26 @@ impl Core {
sphere(vec3(0.1, 0.3, 0.1)),
],
};
if args.show_shapes {
let mut meshes = Vec::new();
for obj in &scene.objects {
let mesh = shape::sphere((obj.radius * 15.) as usize + 7);
let obj_mesh = render::faces::Mesh::new(
&self.device,
&mesh
.vertices
.iter()
.map(|v| render::faces::Vertex {
pos: obj.position + obj.radius * v,
color: 0.5 * (v + 1.),
})
.collect::<Vec<_>>(),
bytemuck::cast_slice(&mesh.indices),
);
meshes.push(obj_mesh);
}
self.mesh_pipe.render(&mut pass, &meshes);
}
let mut prng = rand_pcg::Pcg64::new(42, 0);
let source_rays: Vec<Ray> = (0..10240).map(|_| source.make_ray(&mut prng)).collect();
@ -326,13 +346,16 @@ impl Core {
hits1 = hits2;
}
}
let light_at = {
let mut camera_ray_display: Vec<Vertex> = Vec::with_capacity(camera_rays.len());
if args.show_light {
let sigma2 = args.accum_sigma.powi(2);
let accum_normalizator = (2. * PI * sigma2).sqrt().recip();
let hits = &hits;
move |hit: Hit| -> f32 {
for ray in camera_rays {
let Some(hit) = scene.trace_ray(ray) else {
continue;
};
let mut total_cd = 0.0f32;
for light_hit in hits {
for light_hit in &hits {
let d2 = hit.incident.base.distance_squared(light_hit.incident.base);
if d2 > 9. * sigma2 {
continue;
@ -345,28 +368,14 @@ impl Core {
assert!(hit.incident.dir.is_normalized());
let reflector = Lambertian;
let in_lm = 1.0;
let out_cd =
in_lm * reflector.brdf(normal, light_hit.incident.dir, -hit.incident.dir);
let out_cd = in_lm * reflector.brdf(normal, light_hit.incident.dir, -ray.dir);
let weight = accum_normalizator * (-0.5 * d2 / sigma2).exp();
total_cd += weight * out_cd;
}
total_cd * args.accum_scale
}
};
let colormap = |light: f32| {
let brightness = 3. * (1. - (1. + light).recip());
vec3(brightness, brightness - 1., brightness - 2.)
.clamp(Vec3::splat(0.), Vec3::splat(1.))
};
let mut camera_ray_display: Vec<Vertex> = Vec::with_capacity(camera_rays.len());
if args.show_light {
let r = args.accum_sigma;
for ray in camera_rays {
let Some(hit) = scene.trace_ray(ray) else {
continue;
};
let light = light_at(hit);
let color = colormap(light);
let brightness = 3. * (1. - (1. + total_cd * args.accum_scale).recip());
let r = args.accum_sigma;
let color = vec3(brightness, brightness - 1., brightness - 2.)
.clamp(Vec3::splat(0.), Vec3::splat(1.));
let vertex = |off: Vec3| Vertex {
pos: hit.incident.base + r * off,
color,
@ -381,33 +390,6 @@ impl Core {
]);
}
}
if args.show_shapes {
let mut meshes = Vec::new();
for obj in &scene.objects {
let mesh = shape::sphere((obj.radius * 45.) as usize + 7);
let obj_mesh = render::faces::Mesh::new(
&self.device,
&mesh
.vertices
.iter()
.map(|&v| {
let pos = obj.position + obj.radius * v;
let normal = v;
let dir = (pos - camera.position()).normalize();
let light = light_at(Hit {
incident: Ray::new(pos, dir),
normal,
});
let color = colormap(light);
render::faces::Vertex { pos, color }
})
.collect::<Vec<_>>(),
bytemuck::cast_slice(&mesh.indices),
);
meshes.push(obj_mesh);
}
self.mesh_pipe.render(&mut pass, &meshes);
}
if !source_ray_display.is_empty() {
self.pipeline
.render(&mut pass, [&Mesh::new(&self.device, &source_ray_display)]);