Depth of Field

Blitz3D Forums/Blitz3D Programming/Depth of Field

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

There's many ways to do depth of feild. One is to pivot the camera around your focal point and re-render the scene a bunch of times. That is much too expensive for realtime use though.

Another method was recently outlined in Game Developer Magazine. I won't go into details on it because you can't use that method either but it looks like it might be something implemented in the future on 3D cards and it looks fast. Maybe it can be implemented with pixel shaders, I'm not sure. But there's no pixel shaders in Blitz.

There is one method you might be able to use. Something like whatever Otacon was hinting at. But it may not give you perfect results.

In the code archives I put a hardware blurring function. You should grab that.

Now, all you need to do is take your camera range, and divide it up into X regions of depth. Then you render each region of depth, and blur the resulting image more and more for more distant regions.

Ie, if your camera has a range of 1-256, then render the following ranges, using the amount of blur in parentheses:
1-2 (1)
2-4 (2)
4-8 (4)
8-16 (8)
16-32 (16)
32-64 (32)
64-128 (64)
128-256 (128)

I don't mean to imply that you need to render 8 times. You could do just four. The important thing to note is how the distance covered by each step doubles each time. The number in the parentheses represents how many pixels are sampled for each pixel in the final image. So 1 means no blurring. Each pixel represents one pixel of the blurred image. 4 means that each pixel is the average of a 4x4 pixel region. It might be easier though to imagine that the first leve you sample just the pixel, and the second level you sample one pixel to the left, right top bottom, and the diagonals... a 3x3 pixel area.... then a 5x5 pixel are etc.

But that depends on how you do the blurring. One way to do the blurring is simply to scale the image down. That is probably the fastest way, but the quality of the blur will not be vey good. You know what a low res texture looks like when scaled up? How it has lines running through it and looks liek blurry blocks? That's how that would look.

My blurring function does a higher quality blur, but it's still not without artifacts of it's own. The artifacts are more pleasing visually though.

Once you have rendered each plane, you need to stick them back togehter in the scene. Unfortuinately, this is the hard part, and I'm not sure how to go about doing it.

Then again, maybe I have an idea... and it might actually make the blurring easier.

It occurs to me that one could render the most distant region to a small portion of the screen, and then grab that, and copy it to a texture. Then one would use entityorder to render this textured quad, stetched to fill the camera view, behind everything else in the scene when rendering the next region.