Finished lighting and material reimplementation

This commit is contained in:
Verox001 2025-05-20 12:10:26 +02:00
parent acb7c13a5f
commit 786fff5a6d
3 changed files with 18 additions and 8 deletions

View File

@ -66,23 +66,26 @@ pub async fn run() {
state.light_manager.add_light(Light::new_point( state.light_manager.add_light(Light::new_point(
sun_pos.cast::<f32>().unwrap(), sun_pos.cast::<f32>().unwrap(),
Vector3::from([1.0, 1.0, 0.8]), Vector3::from([1.0, 1.0, 0.8]),
5.0, 1.0,
1.0 / 1000.0, 1.0 / 1000.0,
)); ));
let sun_material = GpuMaterial::new( let sun_material = GpuMaterial::new(
[1.0, 1.0, 0.0, 1.0], [1.0, 1.0, 0.0, 1.0],
[2.0, 2.0, 1.0], [1.0, 1.0, 0.01],
5.0,
); );
let earth_material = GpuMaterial::new( let earth_material = GpuMaterial::new(
[0.0, 0.0, 1.0, 1.0], [0.0, 0.0, 1.0, 1.0],
[0.0, 0.0, 0.0], [0.0, 0.0, 0.0],
1.0
); );
let default_material = GpuMaterial::new( let default_material = GpuMaterial::new(
[0.5, 0.5, 0.5, 1.0], [0.5, 0.5, 0.5, 1.0],
[0.0, 0.0, 0.0], [0.0, 0.0, 0.0],
1.0
); );
state.set_materials(vec![ state.set_materials(vec![

View File

@ -6,15 +6,15 @@ use wgpu::util::DeviceExt;
pub struct GpuMaterial { pub struct GpuMaterial {
pub base_color: [f32; 4], pub base_color: [f32; 4],
pub emission: [f32; 3], pub emission: [f32; 3],
_pad: f32, pub emissive_strength: f32,
} }
impl GpuMaterial { impl GpuMaterial {
pub fn new(base_color: [f32; 4], emission: [f32; 3]) -> Self { pub fn new(base_color: [f32; 4], emission: [f32; 3], emissive_strength: f32) -> Self {
Self { Self {
base_color, base_color,
emission, emission,
_pad: 0.0, emissive_strength,
} }
} }
} }
@ -48,7 +48,7 @@ impl MaterialManager {
GpuMaterial { GpuMaterial {
base_color: [1.0, 1.0, 1.0, 1.0], base_color: [1.0, 1.0, 1.0, 1.0],
emission: [0.0, 0.0, 0.0], emission: [0.0, 0.0, 0.0],
_pad: 0.0, emissive_strength: 0.0,
}, },
]; ];

View File

@ -59,7 +59,8 @@ var<storage, read> cluster_offsets: array<vec2<u32>>;
struct GpuMaterial { struct GpuMaterial {
base_color: vec4<f32>, base_color: vec4<f32>,
emission: vec3<f32> emission: vec3<f32>,
emissive_strength: f32,
}; };
@group(3) @binding(0) @group(3) @binding(0)
@ -106,6 +107,11 @@ fn compute_cluster_id(frag_coord: vec4<f32>, view_pos_z: f32, screen_size: vec2<
return x + y * CLUSTER_COUNT_X + z * CLUSTER_COUNT_X * CLUSTER_COUNT_Y; return x + y * CLUSTER_COUNT_X + z * CLUSTER_COUNT_X * CLUSTER_COUNT_Y;
} }
// Thanks Reinhard
fn tone_map(color: vec3<f32>) -> vec3<f32> {
return color / (color + vec3<f32>(1.0));
}
@fragment @fragment
fn fs_main(input: VSOutput) -> @location(0) vec4<f32> { fn fs_main(input: VSOutput) -> @location(0) vec4<f32> {
let material = materials[input.material_id]; let material = materials[input.material_id];
@ -151,6 +157,7 @@ fn fs_main(input: VSOutput) -> @location(0) vec4<f32> {
lighting += light_contrib; lighting += light_contrib;
} }
let final_rgb = material.base_color.rgb * lighting + material.emission; let emissive_rgb = material.emission * material.emissive_strength;
let final_rgb = tone_map(material.base_color.rgb * lighting + emissive_rgb);
return vec4<f32>(final_rgb, material.base_color.a); return vec4<f32>(final_rgb, material.base_color.a);
} }