Archive for January 2nd, 2009

Cube Inside Faces and Outside Faces

Friday, January 2nd, 2009 | examples | Comments

*updated to address comments


source

package
{
	import flash.display.Bitmap;
	import flash.events.Event;
	import flash.events.MouseEvent;
 
	import org.papervision3d.core.proto.MaterialObject3D;
	import org.papervision3d.lights.PointLight3D;
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.materials.shaders.CellShader;
	import org.papervision3d.materials.shaders.ShadedMaterial;
	import org.papervision3d.materials.shaders.Shader;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.objects.primitives.Cube;
	import org.papervision3d.view.BasicView;
	import org.papervision3d.view.stats.StatsView;
 
	[SWF(width="640", height="480", backgroundColor="#000000", frameRate="60")]
	public class InsideFacesOfACube extends BasicView
	{
		[Embed(source="assets/back.jpg")]
		private var backAsset:Class;
 
		[Embed(source="assets/bottom.jpg")]
		private var bottomAsset:Class;
 
		[Embed(source="assets/front.jpg")]
		private var frontAsset:Class;
 
		[Embed(source="assets/left.jpg")]
		private var leftAsset:Class;
 
		[Embed(source="assets/right.jpg")]
		private var rightAsset:Class;
 
		[Embed(source="assets/top.jpg")]
		private var topAsset:Class;
 
		[Embed(source="assets/287.jpg")]
		private var environmentAsset:Class;
 
		private var light:PointLight3D;
		private var cube:Cube;
		private var materialsList:MaterialsList;
		private var sides:int = Cube.ALL;
 
		public function InsideFacesOfACube()
		{
			createMaterialsList();
 
			cube = new Cube(materialsList, 500, 500, 500, 5, 5, 5, sides);
			scene.addChild(cube);
 
			startRendering();
 
			stage.addEventListener(MouseEvent.CLICK, stage_clickHandler);
		}
 
		private function createMaterialsList():void
		{
			materialsList = new MaterialsList();
 
			light = new PointLight3D();
 
			materialsList.addMaterial(createShadedMaterial(backAsset), "back");
			materialsList.addMaterial(createShadedMaterial(bottomAsset), "bottom");
			materialsList.addMaterial(createShadedMaterial(frontAsset), "front");
			materialsList.addMaterial(createShadedMaterial(leftAsset), "left");
			materialsList.addMaterial(createShadedMaterial(rightAsset), "right");
			materialsList.addMaterial(createShadedMaterial(topAsset), "top");
		}
 
		private function createShadedMaterial(bitmapAsset:Class):MaterialObject3D
		{
			var bitmap:Bitmap = new bitmapAsset() as Bitmap;
			var bitmapMaterial:BitmapMaterial = new BitmapMaterial(bitmap.bitmapData, true);
			var shader:Shader = new CellShader(light, 0xffffff, 0xaaaaaa, 10); 
			var shadedMaterial:ShadedMaterial = new ShadedMaterial(bitmapMaterial, shader);
			return shadedMaterial;
		}
 
		private function stage_clickHandler(event:MouseEvent):void
		{
			if(sides == Cube.ALL)
			{
				sides = Cube.NONE;
			}
			else
			{
				//you could also specify specific sides you want
				//i.e. - if you want every side but the front do:
				//sides = Cube.ALL - Cube.Front;
				//or if you only want the top and the bottom do:
				//sides = Cube.TOP + Cube.BOTTOM;
				sides = Cube.ALL;
			}
 
			scene.removeChild(cube);
			createMaterialsList();
			var tempRotationX:Number = cube.rotationX;
			var tempRotationY:Number = cube.rotationY; 
			cube = new Cube(materialsList, 500, 500, 500, 5, 5, 5, sides);
			cube.rotationX = tempRotationX;
			cube.rotationY = tempRotationY; 
			scene.addChild(cube);
		}
 
		override protected function onRenderTick(event:Event=null):void
		{
			cube.rotationY += (viewport.containerSprite.mouseX - cube.rotationY) * .1;
			cube.rotationX += (viewport.containerSprite.mouseY - cube.rotationX) * .1;
			renderer.renderScene(scene, camera, viewport);
		}
	}
}

Tags: ,

Search

Recommended Books

Speaking at FITC Toronto

 

January 2009
M T W T F S S
« Dec   Feb »
 1234
567891011
12131415161718
19202122232425
262728293031  

Preferred Video Tutorial Resolution

  • 1024x768 (53%, 85 Votes)
  • 1280x1024 (15%, 24 Votes)
  • 1920x1080 (15%, 24 Votes)
  • 800x600 (13%, 20 Votes)
  • 480x320 (4%, 6 Votes)
  • 640x480 (0%, 2 Votes)

Total Voters: 160

Loading ... Loading ...