Skip to content

Commit

Permalink
volumetric: Add a height based fog
Browse files Browse the repository at this point in the history
  • Loading branch information
vlj committed Jan 14, 2024
1 parent 790b8d2 commit 32bf63d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
16 changes: 10 additions & 6 deletions data/base/shaders/vk/pointlights.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,12 @@ vec4 iterateOverAllPointLights(
return light;
}

//volumetric
vec4 volumetricIterateOverAllPointLights(
// based on equations found here : https://www.shadertoy.com/view/lstfR7
vec4 volumetricLights(
vec2 clipSpaceCoord,
vec3 cameraPosition,
vec3 WorldFragPos
vec3 WorldFragPos,
vec3 sunLightColor
) {
vec3 result = vec3(0);
ivec2 bucket = ivec2(WZ_BUCKET_DIMENSION * clipSpaceCoord);
Expand All @@ -93,12 +94,15 @@ vec4 volumetricIterateOverAllPointLights(
#define STEPS 64
for (int i = 0; i < STEPS; i++)
{
vec3 od = fogColor.xyz * length(viewLine / STEPS) / 10000;

vec3 posOnViewLine = WorldFragPos + viewLine * i / STEPS;
// fog is thicker near 0
float thickness = exp(-posOnViewLine.y / 300);

vec3 od = fogColor.xyz * thickness * length(viewLine / STEPS) / 1000;

float sunLightEnergy = getShadowVisibility(posOnViewLine);
vec3 scatteredLight = vec3(sunLightEnergy) * od;
float sunLightEnergy = getShadowVisibility(posOnViewLine) ;
vec3 scatteredLight = vec3(sunLightEnergy) * sunLightColor * od;

for (int i = 0; i < bucketOffsetAndSize[bucketId].y; i++)
{
Expand Down
2 changes: 1 addition & 1 deletion data/base/shaders/vk/tcmask_instanced.frag
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ void main()

#if 1
vec2 clipSpaceCoord = gl_FragCoord.xy / vec2(viewportWidth, viewportHeight);
vec4 volumetric = volumetricIterateOverAllPointLights(clipSpaceCoord, cameraPos.xyz, fragPos);
vec4 volumetric = volumetricLights(clipSpaceCoord, cameraPos.xyz, fragPos, diffuse.xyz);
fragColour.xyz = toneMap(fragColour.xyz * volumetric.a + volumetric.xyz);
#else
if (fogEnabled > 0)
Expand Down
2 changes: 1 addition & 1 deletion data/base/shaders/vk/terrain_combined_high.frag
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ void main()
#if 1

vec2 clipSpaceCoord = gl_FragCoord.xy / vec2(viewportWidth, viewportHeight);
vec4 volumetric = volumetricIterateOverAllPointLights(clipSpaceCoord, cameraPos.xyz, frag.fragPos);
vec4 volumetric = volumetricLights(clipSpaceCoord, cameraPos.xyz, frag.fragPos, diffuseLight.xyz);
fragColor.xyz = toneMap(fragColor.xyz * volumetric.a + volumetric.xyz);
#else
if (fogEnabled > 0)
Expand Down

0 comments on commit 32bf63d

Please sign in to comment.