Search Unity

Background color + alpha texture on top

Discussion in 'Shaders' started by shoo, Nov 3, 2016.

  1. shoo

    shoo

    Joined:
    Nov 19, 2012
    Posts:
    67
    Hello! I am not experienced with shaders. I need to make unlit shader which will have non-transparent solid color on background, and on top of it, it will have texture, which transparency I can controll with _Alpha value.

    So it is like:
    fixed4 tex = tex2D(_MainTex, i.texcoord);
    fixed4 alphaTex = fixed4(tex.r, tex.r, tex.r, _Alpha);
    fixed4 col = _Color;


    How could I make shader draw alphaTex on top of my color?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,339
    You want to do alpha blending in the shader code. If you actually try to search for that you'll get lots of links talking about blend modes and alpha keywords, none of which is applicable here as that would be for having a shader that is transparent. However the math behind alpha blending has another name, linear interpolation, and hlsl has a built in function for this, lerp().

    fixed4 tex = tex2D(_MainTex, i.texcoord);
    fixed4 col = lerp(_Color, tex, _Alpha);
     
    shoo likes this.