- Thread starter Edgamer63
- Start date

You're going to need 5 uniform or constant components. 3 for the colour of the fog. And 2 more to define its start and end distances.

The first thing you need to do in the vertex shader is get the distance from the camera to the vertex, and pass that distance as a varying to the fragment shader. I think that can most easily be accomplished like this:

Some applications only use depth as a basis for calculating fog, but that causes the fog level to change weirdly as you rotate the camera. So why not use the proper distance instead? That division by a couple of matrix elements is to bring the vertex position into view space as opposed to whatever is the name of the space which results from the projection matrix multiplication. You could also just multiply the object space column vector with MATRIX_WORLD_VIEW, but that is a lot of extra math. Note: The above is for when using a perspective projection, it will give incorrect results with an orthographic projection.

Then in the fragment shader, you use the distance value to compute the fog level. Fog level should be (dist-near)/(far-near), and you'll want to clamp that between 0 and 1. It helps if you precalculated 1/(far-near) and pass that in as the second component of your "fog" uniform. So, x component of "fog" would be its near value. And y component would be 1/(far-near).

You can then use fog_level to interpolate between the base colour of the fragment, and the fog colour:

I feel like there could be some possibility of error there when dealing with sem-transparent images. So if you are making use of any, double check that produces correct results.

The first thing you need to do in the vertex shader is get the distance from the camera to the vertex, and pass that distance as a varying to the fragment shader. I think that can most easily be accomplished like this:

Code:

```
varying float dist;
//....
dist = length(vec3( gl_Position.xy / vec2(gm_Matrices[MATRIX_PROJECTION][0][0],gm_Matrices[MATRIX_PROJECTION][1][1]), gl_Position.w ));
```

Then in the fragment shader, you use the distance value to compute the fog level. Fog level should be (dist-near)/(far-near), and you'll want to clamp that between 0 and 1. It helps if you precalculated 1/(far-near) and pass that in as the second component of your "fog" uniform. So, x component of "fog" would be its near value. And y component would be 1/(far-near).

Code:

`float fog_level = clamp((dist-fog.x)*fog.y,0.0,1.0);`

Code:

```
vec4 base_colour = v_vColour * texture2D( gm_BaseTexture, v_vTexcoord );
gl_FragColor = vec4( mix( base_colour.rgb, fog_colour, fog_level ), base_colour.a );
```

Last edited:

As far as I know the gm fog uniforms don't work in cursom shaders. This information might be out of date. You don't really need them though, since you can just make your own uniforms.