In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

PV3D Depth of Field

PV3D Depth Of Field
by Duarte Peixinho
duarte.peixinho@gmail.com
page: http://duartepeixinho.com
Get Adobe Flash player
by peixinho 05 Mar 2009
package
{

	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.BlurFilter;
	import org.papervision3d.objects.primitives.Cube;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.view.BasicView;
	import org.papervision3d.core.material.TriangleMaterial;
	import org.papervision3d.materials.BitmapColorMaterial;
	import org.papervision3d.view.layer.ViewportLayer;

	[SWF(width = "600", height = "600", backgroundColor = "#ffffff", frameRate = "60")]

	/*
	 *
	 * PV3D Depth Of Field
	 * by Duarte Peixinho
  	 * duarte.peixinho@gmail.com
	 * page: http://duartepeixinho.com
	 *
	 */

	public class Main extends BasicView
	{
		
		private var cubes:Array = new Array();
		private var vpl:Array = new Array();
		private const DISTANCE:int = 1500;
		
		public function Main():void 
		{

			init3d();

			addEventListener(Event.ENTER_FRAME, processFrame);
			startRendering();
		}

		public function init3d():void {
    
			//initiation 3d Scene
			//cubes
			for (var i:int = 0; i < 50; i++) {
				cubes[i] = new Cube(materialCube(), 200, 200, 200, 1, 1, 1);
				cubes[i].x = Math.random() * 1600 - 800;
				cubes[i].y = Math.random() * 1600 - 800;
				cubes[i].z = Math.random() * 8000;
				vpl[i] = viewport.getChildLayer(cubes[i], true);
				vpl[i].filters = [new BlurFilter(10, 10)];
		        	scene.addChild(cubes[i],i.toString());
			}
		}

		private function materialCube():MaterialsList {
			var materialist : MaterialsList = new MaterialsList();
			var front : TriangleMaterial = new BitmapColorMaterial(Math.random()* 0xffffff);
			var back : TriangleMaterial = new BitmapColorMaterial(Math.random()* 0xffffff);
			var left : TriangleMaterial = new BitmapColorMaterial(Math.random()* 0xffffff);
			var right : TriangleMaterial = new BitmapColorMaterial(Math.random()* 0xffffff);
			var top : TriangleMaterial = new BitmapColorMaterial(Math.random()* 0xffffff);
			var bottom : TriangleMaterial = new BitmapColorMaterial(Math.random()* 0xffffff);			
			materialist.addMaterial(front , "front");
			materialist.addMaterial(back , "back");
			materialist.addMaterial(left , "left");
			materialist.addMaterial(right , "right");
			materialist.addMaterial(top , "top");
			materialist.addMaterial(bottom , "bottom");	

			return materialist;
		}

		private function processFrame(e:Event):void {
			for (var i:int = 0; i < cubes.length; i++) {
				if (_camera.distanceTo(cubes[i]) > DISTANCE * 5 || cubes[i].z<-600) scene.removeChild(cubes[i]);
				else if (!scene.getChildByName(i.toString())) scene.addChild(cubes[i], i.toString());
				cubes[i].z += (stage.stageHeight / 2 - mouseY) / 20;			

				//depth of field
				if (Math.sqrt(Math.pow(_camera.distanceTo(cubes[i]) - DISTANCE, 2)) <= DISTANCE) {
					if (Math.sqrt(Math.pow(_camera.distanceTo(cubes[i]) - DISTANCE, 2)) / 50 < 10) vpl[i].filters = [new BlurFilter(Math.sqrt(Math.pow(_camera.distanceTo(cubes[i]) - DISTANCE, 2)) / 50, Math.sqrt(Math.pow(_camera.distanceTo(cubes[i]) - DISTANCE, 2)) / 50)];

					else vpl[i].filters = [new BlurFilter(10, 10)];
				}				
			}

			singleRender();
		}
	}

}