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

Using AS3Dmod: Dynamic Twisting

Objective of the tutorial

In the first AS3Dmod tutorials we have learned how to statically apply a simple modifier. We will now see how to dynamically apply another modifier: the Twist modifier.

How to

Set up

I've placed the AS3Dmod library in the first tutorial zip file. So please refer to the first tutorial or visit the official AS3Dmod website.

example041.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.Event;

	import com.as3dmod.ModifierStack;
	import com.as3dmod.plugins.sandy3d.LibrarySandy3d;
	import com.as3dmod.modifiers.Bend;
	import com.as3dmod.modifiers.Noise;
	import com.as3dmod.modifiers.Perlin;
	import com.as3dmod.modifiers.Skew;
	import com.as3dmod.modifiers.Taper;
	import com.as3dmod.modifiers.Twist;
	import com.as3dmod.util.ModConstant;
	import com.as3dmod.util.Phase;	
	import com.carlcalderon.arthropod.Debug;

	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 Example041 extends Sprite {

		private var scene:Scene3D;
		private var c:Box;
		
		// variable for as3Dmod
		private var m:ModifierStack;
		private var bone:Bend;
		private var bonePhase:Phase;
		private var btwo:Bend;
		private var btwoPhase:Phase;
		private var n:Noise;
		private var nph:Phase;
		private var s:Skew;
		private var sph:Phase;
		private var t:Taper;
		private var tph:Phase;
		private var w:Twist;
		private var wph:Phase;
		private var p:Perlin;
		
		
		
		public function Example041() {
			stage.quality = StageQuality.LOW;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			scene = new Scene3D("myScene", this, new Camera3D(stage.stageWidth, stage.stageHeight), new Group("root"));
			
			var materialAttr:MaterialAttributes = new MaterialAttributes(new LineAttributes(1, 0x49a51c, 1));
			var material:Material = new ColorMaterial(0x27590e, 1, materialAttr);
			
			var materialAttrb:MaterialAttributes = new MaterialAttributes(new LineAttributes(1, 0x49a51c, 1));
			var materialb:Material = new ColorMaterial(0x27590e, 1, materialAttrb);
			
			var app:Appearance = new Appearance(material, materialb);
			c = new Box("myBox", 200, 400, 200, "tri", 4);
			c.rotateZ = 90;
			c.rotateX = 60;
			c.rotateY = -45;
			c.z = 400;
			c.appearance = app;
			
			scene.root.addChild(c);
			
			// here is the code for the as3dmod
			//1. istantiate the ModifierStack 
			m = new ModifierStack(new LibrarySandy3d(), c);
		   
		    //2. define some modifier parameters
			w = new Twist(Math.PI / 2);
			wph = new Phase();
			
			// let's add the modifiers to the Stack
			m.addModifier(w);
			
				
			addEventListener(Event.ENTER_FRAME, render);
		}

		private function render(event:Event):void {
			
			// render the modification dinamically
			wph.value += 0.10;
			w.angle = Math.PI / 8 * wph.phasedValue;
			
			// apply the modification
			
			m.apply();
			
			// render with Sandy Engine
			scene.render();
		}
	}
}

Examining the code

In order to make a transformation dynamic you just need to remember and apply two simple rules:

  1. Define your object(s) and modifiers in the beginning of your work (outside the Event.ENTER_FRAME listener).
  2. Modify the modifiers parameters inside the Event.ENTER_FRAME listener.
  3. Apply the modifiers just before calling the Sandy render() function.

In our example we will use the Phase class provided by the AS3Dmod library. This class is just an helper class that implement a sin function, and it helps us to define a cycling between two values as time pass by.

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

This step is the same as the previous tutorial.

Create one (or more) modifiers
w = new Twist(Math.PI / 2);
wph = new Phase();

We have now defined the Twist modifier giving 90 degree as a the beginning value of the angle we want to twist or object.

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

This is a pretty simple step!

Tell the engine to render the modifiers
private function render(event:Event):void {
 
// render the modification dynamically
wph.value += 0.10;
w.angle = Math.PI / 8 * wph.phasedValue;
 
// apply the modification
m.apply();
 
// render with Sandy Engine
scene.render();
}

The main difference with the previous tutorial is that we continue to change the value of our modifier each time Flash engine run inside the listener (Event.ENTER_FRAME). Just before calling the Sandy scene.render(), we call the m.apply() AS3Dmod function.

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

The output

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