The fog does not disappear completely with FOGMODE_NONE on the MixMaterial


I add fog underwater. When I use StandardMaterial it works well, but not with MixMaterial.
If I put my camera underwater, I get the fog, but if I go back above the ground, the fog is stay on the ground, it seems to have disappeared from the rest of the environment because we see the Sky. Only the terrain has a part of the fog which is very strange.

On this PG, go with the camera underwater (the fog appears) then go up above the ground (The fog is on the ground but shouldn’t)


It seems that the problem comes from FOGMODE_LINEAR because it works if I switch the fog mode to FOGMODE_EXP and play with fogDensity In place of BABYLON.Scene.FOGMODE_NONE

It’s strange. I also had a shadow problem that did not display which now works using FOGMODE_EXP

Is this normal with LINEAR Mode or can it be a bug?
So I use the EXP mode that works, but the LINEAR mode allowe me to give a start and an end to the fog.

pinging @PirateJC to see if he has some cycles to help

In fact you think it works in your second PG because by setting the density to 0 you make the fog disappear, but the fog code is still there whereas it should not.

As a workaround, you can set the checkReadyOnEveryCall property of the mix material to true to make it work:

1 Like

Digging into this a bit, it definitely seems like something strange is happening.

Check out this playground:

What I’ve done is kept the fog as linear, but I’ve raised the level where the fog switches between NONE and LINEAR.

If you go up above 50 in y, you can see the fog disappear, but then as soon as you drop back below 50, you can see the fog reappears, but not correctly as when it’s first rendered.

Still doing some digging, but definitely doesn’t seem right.

You need to set the checkReadyOnEveryCall to true to make it work.

I think it’s because the ground has been processed a first time when it had no sub meshes (before the call to optimize, which happens later as it is asynchronous), and the effect has been created at that time. Later on, when the structure of the mesh is modified (by the call to optimize which creates several submeshes) it is not noticed by the engine because the effect was already ok with the previous structure, so it does not refresh the effect. Setting checkReadyOnEveryCall makes the engine check everything is in order at each frame.

As an aside, checkReadyOnEveryCall can be reset back to false after the engine has reprocessed the mesh, which is normally the next frame after optimize has been called.

1 Like

Thanks @Evgeni_Popov! And that’s a fantastic suggestion to set it back to false after it’s reprocessed the mesh. That would be a great way to save perf!

You rock!

Yes it works better with checkReadyOnEveryCall, but if I use the LINEAR mode of the fog my shadows disappear, but not with the EXP mode.
In addition if I deactivate after checkReadyOnEveryCall to false and my linear shadow appears, it will recreate the same problem. I should always leave checkReadyOnEveryCall at true, but for performance it is not very good.

In fact, with EXP Mode no problem with shading and putting checkReadyOnEveryCall.

I still think there is a little problem well hidden.

Do you have a PG for the shadow problem?

Setting back checkReadyOnEveryCall to false later on should work, but you must make sure the engine had time to reprocess your meshes.

Yes i will try to make a pg for the shadows

I tried to do a PG, but I can’t reproduce the problem I have at home.

So I try to understand what is happening on my side. Because it works if I use fog EXP mode and it also works if I use StandardMaterial instead of MixMaterial. So I can’t understand why it works in the current PG. If I can understand it I will come back.

I will leave this LINEAR mode which creates problems for me and keep EXP mode which allows me to obtain shadows.

Thanks for the help everyone

Problem found and fix is on its way :slight_smile:

You are awesome. Just out of curiosity, you managed to reproduce the problem ? What is the problem ?Because I try to understand in vain

it was a tricky one and honestly @Evgeni_Popov did all the repro work :slight_smile:

The problem was that we store the frame id for each material on the defines themselves but we forgot to do it for additional materials

Here is the PR:


Thank you @Evgeni_Popov for all your work. And thank you for the explanations.
I would test this in the evening.

Yes it works now with LINEAR and without checkReadyOnEveryCall at true.

For my shadow problem, it is always present. In fact the shadows appear when the fog activates underwater and disappears when I go back to the ground. Maybe a problem adjusting my shadows or some other problem ?

Ok, my shadow was actually very weak. I had to play with shadowMaxZ and set a smaller value to 100 instead of 500 for it to display correctly.
Everything is ok now.