Author Topic: Changing Textures on Collada Model  (Read 311 times)

Thom Falter   «   on: April 09, 2012, 05:47:59 PM »
I want to be able to change the texture on a Collada model with a keystroke.  I'd be happy for now to know how to do it within the .as file. I've tried various modifications to Test19_EmbedingColladaFile with no luck.  Thank you.

Ivan Vodopiviz   «   Reply #1 on: April 10, 2012, 10:06:44 AM »
Hi,

Once it's loaded, it doesn't matter what format the model is, changing its texture is always done in the same way. You can see an example on how to do this here.

Hope it helps!

Jose Luis Ressia   «   Reply #2 on: April 10, 2012, 10:09:49 AM »
Hi F.T.,
The example 34 shows how to change a texture in real time.
Basically, you need to get the material where you want to apply the texture..
shader = scene.getMaterialByName( "mElsa" ) as Shader3D;
And then, you change it..
if ( Input3D.keyHit( Input3D.NUMBER_1 ) ) shader.filters[0].texture = new Texture3D( "../resources/metal.jpg" );Regards

Ryan Schaefer   «   Reply #3 on: May 10, 2012, 10:18:54 AM »
How do you know what the material name is?

Ivan Vodopiviz   «   Reply #4 on: May 10, 2012, 10:50:52 AM »
Hi Ryan,

You can specify the material name in MAX before exporting the model. But even if you don't know the material name, you can iterate through the Mesh3D's surfaces and get their materials. Something like this:

for each ( var surface:Surface3D in mesh.surfaces )
{
    // now you can set or get surface.material
}

Cheers,

Ryan Schaefer   «   Reply #5 on: May 10, 2012, 11:34:55 AM »
Thanks for the quick reply, Ivan.  Sorry, I'm very new to this.  I just downloaded a free collada model to test with and I'm working with the Test16 example of loading in a model.  The model is a Pivot3D object and does not have a surfaces property.  Is there a way to get a mesh from a Pivot3D object?  Or should my model be of type Mesh3D?


Ivan Vodopiviz   «   Reply #6 on: May 10, 2012, 11:53:20 AM »
Yes, you can do something like this:

for ( var i:int = 0; i < pivot.children.length; i++ )
{
       var mesh:Mesh3D = pivot.children as Mesh3D;
}

Ryan Schaefer   «   Reply #7 on: May 10, 2012, 02:40:44 PM »
I'm getting an error trying to cast the pivot object (or colladaloader) to a Mesh3D object.  With traditional casting: Mesh3D(pivot), it generates an error; using "as" the object is simply null.  Any ideas?


Ivan Vodopiviz   «   Reply #8 on: May 10, 2012, 05:33:33 PM »
Can you please post some code showing how are you loading your assets?

Thanks!

Ryan Schaefer   «   Reply #9 on: May 10, 2012, 05:58:25 PM »
Sure!

// member var
private var model:Pivot3D;

// in constructor
model = scene.addChildFromFile("../Male_Avatar/maleAvatar.dae", null, ColladaLoader);

Then if I try something like this:

// m becomes null
var m:Mesh3D = model as Mesh3D; 

// or, in this case, a type conversion error is generated
var m:Mesh3D = Mesh3D(model);

I get the same result if I try to cast model.children
  • to a Mesh3D object as well.


Thanks, Ivan.

Ivan Vodopiviz   «   Reply #10 on: May 11, 2012, 12:02:23 PM »
I see,

The actual mesh is stored as a children of the pivot returned by addChildFromFile. So if you do something like:

for ( var i:int = 0; i < pivot.children.length; i++ )
{
       var mesh:Mesh3D = pivot.children as Mesh3D;
}

This should give you access to every mesh contained by your pivot. Please make sure that Flare3D has finished loading the assets before trying to access them. You could, for example, listen to the Scene3D.COMPLETE_EVENT that Scene3D dispatches once it's finished loading its assets.

Ryan Schaefer   «   Reply #11 on: May 11, 2012, 03:31:50 PM »
Ivan,

That did it; everything is working now.  Thank you for all your help!

Cheers!