From 966aabc6490d8c3b55e43ebe68f6e4198a9ce0d4 Mon Sep 17 00:00:00 2001 From: Verox001 Date: Tue, 6 May 2025 18:13:42 +0200 Subject: [PATCH] Made the Sun a directional light source and fixed some lighting bugs, which affect the sun to have a shadow --- simulator/src/main.rs | 2 ++ solar_engine/src/render.rs | 5 +++++ solar_engine/src/shader.wgsl | 22 ++++++++++++++++------ 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/simulator/src/main.rs b/simulator/src/main.rs index c5e3e22..e6b9afb 100644 --- a/simulator/src/main.rs +++ b/simulator/src/main.rs @@ -85,6 +85,8 @@ pub async fn run() { }, scale: 0.05, shape: solar_engine::Shape::Sphere, + always_lit: i == 0, // Sun + is_transparent: false } }) .collect(); diff --git a/solar_engine/src/render.rs b/solar_engine/src/render.rs index 5b23370..da1f440 100644 --- a/solar_engine/src/render.rs +++ b/solar_engine/src/render.rs @@ -22,6 +22,8 @@ pub struct RenderInstance { pub color: [f32; 3], pub scale: f32, pub shape: Shape, + pub always_lit: bool, + pub is_transparent: bool } impl RenderInstance { @@ -32,6 +34,7 @@ impl RenderInstance { InstanceRaw { model: model.into(), color: self.color, + flags: (self.always_lit as u32) | ((self.is_transparent as u32) << 1) } } } @@ -41,6 +44,7 @@ impl RenderInstance { pub struct InstanceRaw { model: [[f32; 4]; 4], color: [f32; 3], + flags: u32, } impl InstanceRaw { @@ -54,6 +58,7 @@ impl InstanceRaw { wgpu::VertexAttribute { offset: 32, shader_location: 7, format: wgpu::VertexFormat::Float32x4 }, wgpu::VertexAttribute { offset: 48, shader_location: 8, format: wgpu::VertexFormat::Float32x4 }, wgpu::VertexAttribute { offset: 64, shader_location: 9, format: wgpu::VertexFormat::Float32x3 }, + wgpu::VertexAttribute { offset: 76, shader_location: 10, format: wgpu::VertexFormat::Uint32 }, ], } } diff --git a/solar_engine/src/shader.wgsl b/solar_engine/src/shader.wgsl index 4bd2e89..2368ee3 100644 --- a/solar_engine/src/shader.wgsl +++ b/solar_engine/src/shader.wgsl @@ -10,6 +10,7 @@ struct InstanceInput { @location(7) model_row2: vec4, @location(8) model_row3: vec4, @location(9) color: vec3, + @location(10) flags: u32, }; struct VSOutput { @@ -17,6 +18,7 @@ struct VSOutput { @location(0) frag_color: vec3, @location(1) world_pos: vec3, @location(2) normal: vec3, + @location(3) flags: u32, }; struct Globals { @@ -57,19 +59,27 @@ fn vs_main(vertex: VertexInput, instance: InstanceInput) -> VSOutput { out.frag_color = instance.color * vertex.color; out.world_pos = world_position; out.normal = normalize(normal_matrix * vertex.normal); + out.flags = instance.flags; return out; } @fragment fn fs_main(input: VSOutput) -> @location(0) vec4 { - var lighting: vec3 = vec3(0.0); + var lighting: vec3; - for (var i = 0u; i < 10u; i = i + 1u) { - let light = lights[i]; - let light_dir = normalize(light.position - input.world_pos); - let diff = max(dot(input.normal, light_dir), 0.0); - lighting += light.color * light.intensity * diff; + let always_lit = (input.flags & 0x1u) != 0u; + + if (always_lit) { + return vec4(input.frag_color * 2.0, 1.0); + } else { + lighting = vec3(0.0); + for (var i = 0u; i < 10u; i = i + 1u) { + let light = lights[i]; + let light_dir = normalize(light.position - input.world_pos); + let diff = max(dot(input.normal, light_dir), 0.0); + lighting += light.color * light.intensity * diff; + } } let final_color = input.frag_color * lighting;