Game Development Community

Changing a model material

by Tony Pitman · in Torque X 3D · 03/17/2010 (8:56 pm) · 9 replies

I am using Torque X 3D for my game. I have a model loaded into the T3DTSRenderComponent object. I would like to change the texture programmatically after the model has already been loaded. Does anyone know how I can do this?

#1
03/18/2010 (3:09 am)
At the moment you can't without altering the source. This is what I was talking about in another thread when John said he was going through making them all public so they can be changed via code.

It's probably as simple as changing the private to public in the RenderMaterial but never tried as I was expecting the january update a week later..

Ha!
#2
03/18/2010 (6:41 am)
I have the source code and don't mind changing it. Maybe you could give some more details or a pointer to the thread.

One thing to keep in mind about what I want to do. I have several of the same model. I don't want the same material to be changed for all of them. I just want to vary which material gets used for each one individually and change them over time.

This is not the same as using the IFI stuff that automatically changes the material based on the frame #. I just want to have the same model, that I reuse, have its material changed from one use to the next time I use it.
#3
03/18/2010 (6:07 pm)
T3DTSRenderComponent has a ShapeInstance which has a _materialList. You can swap in a new material, but you can't set the RenderMaterial of the Material structure because it's internal, so if it's not loaded by the xml you have no way of creating a new material.

Pretty trivial code change, but whether it affects the way other parts of the engine work, I have no idea at this time.
#4
03/18/2010 (7:58 pm)
I assume when you say to swap in a new material that you mean put the material in the xml file so the engine creates it. Then get the material using the Find???? function to load it into the _materialList[0] of the shape instance, right?

I have not loaded any materials in code yet. What is the proper way to do that?
#5
03/18/2010 (8:11 pm)
I don't know what the proper way is. I do not use xml at all for any of my game, it's all code, so no, I want to create say a SimpleMaterial, assign it to a Material then assign that to _materialList[0]. I just tried this in my game and it works fine though.
#6
03/18/2010 (8:20 pm)
Awesome. If I can do it just in code that helps.
#7
03/24/2010 (8:57 pm)
For some reason I can't seem to figure out how to assign the SimpleMaterial to the Material. Can you tell me how?
#8
03/24/2010 (9:45 pm)
Here's my code that worked. I had to make _renderMaterial public in the Material class.
var simple = new SimpleMaterial();
simple.TextureFilename = "data/models/alien_fighter/alien_fighter";
var mat = new Material();
mat._renderMaterial = simple;
render.ShapeInstance._materialList[0] = mat;
entity.Components.AddComponent(render);
#9
03/25/2010 (6:46 am)
Ah, that makes sense. thanks!