Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unlit alpha shader with an extra highlighting color

Discussion in 'Shaders' started by StridingDragon, May 24, 2016.

  1. StridingDragon

    StridingDragon

    Joined:
    Jan 16, 2013
    Posts:
    78
    I have a standard unlit alpha shader that I am using that looks like this.

    Code (CSharp):
    1. Shader "Unlit/Game Piece"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Color Tint", Color) = (1,1,1,1)
    6.         _MainTex ("Base (RGB) Alpha (A)", 2D) = "white"
    7.     }
    8.     Category
    9.     {
    10.         Lighting Off
    11.         ZWrite Off
    12.         Cull back
    13.         Blend SrcAlpha OneMinusSrcAlpha
    14.         Tags {"Queue"="Transparent"}
    15.         SubShader
    16.         {
    17.             Pass
    18.             {
    19.                 SetTexture [_MainTex]
    20.                 {
    21.                     ConstantColor [_Color]
    22.                     Combine Texture * constant
    23.                 }
    24.             }
    25.         }
    26.     }
    27. }
    When a game piece that is drawn with this shader is selected I want it to be highlighted. For that, I would like essentially a layer of white (or any color) applied on top of the image (only where the image is non-transparent) with an additive/screen blending mode to make it brighter. Ideally, I would like it to pulse, so it should be possible to adjust the amount of visibility of the highlight color through a script.

    So, I thought about adding a _HiColor definition to the properties section for the highlight color, but I have no idea how I would apply it to the image because I am not sure how a second blend mode would be implemented in a shader like this.

    Can someone please tell me what I'd have to do to achieve this? I can't seem to find any info on this.
     
  2. sunduk

    sunduk

    Joined:
    May 3, 2011
    Posts:
    15
    It is easy.
    First. Add "_HiColor" property to your shader.
    Second. Add another "SetTexture" command.


    Code (CSharp):
    1. Shader "Unlit/Game Piece"
    2. {
    3.     Properties
    4.     {
    5.         _Color("Color Tint", Color) = (1,1,1,1)
    6.         _HiColor("Hi Color Tint", Color) = (1,1,1,1)
    7.         _MainTex("Base (RGB) Alpha (A)", 2D) = "white"
    8.     }
    9.     Category
    10.     {
    11.         Lighting Off
    12.         ZWrite Off
    13.         Cull back
    14.         Blend SrcAlpha OneMinusSrcAlpha
    15.         Tags{ "Queue" = "Transparent" }
    16.         SubShader
    17.         {
    18.             Pass
    19.             {
    20.                 SetTexture[_MainTex]
    21.                 {
    22.                     ConstantColor[_Color]
    23.                     Combine Texture * constant
    24.                 }
    25.  
    26.                 SetTexture[_MainTex]
    27.                 {
    28.                     ConstantColor[_HiColor]
    29.                     Combine previous + constant
    30.                 }
    31.             }
    32.         }
    33.     }
    34. }
     
  3. StridingDragon

    StridingDragon

    Joined:
    Jan 16, 2013
    Posts:
    78
    Thanks for the info, but you're not changing the blend mode between the SetTextures, but I'll have to give it a try.

    I actually solved the problem myself after a bit of mulling and ended up with this shader. It has two passes, which I'm not too sure about, performance-wise, but it works. Here it is.

    Code (CSharp):
    1. Shader "Unlit/Game Piece"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Color Tint", Color) = (1,1,1,1)
    6.         _SpecColor ("Highlight Color", Color) = (1,1,1,1)
    7.         _MainTex ("Base (RGB) Alpha (A)", 2D) = "white"
    8.     }
    9.     Category
    10.     {
    11.         Lighting Off
    12.         ZWrite Off
    13.         Cull back
    14.         Tags {"Queue"="Transparent"}
    15.         SubShader
    16.         {
    17.             Pass
    18.             {
    19.                 Blend SrcAlpha OneMinusSrcAlpha
    20.                 SetTexture [_MainTex]
    21.                 {
    22.                     ConstantColor [_Color]
    23.                     Combine Texture * constant
    24.                 }
    25.             }
    26.  
    27.             Pass
    28.             {
    29.                 Blend SrcAlpha One
    30.                 SetTexture [_MainTex]
    31.                 {
    32.                     ConstantColor [_SpecColor]
    33.                     Combine Texture * constant
    34.                 }
    35.             }
    36.         }
    37.     }
    38. }