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

@ -9,3 +9,5 @@ const FOG_MAX_STEPS = 20;
const FOG_MAX_DIST = 300.0;
const FOG_SCALE = 0.01;
const FOG_DENSITY = 1.0;
const FOG_ALPHA = 1.0;
const FOG_BLEND_DIST = 10.0;

View file

@ -1,10 +1,11 @@
#include globals.wgsl
#include constants.wgsl
#include globals.wgsl
#include light.wgsl
#include noise.wgsl
struct FogVertexOutput {
@builtin(position) clip_position: vec4<f32>,
@location(0) world_position: vec3<f32>,
@location(0) world_position: vec4<f32>,
@location(1) light_world_position: vec3<f32>,
}
@ -26,7 +27,7 @@ fn vs_main(
var out: FogVertexOutput;
out.clip_position = camera.proj * camera.view * world_position;
out.world_position = world_position.xyz / world_position.w;
out.world_position = world_position;
out.light_world_position = light.position;
return out;
@ -72,9 +73,11 @@ fn ray_march(origin: vec3<f32>, direction: vec3<f32>, scene_depth: f32) -> f32 {
var depth = 0.0;
for (var i = 0; i < FOG_MAX_STEPS; i++)
{
let noise = fog_noise(origin + direction * depth);
depth += FOG_MAX_DIST / f32(FOG_MAX_STEPS);
let p = origin + direction * depth;
density += fog_noise(p) * FOG_DENSITY / f32(FOG_MAX_STEPS);
let blend = min(depth / FOG_BLEND_DIST, 1.0);
let contribution = FOG_DENSITY / f32(FOG_MAX_STEPS);
density += blend * noise * contribution;
if (density >= 1.0)
{
density = 1.0;
@ -88,7 +91,6 @@ fn ray_march(origin: vec3<f32>, direction: vec3<f32>, scene_depth: f32) -> f32 {
return density;
}
// FIXME: always 0???
fn scene_depth(clip_position: vec4<f32>) -> f32 {
if (clip_position.w <= 0.0) {
return 0.0;
@ -96,24 +98,76 @@ fn scene_depth(clip_position: vec4<f32>) -> f32 {
let ndc = clip_position.xy / clip_position.w;
let uv = ndc * vec2<f32>(0.5, -0.5) + vec2<f32>(0.5, 0.5);
return textureSample(t_geometry_depth, s_geometry_depth, uv);
let depth = textureSample(t_geometry_depth, s_geometry_depth, uv);
// convert to linear [near, far] range
let z_near = camera.planes.x;
let z_far = camera.planes.y;
return z_near * z_far / (z_far + depth * (z_near - z_far));
}
@fragment
fn fs_main(vert: FogVertexOutput) -> @location(0) vec4<f32> {
var color = vec4<f32>(0.5, 0.5, 0.5, 1.0);
let cam_to_volume = vert.world_position.xyz - camera.position.xyz;
let distance_to_volume = length(cam_to_volume);
let direction = cam_to_volume / distance_to_volume;
// TODO: pass near and far plane in uniforms
let geometry_depth = scene_depth(vert.clip_position) * (3000.0 - 1.0) + 1.0 - distance_to_volume;
if (geometry_depth <= 0.0)
{
return vec4<f32>(0.0);
}
// FIXME: t_geometry_depth is 0
// let geometry_depth = scene_depth(vert.clip_position) - distance_to_volume;
// if (geometry_depth <= 0.0)
// {
// return vec4<f32>(0.0);
// }
let geometry_depth = 3000.0;
let density = ray_march(vert.world_position.xyz, direction, geometry_depth);
color.a *= density;
return color;
var in_light = 0.0;
if (global_uniforms.use_shadowmaps > 0u) {
for (var i: i32 = 0; i < 6; i++) {
let light_coords = light.matrices[i] * vert.world_position;
let light_dir = normalize(light_coords.xyz);
let bias = 0.01;
// z can never be smaller than this inside 90 degree frustum
if (light_dir.z < INV_SQRT_3 - bias) {
continue;
}
// x and y can never be larger than this inside frustum
if (abs(light_dir.y) > INV_SQRT_2 + bias) {
continue;
}
if (abs(light_dir.x) > INV_SQRT_2 + bias) {
continue;
}
in_light = sample_direct_light(i, light_coords);
// TODO should break even if 0 since we're inside frustum.
// See if causes issues with bias overlap between directions.
if (in_light > 0.0) {
break;
}
}
} else {
in_light = 1.0;
}
var color = vec3<f32>(0.5, 0.5, 0.5);
let ambient_strength = 0.02;
let ambient_color = color * ambient_strength;
var radiance = vec3<f32>(0.0);
if (in_light > 0.0) {
// attenuation
let light_dist = length(light.position - vert.world_position.xyz);
let coef_a = 0.0;
let coef_b = 1.0;
let light_attenuation = 1.0 / (1.0 + coef_a * light_dist + coef_b * light_dist * light_dist);
radiance = light.color.rgb * light.color.a * light_attenuation * in_light;
}
var result = ambient_color + radiance;
// tonemap
result = result / (result + vec3(1.0));
return vec4(result, density * FOG_ALPHA);
}

View file

@ -5,6 +5,7 @@ struct CameraUniform {
proj: mat4x4<f32>,
inv_view_proj: mat4x4<f32>,
position: vec4<f32>,
planes: vec4<f32>,
}
@group(0) @binding(0)
var<uniform> camera: CameraUniform;

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;
}

View file

@ -0,0 +1,26 @@
#include globals.wgsl
struct LightVertexInput {
@location(0) position: vec3<f32>,
};
struct LightVertexOutput {
@builtin(position) clip_position: vec4<f32>,
@location(0) color: vec3<f32>,
};
@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);
}

View file

@ -1,5 +1,6 @@
#include constants.wgsl
#include globals.wgsl
#include light.wgsl
#include brdf.wgsl
// Vertex shader
@ -66,36 +67,6 @@ var t_roughness_metalness: texture_2d<f32>;
@group(3) @binding(5)
var s_roughness_metalness: sampler;
fn sample_direct_light(index: i32, light_coords: vec4<f32>) -> f32 {
if (light_coords.w <= 0.0) {
return 0.0;
}
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;
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;
}
}
return total_sample;
}
@fragment
fn fs_main(vert: VertexOutput) -> @location(0) vec4<f32> {
// textures
@ -159,7 +130,7 @@ fn fs_main(vert: VertexOutput) -> @location(0) vec4<f32> {
// radiance
let radiance_strength = max(dot(normal_dir, light_dir), 0.0);
let radiance = radiance_strength * light.color.xyz * light.color.w * light_attenuation * in_light;
let radiance = radiance_strength * light.color.rgb * light.color.a * light_attenuation * in_light;
// brdf shading
total_radiance += radiance * brdf(