Search Unity

Question Add transparent and option to change color to a shader

Discussion in 'Shaders' started by Only4gamers, Jul 11, 2022.

  1. Only4gamers

    Only4gamers

    Joined:
    Nov 8, 2019
    Posts:
    327
    Hello everyone,
    I copied this shader from internet and I want to add transparency and color change option to this shader.

    here is the post:
    https://medium.com/@cuilongchang/un...i-object-behind-a-transparent-ui-2700c12372c1

    Code (CSharp):
    1. Shader "Unlit/TransparentHideShader"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex("Texture", 2D) = "white" {}
    6.     }
    7.         SubShader
    8.     {
    9.         Tags
    10.         {
    11.             "Queue" = "Transparent+2"
    12.             "RenderType" = "Transparent"
    13.         }
    14.         LOD 100
    15.  
    16.         Pass
    17.         {
    18.             CGPROGRAM
    19.             #pragma vertex vert
    20.             #pragma fragment frag
    21.             #pragma multi_compile_fog
    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.                 UNITY_FOG_COORDS(1)
    34.                 float4 vertex : SV_POSITION;
    35.             };
    36.  
    37.             sampler2D _MainTex;
    38.             float4 _MainTex_ST;
    39.  
    40.             v2f vert(appdata v)
    41.             {
    42.                 v2f o;
    43.                 o.vertex = UnityObjectToClipPos(v.vertex);
    44.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    45.                 UNITY_TRANSFER_FOG(o,o.vertex);
    46.                 return o;
    47.             }
    48.  
    49.             fixed4 frag(v2f i) : SV_Target
    50.             {
    51.                 fixed4 col = tex2D(_MainTex, i.uv);
    52.                 UNITY_APPLY_FOG(i.fogCoord, col);
    53.                 return col;
    54.             }
    55.             ENDCG
    56.         }
    57.     }
    58. }
    Thank you so much
     
    Last edited: Jul 11, 2022
  2. wwWwwwW1

    wwWwwwW1

    Joined:
    Oct 31, 2021
    Posts:
    769
    Hi, you can try changing the alpha value of "_Color" to adjust transparency.

    You can try different color blend modes for different effect. (The one below is multiply)
    Code (CSharp):
    1. Shader "Unlit/TransparentHideShader"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex("Texture", 2D) = "white" {}
    6.         _Color("Color", Color) = (1.0, 1.0, 1.0, 1.0)
    7.     }
    8.         SubShader
    9.     {
    10.         Tags
    11.         {
    12.             "Queue" = "Transparent+2"
    13.             "RenderType" = "Transparent"
    14.         }
    15.         LOD 100
    16.         Pass
    17.         {
    18.             CGPROGRAM
    19.             #pragma vertex vert
    20.             #pragma fragment frag
    21.             #pragma multi_compile_fog
    22.             #include "UnityCG.cginc"
    23.             struct appdata
    24.             {
    25.                 float4 vertex : POSITION;
    26.                 float2 uv : TEXCOORD0;
    27.             };
    28.             struct v2f
    29.             {
    30.                 float2 uv : TEXCOORD0;
    31.                 UNITY_FOG_COORDS(1)
    32.                 float4 vertex : SV_POSITION;
    33.             };
    34.             sampler2D _MainTex;
    35.             float4 _MainTex_ST;
    36.             float4 _Color;
    37.             v2f vert(appdata v)
    38.             {
    39.                 v2f o;
    40.                 o.vertex = UnityObjectToClipPos(v.vertex);
    41.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    42.                 UNITY_TRANSFER_FOG(o,o.vertex);
    43.                 return o;
    44.             }
    45.             fixed4 frag(v2f i) : SV_Target
    46.             {
    47.                 fixed4 col = tex2D(_MainTex, i.uv);
    48.                 col *= _Color;
    49.                 UNITY_APPLY_FOG(i.fogCoord, col);
    50.                 return col;
    51.             }
    52.             ENDCG
    53.         }
    54.     }
    55. }
     
    Only4gamers likes this.
  3. Only4gamers

    Only4gamers

    Joined:
    Nov 8, 2019
    Posts:
    327
    Thanks for the help but it's not working. After updating the shader code I tried to change the color and transparency from the inspector but nothing is changing. This is the first time I am even looking into a shader code, So I have absolutely no idea about shader codes for now.
     
  4. Only4gamers

    Only4gamers

    Joined:
    Nov 8, 2019
    Posts:
    327
    Actualy I was trying to change the color from Image color. Now I see there is a color option in shader. So I can change the color from Shader but transparency is still not changing.
     
  5. Only4gamers

    Only4gamers

    Joined:
    Nov 8, 2019
    Posts:
    327
    Ok, fixed the transparency problem as well:
    Just need to add this line:
    Code (CSharp):
    1. Blend SrcAlpha OneMinusSrcAlpha
    Complete code now:
    Code (CSharp):
    1. Shader "Unlit/TransparentHideShader"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex("Texture", 2D) = "white" {}
    6.         _Color("Color", Color) = (1.0, 1.0, 1.0, 1.0)
    7.     }
    8.         SubShader
    9.     {
    10.         Tags
    11.         {
    12.             "Queue" = "Transparent+2"
    13.             "RenderType" = "Transparent"
    14.         }
    15.         LOD 100
    16.         Pass
    17.         {
    18.             Blend SrcAlpha OneMinusSrcAlpha
    19.             CGPROGRAM
    20.             #pragma vertex vert
    21.             #pragma fragment frag
    22.             #pragma multi_compile_fog
    23.             #include "UnityCG.cginc"
    24.             struct appdata
    25.             {
    26.                 float4 vertex : POSITION;
    27.                 float2 uv : TEXCOORD0;
    28.             };
    29.             struct v2f
    30.             {
    31.                 float2 uv : TEXCOORD0;
    32.                 UNITY_FOG_COORDS(1)
    33.                 float4 vertex : SV_POSITION;
    34.             };
    35.             sampler2D _MainTex;
    36.             float4 _MainTex_ST;
    37.             float4 _Color;
    38.             v2f vert(appdata v)
    39.             {
    40.                 v2f o;
    41.                 o.vertex = UnityObjectToClipPos(v.vertex);
    42.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    43.                 UNITY_TRANSFER_FOG(o,o.vertex);
    44.                 return o;
    45.             }
    46.             fixed4 frag(v2f i) : SV_Target
    47.             {
    48.                 fixed4 col = tex2D(_MainTex, i.uv);
    49.                 col *= _Color;
    50.                 UNITY_APPLY_FOG(i.fogCoord, col);
    51.                 return col;
    52.             }
    53.             ENDCG
    54.         }
    55.     }
    56. }
    Thank you
     
    wwWwwwW1 likes this.