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
skip to main |
skip to sidebar
Jim Foley's Brain. Flex, Flash, Papervision 3D, Swift 3D and other cool stuff.
Back on top ^
created by Nuvio | Webdesign
MAD VERTICES © 2008 Ken ahlin | Converted to XML Blogger Template by ThemeLib
yeah but that doesnt work with a cube
A cube is using a MaterialsList. I haven't tried swapping a materials list yet. Sorry man.
Thank you very much !!
this does not seem to be working with the latest GreatWhite or Effects trunks
@ Jacob - you are right. This feature no longer works. I'll have to see what the new alternative is.
@ 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.
myObject.material.copy(newMaterial);
hope that saves someone some time
Kal-el
Kal-el thx !!
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
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);
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?
The Collada file requires you to swap a MaterialsList object. I haven't tried that yet myself.
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
"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.
This is what I use to swap materials at runtime.
var material:ColorMaterial= new ColorMaterial(0xFF0000);
cube.replaceMaterialByName(material, "front")
@ Mike - Nice One, works for me and saved my bacon at the same time. Thanks. And thanks to Mad Vertices as well :)
To swap materials on a collada file at runtime use replaceMaterialByName()
"this works for me on planes...
3DObject.material = newMaterial;" said Mike... works for me with spheres too :)
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");