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 Example Quadrant Render And Layers

@author duarte peixinho
mail: webmaster@duartepeixinho.com
package
{
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	import flash.filters.BlurFilter;
	import org.papervision3d.core.material.TriangleMaterial;
	import org.papervision3d.core.render.data.QuadTree;
	import org.papervision3d.materials.BitmapColorMaterial;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.objects.primitives.Cube;
	import org.papervision3d.objects.primitives.Cylinder;
	import org.papervision3d.view.BasicView;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.view.layer.ViewportLayer;
	import org.papervision3d.render.QuadrantRenderEngine;
	import org.papervision3d.core.effects.view.ReflectionView;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.text.AntiAliasType;
	import flash.display.BlendMode;
	
	/**
	 * @author duarte peixinho
	 * mail: webmaster@duartepeixinho.com
	 */
	public class relativeRotation extends ReflectionView
	{
		
		private var cube1:Cube;
		private var cube2:Cube;
		private var container:DisplayObject3D;
		
		private var car:DisplayObject3D;
		private var containerWheel1:DisplayObject3D;
		private var containerWheel2:DisplayObject3D;
		private var containerWheelSteer1:DisplayObject3D;
		private var containerWheelSteer2:DisplayObject3D;
		
		private var cylinder1:Cylinder;
		private var cylinder2:Cylinder;		
		private var cylinder3:Cylinder;
		private var cylinder4:Cylinder;	
		
		private var quadRenderer:QuadrantRenderEngine;
		private var quadtree:QuadTree=new QuadTree();
		private var render:Boolean = false;
		
		private var text:TextField = new TextField();
		
		public function relativeRotation():void 
		{
			super(stage.stageWidth, stage.stageHeight, true, true);
			
			surfaceHeight = -235;
			viewportReflection.alpha = 0.2;	
			viewportReflection.filters = [new BlurFilter()];
			
			init3d();
			
			// events
			stage.addEventListener(MouseEvent.CLICK, mouseClicked);
			stage.addEventListener(Event.ENTER_FRAME, processFrame); 
			quadRenderer = new QuadrantRenderEngine(QuadrantRenderEngine.CORRECT_Z_FILTER);
			
			singleRender();		
			
			text.antiAliasType = AntiAliasType.ADVANCED;
			text.blendMode = BlendMode.LAYER;
			text.x = 10;
			text.y = 30;
			text.autoSize = TextFieldAutoSize.LEFT;
			text.setTextFormat(new TextFormat("Arial", 14, 0x000000, true));
			text.textColor = 0x000000;
			text.selectable = false;			
			text.text = "Using BasicRender (Layers) - Click on the Screen";
			addChild(text);
			
		}
		
		public function mouseClicked(e:MouseEvent):void {
			if (render) {
				render = false;
				text.text = "Using BasicRender (Layers)";
				viewportReflection.alpha = 0.2;
			}
			else {
				render = true;
				text.text = "Using Quadtree Render - Click on the Screen";
				viewportReflection.alpha = 0;
			}
		}
		public function init3d():void {
			
			//container for all scene
			container = new DisplayObject3D();
			
			//car container
			car = new DisplayObject3D();	
			
			//container for wheels and for steering
			containerWheel1 = new DisplayObject3D();
			containerWheel2 = new DisplayObject3D();
			containerWheelSteer1 = new DisplayObject3D();
			containerWheelSteer2 = new DisplayObject3D();
			
			//creating primitives
			cube1 = new Cube(addMaterial(), 200, 200, 200);			
			cube2 = new Cube(addMaterial(), 200, 200, 200);
			cylinder1 = new Cylinder(new BitmapColorMaterial(0x000000), 50, 10);
			cylinder2 = new Cylinder(new BitmapColorMaterial(0x000000), 50, 10);
			cylinder3 = new Cylinder(new BitmapColorMaterial(0x000000), 50, 10);
			cylinder4 = new Cylinder(new BitmapColorMaterial(0x000000), 50, 10);
			
			//setting primitives location in space
			cube1.x = -300;
			car.x = 300;
			
			//setting the steer container
			containerWheelSteer1.addChild(cylinder1);
			containerWheelSteer2.addChild(cylinder2);
			//setting the position container
			containerWheel1.addChild(containerWheelSteer1);
			containerWheel2.addChild(containerWheelSteer2);
			
			//setting the wheels position			
			containerWheel1.y = containerWheel2.y = cylinder3.y = cylinder4.y = -70;
			containerWheel1.rotationX = containerWheel1.rotationY = containerWheel2.rotationY = containerWheel2.rotationX = cylinder3.rotationY = cylinder3.rotationX = cylinder4.rotationY = cylinder4.rotationX =90;
			containerWheel1.x = cylinder3.x = cube2.x - 110;
			containerWheel2.x = cylinder4.x = cube2.x + 110;
			containerWheel1.z = containerWheel2.z = -90;
			cylinder3.z = cylinder4.z = 90;
			
			//setting the quadtree level
			quadtree.maxLevel = 1;
			
			//don't check cube1 when the rendering is quadtree
			cube1.testQuad = false;
			
			//setting layers
			var layer:ViewportLayer = viewport.getChildLayer(cube1, true);			
			layer.filters = [new BlurFilter()];
			layer.alpha = 0.5;
			
			var layer2:ViewportLayer = viewport.getChildLayer(cube2, true);
			layer2.layerIndex = layer.layerIndex = 2;
			
			var layerWheels1:ViewportLayer = viewport.getChildLayer(cylinder1, true);
			var layerWheels2:ViewportLayer = viewport.getChildLayer(cylinder2, true);
			var layerWheels3:ViewportLayer = viewport.getChildLayer(cylinder3, true);					
			var layerWheels4:ViewportLayer = viewport.getChildLayer(cylinder4, true);
			layerWheels1.layerIndex = layerWheels2.layerIndex = layerWheels3.layerIndex = layerWheels4.layerIndex = 1;
			
			//displaying objects
			car.addChild(cube2);
			car.addChild(containerWheel1);
			car.addChild(containerWheel2);
			car.addChild(cylinder3);
			car.addChild(cylinder4);
			container.addChild(car);
			container.addChild(cube1);
			
			scene.addChild(container);
			_camera.target = car;
		}
		
		public function processFrame(e:Event):void {			
			_camera.y = (stage.stageHeight / 2 - mouseY) * 2;				
			
			//-------------- steering -------------
			containerWheelSteer1.localRotationZ = containerWheelSteer2.localRotationZ = 20;
			
			//
			cube1.yaw(3);			
			
			//wheels rotating
			cylinder1.localRotationY = cylinder2.localRotationY = cylinder3.localRotationY = cylinder4.localRotationY += 5;

			container.yaw(2);
			if (!render) singleRender();
			else quadRenderer.renderScene(scene, _camera, viewport);
		}
		
		public function addMaterial():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;
		}
		
	}
	
}