1
\$\begingroup\$

I searched for a shader, where I can set a solid color, which I can fade in or out like I want. There is not such a thing build in as far as I could see. Now I got this code, which works fine in case of the alpha value, but it turns everything behind upside down. Since I'm absolutely not into shaders, I need help to fix that to a simple alpha shader.

Shader "Unity Answers/Solid Transparency Color" 
{ 
    Properties 
    { 
        _Color ("Color", Color) = (0.5, 0.5, 0.5, 0.5) 
    } 

    SubShader 
        { 
            Tags {"Queue" = "Transparent"} 
            ZWrite Off GrabPass { } 
            Pass { Fog { Mode Off } Blend SrcAlpha OneMinusSrcAlpha

         CGPROGRAM
         #pragma vertex vert
         #pragma fragment frag

         fixed4 _Color;
         sampler2D _GrabTexture;
         struct appdata
         {
             float4 vertex : POSITION;
         };
         struct v2f
         {
             float4 pos : SV_POSITION;
             float4 uv : TEXCOORD0;
         };
         v2f vert (appdata v)
         {
             v2f o;
             o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
             o.uv = o.pos;
             return o;
         }
         half4 frag(v2f i) : COLOR
         {
             float2 coord = 0.5 + 0.5 * i.uv.xy / i.uv.w;
             fixed4 tex = tex2D(_GrabTexture, float2(coord.x, 1 - coord.y));
             return fixed4(lerp(tex.rgb, _Color.rgb, _Color.a), 1);
         }
         ENDCG
     }
 }
}
\$\endgroup\$
1
\$\begingroup\$

Okay, I found the answer, with debugging it by myself. The fix is kinda simple, just remove the 1 - in this line fixed4 tex = tex2D(_GrabTexture, float2(coord.x, 1 - coord.y));

Shader "Unity Answers/Solid Transparency Color" 
{ 
    Properties 
    { 
        _Color ("Color", Color) = (0.5, 0.5, 0.5, 0.5) 
    } 

    SubShader 
        { 
            Tags {"Queue" = "Transparent"} 
            ZWrite Off GrabPass { } 
            Pass { Fog { Mode Off } Blend SrcAlpha OneMinusSrcAlpha

         CGPROGRAM
         #pragma vertex vert
         #pragma fragment frag

         fixed4 _Color;
         sampler2D _GrabTexture;
         struct appdata
         {
             float4 vertex : POSITION;
         };
         struct v2f
         {
             float4 pos : SV_POSITION;
             float4 uv : TEXCOORD0;
         };
         v2f vert (appdata v)
         {
             v2f o;
             o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
             o.uv = o.pos;
             return o;
         }
         half4 frag(v2f i) : COLOR
         {
             float2 coord = 0.5 + 0.5 * i.uv.xy / i.uv.w;
             fixed4 tex = tex2D(_GrabTexture, float2(coord.x, coord.y)); //Here is the fix
             return fixed4(lerp(tex.rgb, _Color.rgb, _Color.a), 1);
         }
         ENDCG
     }
 }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.