Navigation


RSS : Articles / Comments


Papervision 3D - Swapping materials at runtime

6:24 PM, Posted by Jim Foley, 20 Comments

So I've asked myself this question several times, "How can I swap a material at runtime?". I've looked for answers in forums in the past but nothing ever formulated. Someone else asked the same question today and I thought I'd take a look again. BAM! Found a thread on the Papervision Nabble forums in about 2 seconds. And ya know what, the solution is super simple! Duh...

it is as simple as saying: myObject.material = newMaterial;

Swapping materials at runtime in Papervision 3D:
example: swapMaterials.swf
source: swapMaterials.zip

20 Comments

pawn @ February 21, 2008 11:27 PM

yeah but that doesnt work with a cube

Jim Foley @ February 29, 2008 3:43 PM

A cube is using a MaterialsList. I haven't tried swapping a materials list yet. Sorry man.

Minsuk Kim @ March 12, 2008 7:06 PM

Thank you very much !!

jacob @ March 22, 2008 2:45 PM

this does not seem to be working with the latest GreatWhite or Effects trunks

Jim Foley @ March 24, 2008 9:29 AM

@ Jacob - you are right. This feature no longer works. I'll have to see what the new alternative is.

Jim Foley @ March 24, 2008 9:32 AM

@ Jacob - it appears to allow you to get the material value but it doesn't allow you to set the material anymore. If anyone happens to have an update to this please let me know. Thanks.

Kal-el @ April 27, 2008 7:17 PM

myObject.material.copy(newMaterial);

hope that saves someone some time

Kal-el

Anonymous @ May 7, 2008 2:53 AM

Kal-el thx !!

Anonymous @ May 19, 2008 8:17 AM

I don't have much experience with pv3d, but i happened to notice that if you change the material.lineColor property thin goes to change the color of all the objects made with that material

Programmatur @ August 19, 2008 8:38 AM

to change a face (for exemple the right face) in a cube you have to use this script:

var image:ColorMaterial = new ColorMaterial(0xffcc00);
cubo.materials.getMaterialByName("right").copy(image);

Anonymous @ September 3, 2008 4:53 PM

ok thanks for this but it seems that it does not work with collada...

for exemple, once I have applied a materiallist to a collada object with:
box = new Collada("resources/anything.dae", materiallist);
it is working...but if later I put an interactiveScene3DEvent listener with a function that tells:

mycollada.materials.getMaterialByName("face")
.copy(emptypage);

I get an error #1009 when I'm calling the function...

any ideas?

Jim Foley @ October 1, 2008 3:20 PM

The Collada file requires you to swap a MaterialsList object. I haven't tried that yet myself.

mike @ November 3, 2008 1:42 AM

this works for me on planes...

3DObject.material = newMaterial;

the material.copy() method was giving me a glitch (my plane would go black the first switch, until I ran the switch again, then it would work)

haven't tried lists but it seems like it would work

Ecksley @ November 16, 2008 11:30 PM

"to change a face (for exemple the right face) in a cube you have to use this script:

var image:ColorMaterial = new ColorMaterial(0xffcc00);
cubo.materials.getMaterialByName("right").copy(image);
"

This is more of a work around than a real solution. Rather than simply swapping one material for another, it overwrite the original material.

If you are using the original material elsewhere it will be changes there as well.

Also, if you are changing material types it doesn't work at all. For example, if the original material was a movieMaterial, and the replacement was to be a colorMaterial... won't work.

Marco @ November 18, 2008 10:59 PM
This post has been removed by the author.
Marco @ November 18, 2008 11:01 PM

This is what I use to swap materials at runtime.

var material:ColorMaterial= new ColorMaterial(0xFF0000);
cube.replaceMaterialByName(material, "front")

Anonymous @ December 2, 2008 7:42 AM

@ Mike - Nice One, works for me and saved my bacon at the same time. Thanks. And thanks to Mad Vertices as well :)

mark @ January 29, 2009 10:19 AM

To swap materials on a collada file at runtime use replaceMaterialByName()

jose senra @ June 25, 2009 2:31 AM

"this works for me on planes...

3DObject.material = newMaterial;" said Mike... works for me with spheres too :)

Anonymous @ March 11, 2010 2:10 AM

to replace a face of a cube its important to add a material on each face when you create teh cube.

private var facelist:Array = ["left", "back", "right", "top", "front", "bottom"];

var materials:MaterialsList = new MaterialsList();

for (var i:String in facelist) { materials.addMaterial(new PhongMaterial(light, colorTransform.brightness(style.color, 60), style.color, 0),facelist[i]);
}

then:
var material:ColorMaterial = new ColorMaterial(0xffcc00);
Cube(object).replaceMaterialByName(material, "top");