Fog - FogFilter in JME3

Hi people - I’m new to JME3 but have spent a lot of time (going back some years) with JME1 - I have a pretty complete game in JME1 and now am thinking about biting the bullet and migrating it into JME3. I’ve found lots of good stuff so far but also some holes - most recently: fog.

Fog needs to:

  • not hide the Skybox
  • have a start (0%) and an end (100%) distance
  • use the same distance values as the rest of the engine
  • ideally use distance from camera, not from camera plane.

FogState in JME1 did the first three of these nicely. FogFilter in JME3 doesn’t do the first three (not sure about the fourth) so doesn’t seem very useful. Searching this forum there are lots of people asking about fog and various solutions posted over a number of years but it’s not clear if any of them are full working solutions eg this thread (from 2012):

I have tried the code and it solves the sky problem but doesn’t seem to solve the distance problem.

Or more recently this thread (from 2016):

Which closes with the advice ‘learn shaders and write your own’. I’d like to avoid doing that if at all possible.

I assume fog must be a common requirement (at least for anyone doing an outdoor game environment), so I’m hoping there is a full solution out there somewhere - I can’t believe JME3 lacks working fog, and I don’t want to re-invent the wheel. Many thanks if anyone can point me in the right direction.

1 Like

You will find snippets that you will be able to throw together but in the end it is almost a requirement to learn glsl. It is daunting but it’s actually quite a shallow curve for any intermediate. Its just math really. Stupid thing to say but there we go :stuck_out_tongue: but you will want to learn because my stuff might not be quite what you want, or work with your design. It is… inevitabru.

When i get on my PC ill paste a snippet if nobody has done already.

Edit: wasnt there some recent discussion specifically about this radius thing in a monthly wip thread or something?

1 Like

All of these are handled the same way… with like two or three lines added to the lighting shader. If the code you found doesn’t work because of 4 or whatever then it’s just calculating distance wrong.

JME1 used OpenGL 1’s built in fog which was part of the fixed function pipeline. We are in the land of shaders now.

Many thanks jayfella - anything would be helpful.

Though I’ve just also discovered this thread:

Where t0neg0d gives a solution that (after removing a bug in the posted shader) might do just what I need, by the looks of it. I just hope it works with transparency and particles…

I understand about learning shaders myself but I hoped this was just a bog-standard fog feature (which people have been asking about for years). Could t0neg0d’s solution could be incorporated in the core code if it does the job? Would save having to hunt through forums.


Yes, simple fog might be nice and all, if that’s all you want. :slight_smile:

On the other hand, it’s just few lines in a shader, once learnt you can add more features such as:

  • Height varied fog (valleys get extra fog!)
  • Varied density fog
  • Animation

Even then it’s still just a few lines in a shader.

Nevertheless, it would be very helpful if one could post an example which shows the desired effect. I mean why making it unnecessary complicated if someone doesn’t know much about shaders anyway?

1 Like

Yes. Sorry. That was me. Give me half an hour.

I tried to document what is happening. Your best bet would be to look at the default shaders. I would strongly recommend duplicating the Unshaded material as a “base” material for all of your experiments and trying out stuff on that. Let me know if you get stuck but I’m sure a lot of it will come together as you play about.

In my material definition somewhere…

        Float FogDistance : 100.0


varying float dist;
// ... 
dist = distance(vec3(0, g_CameraPosition.y, 0), (g_WorldMatrix * modelSpacePos).xyz);


// fog
    vec4 original_output; // your original gl_FragColor (presumedly).

    // get the middle distance. Biased for effet.
    float mid = m_FogDistance * 0.45;

    float fade = min(1.0, (m_FogDistance - dist) / mid);
    // lets do some math using default values.
    // (100 - distance of camera) Divided by mid ( = 45.0 )
    // Pick any distance, say 90% of the furthest distance, so that would be a nice round 90.
    // fade = (100.0 - 90.0) / 45.0; = .0.2r
    // about 22% fade because we biased the original mid-point.
    // The result is polar to our requirement. Make note of that.
    // Choose a fog color.
    // some kind of very-slightly blue-grey....
    vec3 fogColor = vec3(0.65, 0.65, 0.71);

    // mix the original output with the fog color by the fade value.
    // Note the value is polar, so ensure the order of input to reverse the effect.
    vec3 resultCol = mix(fogColor, original_output.rgb, fade);

    gl_FragColor = vec4(resultCol.rgb, 1.0);


Many thanks - your help is much appreciated, and I will have a play with this and see where I get to.