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:

  • John Lindquist

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

  • It seems that the replaceMaterialByName is no longer available in the latest version of Papervision. I’ve got a workaround by using:

    cube.materials.getMaterialByName( “front” ).copy( newMaterial );

    Where newMaterial could be any extension of MaterialObject3D.

    Hope that helps.

  • mateus

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

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

    do you know why?

    Thanks

  • psych

    Thank you very much for this example :)

    Best regards

blog comments powered by Disqus

Search

Recommended Books

Speaking at FITC Toronto

 

December 2008
M T W T F S S
« Nov   Jan »
1234567
891011121314
15161718192021
22232425262728
293031  

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