Swapping materials on faces

Saturday, December 13th, 2008 | examples

I starting playing around with the last post and got this:


source

package
{
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.geom.ColorTransform;
	import flash.geom.Rectangle;
 
	import org.papervision3d.core.geom.renderables.Triangle3D;
	import org.papervision3d.events.InteractiveScene3DEvent;
	import org.papervision3d.materials.BitmapColorMaterial;
	import org.papervision3d.objects.primitives.Sphere;
	import org.papervision3d.view.BasicView;
 
	[SWF(width="640", height="480", backgroundColor="#000000", frameRate="60")]
	public class SwappingMaterialsOnFaces extends BasicView
	{
		private const WHITE:uint = 4294967295;
		private const COLOR:uint = 0x141414;
 
		private var whiteMaterial:BitmapColorMaterial;
		private var sphere:Sphere
		private var rect:Rectangle;
		private var whiteColorTransform:ColorTransform = new ColorTransform(1, 1, 1, 1, 3, 3, 7);
		private var bitmaps:Array = [];
		private var currentFace3d:Triangle3D = null;
 
		public function SwappingMaterialsOnFaces()
		{
 
			viewport.interactive = true;
 
			whiteMaterial = new BitmapColorMaterial(0xffffff);
			whiteMaterial.interactive = true;
 
			sphere = new Sphere(whiteMaterial, 500, 30, 20);
			sphere.addEventListener(InteractiveScene3DEvent.OBJECT_MOVE, sphere_objectMoveHandler);
 
			scene.addChild(sphere);
 
			rect = whiteMaterial.bitmap.rect;
 
			startRendering();
		}
 
		private function sphere_objectMoveHandler(event:InteractiveScene3DEvent):void
		{
			if(currentFace3d != event.face3d)
			{
				var bitmapMaterial:BitmapColorMaterial = new BitmapColorMaterial(COLOR);;
				bitmapMaterial.interactive = true;
 
				event.face3d.material = bitmapMaterial;
 
				bitmaps.push(event.face3d.material.bitmap);
				currentFace3d = event.face3d;
			}
		}
 
		override protected function onRenderTick(event:Event=null):void
		{
			sphere.rotationY += .3;
 
			for(var i:int = 0; i < bitmaps.length; i++)
			{
				if(BitmapData(bitmaps[i]).getPixel32(0,0) != WHITE)
				{
					BitmapData(bitmaps[i]).colorTransform(rect, whiteColorTransform);
				}else
				{
					bitmaps.splice(i, 1);
				}
			}
 
			renderer.renderScene(scene, camera, viewport);
		}
	}
}

Tags: ,

No comments yet.

Leave a comment