Ultrashock Forums > Flash > ActionScript
[Papervision3D] Creating a Depth of Field with alpha levels?

You are currently viewing our website as a guest which gives you limited access to forums, files and other resources.

Click here to join now for free, and start interacting with our members, download files and much more!

Click here if you are looking for our Flash files and other professional assets.
 
Post Reply | View first unread | Rate Thread Search this Thread | Thread Tools | Display Modes

#1
Bookmark and Share!
[Papervision3D] Creating a Depth of Field with alpha levels?
Old 2008-11-20

Hi,

I'm new to Papervision, and I'm working with some PV3D basics. I currently have a project that I'm stuck on. Use W and S to control the camera:

http://www.bobthabuilda.net/projects/dof/plane.swf

I apologize if the elements and camera snap to the right side of the stage... I'm not sure how to fix that quite yet (aside from embedding it via HTML).

Anyways I'm trying to figure out a function to modify the alpha levels of each plane based on their Z distance from the camera. I'd like this to be an ENTER_FRAME function.

Here is the code for the document class:
Code:
package {
   
    import flash.events.*;
    import org.papervision3d.PaperBase;
    import org.papervision3d.materials.*;
    import org.papervision3d.objects.*;
    import org.papervision3d.objects.primitives.*;
   
    public class PlaneTest extends PaperBase {
       
        public var _mat:BitmapFileMaterial;
        public var _p:DisplayObject3D;
        public var _wdown:Boolean;
        public var _sdown:Boolean;
        public var _pArray:Array;
        public var i:Number;
       
        public function PlaneTest() {
            _pArray = new Array();
            _mat = new BitmapFileMaterial("bitmap.jpg");
            _mat.doubleSided = true;
            init(stage.stageWidth, stage.stageHeight, 8, 2000);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
            stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
        }
       
        public function onKeyDown(event:KeyboardEvent):void {
            switch(event.keyCode) {
                case 87: // w
                    _wdown = true;
                    break;
                case 83: // s
                    _sdown = true;
                    break;
            }
        }
       
        public function onKeyUp(event:KeyboardEvent):void {
            switch(event.keyCode) {
                case 87: // w
                    _wdown = false;
                    break;
                case 83: // s
                    _sdown = false;
                    break;
            }
        }
       
        override protected function init3d():void {
            _camera.focus = 8;
            _camera.zoom = 100;
            _camera.z = -2000;
            for (var i:uint = 0; i<20; i++) {
            _p = new Plane(_mat, 234, 234, 2);
            _p.useOwnContainer = true; // allows alpha, blend modes, etc to be applied to each seperate plane object.
            _p.name = 'cover'+i;
            _p.x = Math.random() * 1500 - 750;
            _p.y = Math.random() * 1500 - 750;
            _p.z = i * 250;
            _scene.addChild(_p);
            _pArray.push(_p);
            trace(_pArray[i]);
            }
        }
       
        override protected function processFrame():void {
            if (_wdown) {
                _camera.zoom += 2.5;
                _camera.focus += 1.5;
            }
           
            if (_sdown) {
                _camera.zoom -= 2.5;
                _camera.focus -= 1.5;
            }
           
            i = 0;
           
            while(_p = _scene.getChildByName('cover'+i)) {
                _p.alpha = Math.min(1, );
                _p.lookAt(_camera);
                i++;
            }
        }
    }
}
And here is the code for the base class:
Code:
package org.papervision3d {
   
    // These lines make different 'pieces' available in your code.
    import flash.display.Sprite; // To extend this class
    import flash.events.Event; // To work out when a frame is entered.
    import org.papervision3d.view.Viewport3D; // We need a viewport
    import org.papervision3d.cameras.*; // Import all types of camera
    import org.papervision3d.scenes.Scene3D; // We'll need at least one scene
    import org.papervision3d.render.BasicRenderEngine; // And we need a renderer
   
    public class PaperBase extends Sprite { // Must be "extends Sprite"
   
       public var _viewport:Viewport3D; // The Viewport
       public var _renderer:BasicRenderEngine; // Rendering engine
       // -- Scenes -- //
       public var _scene:Scene3D; // A Scene
       // -- Cameras --//
       public var _camera:Camera3D; // A Camera
      
       public function init(vpWidth:Number, vpHeight:Number, camNear:Number, camFar:Number):void {
         initPapervision(vpWidth, vpHeight, camNear, camFar); // Initialize papervision
         init3d(); // Initialize the 3d stuff..
         init2d(); // Initialize the interface..
         initEvents(); // Set up any event listeners..
       }
      
       protected function initPapervision(vpWidth:Number, vpHeight:Number, camNear:Number, camFar:Number):void {
         // Here is where we initialise everything we need to
         // render a papervision scene.
         _viewport = new Viewport3D(vpWidth, vpHeight);
         // The viewport is the object added to the flash scene.
         // You 'look at' the papervision scene through the viewport
         // window, which is placed on the flash stage.
         addChild(_viewport); // Add the viewport to the stage.
         // Initialise the rendering engine.
         _renderer = new BasicRenderEngine();
         // -- Initialise the Scenes -- //
         _scene = new Scene3D();
         // -- Initialise the Cameras -- //
         _camera = new Camera3D(60, camNear, camFar);
         // The argument passed to the camera
         // is the object that it should look at. I've passed the scene object
         // so that the camera is always pointing at the centre of the scene.
       }
      
       protected function init3d():void {
         // This function should hold all of the stages needed
         // to initialise everything used for papervision.
         // Models, materials, cameras etc.
       }
      
       protected function init2d():void {
         // This function should create all of the 2d items
         // that will be overlayed on your papervision project.
         // User interfaces, Heads up displays etc.
       }
      
       protected function initEvents():void {
         // This function makes the onFrame function get called for
         // every frame.
         addEventListener(Event.ENTER_FRAME, onEnterFrame);
         // This line of code makes the onEnterFrame function get
         // called when every frame is entered.
       }
      
       protected function processFrame():void {
         // Process any movement or animation here.
       }
      
       protected function onEnterFrame( ThisEvent:Event ):void {
         //We need to render the scene and update anything here.
         processFrame();
         _renderer.renderScene(_scene, _camera, _viewport);
       }
      
    }
}
If anyone could help me out here I'd really appreciate it.

Regards,

John
postbit arrow Be the first to comment! | 780 views postbit arrow Reply: with Quote   
Registered User
BOBThaBuilda is offline
seperator
Posts: 25
2008-03-16
BOBThaBuilda lives in United States
seperator
Thread Tools
Display Modes Rate This Thread
Rate This Thread: