diff --git a/res/shaders/fog.wgsl b/res/shaders/fog.wgsl index 7cdb00f..456e908 100644 --- a/res/shaders/fog.wgsl +++ b/res/shaders/fog.wgsl @@ -61,7 +61,7 @@ fn fog_noise(pos: vec3) -> f32 { return 0.8 * noise1 + 0.2 * noise2; } -fn ray_march(origin: vec3, direction: vec3, scene_depth: f32) -> f32 { +fn ray_march(origin: vec3, direction: vec3, scene_depth: f32) -> vec2 { var density = 0.0; var depth = 0.0; for (var i = 0; i < FOG_MAX_STEPS; i++) @@ -81,7 +81,7 @@ fn ray_march(origin: vec3, direction: vec3, scene_depth: f32) -> f32 { break; } } - return density; + return vec2(density, depth); } fn depth_to_linear(depth: f32) -> f32 { @@ -103,7 +103,9 @@ fn fs_main(vert: FogVertexOutput) -> @location(0) vec4 { return vec4(0.0); } - let density = ray_march(vert.world_position.xyz, direction, max_fog_depth); + let dd = ray_march(vert.world_position.xyz, direction, max_fog_depth); + let fog_density = dd.x; + let fog_depth = dd.y; var in_light = 0.0; if (global_uniforms.use_shadowmaps > 0u) { @@ -134,14 +136,15 @@ fn fs_main(vert: FogVertexOutput) -> @location(0) vec4 { in_light = 1.0; } - var base_color = vec3(mix(0.5, 0.1, density)); + var base_color = vec3(mix(0.5, 0.1, fog_density)); let ambient_strength = 0.04; let ambient_color = base_color * ambient_strength; var radiance = vec3(0.0); if (in_light > 0.0) { // attenuation - let light_dist = length(light.position - vert.world_position.xyz); + let fog_position = vert.world_position.xyz + direction * fog_depth; + 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); @@ -154,5 +157,5 @@ fn fs_main(vert: FogVertexOutput) -> @location(0) vec4 { // tonemap result = result / (result + vec3(1.0)); - return vec4(result, density * FOG_ALPHA); + return vec4(result, fog_density * FOG_ALPHA); }