Seb Lee-Delisle

Menu

Changing a cube’s material dynamically in Papervision3D

It’s long been a bug bear that you can’t dynamically change a cube’s materials, so I’ve just committed a simple update that has simply moved Tim Knip’s clever material swapping methods from the DAE object into the DisplayObject3D. So now we can call replaceMaterialsByName on any DisplayObject3D!

So you can use it by calling :

cube.replaceMaterialByName(new ColorMaterial(0xff0000,1), "front");

Note that this will only work if each material in the MaterialsList is unique. In other words when you set up the cube’s material list, you need to assign a different material to each of the sides of the cube.

Here’s a quick demo :

And here's the source :
(Sorry about the lack of comments!)

 
package {
	import flash.events.Event;
 
	import org.papervision3d.cameras.CameraType;
	import org.papervision3d.core.effects.view.ReflectionView;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.materials.WireframeMaterial;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.objects.primitives.Cube;
 
	[SWF (width="640", height="480", backgroundColor="0x111111", frameRate="30")]
 
	public class CubeTest extends ReflectionView
	{
 
		private var cube : Cube;
		private var counter:int = 0;
		private var facelist : Array = ["left", "back", "right", "top", "front", "bottom"];
		private var currentFace :uint = 0;
		private var colorMaterials : Array = new Array();
		private var wireframeMaterials : Array = new Array();
		private var materials : Array = new Array();
		private var currentMaterial:int = 0;
 
		public function CubeTest()
		{
			super(640,480);
			surfaceHeight = -150;
			camera.fov = 30;
			camera.y = 200;
 
 
			var ml : MaterialsList = new MaterialsList();
 
			for (var i:int = 0; i<6; i++)
			{
 
				var colorMaterial : ColorMaterial = new ColorMaterial(0xff0000,0.4+(i/10));
				colorMaterial.doubleSided = true;
				colorMaterials.push(colorMaterial);
 
				var wireframeMaterial : WireframeMaterial = new WireframeMaterial(0xff0000,0.4+(i/10));
				wireframeMaterial.doubleSided = true;
				wireframeMaterials.push(wireframeMaterial);
 
 
			}
			materials = [wireframeMaterials, colorMaterials];
 
			ml.addMaterial(colorMaterials[0], "left");
			ml.addMaterial(colorMaterials[1], "back");
			ml.addMaterial(colorMaterials[2], "top");
			ml.addMaterial(colorMaterials[3], "right");
			ml.addMaterial(colorMaterials[4], "bottom");
  			ml.addMaterial(colorMaterials[5], "front");
 
            cube = new Cube(ml,150,150,150,5,5,5);
			scene.addChild(cube);
 
			addEventListener(Event.ENTER_FRAME, enterFrame);
 
		}
 
		public function enterFrame(e:Event) : void
		{
 
			if(counter%10==0)
			{
				trace("replacing ", facelist[currentFace], "with material number ", currentMaterial);
 
				cube.replaceMaterialByName(materials[currentMaterial][currentFace], facelist[currentFace]);
				currentFace++;
 
				if(currentFace==facelist.length)
				{
					currentFace = 0;
					currentMaterial++;
					if(currentMaterial==materials.length)
					{
						currentMaterial = 0;
					}
 
				}
			}
 
			cube.yaw(-1);
			cube.y=(120-(mouseY/2));
			if(cube.y<0) cube.y = 0;
 
			singleRender();
			counter++;
 
		}
 
	}
}
This entry was posted in Actionscript, Flash, Flash 3D, Obsolete, Papervision3D. Bookmark the permalink.

30 Responses to Changing a cube’s material dynamically in Papervision3D