Depth of Field effect with PaperVision3D


We have all seen the wonderful effects created by the wizard Roxik (creator of ecodazoo.com) on his website with his depth of field  AS3 performance test and also the dof tests on Mr Doobs website. Well I have been itching to have a little go myself and see if this is something that I could achieve. Using papervision and the viewportLayers built into the library you are able to apply a simple blur filter to an object during the render loop cycle, depending on its distance from the camera.

Actionscript:
  1. var layer:ViewportLayer = viewport.getChildLayer(marbs[i], true);
  2. blur = new BlurFilter(b, b, 3);
  3. layer.filters = [blur];

The important piece of code which works out the level of filter is as follows:

Actionscript:
  1. var dx:Number = camera.z - marbs[i].z;
  2. var dy:Number = camera.y - marbs[i].y;
  3. var b:Number = (Math.sqrt(dx*dx + dy*dy) / 100) - 5;

This gives you a basic number mostly ranging from 1-10, sometimes over and applies this number to the blur filter each frame. The -5 has been added to give the variable a minus number when it gets too close to the camera this is flipped to a positive number:

Actionscript:
  1. if(b <0){
  2.     b = Math.abs(b);
  3.     }

This gives the object a blur if it becomes too close to the camera producing a realistic effect.
Although my tests are not a touch on the examples mentioned earlier in this post they are at least effective and show how easy it is to produce realistic depth of field with very little effort.
Get the source.

This entry was posted in Actionscript 3.0, AS3 3D and tagged , . Bookmark the permalink.

3 Responses to Depth of Field effect with PaperVision3D

  1. saul says:

    Really nice job!

  2. Нерон says:

    Long looked on this topic, thanks, it was interesting.

  3. kavkaz says:

    We look forward to new articles.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>