From 59bc5b640f515318c0af780921bcf9eeb2dac0e8 Mon Sep 17 00:00:00 2001 From: numzero Date: Mon, 24 Nov 2025 18:31:23 +0300 Subject: [PATCH] per-vertex lighting! --- src/lib.rs | 47 +++++++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d3713d7..4393e0e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -249,26 +249,6 @@ 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::>(), - 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 = (0..10240).map(|_| source.make_ray(&mut prng)).collect(); @@ -401,6 +381,33 @@ 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::>(), + 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)]);