The most basic away3d scene

Time to get cracking getting back to doing 3D in AS3. I’m very excited for molehill, and i’m excited that at work I am just starting a new project in which we need to use one of the existing 3D packages. Previously i’ve always used Papervision3D, and i felt very comfortable with it and how it works and the ins-and-outs so I was able to get my hands dirty with it whenever I needed and not feel lost.

Well that time is over, Papervision3D has long since been dead.

With Away3D already working with adobe on their future release of their API that supports Molehill, and a new project at work that will require using one of the 3D packages, I can’t think of a better time to start using/learning Away3D. Especially with the aid of my fellow co worker, ‘wu media‘ – who’s a bit of a wizkid when it comes to 3D

Below is a very basic stub. It contains a barebones document class with barebones 3D scene, including one light source.

I find these useful when you need to just have something on the screen and then go from there. So this is also kind of a reference for myself as well.

package 
{
	import away3d.cameras.Camera3D;
	import away3d.containers.Scene3D;
	import away3d.containers.View3D;
	import away3d.lights.DirectionalLight3D;
	import away3d.primitives.Cube;
 
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.geom.Vector3D;
 
	[SWF(width="800", height="800", backgroundColor="0x000000", frameRate="60")]
	public class MostBasicAwayDocumentClass extends Sprite
	{
		// Away3D 
		private var camera	:Camera3D;
		private var view 	:View3D;
		private var scene	:Scene3D;
		// Constants
		private const WORLD_SIZE	:Number = 800;
 
 
		public function MostBasicAwayDocumentClass()
		{
			if(stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
 
		private function init(e:Event = null):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
 
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
 
			initEngine();
			initObjects();
			addEventListener(Event.ENTER_FRAME, run);			
		}
 
		private function initEngine():void
		{
			camera = new Camera3D();
			view = new View3D();
			view.camera.moveTo(0, 0, -2000);
			view.camera.lookAt(new Vector3D(0, 0, 0));
			view.camera.zoom = 10;
			view.camera.focus = 50;
			view.x = stage.stageWidth/2;
			view.y = stage.stageHeight/2;
 
			addChild(view);
			scene = view.scene;	
		}
 
		private function initObjects():void
		{
			var light	:DirectionalLight3D = new DirectionalLight3D();
			light.color = 0xFFFFFF;
			light.ambient = 0.45;
			light.diffuse = 0.55;
			light.specular = 0.9;
			light.direction = new Vector3D(-1, -1, -1);
 
			scene.addLight(light);
 
 
			//
			var cube	:Cube = new Cube();
			scene.addChild(cube);
		}
 
		private function run(e:Event):void
		{
			view.render();
		}
	}
}

Leave a Reply

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


*