Cube Inside Faces and Outside Faces

Friday, January 2nd, 2009 | examples

*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: ,

  • Andreas
    Hello, I just started playing around with PV3D and it seems really cool and i have a little question about this example.
    Is it possible to find out what side of the cube that have been pressed so you can specify different behavior on the different sides?

    Thank you =)
  • wow, optical illusion really doesn't work for me. i clicked the link expecting to see it as the outside of the cube and it looked very strange (because it was showing me the "inside"). had to close my eyes and imagine i was actually inside for it to work
  • nice, that looks much better.
  • John Lindquist
    I updated the example to address both of your comments. You still need to recreate the cube to take advantage of the "sides" parameter, but you also need to recreate the materialsList to release all the references to the old cube.
  • oh, disregard my question above, i just realized after Andre's comments that you're removing/readding the cubes. i think it'd be a cooler effect if you just hid/unhid them so they animate the same way as i mentioned above :P
  • when switch the cube many times, it appear that the processing go down... but you are removing it, what is going on?
  • just curious, when you switch the views, it seems like the cube re-animates into the spot rather than just having the new other one appear where the current one sat. why does that happen? or are my eyes playing tricks on me?
blog comments powered by Disqus

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 ...