Author: Max Pellizzaro
Date: October 29th 2008
version: 3.0.3

Simple Extrusion

Objective of the tutorial

If you have completed the first tutorial of this series, you are ready to really start to extrude objects, and this is the goal of this tutorial.

How to

Set up

No need to set up for this tutorial, just download the classes and they are ready to go.

example44.zip

The AS Code

In this section we report the AS code as a reference, and it will be explained in the next paragraph.

package {
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageQuality;
	import flash.display.StageScaleMode;
	import flash.events.*;
    import flash.ui.*;
	import flash.geom.Point;
	
	import sandy.core.Scene3D;
	import sandy.core.scenegraph.Camera3D;
	import sandy.core.scenegraph.Group;
	import sandy.core.scenegraph.Shape3D;
	import sandy.materials.*;
	import sandy.materials.attributes.*;
	import sandy.primitive.Box;
	import sandy.extrusion.Extrusion;
	import sandy.extrusion.data.*;
	import sandy.core.data.*;

	public class Example044 extends Sprite {

		private var scene:Scene3D;
		private var camera:Camera3D;
		private var c:Box;
		private var ext:Extrusion;
		
		public function Example044() {
			
			camera = new Camera3D(stage.stageWidth, stage.stageHeight);
			scene = new Scene3D("myScene", this, camera , new Group("root"));
			
			var arrayTri:Array = new Array();
			arrayTri[0] = new Point(0,0);
			arrayTri[1] = new Point(50,50);
			arrayTri[2] = new Point(100,0);
			arrayTri[3] = new Point(0,0);
			var theTri:Polygon2D = new Polygon2D(arrayTri);
			
			var m0:Matrix4 = new Matrix4(); m0.identity();
			var m1:Matrix4 = new Matrix4(); m1.translation(0,0,350);
			
			ext = new Extrusion("tri", theTri, [m0, m1]);
			ext.appearance = new Appearance (new ColorMaterial (0xFFAF00, 1,
				new MaterialAttributes (new LightAttributes( true, 0.1), 
                                new OutlineAttributes(3, 0xFC5858, 1),
								new LineAttributes(1, 0x000000, 1))));
			ext.appearance.frontMaterial.lightingEnable = true;
            
			ext.geometryCenter= new Vector(-48,-15,0);
			
			ext.rotateY = 90;
			ext.x = 120;
			ext.pan = 40;
			scene.root.addChild(ext);
			
			
			stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMovedHandler);
			addEventListener(Event.ENTER_FRAME, render);
		}

		private function render(event:Event):void {
			scene.render();
			ext.roll += 4;
		}
		
		private function mouseMovedHandler(event:MouseEvent):void {
           //ext.pan=(event.stageX-550/2)/5;
		   //ext.tilt=(event.stageY-400/2)/20;
 
        }
	}
}

Examining the code

The first thing we need to build is the polygon as we did in the first tutorial. In this case we will use a simple triangle as a simple polygon.

var arrayTri:Array = new Array();
arrayTri[0] = new Point(0,0);
arrayTri[1] = new Point(50,50);
arrayTri[2] = new Point(100,0);
arrayTri[3] = new Point(0,0);
 
var theTri:Polygon2D = new Polygon2D(arrayTri);

Now in order to “define the extrusion” we need to tell Sandy the direction and the dimension of the extrusion. For simple straight extrusion we just need to defiine Z value for a matrix transformation. The Z value will tell the distance between the front and back face (represented by the polygon2D we have just defined).

var m0:Matrix4 = new Matrix4(); 
m0.identity();
var m1:Matrix4 = new Matrix4(); 
m1.translation(0,0,350);

Once we have defined the starting matrix (m0) and the ending matrix (m1), we just need to pass them with the constructor of the Extrusion class.

ext = new Extrusion("tri", theTri, [m0, m1]);

The constructor also takes two more Boolean parameters to tell Sandy to render or not the front and the back face of the object. If we don't pass anything we are implicitly asking Sandy to render the faces.

Let’s see our result now.

The output