Better ambient light estimation

This commit is contained in:
Lauri Räsänen 2023-12-26 20:23:40 +02:00
parent 47ebcf6fed
commit 4203391783
4 changed files with 40 additions and 21 deletions

View file

@ -14,7 +14,5 @@ const FOG_LIGHT_STEP_SIZE = 10.0;
const FOG_DENSITY = 2.0;
const FOG_LIGHT_DENSITY = 3.0;
const FOG_ALPHA = 1.0;
const FOG_AMBIENT = 0.06;
const FOG_DENSITY_COLOR = vec2<f32>(0.5, 0.1);
const PBR_AMBIENT = 0.02;

View file

@ -140,24 +140,24 @@ fn fs_main(vert: FogVertexOutput) -> @location(0) vec4<f32> {
let fog_depth = march_result.y;
let occlusion = march_result.z;
let fog_position = vert.world_position.xyz + direction * fog_depth;
let light_dist = length(light.position - fog_position);
let base_color = vec3<f32>(mix(FOG_DENSITY_COLOR.x, FOG_DENSITY_COLOR.y, fog_density));
let ambient_strength = FOG_AMBIENT;
let ambient_color = base_color * ambient_strength;
var ambient = 2.0 * sample_ambient_light(light.color, light_dist, 1.0);
ambient *= base_color;
var radiance = vec3<f32>(0.0);
let fog_position = vert.world_position.xyz + direction * fog_depth;
let in_light = sample_direct_light(vec4<f32>(fog_position, 1.0));
if (in_light > 0.0) {
// attenuation
let light_dist = length(light.position - fog_position);
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 * (1.0 - occlusion);
}
var result = ambient_color + radiance;
var result = ambient + radiance;
// tonemap
result = result / (result + vec3(1.0));

View file

@ -59,3 +59,20 @@ fn sample_direct_light(world_position: vec4<f32>) -> f32 {
}
return in_light;
}
fn sample_ambient_light(light: vec4<f32>, light_dist: f32, surface_light_dot: f32) -> vec3<f32> {
// base ambient
var ambient = vec3(0.01);
// lower attenuation to reduce light bleed
let diff_coef_a = -0.75;
let diff_coef_b = 0.25;
let diff_light_attenuation = 1.0 / (1.0 + diff_coef_a * light_dist + diff_coef_b * light_dist * light_dist);
let diff_direct_light = light.rgb * light.a * diff_light_attenuation;
// very rough bounce light estimation
let diffuse_mult = max(surface_light_dot, 0.0);
ambient += diffuse_mult * 0.03 * diff_direct_light;
return ambient;
}

View file

@ -84,23 +84,27 @@ fn fs_main(vert: VertexOutput) -> @location(0) vec4<f32> {
var total_radiance: vec3<f32>;
let normal_dir = tex_normal.xyz * 2.0 - 1.0;
var light_dir = normalize(vert.tangent_light_position - vert.tangent_position);
let surface_light_dot = dot(normal_dir, light_dir);
let light_dist = length(light.position - vert.world_position.xyz);
// attenuation
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);
let direct_light = light.color.rgb * light.color.a * light_attenuation;
let in_light = sample_direct_light(vert.world_position);
if (in_light > 0.0) {
// lighting vecs
let normal_dir = tex_normal.xyz * 2.0 - 1.0;
var light_dir = normalize(vert.tangent_light_position - vert.tangent_position);
let view_dir = normalize(vert.tangent_view_position - vert.tangent_position);
let half_dir = normalize(view_dir + light_dir);
// 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
let radiance_strength = max(dot(normal_dir, light_dir), 0.0);
let radiance = radiance_strength * light.color.rgb * light.color.a * light_attenuation * in_light;
let radiance_strength = max(surface_light_dot, 0.0);
let radiance = radiance_strength * direct_light * in_light;
// brdf shading
total_radiance += radiance * brdf(
@ -114,10 +118,10 @@ fn fs_main(vert: VertexOutput) -> @location(0) vec4<f32> {
);
}
// ambient
let ambient_color = PBR_AMBIENT * albedo;
var ambient = sample_ambient_light(light.color, light_dist, surface_light_dot);
ambient *= albedo;
var result = ambient_color + total_radiance;
var result = ambient + total_radiance;
// tonemap
result = result / (result + vec3(1.0));