- Home /
One material for many objects with different textures
Hello.
I am trying something on the project i work on and it works, but i am not sure if it's going to help or not for the performance.
It's a puzzle game and let's say 90% of the objects on the screen can use the same material. So what i am doing is this:
I apply 1 material to all of these objects
Add a script to all of these objects which looks like this:
// Public Variables public var textureToLoad : Texture2D; // The texture that will be displayed
// Private Variables private var trans : Transform; // this.transform. function Start () { trans = this.transform; trans.renderer.material.mainTexture = textureToLoad; }
Add the different textures for each object to the custom created Texture2D field in the inspector
So when the game starts, all of the objects are getting their own textures dynamically at start...
This is specifically for mobile game and i am wondering if this is going to make the game run faster, because 90% of the game objects can use only one shader...
Please mark your question as answered unless you have more questions? :)
Answer by Alec Thilenius · Dec 20, 2012 at 05:17 AM
This will ruin your performance, not improve it. A material is more than just an organizational tool in game engines. It is an entire state that is saved in GPU memory. The HLSL shaders used to render the textures, buffers, the texture itself... Swapping the texture like that will result in graphics memory IO if unity does what I think they do with their materials. This will murder your performance.
If you don’t have a frame rate problem, then do not start playing with things like this. The boys who wrote the rendering code know what they are doing. Trust that they have thought of stuff like this.
Thanks for the info.. I'll use separate materials for the different objects then... I have a lot of static objects (Set as Static) and i guess, if i turn on "Static Batching", that will help in some way...
As far as performance goes in computer program$$anonymous$$g, a problem does not exist until you diagnose one (With a profiler). Unless you need to pull more frame rates out of your game to meet the required $$anonymous$$imum target platform, then don't worry about any of it.
Answer by Dom3D · Dec 20, 2012 at 05:00 AM
I think under the hood you still have a different material per object. It would be only one material when accessing Renderer.sharedMaterial. See docs for material duplication info: http://docs.unity3d.com/Documentation/ScriptReference/Renderer-material.html?from=Material
In addition to that using a different texture is a so called renderstate change and has a performance hit. Therefore using a texture atlas instead would be a solution that allows you to only use one material and one texture and thus save performance. That is, if the textures are not tiled on both directions.
I'm trying to figure out how the texture atlas works to use it.. Thanks :)