Away3d duplicate material

I’m importing an awd model in Away3d and trying to duplicate one of the materials that is applied to multiple meshes to create a unique ColourMaterial with a different alpha value.

The intent being on rollover I can change the alpha value of the target mesh by swapping the materials.

Any ideas?


Hello TSmithF,

Not sure what you need help with. What did you try? What worked, and what didn’t work?!

If you want to duplicate / clone a material you can use this:
var newMaterial=material.clone();
If you want to alter an alpha value you can try this:

Or do you have a problem with the submeshes? Maybe an imported Mesh has subMeshes so you might need to loop through them. (not tested but something like:)
for(i in yourMesh.subMeshes.length){
yourMesh.subMeshes[i].material = newMaterial;

Thanks for the quick response :slight_smile:

I have tried the clone method…

var materialCopy:ColorMaterial = mesh.material.clone();
var materialCopy:ColorMaterial = cast(mesh.material, ColorMaterial).clone();

but I get away3d.materials.ColorMaterial has no field clone

I was also looking at Reflect.copy, but that’s will only copy the attributes of the ColorMaterial.

The problem with changing the alpha value of the imported material is it changes it for all meshes that use this material.

ah ok. It is a ColorMaterial.Apparently there isn’t a clone method for this.

Well you might want to use this:

var newMaterial:ColorMaterial = new ColorMaterial();
newMaterial.color=yourImportMesh.material.color; (or using the subMesh technique from earlier)
newMaterial.alpha = 0.5;

Then apply this material to the mesh on the mouse over event (or whatever you need):
theMesh.material=newMaterial; // or the subMesh technique

Ah, OK, so I have to copy all attributes relevant to the imported material to a new material.

That was what I was hoping to avoid to ensure nothing was missed if the base material was amended in the future with new settings/methods etc.

Yeah indeed. I thought I would chip in how to get the color and alpha, but there could be other things set on the material. Lightpicker,shadowmethods etc…

You already mentioned Reflection and that might be a solution, but it would really be better if there was a built-in system for this.

Is my assumption correct that an imported mesh with texture materials use the clone() out of the box like you want?!

Yes mesh clone works fine, and is indeed what I’m doing on import, however the cloned mesh is still using the original material, I guess this is preferred anyway, there’s just no easy way to duplicate a material that I can see.