Search Unity

  1. Unity 2017.2 beta is now available for download.
    Dismiss Notice
  2. Unity 2017.1 is now released.
    Dismiss Notice
  3. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  4. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  5. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice

Unlit Single Color Shader

Discussion in 'Shaders' started by rextr09, May 3, 2013.

  1. rextr09

    rextr09

    Joined:
    Dec 22, 2011
    Posts:
    416
    Hi, I'm trying to make a very simple shader which has a single color and is not affected by lightning.

    Here is what I have so far. But it does not work. It's always full black whatever color I choose.

    Code (csharp):
    1. Shader "Unlit Single Color" {
    2.     Properties {
    3.         _Color ("Main Color", COLOR) = (1,1,1,1)
    4.     }
    5.     SubShader {
    6.         Pass {
    7.             Material {
    8.                 Diffuse [_Color]
    9.             }
    10.             Lighting Off
    11.         }
    12.     }
    13. }
    So, what's wrong with this shader?

    Thanks..
     
  2. twiesner

    twiesner

    Joined:
    Oct 4, 2011
    Posts:
    309
    Code (csharp):
    1. Shader "Unlit Single Color" {
    2.     Properties {
    3.         _Color ("Main Color", Color) = (1,1,1,1)
    4.         _MainTex ("Base (RGB)", 2D) = "white" {}
    5.     }
    6.     Category {
    7.        Lighting Off
    8.        ZWrite On
    9.        Cull Back
    10.        SubShader {
    11.             Pass {
    12.                SetTexture [_MainTex] {
    13.                     constantColor [_Color]
    14.                     Combine texture * constant, texture * constant
    15.                  }
    16.             }
    17.         }
    18.     }
    19. }
     
  3. rextr09

    rextr09

    Joined:
    Dec 22, 2011
    Posts:
    416
    So, I guess we can't get rid of that MainTex property even though we don't have any texture to choose. Anyway, thanks. It works like a charm.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    31,300
    You don't need a texture, you just need to specify the color.

    Code (csharp):
    1. Shader "Unlit Color Only" {
    2.  
    3. Properties {
    4.     _Color ("Color", Color) = (1,1,1)
    5. }
    6.  
    7. SubShader {
    8.     Color [_Color]
    9.     Pass {}
    10. }
    11.  
    12. }
    --Eric
     
  5. rextr09

    rextr09

    Joined:
    Dec 22, 2011
    Posts:
    416
    Thanks Eric, I think this is much cheaper to render.
     
  6. rextr09

    rextr09

    Joined:
    Dec 22, 2011
    Posts:
    416
    FYI, for some reason the shader provided by @Eric does not work properly on my iPod Touch 4th gen (it was only visible under a point light and invisible otherwise), so I had to use @twiesner's shader instead. Thanks.
     
  7. cician

    cician

    Joined:
    Dec 10, 2012
    Posts:
    230
    What do you mean it was not visible? Did it turn black or the object was not rendered?

    I'm no expert in fixed function or shaderlab and don't know ipod specs, anyway you can try:

    1. this one does a few things explicitly, but I guess does not differ much from Eric's one (both render OK on on my workstation)
    Code (csharp):
    1. Shader "Unlit Color Only" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1)
    4.     }
    5.    
    6.     SubShader {
    7.         Tags { "RenderType"="Opaque" }
    8.         LOD 100
    9.         Pass {
    10.             Lighting Off
    11.             ZWrite On
    12.             Cull Back
    13.             SetTexture[_] {
    14.                 constantColor [_Color]
    15.                 Combine constant
    16.             }
    17.         }
    18.     }
    19. }
    2. this one is made in pure CG and I expect it to work on enything with programmable shaders. Since GLES 2.0 devices don't have fixed function stuff it's probably better anyway.
    Code (csharp):
    1. Shader "Unlit Color Only CG" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1)
    4.     }
    5.     SubShader {
    6.         Tags { "RenderType"="Opaque" }
    7.         LOD 200
    8.        
    9.         Pass {
    10.             Tags { "LightMode" = "Always" }
    11.            
    12.             Fog { Mode Off }
    13.             ZWrite On
    14.             ZTest LEqual
    15.             Cull Back
    16.             Lighting Off
    17.    
    18.             CGPROGRAM
    19.                 #pragma vertex vert
    20.                 #pragma fragment frag
    21.                 #pragma fragmentoption ARB_precision_hint_fastest
    22.                
    23.                 fixed4 _Color;
    24.                
    25.                 struct appdata {
    26.                     float4 vertex : POSITION;
    27.                 };
    28.                
    29.                 struct v2f {
    30.                     float4 vertex : POSITION;
    31.                 };
    32.                
    33.                 v2f vert (appdata v) {
    34.                     v2f o;
    35.                     o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    36.                     return o;
    37.                 }
    38.                
    39.                 fixed4 frag (v2f i) : COLOR {
    40.                     return _Color;
    41.                 }
    42.             ENDCG
    43.    
    44.         }
    45.     }
    46. }
    47.  
    If nr.2 works and nr.1 doesn't then I'd combine them in one so it works on both programmable and fixed function (CG first, fixed/shaderlab one following as subshaders). Though I'd expect unity to compile shaderlab stuff to GLES, so the issue may be different.
     
  8. rextr09

    rextr09

    Joined:
    Dec 22, 2011
    Posts:
    416
    Thanks @cician, your both shaders work without a problem in my iPod.
    I don't know the difference between these, so which one should I prefer? Will they have equal performance?
     
  9. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    31,300
    I don't have my 4th gen here to test, but it works as expected on my 5th gen.

    --Eric
     
  10. graphite116

    graphite116

    Joined:
    Mar 15, 2016
    Posts:
    3
    How would you modify this shader to receive shadows?