Loading Complex Models with Away3D

Away3D Loading Complex Models

Download Away3D Tutorial Source

When working with Away3D you’ll eventually need to load a complex model. In my experience, the issues with loading and rendering models has been with the model itself. You’ll want to work closely with a 3D modeling expert in the creation of the models you’ll be using with Away3D.

Hopefully you are one of the few blessed enough to have an resource available to modify and create Away3D ready models for you. If not, just know you’re not alone.

Those issues aside, the code for loading a 3D model into Away3D is fairly easy. It is almost as simple as loading a plane, cube, or cone.

This is just one way to work with models and Away3D. You can also embed the models into your files, I’ll reserve that for another tutorial.

1) Load your model

var loader3D:Loader3D = Collada.load("daeModel/cow.dae");
loader3D.addEventListener(Loader3DEvent.LOAD_SUCCESS, onModelLoadSuccess);

Papervision to Away3D Tip:
You’ll notice here that we are using a Loader3D. If you’re coming from Papervision this was done slightly different. With Papervision you could pass the reference to the model path in the constructor of the Colloda object. example: cow = new Collada("daeModel/cow.dae",materialList);.

2) Handle the load success

protected function onModelLoadSuccess(event:Loader3DEvent):void 
{
	cow = event.loader.handle as ObjectContainer3D;
	view.scene.addChild(cow);
}

Now that we have successfully loaded our model we need to get it from our Loader3D and add it to the scene. We do this by using event.loader.handle and casting the object returned as an ObjectContainer3D.

From the docs, here is what handle is doing:
Once the file has been loaded and is ready to view, the handle returns the parsed 3d object file

Here is the full code for your review:

package  
{
	import away3d.containers.ObjectContainer3D;
	import away3d.containers.View3D;
	import away3d.events.Loader3DEvent;
	import away3d.loaders.Collada;
	import away3d.loaders.Loader3D;
	import flash.display.Sprite;
	import flash.events.Event;
 
	public class Main extends Sprite 
	{
		protected var cow:ObjectContainer3D;
		protected var view:View3D;
 
		public function Main() 
		{
			super();
			this.addEventListener(Event.ADDED_TO_STAGE,onAddedToStage);	
		}
 
		protected function onAddedToStage(event:Event):void 
		{
			createChildren();
			loadModel();
			startRendering();
		}
 
		protected function createChildren():void 
		{
			view = new View3D({x:stage.stageWidth/2, y:stage.stageHeight/2});
			addChild(view);
		}
 
		protected function loadModel():void 
		{
			var loader3D:Loader3D = Collada.load("daeModel/cow.dae");
			loader3D.addEventListener(Loader3DEvent.LOAD_SUCCESS, onModelLoadSuccess);
		}
 
		protected function onModelLoadSuccess(event:Loader3DEvent):void 
		{
			cow = event.loader.handle as ObjectContainer3D;
			cow.scale(100);
			cow.moveDown(1);
			view.scene.addChild(cow);
		}
 
		protected function startRendering():void 
		{
			addEventListener(Event.ENTER_FRAME, onRenderTick);
		}
 
		protected function onRenderTick(event:Event = null):void 
		{
			//Render View
			view.render();
 
			//If our cow is ready - spin
			if (cow)
			{
				cow.yaw(2);
			}	
		}
	}
}

Download Away3D Tutorial Source

This entry was posted in Away3D, Away3D Loading Complex Models and tagged , , , , , . Bookmark the permalink.

3 Responses to Loading Complex Models with Away3D

  1. Pingback: Tweets that mention Loading Complex Models with Away3D | Flash 3D Tutorials -- Topsy.com

  2. Pingback: Away3D Tutorial : Loading Complex Models with Away3D | Papervision 3D Tutorials

  3. northmantif says:

    Hello!
    Did you use standard ‘cow’ model .dae found in the net, or just exported the model from 3D software? I have issues with that especially when exporting from Blender2.5x?
    Thanks anyway for handful tutorial.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>