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

Question Is it possible to add transparency to this shader?

Discussion in 'Shaders' started by Balphagore, Nov 27, 2022.

  1. Balphagore

    Balphagore

    Joined:
    Jul 18, 2019
    Posts:
    81
    I'm using a very simple LineRenderer material shader:

    Code (CSharp):
    1. Shader "Custom/Two color shader"
    2. {
    3.     Properties
    4.     {
    5.         _MainColor ("Main Color", Color) = (0,0,0,1)
    6.         _SecondaryColor ("Secondary Color", Color) = (1,1,1,1)
    7.         _Mask ("Mask (A)", 2D) = "white" {}
    8.     }
    9.     SubShader
    10.     {
    11.         Lighting Off
    12.         ZTest Off
    13.      
    14.         Pass
    15.         {
    16.             SetTexture [_Mask]
    17.             {
    18.                 ConstantColor [_MainColor]
    19.                 combine constant
    20.             }
    21.  
    22.             SetTexture [_Mask]
    23.             {
    24.                 ConstantColor [_SecondaryColor]
    25.                 combine constant lerp (texture alpha) previous
    26.             }
    27.         }
    28.     }
    29. }
    In general, it works as I wanted, but now I need the LineRenderer lines to become transparent and this does not work with this shader.

    I am changing the material color of a LineRenderer with:

    .material.SetColor("_MainColor", color);

    In the inspector in the color window, it shows that the alpha of the color is changing, but this does not affect the line itself.

    Is it possible to somehow add transparency to this shader?
     
  2. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,495
    Under where it says "ZTest Off" add the line
    Blend SrcAlpha OneMinusSrcAlpha

    This will tell it to blend between the existing scene color and the color this shader is outputting, based on the Alpha value this shader is outputting. The source color (what this is outputting) will by multiplied by "SrcAlpha" the alpha of this source. And the Destination color (which would be everything that has rendered before this shader), will be multiplied by 1-SrcAlpha. And then those resulting Source and Destination values get added together.

    Keep in mind the shader you have is the old fixed-function style and simply gets compiled to a proper vert/frag shader these days. Here is how your shader would be as a regular shader:
    Code (CSharp):
    1. Shader "Unlit/Two color shader"
    2. {
    3.     Properties
    4.     {
    5.         _MainColor("Main Color", Color) = (0,0,0,1)
    6.         _SecondaryColor("Secondary Color", Color) = (1,1,1,1)
    7.         _Mask("Mask (A)", 2D) = "white" {}
    8.     }
    9.     SubShader
    10.     {
    11.         Tags { "RenderType"="Transparent" "Queue"="Transparent"}
    12.         Lighting Off
    13.         ZTest Off
    14.         Blend SrcAlpha OneMinusSrcAlpha
    15.  
    16.         Pass
    17.         {
    18.             CGPROGRAM
    19.             #pragma vertex vert
    20.             #pragma fragment frag
    21.  
    22.             #include "UnityCG.cginc"
    23.  
    24.             struct appdata
    25.             {
    26.                 float4 vertex : POSITION;
    27.                 float2 uv : TEXCOORD0;
    28.             };
    29.  
    30.             struct v2f
    31.             {
    32.                 float2 uv : TEXCOORD0;
    33.                 float4 vertex : SV_POSITION;
    34.             };
    35.  
    36.             float4 _MainColor;
    37.             float4 _SecondaryColor;
    38.             sampler2D _Mask;
    39.             float4 _Mask_ST;
    40.  
    41.             v2f vert (appdata v)
    42.             {
    43.                 v2f o;
    44.                 o.vertex = UnityObjectToClipPos(v.vertex);
    45.                 o.uv = TRANSFORM_TEX(v.uv, _Mask); //Take into account the "Tiling and Offset" values set on the Material
    46.                 return o;
    47.             }
    48.  
    49.             fixed4 frag (v2f i) : SV_Target
    50.             {
    51.                 float mask = tex2D(_Mask, i.uv).a; //Make sure to set texture importer format to "Alpha 8" for memory savings.
    52.                 return lerp(_MainColor, _SecondaryColor, mask);
    53.             }
    54.             ENDCG
    55.         }
    56.     }
    57. }
    This will make it easier for you to modify and expand the features the shader has if you ever need to :)
     
    Last edited: Nov 28, 2022
  3. Balphagore

    Balphagore

    Joined:
    Jul 18, 2019
    Posts:
    81
    Yes, I was looking for this shader a long time ago on the principle of maximum simplicity. And now it needed to be improved, but I did not work much with shaders.

    Your improved version added transparency, but it works in a strange way. I recorded a video.

     
    Last edited: Nov 28, 2022
  4. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,495
    Could you describe what is strange? It's behaving as I'd expect. What are you expecting to happen when those values are set?
     
  5. Balphagore

    Balphagore

    Joined:
    Jul 18, 2019
    Posts:
    81
    In PlayMode, transparency is only applied in the SceneView. In GameView, it remains unchanged and is applied only after exiting PlayMode. At the same time, outside of PlayMode, transparency changes simply when the cursor moves between Editor windows, although the value of alpha does not change at this moment.
     
  6. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,495
    Looks like a render queue issue, I had a typo in the shader. In the Tags{} section, it should be "Queue" not "RenderQueue". I updated the previous post.
     
    Balphagore likes this.
  7. Balphagore

    Balphagore

    Joined:
    Jul 18, 2019
    Posts:
    81
    Yes, now it works perfectly, thank you very much)
     
    Invertex likes this.