Finished Ray Tracing lighting

This commit is contained in:
Verox001 2025-01-16 13:18:43 +01:00
parent d7378ab5ca
commit b8cc592235
2 changed files with 15 additions and 5 deletions

View File

@ -56,19 +56,23 @@ impl Camera {
} }
#[repr(C)] #[repr(C)]
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)] #[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
struct CameraUniform { struct CameraUniform {
view_position: [f32; 4],
view_proj: [[f32; 4]; 4], view_proj: [[f32; 4]; 4],
} }
impl CameraUniform { impl CameraUniform {
fn new() -> Self { fn new() -> Self {
Self { Self {
view_position: [0.0; 4],
view_proj: cgmath::Matrix4::identity().into(), view_proj: cgmath::Matrix4::identity().into(),
} }
} }
fn update_view_proj(&mut self, camera: &Camera) { fn update_view_proj(&mut self, camera: &Camera) {
// We're using Vector4 because of the uniforms 16 byte spacing requirement
self.view_position = camera.eye.to_homogeneous().into();
self.view_proj = (OPENGL_TO_WGPU_MATRIX * camera.build_view_projection_matrix()).into(); self.view_proj = (OPENGL_TO_WGPU_MATRIX * camera.build_view_projection_matrix()).into();
} }
} }
@ -467,7 +471,7 @@ impl<'a> State<'a> {
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
entries: &[wgpu::BindGroupLayoutEntry { entries: &[wgpu::BindGroupLayoutEntry {
binding: 0, binding: 0,
visibility: wgpu::ShaderStages::VERTEX, visibility: wgpu::ShaderStages::VERTEX | wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Buffer { ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform, ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false, has_dynamic_offset: false,
@ -634,7 +638,6 @@ impl<'a> State<'a> {
self.camera_controller.update_camera(&mut self.camera); self.camera_controller.update_camera(&mut self.camera);
log::info!("{:?}", self.camera); log::info!("{:?}", self.camera);
self.camera_uniform.update_view_proj(&self.camera); self.camera_uniform.update_view_proj(&self.camera);
log::info!("{:?}", self.camera_uniform);
self.queue.write_buffer( self.queue.write_buffer(
&self.camera_buffer, &self.camera_buffer,
0, 0,

View File

@ -8,6 +8,7 @@ struct Light {
var<uniform> light: Light; var<uniform> light: Light;
struct Camera { struct Camera {
view_pos: vec4<f32>,
view_proj: mat4x4<f32>, view_proj: mat4x4<f32>,
} }
@group(1) @binding(0) @group(1) @binding(0)
@ -74,14 +75,20 @@ fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
let light_dir = normalize(light.position - in.world_position); let light_dir = normalize(light.position - in.world_position);
let view_dir = normalize(camera.view_pos.xyz - in.world_position);
let half_dir = normalize(view_dir + light_dir);
let specular_strength = pow(max(dot(in.world_normal, half_dir), 0.0), 32.0);
let specular_color = specular_strength * light.color;
let diffuse_strength = max(dot(in.world_normal, light_dir), 0.0); let diffuse_strength = max(dot(in.world_normal, light_dir), 0.0);
let diffuse_color = light.color * diffuse_strength; let diffuse_color = light.color * diffuse_strength;
// We don't need (or want) much ambient light, so 0.1 is fine // We don't need (or want) much ambient light, so 0.1 is fine
let ambient_strength = 0.1; let ambient_strength = 0.03;
let ambient_color = light.color * ambient_strength; let ambient_color = light.color * ambient_strength;
let result = (ambient_color + diffuse_color) * object_color.xyz; let result = (ambient_color + diffuse_color + specular_color) * object_color.xyz;
return vec4<f32>(result, object_color.a); return vec4<f32>(result, object_color.a);
} }