Basic fog lighting, refactoring

This commit is contained in:
Lauri Räsänen 2023-11-09 21:04:59 +02:00
parent 5a8dec8d02
commit 1c0b9aa63f
10 changed files with 203 additions and 124 deletions

View file

@ -1,26 +1,29 @@
#include globals.wgsl
fn sample_direct_light(index: i32, light_coords: vec4<f32>) -> f32 {
if (light_coords.w <= 0.0) {
return 0.0;
}
struct LightVertexInput {
@location(0) position: vec3<f32>,
};
let flip_correction = vec2<f32>(0.5, -0.5);
let proj_correction = 1.0 / light_coords.w;
let light_local = light_coords.xy * flip_correction * proj_correction + vec2<f32>(0.5, 0.5);
let bias = 0.000001;
let reference_depth = light_coords.z * proj_correction - bias;
struct LightVertexOutput {
@builtin(position) clip_position: vec4<f32>,
@location(0) color: vec3<f32>,
};
var total_sample = 0.0;
for (var x: i32 = -SHADOW_SAMPLES; x < SHADOW_SAMPLES; x++) {
for (var y: i32 = -SHADOW_SAMPLES; y < SHADOW_SAMPLES; y++) {
let texelSize = vec2<f32>(textureDimensions(t_light_depth));
let offset = vec2<f32>(f32(x), f32(y)) / texelSize.xy;
let s = textureSampleCompare(
t_light_depth,
s_light_depth,
light_local + offset,
index,
reference_depth
);
total_sample += s * INV_SHADOW_SAMPLES;
}
}
@vertex
fn vs_main(
model: LightVertexInput,
) -> LightVertexOutput {
let scale = 10.0;
var out: LightVertexOutput;
out.clip_position = camera.proj * camera.view * vec4<f32>(model.position * scale + light.position, 1.0);
out.color = light.color.xyz;
return out;
}
@fragment
fn fs_main(in: LightVertexOutput) -> @location(0) vec4<f32> {
return vec4<f32>(in.color, 1.0);
return total_sample;
}