Changing a cube’s material dynamically in Papervision3D
Actionscript, Flash, Flash 3D, General, PaperVision3D Add commentsIt’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 :
[kml_flashembed movie="http://sebleedelisle.com/wp-content/uploads/2008/08/cubetest.swf" width="445" height="340" FVERSION="9" QUALITY="high" /]
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++; } } }



August 22nd, 2008 at 12:53 pm
Thanks Seb ;o) Thats what i needed
August 22nd, 2008 at 9:59 pm
Glad I could help Andi!
August 25th, 2008 at 10:01 pm
[...] Changing a cube’s material dynamically in Papervision3D [...]
August 27th, 2008 at 8:16 am
How can i move the functin replaceMaterialByName to DisplayObject3D?
August 27th, 2008 at 8:32 am
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
September 19th, 2008 at 4:14 pm
How use this method??? I think i have lastest version, but dont work??? When iam wronging
September 20th, 2008 at 9:10 am
Hi Slav,
yeah it should all work fine with the latest version. See my previous message for download instructions.
cheers!
Seb
October 3rd, 2008 at 7:11 pm
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
October 3rd, 2008 at 7:47 pm
Hi Slav,
it’s definitely there… make sure you get the as3 branch in trunk.
cheers!
Seb
October 6th, 2008 at 8:20 pm
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
October 15th, 2008 at 10:26 am
I can not open “http://papervision3d.googlecode.com/svn/trunk/branches/GreatWhite/src/”, please make sure is it here?
October 15th, 2008 at 11:59 am
Hi Jas,
the GreatWhite branch no longer exists per se. You just need the main as3 branch now.
cheers!
Seb
October 31st, 2008 at 7:19 am
[...] >> 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 [...]
January 5th, 2009 at 3:58 pm
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
February 26th, 2009 at 2:07 pm
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++;
}
}
}
March 20th, 2009 at 9:45 pm
[...] Seb Lee-Delise – Changing the materials on a cube dynamically. [...]
April 3rd, 2009 at 2:37 pm
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
April 9th, 2009 at 11:21 am
@Samiaji Adisasmito yeah that shouldn’t happen, so not sure what’s happening there… wanna post the source?
May 1st, 2009 at 11:12 am
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
May 3rd, 2009 at 7:25 pm
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)
May 7th, 2009 at 8:56 pm
I’got same problem with Samiaji Adiasasmito & Juul
is there any idea?
just because of cs4?
July 10th, 2009 at 4:02 pm
[...] Changing a cube’s material dynamically in Papervision3D [...]