Skip to content

Instantly share code, notes, and snippets.

@rgngl
Created May 20, 2013 13:40
Show Gist options
  • Save rgngl/5612286 to your computer and use it in GitHub Desktop.
Save rgngl/5612286 to your computer and use it in GitHub Desktop.
fog shader
uniform float u_fogMaxDist;
uniform float u_fogMinDist;
uniform vec4 u_fogColor;
varying vec4 v_eyePos;
float computeLinearFogFactor()
{
float factor;
// Compute linear fog equation
factor = (u_fogMaxDist - length(v_eyePos.xyz)) /
(u_fogMaxDist - u_fogMinDist );
// Clamp in the [0,1] range
factor = clamp( factor, 0.0, 1.0 );
return factor;
}
vec4 getColorWithFog(vec4 baseColor)
{
float a = baseColor.a;
float fogFactor = computeLinearFogFactor();
vec4 fogColor = fogFactor * u_fogColor;
vec4 c = baseColor * fogFactor + fogColor * (1.0 - fogFactor);
c.a = a;
return c;
}
varying vec4 v_eyePos;
uniform mat4 u_worldViewMatrix;
void applyEyePos(vec4 position)
{
v_eyePos = u_worldViewMatrix * position;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment