Monday, 24 February 2014

Fade close to camera.

Quick one.

How unpleasant is it when the camera moves through a nice foggy sprite and this lovely illusion of atmospherics suddenly pops out right in your face? Rather unpleasant in my opinion, it just shouts ‘gamy’.

What you wanna do is simply fade the material as it approaches the camera. I’m sure there are more than one approach to it, but here’s the one I like to use.

I use the PixelDepth (distance of the pixel being draw from the camera), pick a range and remap it from 0 to 1 so it will gradually fade from white to black at your chosen distance. I love this little setup which allows me to pick a range and normalize it, I’ve used it all over the place in the past. Credit for it goes to to Gavin Costello, currently lead programmer at Ninja Theory and all times graphics programming genius :)

How does it work?
If the value is greater than the maximum, the result of (Value - Min) / (Max - Min) is greater than 1 and will always return 1 since we clamp it.

 If the value is lesser than the minimum, the result of (Value - Min) / (Max - Min) is negative and will always return 0 since we clamp it.

If the value is somewhere between the min and the max the result of (Value - Min) / (Max - Min) gives you a ratio. 
Say that Value - Min = 1.5 and Max - Min = 2.
1.5/2 = 0.75, which is roughly what's on the sketch below.

The distance is up to you but I’d advice not to set the min at 0 because it still pops a little. I’ve personally set it to 10 in the following example so the material is properly faded before the camera actually reaches the sprite.

1 comment:

  1. so much tnx.
    i'm z35 :-D but this time have no question, just look your greate tutorial...