Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Dynamic Batching Material Properties

Discussion in 'Editor & General Support' started by Emre, Mar 17, 2013.

  1. Emre

    Emre

    Joined:
    Jul 8, 2012
    Posts:
    21
    Hi

    I have many small objects in my scene and they use the same material, so the dynamic batching was working perfectly.

    After I added a script that changes material color to make them look a little bit different from each other, the dynamic batching stopped working. It is something like that:

    Code (csharp):
    1.  
    2. public class ChangeColor : MonoBehaviour {    
    3.  
    4. public Color[] m_Colors;
    5.    
    6.     void Start () {
    7.         renderer.material.color = m_Colors[Random.Range(0, m_Colors.Length-1)];
    8.     }
    9. }
    10.  
    Is there a way to change material colors individually, without breaking the dynamic batching?

    Thanks
    Emre
     
    Last edited: Mar 17, 2013
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    No. Dynamic batching works only for identical materials.
     
  3. TrentSterling

    TrentSterling

    Joined:
    Jan 4, 2013
    Posts:
    80
    Does unity support setting vertex colors on a mesh and using a material that multiplies texture color with vertex color? (Thus saving batching?)
     
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Yes, that is possible. Though you need to write the shader on your own, as the default shaders from Unity don't take vertex colors into account.
     
  5. Emre

    Emre

    Joined:
    Jul 8, 2012
    Posts:
    21
    As you said, vertex color diffuse shader and adding vertex colors data at runtime solved the problem for most my objects. But there is a drawback. Adding vertex colors dropped the vertex count limit for dynamic batching from 300 vertices (with vertex pos, normals and UVs) to 225 vertices (with vertex pos, normals, UVs and vertex colors).

    My objects are created and destroyed at runtime, but their position remains the same. If getting object positions from shader is possible (and if it is also possible when dynamic batching is working), then maybe it can be used to change colors. What do you think?
     
unityunity