Skip to content

Commit

Permalink
Add density to volumetric fog
Browse files Browse the repository at this point in the history
  • Loading branch information
Popov72 committed Dec 19, 2019
1 parent 680ca4a commit d14dafe
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
5 changes: 3 additions & 2 deletions resources/shader/TR_volumetric_fog.fs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ void main() {
}
float distToCenter = length(cross(volFogCenter - vwCamPos, dir));
float fr = distToCenter < volFogRadius ? smoothstep(0.0, 1.0, cos(distToCenter/volFogRadius*3.141592/2.0)) : 0.0;
//float fr = distToCenter < volFogRadius ? smoothstep(0.0, 1.0, 1.0-distToCenter/volFogRadius) : 0.0;
glFragColor = mix(glFragColor, vec4(volFogColor, glFragColor.a), clamp(dist/(volFogRadius*2.0)*fr, 0.0, 1.0));
float e = dist/(volFogRadius*2.0);
e = 1.0 - exp(-e * 4.0);
glFragColor = mix(glFragColor, vec4(volFogColor, glFragColor.a), clamp(e*fr, 0.0, 1.0));
}

if (useFog == 1) {
Expand Down
62 changes: 62 additions & 0 deletions resources/shader/TR_volumetric_fog_ noconditional.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#version 300 es
precision highp float;

const vec3 fogColor = vec3(0.0, 0.0, 0.0);
const float fogNear = 14000.0;
const float fogFar = 21000.0;

uniform int useFog;
uniform sampler2D map;
uniform sampler2D mapBump;
uniform vec4 offsetBump;

uniform vec3 volFogCenter;
uniform float volFogRadius;
uniform vec3 volFogColor;

in vec3 vColor;
in vec2 vUv;
in vec4 vwPos;
in vec3 vwCamPos;

out vec4 glFragColor;

void main() {
vec4 texelColor = texture( map, vUv );
if (offsetBump.w == 1.0) {
vec4 bumpColor = texture( mapBump, vUv + offsetBump.xy);
float a = texelColor.a;
texelColor = texelColor * (bumpColor + 0.25) * 1.25;
texelColor.a = a;
}
glFragColor = texelColor;

if ( glFragColor.a < 0.5 ) discard;
glFragColor = glFragColor * vec4( vColor, 1.0 );

float volFogRadius2 = volFogRadius * volFogRadius;
float distCamToPos = distance(vwPos.xyz, vwCamPos);
vec3 dir = normalize(vwPos.xyz - vwCamPos);
vec3 L = volFogCenter - vwCamPos;
float tca = dot(L, dir);
float d2 = dot(L, L) - tca * tca;

float thc = sqrt(max(volFogRadius2 - d2, 0.0));
float t0 = tca - thc;
float t1 = tca + thc;
float dist = mix(0.0, min(distCamToPos, t1), max(sign(-t0), 0.0)*max(sign(t1), 0.0))
+ mix(0.0, min(distCamToPos, t1) - t0, max(sign(t0), 0.0)*max(sign(t1), 0.0)*max(sign(distCamToPos - t0), 0.0));
float distToCenter = length(cross(volFogCenter - vwCamPos, dir));
float fr = smoothstep(0.0, 1.0, cos(distToCenter/volFogRadius*3.141592/2.0));
float e = dist/(volFogRadius*2.0);
e = 1.0 - exp(-e * 4.0);
vec4 glFragColorWithFog = mix(glFragColor, vec4(volFogColor, glFragColor.a), e*fr, 0.0, 1.0));

glFragColor = mix(glFragColor, glFragColorWithFog, max(sign(volFogRadius2 - d2), 0.0));

if (useFog == 1) {
float depth = gl_FragCoord.z / gl_FragCoord.w;
float fogFactor = smoothstep( fogNear, fogFar, depth );
glFragColor = mix( glFragColor, vec4( fogColor, glFragColor.w ), fogFactor );
}
}

0 comments on commit d14dafe

Please sign in to comment.