Transparent Solid Color Shader

Hi,

I know nothing of making shaders in Unity, and I’m trying to make a Solid Color shader which is transparent. I currently have the following, which works:

 Properties
 {
      _Color ("Color", Color) = (1,1,1,.5)
 }
 SubShader
 {
      Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
      LOD 100
 
      ZWrite On
      Blend SrcAlpha OneMinusSrcAlpha 
 
      Pass
      {
           Lighting Off
           Color [_Color]
      }
 }

The problem is though that you can see parts of the model itself that are behind it:
alt text

Anyone knows how to fix this? I need/want a solid color, without those bright spots.

Thanks for the help!

This is an old question, but I implemented the two pass shader (with texturing) suggested by @ScroodgeM and it works great. This was tested on Unity 4 and the vertex/fragment shaders were based on the samples from the Unity docs here.

The secret ShaderLab sauce is ColorMask and Blend, as seen on the stoned dino below. Enjoy! =)

Shader "Custom/FlatTransparent" {
	Properties {
		_Color("Main Color", Color) = (1,1,1,1)
		_MainTex("Base (RGB) Trans (A)", 2D) = "white" {}
	}

	SubShader {
		Tags{ "Queue" = "Transparent" "IgnoreProjector" = "False" "RenderType" = "Transparent" }

		/////////////////////////////////////////////////////////
		/// First Pass
		/////////////////////////////////////////////////////////

		Pass {
			// Only render alpha channel
			ColorMask A
			Blend SrcAlpha OneMinusSrcAlpha

			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag

			fixed4 _Color;

			float4 vert(float4 vertex : POSITION) : SV_POSITION {
				return mul(UNITY_MATRIX_MVP, vertex);
			}

			fixed4 frag() : SV_Target {
				return _Color;
			}

			ENDCG
		}

		/////////////////////////////////////////////////////////
		/// Second Pass
		/////////////////////////////////////////////////////////

		Pass {
			// Now render color channel
			ColorMask RGB
			Blend SrcAlpha OneMinusSrcAlpha

			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag

			sampler2D _MainTex;
			fixed4 _Color;

			struct appdata {
				float4 vertex : POSITION;
				float2 uv : TEXCOORD0;
			};

			struct v2f {
				float2 uv : TEXCOORD0;
				float4 vertex : SV_POSITION;
			};

			v2f vert(appdata v) {
				v2f o;
				o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
				o.uv = v.uv;
				return o;
			}

			fixed4 frag(v2f i) : SV_Target{
				fixed4 col = _Color * tex2D(_MainTex, i.uv);
				return col;
			}
			ENDCG
		}
	}

	Fallback "Diffuse"
}

Shader below is do what you want. But your platform should support GrabPass technology. this can be not supported by some mobile platforms.

i can propound 2 more ways without grabpass:

  • create a 2-pass shader that first draws only to alpha-channel and the second draws just color based on alpha value, this can be achieved by blending options
  • create a object mask using render-texture and put it to main camera on top. this way is same as previous but uses render texture instead of shader calculations and blending

anyway issue is not so simple. with a just default blending two surfaces will be drawn twice and it will look not same as single surface.

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
		}
	}
}

Hello,
I am trying to achieve exactly this effect in my URP project. Neither of the two shaders work in my environment. Is there a solution for current Unity version?
Thanks , Jan