Erain 3D
--> FDT Pure coding comfort


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

Using AS3Dmod: Static Bending

Objective of the tutorial

This is the first of a series of tutorials about the usage of AS3Dmod library. The library allows to transform any 3D object using different modifiers. In this tutorial we will see the usage of a simple modifiers, bending, but later on you will learn the usage of all the modifiers. The library can be found here, and this tutorial is based on the version 0.2.

How to

Set up

In order to make this tutorial working, your need to point to the AS3Dmod library. I am placing the library with the tutorial code, but to find newer versions check periodically the official website.

example040.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 com.as3dmod.ModifierStack;
	import com.as3dmod.plugins.sandy3d.LibrarySandy3d;
	import com.as3dmod.modifiers.Bend;
	import com.as3dmod.util.ModConstant;
	import com.as3dmod.util.Phase;	
	
	import sandy.core.Scene3D;
	import sandy.core.scenegraph.Camera3D;
	import sandy.core.scenegraph.Group;
	import sandy.core.scenegraph.Shape3D;
	import sandy.materials.Appearance;
	import sandy.materials.ColorMaterial;
	import sandy.materials.Material;
	import sandy.materials.attributes.LineAttributes;
	import sandy.materials.attributes.MaterialAttributes;
	import sandy.primitive.Box;	

	public class Example040 extends Sprite {

		private var scene:Scene3D;
		private var camera:Camera3D;
		private var c:Box;
		
		// variable for as3Dmod
		private var m:ModifierStack;

		
		public function Example040() {
			
			camera = new Camera3D(stage.stageWidth, stage.stageHeight);
			scene = new Scene3D("myScene", this, camera , new Group("root"));
			
			var materialAttr:MaterialAttributes = new MaterialAttributes(new LineAttributes(0.5, 0x990000, 0.5));
			var material:Material = new ColorMaterial(0xffcc33, 1, materialAttr);
		
			var app:Appearance = new Appearance(material);
			c = new Box("myBox", 200, 200, 200, "tri", 4);
			c.rotateZ = 45;
			c.rotateX = 170;
			c.rotateY = -45;
			c.z = 400;
			c.appearance = app;
			
			c.enableBackFaceCulling = false;
			
			scene.root.addChild(c);
			
			//1. istantiate the ModifierStack 
			m = new ModifierStack(new LibrarySandy3d(), c);
		   
		    //2. define Bend Modifier
			var b1:Bend = new Bend(0.3, 0.5) ;
			var b2:Bend = new Bend(0.3, 0.5) ;
			
			//3. definre some bedning parameters
			b1.bendAxis = ModConstant.X;
			b2.bendAxis = ModConstant.Z;
			
			//4. adding modifier and applying the modifications
			m.addModifier(b1);
			m.addModifier(b2);
			
			m.apply();
				
			stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMovedHandler);
			addEventListener(Event.ENTER_FRAME, render);
		}

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

Examining the code

Using AS3Dmod is pretty simple, because you only need to remember these basic steps :

  1. Instantiate a ModifierStack passing the object to be transformed and rendering engine
  2. Create the modifier object that you want to use passing the required parameters
  3. Add the modifier to the ModifierStach
  4. Tell the ModifierStach to render all the modifier that have been added to it

For this tutorial we will use a simple sandy primitive: the cube, but remember you can use any 3D object, so also any object you can import using all the Sandy parsers.

Create the Modifier Stack
//1. instantiate the ModifierStack 
m = new ModifierStack(new LibrarySandy3d(), c);

In this simple example we need to use the Sandy plug in and the object c is just a simple cube.

Create one (or more) modifiers
//2. define Bend Modifier
var b1:Bend = new Bend(0.3, 0.5) ;
var b2:Bend = new Bend(0.3, 0.5) ;
 
//3. define some bending parameters
b1.bendAxis = ModConstant.X;
b2.bendAxis = ModConstant.Z;

We have now defined the modifier. Since the AS3Dmod is an “additive” library, we can define any transformation we want, and just push them in the stack. In this example we have define two bending modifiers.
At point 3 we tell to the modifier which axis use in order to bend our cube. We have decided to bend the cube on two different axis, for a nice and symmetric transformation.

Adding the modifiers to the stack
//4. adding modifier
m.addModifier(b1);
m.addModifier(b2);

This is a pretty simple step!

Tell the engine to render the modifiers
m.apply();

It is important to have this step separated from the others since you have the choice to tell the program when you need to apply the modifiers to your objects. Maybe in reaction to some events.

This is it! Let’s see our result now.

The output

Let's see our result after applying our modifiers to a simple cube.