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

Hundreds of same objects with slight color change without losing batching?

Discussion in 'Scripting' started by pretender, Nov 23, 2014.

  1. pretender

    pretender

    Joined:
    Mar 6, 2010
    Posts:
    862
    Is there any simple solution for this?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Use vertex colors.

    --Eric
     
  3. pretender

    pretender

    Joined:
    Mar 6, 2010
    Posts:
    862
    can you provide more information please?
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Unity sprites use vertex colors, for example; the shader uses vertex colors to tint objects instead of having the color tied to the material.

    --Eric
     
  5. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    10,960
    Look it up, it has been discussed before. Eric's advice is sound, vertex colors is the way to go.
     
  6. pretender

    pretender

    Joined:
    Mar 6, 2010
    Posts:
    862
    i have 3d objects not sprites...sorry i am not into shaders too much, can default diffuse shader could be used in that way or something else need to be used?
     
  7. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    10,960
    You can use meshes, but you'll need to write your own shader that uses the vertex colors. It's not that hard, there have been a couple of shaders that do that floating around.
     
  8. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    10,960
    Here's a shader that shows vertex colors and nothing else :

    Code (csharp):
    1. Shader "Vertex Color" {
    2. Properties {
    3.  
    4. }
    5. SubShader {
    6.     Tags { "RenderType"="Opaque" "Queue"="Geometry" }
    7.     Pass {
    8.  
    9.             CGPROGRAM
    10.              
    11.                    #include "UnityCG.cginc"
    12.                    #pragma vertex vert
    13.                    #pragma fragment frag
    14.                  
    15.                    struct v2f
    16.                    {
    17.                           fixed4 color : COLOR;
    18.                           half4 pos : SV_POSITION;
    19.                          
    20.                     };
    21.        
    22.                     v2f vert(appdata_full v)
    23.                     {
    24.                             v2f o;
    25.                             o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    26.                             o.color = v.color;
    27.                             return o;
    28.                     }
    29.  
    30.                     fixed4 frag(v2f In) : COLOR
    31.                     {
    32.                             fixed4 c = fixed4(1,1,1,1);
    33.                             c.rgb *= In.color.rgb;
    34.                             return c;
    35.                     }
    36.                  
    37.                 ENDCG
    38.             }
    39.         }
    40.     }
    41.  
    I don't know if it will be helpful.
     
  9. ErrorX

    ErrorX

    Joined:
    May 21, 2014
    Posts:
    21
    If you are using 3DS Max for the models you can do this:
    3d max.JPG
    Other packages like Maya or Blender have the same features. You only need to select a shader in Unity that support vertex color.
     
    Last edited: Nov 27, 2014
  10. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Sprites are 3D objects; the exact same principles apply.

    --Eric