Switching a MovieMaterial on the face of a Cube - replaceMaterialByName
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); } } }
3 Comments to Switching a MovieMaterial on the face of a Cube - replaceMaterialByName
Thank you very much for this example
Best regards
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
January 1, 2009
@mateus - the download includes a “MovieMaterials.swc”. You have to add that .swc to your project in FlashDevelop.
Leave a comment
Search
Recent Posts
- MorphController - Mighty Morphing Papervision3D
- End dump
- Test if a plane is within the view of the camera (aka testing if culled)
- Materials Reference
- Perlin Blob
- Dynamic Text on a Plane
- Maya Texture Baking
- Creating a Custom VectorVision Font
- Workshop video and example dump
- 3D Math Book Recommendation
- Heading to New York. brb ;)
- Launching Flex4.org
- Flex 4 Layouts and Groups
- Flex 4 States
- How to click on stuff in Papervision3D - Viewport, ViewportLayers, InteractiveScene3DEvent, Mouse3D, and MovieMaterial Buttons
Recent Comments
- alexxcz on MorphController - Mighty Morphing Papervision3D
- andre venancio on 3D Math Book Recommendation
- DS on Dynamic Text on a Plane
- John Lindquist on Maya Texture Baking
- Itai on Maya Texture Baking
- Javier on End dump
- samBrown on End dump
- tf on archive
- Pan on requests
- Martin Lindelöf on requests


December 27, 2008