Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Unlit vertex color (only) shader for Mobile

Discussion in 'Shaders' started by The-Oddler, Nov 2, 2014.

  1. The-Oddler

    The-Oddler

    Joined:
    Nov 26, 2010
    Posts:
    133
    Hi,

    I'm making a game where all object just have one color. Currently I'm using a very simple flat color shader, and have a material for each color. Now I want to let some objects fade away, but when I alter the alpha of their color either all objects using that material fade away, or I have to create a new material for each object but then I get a bunch of draw-calls. Now I was thinking of switching to a shader that uses the vertex colors, so I can just change those without getting extra draw-calls. I would even get less draw calls, since all objects would use the same material (I think).

    So I'm looking for a shader that just shows the vertex colors of a mesh. No texture, color overlay or lighting. I was thinking of making two materials, one for the objects that fade and one for those that stay opaque, so I need a shader that supports alpha and one that doesn't.

    I'm very new to shaders in Unity and I Googled around but couldn't find anything. The shaders were either too complex or I didn't understand what they were doing, most of the time both.

    Thanks a lot!
     
    aparajithsairam likes this.
  2. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    349
    -
     

    Attached Files:

    aparajithsairam and _legolas_ like this.
  3. The-Oddler

    The-Oddler

    Joined:
    Nov 26, 2010
    Posts:
    133
    Those work :D Thanks!

    However, for some reason my meshes aren't batched now either, even though they use the same material. They are however generated from code, could this be the reason?
     
  4. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    349
    <<even though they use the same material>>
    Are you sure? You should change only "shared material", because when you write on even read some properties from "material" it's lead to creating a new copy of material object.
    <<They are however generated from code, could this be the reason?>>
    Yes, if object have more vertices(vertex attributes) than may by in batched object. See manual for exact limits.
     
  5. The-Oddler

    The-Oddler

    Joined:
    Nov 26, 2010
    Posts:
    133
    <<shared material>>
    I was indeed using .material instead of .sharedMaterial. However, I still don't get batching after changing it :(
    <<vertex limit>>
    They are very low-poly. At most 10 vertices, without texture coordinates nor normals.

    The whole code of generating the mesh (with a bit extra comments), perhaps I'm still missing something:

    Code (CSharp):
    1.     public void Initialize (Vector2[] points, PlanetoidsManager manager) {
    2.         int sideCount = points.Length; //points.Length is about 5 to 10
    3.         Vector3[] pointsV3 = new Vector3[sideCount];
    4.         Color32[] colors = new Color32[sideCount];
    5.         for (int i = 0; i < sideCount; ++i) {
    6.             pointsV3[i] = points[i];
    7.             colors[i] = _color; //_color is a Color32 variable of my monobehaviour
    8.         }
    9.         // set collider
    10.         _collider.points = points; //a PolygonCollider2D
    11.      
    12.         // create triangles
    13.         int[] triangles = new int[(sideCount-2)*3];
    14.         for (int i = 0; i < sideCount-2; ++i) {
    15.             triangles[i*3]   = 0;
    16.             triangles[i*3+1] = i+2;
    17.             triangles[i*3+2] = i+1;
    18.         }
    19.         // set mesh
    20.         _meshFilter.mesh.Clear();
    21.         _meshFilter.mesh.vertices = pointsV3;
    22.         _meshFilter.mesh.triangles = triangles;
    23.         _meshFilter.mesh.colors32 = colors;
    24.      
    25.         // set mass
    26.         float area = GetArea(points);
    27.         _rigidbody.mass = _density * area;
    28.        
    29.         _renderer.sharedMaterial = manager.MatOpaque; //a material with the opaque shader you gave me
    30.         }
    31.     }
    Even if I comment out the line setting the sharedMaterial, and just leave it on the one set in the prefab, no batching occurs.
     
  6. The-Oddler

    The-Oddler

    Joined:
    Nov 26, 2010
    Posts:
    133
    I found why my objects weren't batching. Somehow I unchecked "Dynamic Batching" in the player settings.

    Thanks for all the help mouurusai!