020 7920 7120 | public@publicreative.com

January 13th, 2009

PV3D with AS3DMod and Shaders

This was my first attempt at using the AS3DMod Library so you can see where the previously posted example came from.

I’ve also added gouraud shading to make it a little more interesting.

Click image to open:

publicFlag.jpg


Here is the source:

package
{
	import com.as3dmod.ModifierStack;
	import com.as3dmod.modifiers.Perlin;
	import com.as3dmod.plugins.pv3d.LibraryPv3d;
 
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.StageAlign;
	import flash.display.StageQuality;
	import flash.display.StageScaleMode;
	import flash.events.Event;
 
	import org.papervision3d.lights.PointLight3D;
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.materials.shaders.GouraudShader;
	import org.papervision3d.materials.shaders.ShadedMaterial;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.view.BasicView;
 
	[SWF(width="600", height="400", backgroundColor="#000000", frameRate="31")]
 
	public class PublicFlag extends BasicView
	{
		[Embed(source="assets/flagLogo.gif")]
		public var Flag:Class;
 
		private var _plane:Plane;
		private var _mod:ModifierStack;
 
		private var _perlin:Perlin;
 
		private var _xPhaseVal:Number = 0;
		private var _yPhaseVal:Number = 0;
		private var _targXPhaseVal:Number = 0;
		private var _targYPhaseVal:Number = 0;
 
		private var _light:PointLight3D;
 
		public function PublicFlag()
		{
			super(stage.stageWidth, stage.stageHeight);
			init();
		}
 
		private function init():void
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			stage.stageFocusRect = false;
 
			_light = new PointLight3D();
			_light.z = -100;
			_light.y = -100;
 
			var logo:Bitmap = new Flag() as Bitmap;
 
			var shader:GouraudShader = new GouraudShader( _light , 0xffffff , 0x555555 , 10);
			var mat:ShadedMaterial = new ShadedMaterial( new BitmapMaterial( logo.bitmapData) , shader);
			mat.doubleSided = true;
			_plane = new Plane( mat , logo.width , logo.height , 12, 8);
			_plane.useOwnContainer = true;
 
			camera.fov=34;
			scene.addChild( _plane );
 
			modify();
 
			startRendering();
		}
 
		private function modify():void
		{
			 _mod = new ModifierStack( new LibraryPv3d() , _plane );
 
			_perlin = new Perlin(3);
			_perlin.setFalloff( 0 , 1 );
 
			_mod.addModifier( _perlin );
		}
 
		override protected function onRenderTick(event:Event=null):void
		{
		 	_xPhaseVal = (stage.mouseX - stage.stageWidth/2)/stage.stageWidth;
		 	_plane.rotationY += ( (160*_xPhaseVal)-_plane.rotationY)/10;
 
		 	_yPhaseVal = (stage.mouseY - stage.stageHeight/2)/stage.stageHeight;
		 	_plane.rotationX += ( (100 * _yPhaseVal) - _plane.rotationX)/10;
 
			_mod.apply();
			super.onRenderTick(event);
		}
	}
}

One Response to “PV3D with AS3DMod and Shaders”

  1. Tim John says:

    Hi,

    I’ve been following this example and a couple of others and it seems something has changed since these examples were posted. I just can’t get shading to work properly on a modified plane – shading just seems to apply as if the plane is still flat. I’m not the only one that’s had this problem by the look of it. Do you know a way of forcing this in these latest libraries?

    Thanks,
    Tim

Tell us what you think!