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++; } } }
Related posts:


Thanks Seb ;o) Thats what i needed
Glad I could help Andi!
[...] Changing a cube’s material dynamically in Papervision3D [...]
How can i move the functin replaceMaterialByName to DisplayObject3D?
Hi Burrows,
You don’t have to move it! It’s in the latest version of GreatWhite. Download instructions are here : http://archive.pv3d.org/?page_id=66
cheers!
Seb
How use this method??? I think i have lastest version, but dont work??? When iam wronging
Hi Slav,
yeah it should all work fine with the latest version. See my previous message for download instructions.
cheers!
Seb
My apology and risk to look like stupid, but i cant use this method because it’s missing.
Yes, I have lastest version of GreatWhite, update with SVN with link from your instruction but it’s not there(the method). I’m so confuse. What am i trying but nothing.
If you want to help me, send your version of GreatWhite to my mail(pensionera@gyuvetch.bg) or help me other way if you can
Hi Slav,
it’s definitely there… make sure you get the as3 branch in trunk.
cheers!
Seb
I have been using the reflectionview recently and run couple of issues. It is standard project with cubes and reflection, but i want my reflection too look like on a Plane so
1. Can I make the plain object not to have reflection
2. Can i make it under the reflections
Thanks
I can not open “http://papervision3d.googlecode.com/svn/trunk/branches/GreatWhite/src/”, please make sure is it here?
Hi Jas,
the GreatWhite branch no longer exists per se. You just need the main as3 branch now.
cheers!
Seb
[...] >> papervision3d Skateboard simulator in Papervision3D Saved by ToxicDetour on Thu 30-10-2008 Changing a cube’s material dynamically in Papervision3D Saved by skydancer850 on Thu 30-10-2008 Using the bend modifier with Collada objects Saved by [...]
This is a fantastic bit of functionality, but how are old BitmapMaterials destroyed when replaced using replaceMaterialByName? Does this have to be done explicitly or is it taken care of within the Great White DisplayObject3D code? (I’m having trouble with memory usage accumulation and am not sure I’ve really got my head around destroying the bitmapData … I’ve read your other post at http://www.sebleedelisle.com/?p=309 and have updated my PV3D accordingly)
Thanks Seb
Nick
Hi All V. interesting …
But what happens when you add other objects, eg
Thiss is the same code as above but with another cude added, cube1, offset y =200
and the same swap materials applied to it, and yet its remains unaltered. Am I over simplifiying here?
Love to know your thoughts
Uni-boy
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 cube1 : 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);
///
cube1 = new Cube(ml,150,150,150,5,5,5);
cube1.x =200;
scene.addChild(cube1);
///
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]);
/// cube1.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;
cube1.yaw(-5);
////
cube1.y=(120-(mouseY/2));
if(cube1.y<0) cube1.y = 0;
////
singleRender();
counter++;
}
}
}
[...] Seb Lee-Delise – Changing the materials on a cube dynamically. [...]
Hi, just wondering, why is it sometimes, when I use replaceMaterialByName and mentioning one of the name of the face of a cube, it change all the cube face into that new face. e.g. cube.replaceMaterialByName(material1, “front”); sometimes all the side change into material1 … is there something wrong that I did, or is it a bug? Cheers
@Samiaji Adisasmito yeah that shouldn’t happen, so not sure what’s happening there… wanna post the source?
Het Seb Lee-Delisle,
I am working on my own site and i needed to switch my cubesides dynamicely. I am working with the newest version of papervision (downloaded with svn) and i work with flash cs4. The only problem is that the replacematerialbyname isnt working on my cube. Is it because i am working with cs4 or do you have any clue wat the problem is ? You can mail me on jjoosten@live.nl.
Greets Juul Joosten
Jow Seb Lee…
Never mind about my previous question…. I have replaced my papervision classes with the trunk classes and know its good to use
tanks for these brilliant classes
(reflection and material change)
I’got same problem with Samiaji Adiasasmito & Juul
is there any idea?
just because of cs4?
[...] Changing a cube’s material dynamically in Papervision3D [...]
[...] Changing a cube’s material dynamically in Papervision3D [...]
I want to ask you some question.
Can I paint some points in this cute in this demo?
paint some lines in this cute?
Awesome! i was trying overwrite whatever the original material like so: cube.materials.addMaterial(material, “front”);
this concise post solved the problem in 2 seconds flat.