Change mist color in real time

Hi :yes:
Recently I have made cycle/night day in my game and I wanna add some mist in color of the currently sun is shining on horizon. Color must be change in real time.




I had tried some of these:
http://www.tutorialsforblender3d.com/GameModule/RasterizerModule.html
but it wasn’t working. Even gets no error.

Try to keyframe the different colours (right-click on the colour button). I already tried something similiar a couple of months ago (It didn’t work…:().

Hope that helps

This is for the 2.49 Blender Api. Try this:

@VincentG
Thanks for new Api link but… my game is unfortunately working on GLSL. so…


@sandboxer_lm
Nice idea but I don’t know exactly how to use it :3


The same effect in loop…

Theres almost no way to get it to work these days. All the old tricks no longer work.

It may be possible with a custom 2d shader, but after I tried some code with the depth texture I found that it doesnt include transparent objects which could cause ugly results.

I did have some limited success though so I may try to clean up the code and see if I can get something useable.

@Smoking_mirror Thanks for interest in this case :slight_smile: Its very important for my project, would be great if something could work.
@ Half way to success, its need a depth. But maybe…

Hello!

I made a basic fog shader:

http://www.mediafire.com/download/dy4v7p0g6azxl96/fog.blend

mouse wheel to control Red value of the fog

But it is really basic and I don’t know if it is a good way to proceed. Furthermore there are errors in the fragment shader while assigning new color value to gl_Fog.color…
FurtherFurthermore, it’s just a part of the problem. Ambient lighting needs also to be modified. But maybe that’s a beginning of answer?

If you need start/end value for the fog, you have to use gl_Fog.start and gl_Fog.end in the equation… Or a python code maybe…

EDIT: For the beginning distance of the fog, you can replace:

gl_FragColor = mix(gl_Fog.color, dif, fogFactor );

by:

if (z < 30.0)
{
gl_FragColor = dif;
}
else
{
gl_FragColor = mix(gl_Fog.color, dif, fogFactor );
}

(z = distance camera>pixel)

But the shader needs to be completed with lights, other textures, specular… (http://en.wikibooks.org/wiki/GLSL_Programming/Blender)

EDIT: It’s a bad idea to run the script at each frame but you can run it 1 time per minute for example to adjust fog color…

More complete shader:

http://www.mediafire.com/download/90taa1rde00jlb6/fog2.blend

There are fixes by HG1 that bring the the possibility of changing world settings in-game when GLSL mode is on.

Unfortunately, they have been stuck in the review tracker and I don’t think any current BGE dev. has had time to look at them more closely, hence one of the reasons why so many in this forum are looking to move on to another engine.

I’m not sure what you did there but your initial fog.blend could run much faster even when the script runs every frame. Here’s an example:

Edit: On second notice, logic seems to get very high still when the blend is opened for the first time … sometimes. So frustrating…

Attachments

Fog_01.blend (97.6 KB)

@Raco: It’s a good thing to update only objects in the frustum. But what is wrong with my initial shader is that this shader is applied to ALL objects (in the frustum or in the scene) and each object in a real game has its own properties (specular, shininess, textures etc…) and the initial shader didn’t take into account those parameters. I forgot transparency too. And when you apply shader to an object, shader replaces object’s material. So you have to write a “complete” shader to take into account specular, transparency, shininess, textures of each object in the frustum or in the scene… (EDIT: and if you run the complete shader each frame, it will slow down your computer… You can execute the script more than one time per minute… That was just an example)

I’ll post an example to illustrate what I’m trying to mean:

http://www.mediafire.com/download/qiqh2flw8phy7qf/fog3.blend

Switch beetween the 2 scripts Text and Text.001 to see the difference

( one object has no transparency but has specular and the other has no specular but is transparent)

Tonight I had a few minutes to hack together something of a 2d mist shader. It’s far from perfect, you’ll notice that if you have anything which is transparent (not clip alpha though) in front of the mist, it will be fully affected.

Here’s the code:

uniform sampler2D bgl_RenderedTexture;
uniform sampler2D bgl_DepthTexture;

uniform float r;
uniform float g;
uniform float b;

uniform float far;
uniform float near;

void main(void)
{    
    vec2 texcoord = gl_TexCoord[0].st;
    
    vec4 col_value = texture2D(bgl_RenderedTexture, gl_TexCoord[0].st);
    
    vec4 depth = texture2D(bgl_DepthTexture, gl_TexCoord[0].st);
            
    depth -= 1.0;
        
    depth *= 120.0;
                
    vec4 gradient = (depth-near) / (far - near);
            
    gl_FragColor = mix(texture2D(bgl_RenderedTexture, gl_TexCoord[0].st), vec4(r,g,b,1.0), clamp(gradient, 0.0, 1.0));
    gl_FragColor.a = 1.0;          
              
}

You’ll need to add these properties to the game object with the 2d filter actuator.

  • near
  • far
  • r
  • g
  • b

Far and Near are your controls. A higher far setting (about 5.0) will thin the mist towards the horizon. A lower near value (-2.0) will cause the mist to start nearer the camera. If near and far values are close together (-0.2,0.2) you’ll get quite thick mist. Far apart values will give thin mist (-12.0,12.0).

You can set the color with the r,g and b values.

Play around with the settings and see what gives you the best results. I’m not glsl expert so I can’t tell you if this is an optimal solution, but it seems to work OK on my computer.

Hi! That’s a very cool solution! I didn’t know bgl_DepthTexture (I didn’t know gl_Fog yesterday too :)). I mistaked mist for fog. 2D filter seems to be the right thing to work with. Thanks for sharing!

@youle in fog 3 I have some error in your script.


Yeahh I agree thats nice. But…
Smoking mirror script is not working for me (no error). For example in sdfgeoff works fine.
I set up all settings, properties fine on 100%. Even I tried to modify/split first script with second but the effect was one color screen (also no error)
I have ATI card and weak knowledge at glsl.
in this moment am still trying to alive Smoking mirror’s script on my computer.
will let you know how I find something…

I see you have the same problem I do:
Near and far aren’t in blender units, or in, well, any units.

– edit –
Hmph, Martinish has some magic in his DOF shader that’s helping. I think I’ll have something useful in half an hour

– edit 2 –

This is the magic:

float linearize(float depth)
{
    return -camFar * camNear / (depth * (camFar - camNear) - camFar);
}

You need the information from the camera to know what scale the z-buffer is in

Now all the standard mist equations are working fine!

Here’s a blend. W to increase mist distance, Q to change it’s redness. This time distances are in blender units and both near and far can be adjusted in a sensible way.
However, you also need to specify the camera clipping distances to the script.

Blend:
Fog2.blend (521 KB)

Many thanks to Martinish for his DOF filter. You can’t see any of it left in the shader here, but it all started with his DOF filter, and I stripped out the blur, stripped out the vinegetting, stripped out this, stripped out that, tweaked what was left, and now it works!

Plans:
Add in exponential decay fog. (Now done, not uploaded yet)
Make it into a mist sphere not a mist plane. (Can of worms. Useful Link?)

Long term plan:
Actually, this fits in really well with something I’m working on currently (A base ‘wrapper’ for any future game I make), so I’ll try to expand this mist script to have the following features:

  • Controllable Color (Done)
  • Controllable density type (Linear, Quadratic, Exponential)
  • Controllable Density/Depth (Done for linear and exponential case)
  • Controllable clipping (Plane Done, Sphere = current WIP)

May take a while… Definitely not tonight.

That’s awesome. I got a lot of hints from looking at Martinsh’s code too. Even though it does something completely different to what I want, just having some signposts to follow is so useful.

My own code is so often just a hack, like in this case, reduce the brightness, increase the contrast and then map to a useable color value.

There’s lots of cool stuff you could do with this base though; heat shimmer in the distance, fast DOF, noisy mist, textured fog…
Cool. :slight_smile:

Nice stuff there :slight_smile: Yeahh Martinsh’s codes is nice base to creat new filters and more.
Thanks again for help and new support mist :smiley:

@edit
I wrote a second post on next site. Not double just refresh.

Ok, One more guestion… :smiley:
How can I add a exception at one of objects? It means that object won’t be rendered by shader. Just like shadeless in light.

You can’t do so with the current available textures in 2d filters, yet you can set up 2 render to texture cameras, the first renders the scene normally, and the other renders your desired “mist-proof” object as a mask for when you apply a fragment shader on your first render to texture, setting the pixel shader coords to window borders.
Sorry for my terrible explanation, the process has to be that “hacky” when you’re working with the bge. :stuck_out_tongue:

Jackii, can you use that technique to produce a highlight?

or make a single object glow?