Search code examples
unity-game-engineshaderalpha

Add transparency handling (alpha set response) to unity shader


I have got a shader for a line renderer that does not respond to alpha change. Taking a look to the code that makes sense. I cannot use a default material with alpha handling because the line rendeder does not paint it properly (it behaves as a billboard and its not rendered correctly). So I would like to incorporate alpha handling to my shader.

The shader finally returns tho color in the line:

finalColor = _Color;

But I cannot figure out how to pass in the alpha, so that I can handle the transparency in the editor or with code when this shader is used in a unity material.

Find below the shader code:

Shader "CustomShaders/UnlitColorAbove"
{
    Properties
    {
        _Color("Color", Color) = (0,0,0,0)
    }
    
    SubShader
    {
        
        
        Tags { "RenderType"="Opaque" }
        LOD 100

        CGINCLUDE
        #pragma target 3.0
        ENDCG
        Blend Off
        Cull Back
        ColorMask RGBA
        ZWrite On
        ZTest Always
        
        
        
        Pass
        {
            Name "Unlit"
            
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_instancing
            #include "UnityCG.cginc"            

            struct appdata
            {
                float4 vertex : POSITION;
                float4 color : COLOR;
                UNITY_VERTEX_INPUT_INSTANCE_ID
                
            };
            
            struct v2f
            {
                float4 vertex : SV_POSITION;
                UNITY_VERTEX_INPUT_INSTANCE_ID
                UNITY_VERTEX_OUTPUT_STEREO
                
            };

            uniform float4 _Color;
            
            v2f vert ( appdata v )
            {
                v2f o;
                UNITY_SETUP_INSTANCE_ID(v);
                UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
                UNITY_TRANSFER_INSTANCE_ID(v, o);

                
                float4 vertexValue =  float3(0,0,0,0) ;
                #if ASE_ABSOLUTE_VERTEX_POS
                v.vertex.xyzw = vertexValue;
                #else
                v.vertex.xyzw += vertexValue;
                #endif
                o.vertex = UnityObjectToClipPos(v.vertex);
                return o;
            }
            
            fixed4 frag (v2f i ) : SV_Target
            {
                UNITY_SETUP_INSTANCE_ID(i);
                fixed4 finalColor;
                
                
                finalColor = _Color;
                return finalColor;
            }
            ENDCG
        }
    }
    CustomEditor "ASEMaterialInspector"
}

And some of the things I tried to incorporate alpha handling to the shader:

https://answers.unity.com/questions/676989/how-can-i-add-transparency-to-this.html

https://forum.unity.com/threads/how-add-support-to-alpha-on-this-shader.494460/

Add alpha to shader in Unity3D

But did not succed because the shader does not work or does not compile, as I am not familiar with shader programming.


Solution

  • The third link you provided* shows what your shader is actually missing to support alpha transparency.

    You need to add alpha blending into your shader, so instead of:

    Blend Off
    

    It should be, for example**:

    Blend SrcAlpha OneMinusSrcAlpha
    

    And more or less thats it***


    I have also noticed that you are using Amplify Shader Editor (because there is the comment at the bottom of your code CustomEditor "ASEMaterialInspector")

    The settings you are looking for are here:

    enter image description here


    .* The two previous thread you linked talk about adding alpha transparency to surface shaders while the shader you provided is a vertex-fragment shader.

    .** For more info about types of blending see unity docs). Also I could recommend this cool talk about VFX in Diablo which shows how the selection of proper blending can be important for you shader.

    .*** You have a typo in line float4 vertexValue = float3(0,0,0,0) ; it should be float4(0,0,0,0), also you should consider changing Tags { "RenderType"="Opaque" } to Tags { "Queue"="Transparent" } and you can read more about SubShader tags here