flashgameblog.at

20.03.2010
13:06

Depth of Field in flash (DOF)

For my latest game I wanted to simulate DOF in flash. In flash we can apply filters to objects and flash provides a blur filter, this is the base of the work. What we need is a mechanism to coordinate the depths of the objects.

First We build a class that manages everything about DOF. This class needs an array which holds references to all the objects we want to use with DOF. We build a virtual space with the depths of 1 where 0 is the position of the camera and 1 is the point most faraway from the camera. Our class has a function addObject which takes the display object and a depths in our virtual space. Next our class has got a property maxBlur that defines how strong the maximum blur effect will be. And of course there is a property focus which defines the current focus.

In my game I needed the effect animated, so I added a main function which manipulates the focus and recalculates the blur effect for each object.

  1. package at.lueftenegger.dof {
  2.  
  3.   import at.lueftenegger.dof.operator.Operator;
  4.   import flash.display.DisplayObject;
  5.   import flash.filters.BlurFilter;
  6.  
  7.   public class Dof{
  8.    
  9.     public var maxBlur:Number   = 5;
  10.     private var _focus:Number   = 0; // 0 -> 1
  11.     private var last_focus:Number;
  12.     public var operator:Operator;
  13.    
  14.     public function Dof(){
  15.       objects = new Array();
  16.       operator = new Operator();
  17.     }
  18.    
  19.     protected var objects:Array;
  20.    
  21.     public function set focus(value:Number):void {
  22.       _focus = value;
  23.       render();
  24.     }
  25.    
  26.     public function addLayer( object:DisplayObject, position:Number ):void {
  27.      
  28.       if (position < 0) position = 0;
  29.       if (position > 1) position = 1;
  30.      
  31.       var l:Layer = new Layer();
  32.       l.dispObj = object;
  33.       l.position = position;
  34.       objects.push(l);
  35.     }
  36.    
  37.     public function main():void {
  38.      
  39.       _focus = operator.giveFocusValue();
  40.      
  41.       if(_focus == last_focus) return;
  42.      
  43.       render();
  44.       last_focus = _focus;
  45.     }
  46.    
  47.     private function render():void {
  48.       for ( var i:String in objects) {
  49.         var h:Number( (objects[i].position - _focus) < 0 ) ? -(objects[i].position - _focus) : (objects[i].position - _focus);
  50.         var blur:Number = h * maxBlur;
  51.         var filter:BlurFilter = new BlurFilter( blur, blur, 1 );
  52.         var filterss:Array = [];
  53.         filterss.push(filter);
  54.         objects[i].dispObj.filters = filterss;
  55.       }
  56.     }
  57.   }
  58. }
  59.  
  60. package at.lueftenegger.dof{
  61.   import flash.display.DisplayObject;
  62.   public class Layer{
  63.     public var position:Number;
  64.     public var dispObj:DisplayObject;
  65.   }
  66.  
  67. }
  68.  
  69. package at.lueftenegger.dof.operator{
  70.  
  71.   public class Operator{
  72.    
  73.     public function Operator(speed:Number = 0.15){
  74.      
  75.     }
  76.    
  77.     public function giveFocusValue():Number {
  78.       return 0.5;
  79.     }
  80.    
  81.   }
  82.  
  83. }
  84.  
  85. package at.lueftenegger.dof.operator{
  86.  
  87.   public class Sinus extends Operator{
  88.    
  89.     private var speed:Number;
  90.     private var run:Number = 0;
  91.    
  92.     public function Sinus(speed:Number = 0.15){
  93.       this.speed = speed;
  94.     }
  95.    
  96.     override public function giveFocusValue():Number {
  97.       run += speed;
  98.       return  0.5 + Math.sin(run) * 0.5;
  99.     }
  100.   }
  101.  
  102. }
  103.  

How to use it

  1. Create a new FLA file.
  2. Put some MovieClips on screen and give them names.
  3. Add the following script.
  1. var dof:Dof = new Dof();
  2. dof.operator = new Sinus(0.1);
  3. dof.maxBlur = 10;
  4. dof.addLayer( getChildByName("object1"),0);
  5. dof.addLayer( getChildByName("object2"),0.2);
  6. dof.addLayer( getChildByName("object3"),0.4);
  7. dof.addLayer( getChildByName("object4"),0.6);
  8. dof.addLayer( getChildByName("object5"),0.8);
  9. dof.focus = 0.4;
  •  
  • 1 Comments
  •  
Gravatar: Michael LuefteneggerMichael Lueftenegger
21.03.2010
01:15
update

Hi!

I have updated the class. Some improvements, inlining math functions. Usage of Object replaced with the Layer class. State pattern added to control the movement of the focus.

michael

Your comment

Notify me when someone adds another comment to this post

back

< < March 2010 > >
S M T W T F S
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      

Blog rolls

Latest Comments

ISO Blender Camera Setup
24.04.2010 06:58
update
21.03.2010 01:15
if-Statement
16.11.2009 08:34
hmm...
16.11.2009 08:00

Archive

  • [-]2010(8)
    • [-]August(1)
    • [-]July(1)
    • [-]June(2)
    • [-]April(1)
    • [-]March(2)
    • [-]February(1)
  • [-]2009(13)
    • [-]December(4)
    • [-]November(8)
    • [-]October(1)

Copy and paste this link into your RSS news reader

RSS 0.91Posts
RSS 2.0Posts

Social Bookmarking

Bookmark bei: Mr. Wong Bookmark bei: Webnews Bookmark bei: Icio Bookmark bei: Oneview Bookmark bei: Linkarena Bookmark bei: Favoriten Bookmark bei: Seekxl Bookmark bei: Favit Bookmark bei: Social Bookmarking Tool Bookmark bei: Power Oldie Bookmark bei: Bookmarks.cc Bookmark bei: Newskick Bookmark bei: Newsider Bookmark bei: Linksilo Bookmark bei: Readster Bookmark bei: Folkd Bookmark bei: Yigg Bookmark bei: Digg Bookmark bei: Del.icio.us Bookmark bei: Reddit Bookmark bei: Simpy Bookmark bei: StumbleUpon Bookmark bei: Slashdot Bookmark bei: Netscape Bookmark bei: Furl Bookmark bei: Yahoo Bookmark bei: Spurl Bookmark bei: Google Bookmark bei: Blinklist Bookmark bei: Blogmarks Bookmark bei: Diigo Bookmark bei: Technorati Bookmark bei: Newsvine Bookmark bei: Blinkbits Bookmark bei: Ma.Gnolia Bookmark bei: Smarking Bookmark bei: Netvouz Information