Search Unity

Color fill a texture

Discussion in 'Shaders' started by Essential, Aug 29, 2013.

  1. Essential

    Essential

    Joined:
    Sep 8, 2011
    Posts:
    265
    Can someone teach me how I'd modify this mobile transparent unlit shader so that it fills the texture area with a single solid color, while retaining the alpha channel as a mask? I know some basics of ShaderLab but not how to replace the texture entirely with a color (instead of tinting the texture). Seems easy though.

    Code (csharp):
    1. Shader "Mobile/Unlit/Transparent" {
    2.  
    3.     Properties {
    4.         _Color ("Main Color (A=Opacity)", Color) = (1,1,1,1)
    5.         _MainTex ("Base (A=Opacity)", 2D) = ""
    6.     }
    7.  
    8.     Category {
    9.         Tags {"Queue"="Transparent" "IgnoreProjector"="True"}
    10.         ZWrite Off
    11.         Blend SrcAlpha OneMinusSrcAlpha
    12.  
    13.         SubShader {Pass {
    14.             GLSLPROGRAM
    15.             varying mediump vec2 uv;
    16.  
    17.             #ifdef VERTEX
    18.             uniform mediump vec4 _MainTex_ST;
    19.             void main() {
    20.                 gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    21.                 uv = gl_MultiTexCoord0.xy * _MainTex_ST.xy + _MainTex_ST.zw;   
    22.             }
    23.             #endif
    24.  
    25.             #ifdef FRAGMENT
    26.             uniform lowp sampler2D _MainTex;
    27.             uniform lowp vec4 _Color;
    28.             void main() {  
    29.                 gl_FragColor = texture2D(_MainTex, uv) * _Color;   
    30.             }
    31.             #endif      
    32.             ENDGLSL
    33.         }}
    34.  
    35.         SubShader {Pass {
    36.             SetTexture[_MainTex] {Combine texture * constant ConstantColor[_Color]}
    37.         }}
    38.     }
    39. }
     
  2. Essential

    Essential

    Joined:
    Sep 8, 2011
    Posts:
    265
    Ah, I figured this would be easy. Maybe not so much…? :)
     
  3. Essential

    Essential

    Joined:
    Sep 8, 2011
    Posts:
    265
    Tried it. Threw a couple of errors:

    Shader warning in 'Mobile/Unlit/Transparent Color Fill': No subshaders can run on this graphics card
    Shader error in 'Mobile/Unlit/Transparent': GLSL Error in Fragment Shader: ERROR: 0:59: 'c' : syntax error syntax error
    at line 0
     
  4. Essential

    Essential

    Joined:
    Sep 8, 2011
    Posts:
    265
    Cool, it's working nicely! Thanks, meh...

    No, no, I really am thankful… I'm not shrugging as I say that but… meh.