Depth of Field

Blitz3D Forums/Blitz3D Programming/Depth of Field

Akat(Posted 2004) [#1]
anyone know how to do an effect like depth of field view


AntonyWells(Posted 2004) [#2]
Zombie apocalypse 3 has depth of field.

Basically, you grab the portion to blur to a textured quad, then render it say 8 times, alphaed in slightly differant positions, the more you jiggle(Constant, never random) the more blurry it appears.

It's far from easy..


AntonyWells(Posted 2004) [#3]
Umm, the overall effect that is, not just blurring it..that part is easy.


IPete2(Posted 2004) [#4]
If you want to see this thing in a demo (not a b3d demo I hasten to add) then go here and get the shader demo its 3MB but worth every bit.

http://www.virtools.com/applications/technology-effects.asp

Play with this to see depth of field, blur, hair, fur, light, toon shading and alsorts it is very very good.

IPete2.


Jeroen(Posted 2004) [#5]
i wish Blitz could do that all :)


AntonyWells(Posted 2004) [#6]
So does Mark.;) (Probably!)


HNPhan(Posted 2004) [#7]
well good thing is that most of the effects we can do


Jeroen(Posted 2004) [#8]
I was referring to programming shaders :-)
I hope Blitzmax will be able to let us program shaders, or perhaps follow a standard. I found out that Microsoft has it's own shader language for DirectX....


sswift(Posted 2004) [#9]
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.


sswift(Posted 2004) [#10]
Continuation fo the above... (justin case my PC crashes cause it did the first time I wrote this)

So let's say you want to render four pass depth of feild.

Your depths are as follows:

1-64
64-128
128-256
256-512

Now let's assume our display is 1024x768.

For speed reasons we're going to take a shortcut and render the most distant regions to a smaller camera view. But you could instead blur the full size image. However, that is unlikely to be fast enough for realtime use. Even this method is probably pushing it with 1024x768 res.

So we render the first layer. The mist distant layer.

Set the camera range to 256, 512. (You'll have to correct the fog since the near and far planes are different. I'll leave that as an excerise for the reader) Your sky should be visible in this first pass.

Now, render the view to a 128x96 viewport. Copy to a texture. Do not blur. There is no need to.

Next create a quad in front of the camera filling the camera view, and texture it with our new texture. Check out my distant object rendering code in the code archives for an example of how to do this. Set it's entityorder so it renders before everything else.

Now, render your view again. But this time render a 256x192 image. You will now have a texture that has blurry stuff behind it and stuff that's in focus in front of it.

Repeat this process, doubling the camera view size each time until you get to 1024x768, and changing the camera's range each time to those values I listed above. The distance between the near and far camera planes should be half that which it was the previous time each time you render.

On the last full-screen pass you don't need to copy to a texture. Just flip so the player can see the final scene. Objects will get blurrier as they recceed into the distance.

Don't ask me how to do Depth of Feild which can vary and make stuff up close blurry and stuff in the distance sharp, that's up to you to figure out. :-) I'm sure it's not nearly as easy though.


Tom(Posted 2004) [#11]
Check out the Splinter Cell: Pandora Tomorrow multiplayer demo (60 meg), the spies night vision uses DOF, looks real nice.



Tom


sswift(Posted 2004) [#12]
It may look nice, but personally, I'd only ever use DOF to blur VERY distant objects, to simulate natural atmospheric scattering and heat shimmer, during gameplay.

But for cutscenes it's nice.

The problem with DOF during gameplay is that DOF is like lens flare. It's something a camera has, not your eyes.

Technically, your eyes have DOF. But your eyes focus on whatever it is you want to look at. DOF in a game does not. If I look down that hall, it looks blurry cause the camera's focusing up close. That's annoying and unrealistic. It just makes the screenshots prettier, but it makes the game unpleasant to play.


AntonyWells(Posted 2004) [#13]
Swift, you only get dof in splinter cell when you're using the night vison camera.(others being heat vison, electromagnetic and motion sensitive...really great game.)


Ice9(Posted 2004) [#14]
It looks more like the player is near sighted.
Depth of field is a camera trick like lense flare. It
looks great in photographs or nature movies but for
a 3D interactive environment I don't think it's worth
doing unless it is part of the gameplay.

Depth of field is basicly focus. Your object of focus
is the only thing that is in focus. It helps attract
the eye to the subject that is being photographed making
it more detailed. forground objects are blurred as well as
background objects.

This table
1-2 (1)
2-4 (2)
4-8 (4)
8-16 (8)
16-32 (16)
32-64 (32)
64-128 (64)
128-256 (128)

shoudl look more like this if your point of focus is at 4-8
1-2 (16)
2-4 (8)
4-8 (1)
8-16 (8)
16-32 (16)
32-64 (32)
64-128 (64)
128-256 (128)

The technique would work great for a bird watching game where you are trying to find a rare bird with a pair of binoculars or a deer hunter game where you have to adjust your scope. A photographer game would also work well.
Other than that in a 3D environment such as an fps you never
know what the player is focused on so you end up with the
player view looking like it needs an optometrist. One way it could be used in an fps is to indicate a switch or an
object that a player should interact with as if the player's
vision is focused on the object.