Papervision3D + Loading Collada

Posted on January 13, 2009
Filed Under Actionscript 3.0, experiments | 2 Comments

Playing around with collada (dae) files which use textures. You can get lots of models at google’s 3D warehouse, check it and start playing around with it. Its really easy!

package
{
	// import flash
	import flash.filters.BlurFilter;
	import flash.display.StageScaleMode;
	import flash.display.StageAlign;
	import flash.display.Sprite;
	import flash.events.Event;	

	//import main papervision assets
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.lights.PointLight3D;
	import org.papervision3d.render.QuadrantRenderEngine;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.view.Viewport3D;

	//import objects
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.parsers.DAE;

	//import event listener
	import org.papervision3d.events.FileLoadEvent;

	public class EifelTowerCollada extends Sprite
	{
		//papervision main assets
		private var scene:Scene3D;
		private var viewport:Viewport3D;
		private var camera:Camera3D;
		private var light:PointLight3D;
		private var quadRenderer:QuadrantRenderEngine;

		//other things needed for dae
		public var universe:DisplayObject3D;
		private var daeFile:DAE;
		private var bl:Number;
		private var bt:Number;
		private var per:Number;

		//////////////////////////////////////////////////////////////////////////////////////
		//
		// Constructor
		//
		//////////////////////////////////////////////////////////////////////////////////////

		// -----------------------------------------------------------------------------------
		public function EifelTowerCollada()
		{
			addEventListener(Event.ADDED_TO_STAGE, initialize);
		}

		// -----------------------------------------------------------------------------------
		// constructor listener
		private function initialize(event:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, initialize);

			// blur for more realness
			filters = [new BlurFilter(1.5,1.5,3)];

			//Setup stage.
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.quality = "BEST";

			//Setup viewport, add to stage
			viewport = new Viewport3D(0,0,true);
			addChild(viewport);

			//Setup renderer
			quadRenderer = new QuadrantRenderEngine(1);

			//Setup camera
			camera = new Camera3D();
			camera.y = 100;
			camera.z = -200;

			//Setup light
			light = new PointLight3D(true);
			light.x = 0;
			light.y = 500;//camera.y;
			light.z = -200;//camera.z;

			//Setup scene
			scene = new Scene3D();

			//add dae object
			daeFile = new DAE();
			daeFile.load("daefiles/eifelTower.dae");

			// scale
			daeFile.y = -100;

			//add loading listeners to your dae
			daeFile.addEventListener(FileLoadEvent.LOAD_COMPLETE, handleLoadComplete);
			daeFile.addEventListener(FileLoadEvent.LOAD_ERROR, handleLoadError);
			daeFile.addEventListener(FileLoadEvent.LOAD_PROGRESS, handleProgress);
			daeFile.addEventListener(FileLoadEvent.SECURITY_LOAD_ERROR, handleSecurity);
			daeFile.addEventListener(FileLoadEvent.COLLADA_MATERIALS_DONE, handleMaterialsDone);

			//Setup container, add dae to container, add container to scene.
			universe = new DisplayObject3D();
			universe.addChild(daeFile);
			scene.addChild(universe);

			//Listen to enter frame
			addEventListener(Event.ENTER_FRAME, handleRender);
		}

		private function handleLoadComplete(e:Event):void
		{
			trace("IN EVENT LISTENER, LOAD COMPLETE");
		};

		private function handleLoadError(e:Event):void
		{
			trace("THERE HAS BEEN A LOADING ERROR");
		};

		private function handleProgress(e:Event):void
		{
			bl = e.target.bytesLoaded;
			bt = e.target.bytesTotal;
			per = Math.round(bl/bt*100);
			trace("COLLADA "+per+"% LOADED, PLEASE WAIT");
		};

		private function handleSecurity(e:Event):void
		{
			trace("THERE HAS BEEN A SECURITY ERROR");
		};

		private function handleMaterialsDone(e:Event):void
		{
			trace("COLLADA MATERIALS LOAD COMPLETE");
		};

		// _______________________________________________________________________
		// render on enterframe

		private function handleRender(e:Event):void
		{
			camera.lookAt(universe);
			quadRenderer.renderScene(scene, camera, viewport);
		}
	}
}

Now add the object to your stage

_eifelTower = new EifelTowerCollada();
addChild(_eifelTower);

Comments

2 Responses to “Papervision3D + Loading Collada”

  1. Brent on July 11th, 2011 6:02 am

    Thanks! I have been struggling with a few things (probably event listeners) and learned tons from looking at this.

  2. jobs spain live on September 16th, 2012 12:57 am

    Very great post. I just stumbled upon your weblog and wanted to say that I have
    really loved surfing around your blog posts. After all I’ll be subscribing in your rss feed and I’m hoping you write once more very soon!

-->