Basic fog functions for GLSL
Switch branches/tags
Nothing to show
Clone or download
Latest commit defd3a9 Oct 11, 2013
Failed to load latest commit information. first commit Oct 11, 2013 update readme Oct 11, 2013
exp.glsl first commit Oct 11, 2013
exp2.glsl first commit Oct 11, 2013
linear.glsl first commit Oct 11, 2013
package.json 0.0.1 Oct 11, 2013

glsl-fog experimental

Basic fog functions for GLSL, generic but intended for use with glslify.



Here's a hypothetical example of linear fog calculated in a vertex shader:

#define FOG_START 100
#define FOG_END 500

uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;

attribute vec3 position;
varying float fogAmount;

#pragma glslify: fog_linear = require(glsl-fog/linear)

void main() {
  gl_Position = projection * view * model * vec4(positon, 1.0);
  float fogDistance = length(;
  fogAmount = fog_linear(fogDistance, FOG_START, FOG_END);

And another (separate) example using exp/exp2 per-pixel fog:

#define FOG_DENSITY 0.05

varying vec4 vertexColor;

// Take your pick: these should be usable interchangeably.
#pragma glslify: fog_exp2 = require(glsl-fog/exp2)
#pragma glslify: fog_exp = require(glsl-fog/exp)

void main() {
  float fogDistance = gl_FragCoord.z / gl_FragCoord.w;
  float fogAmount = fog_exp2(fogDistance, FOG_DENSITY);
  const float fogColor = vec4(1.0); // white

  gl_FragColor = mix(vertexColor, fogColor, fogAmount);


MIT. See for details.