Switching a MovieMaterial on the face of a Cube - replaceMaterialByName

Friday, December 26th, 2008 | examples


source

package
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
 
	import org.papervision3d.materials.MovieMaterial;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.objects.primitives.Cube;
	import org.papervision3d.view.BasicView;
 
	[SWF(width="640", height="480", backgroundColor="#000000", frameRate="60")]
	public class SwitchingMovieMaterialsOnACube extends BasicView
	{
		private var cube:Cube;
		private var currentAsset:Class = blueBoxAsset;
 
		public function SwitchingMovieMaterialsOnACube()
		{
			var materialsList:MaterialsList = new MaterialsList();
 
			//blueBoxAsset is the classname of the movie from the library in the MovieMaterials .swc
			//you have to create a new material for each side or the materialsList will replace all
			//the materials when you try to replace one side
			materialsList.addMaterial(createMovieMaterialFromAsset(blueBoxAsset), "front");
			materialsList.addMaterial(createMovieMaterialFromAsset(blueBoxAsset), "back");
			materialsList.addMaterial(createMovieMaterialFromAsset(blueBoxAsset), "left");
			materialsList.addMaterial(createMovieMaterialFromAsset(blueBoxAsset), "right");
			materialsList.addMaterial(createMovieMaterialFromAsset(blueBoxAsset), "top");
			materialsList.addMaterial(createMovieMaterialFromAsset(blueBoxAsset), "bottom");
 
			cube = new Cube(materialsList);
 
			scene.addChild(cube);
 
			startRendering();
 
			stage.addEventListener(MouseEvent.CLICK, stage_clickHandler);
		}
 
		//Creates a MovieMaterial based on a class reference
		//in this case, movieClips in the .swc
		private function createMovieMaterialFromAsset(asset:Class):MovieMaterial
		{
			var movieAsset:MovieClip = MovieClip(new asset());
			var movieMaterial:MovieMaterial = new MovieMaterial(movieAsset, true, true, true);
			movieMaterial.doubleSided = true;
 
			return movieMaterial;
		}
 
		private function stage_clickHandler(event:MouseEvent):void
		{
			var movieMaterial:MovieMaterial;
			if(currentAsset == blueBoxAsset)
			{
				movieMaterial = createMovieMaterialFromAsset(redBoxAsset);
				currentAsset = redBoxAsset;			
			}
			else
			{
				movieMaterial = createMovieMaterialFromAsset(blueBoxAsset);
				currentAsset = blueBoxAsset;			
			}
			//since the camera defaults to -1000 z, the "back" is the "front"
			cube.replaceMaterialByName(movieMaterial, "back");
		}
 
		override protected function onRenderTick(event:Event = null):void
		{
			cube.rotationX += (viewport.containerSprite.mouseY * .25 - cube.rotationX) * .1;
			cube.rotationY += (viewport.containerSprite.mouseX  * .25- cube.rotationY) * .1;
			super.onRenderTick(event);
		}
 
	}
}

Tags:

3 Comments to Switching a MovieMaterial on the face of a Cube - replaceMaterialByName

psych
December 27, 2008

Thank you very much for this example :)

Best regards

mateus
January 1, 2009

hi there, I’m getting this error (using Flash Develop)

H:\flash\projects\SwitchingMovieMaterialsOnACube\src\SwitchingMovieMaterialsOnACube.as(16): col: 36 Error: Access of undefined property blueBoxAsset.

do you know why?

Thanks

John Lindquist
January 1, 2009

@mateus - the download includes a “MovieMaterials.swc”. You have to add that .swc to your project in FlashDevelop.

Leave a comment