Search Unity

transparency + gradient Shader

Discussion in 'Shaders' started by joe_jai001, Jun 19, 2022.

  1. joe_jai001

    joe_jai001

    Joined:
    Feb 22, 2020
    Posts:
    1
    may i ask can this effect be achieved?

    I tried many built-in materials but it didn't work
    My understanding is
    transparency + gradient shader
    I've been stuck with a simple question all night
     
  2. tmcthee

    tmcthee

    Joined:
    Mar 8, 2013
    Posts:
    119
    Here's a simple shader that will do that for you. Just add it to a new material and assign a gradient texture to it (with an alpha channel.)
    Between lines 11 and 16 are the different forms of transparency you can use. Currently it's using traditional transparency which is what your example image is using. Although you might want to comment that line out and try some of the other modes, which might look better.
    (Only the first two; traditional transparency and premultiplied transparency require an alpha channel in the texture. The other modes use colour to determine the blend of the pixel.)

    Code (CSharp):
    1. Shader "TMCShaderQuarantine/SimpleUnlitTransparent"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.     }
    7.     SubShader
    8.     {
    9.         Tags { "Queue" = "Transparent" }
    10.  
    11.         Blend SrcAlpha OneMinusSrcAlpha // Traditional transparency
    12.         //Blend One OneMinusSrcAlpha // Premultiplied transparency
    13.         //Blend One One // Additive
    14.         //Blend OneMinusDstColor One // Soft additive
    15.         //Blend DstColor Zero // Multiplicative
    16.         //Blend DstColor SrcColor // 2x multiplicative
    17.  
    18.         Pass
    19.         {
    20.             CGPROGRAM
    21.             #pragma vertex vert
    22.             #pragma fragment frag
    23.  
    24.             #include "UnityCG.cginc"
    25.  
    26.             struct appdata
    27.             {
    28.                 float4 vertex : POSITION;
    29.                 float2 uv : TEXCOORD0;
    30.             };
    31.  
    32.             struct v2f
    33.             {
    34.                 float2 uv : TEXCOORD0;
    35.                 float4 vertex : SV_POSITION;
    36.             };
    37.  
    38.             sampler2D _MainTex;
    39.  
    40.             v2f vert (appdata v)
    41.             {
    42.                 v2f o;
    43.                 o.vertex = UnityObjectToClipPos(v.vertex);
    44.                 o.uv = v.uv;
    45.                 return o;
    46.             }
    47.  
    48.             fixed4 frag (v2f i) : SV_Target
    49.             {
    50.                 fixed4 col = tex2D(_MainTex, i.uv);
    51.                 return col;
    52.             }
    53.             ENDCG
    54.         }
    55.     }
    56. }
     
  3. DualOnGames

    DualOnGames

    Joined:
    Jan 20, 2022
    Posts:
    33
    Hia, I have a question. Using this code returns this error: Parse error: syntax error, unexpected $end.
    Any Ideas?
     
  4. tmcthee

    tmcthee

    Joined:
    Mar 8, 2013
    Posts:
    119
    You have probably missed one of the curly brackets at the end. When you copy the code make sure copy it all and paste it in its entirety into the shader.