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
}
}
}
Thanks! I get an error "No subshader can run on this graphics card" though. Any idea? I'm using Unity Indie, can this be a problem?
– HypothesizeAFAIK shaders are not depends on Unity license. so this is a hardware limits. which hardware do you use? graphic adapter model? and try menu "edit -> graphic emulation" - does it affect shader? which modes? i think GrabPass{} can be not supported y some cards
– HarlandharleI currently have a Nvidia 460gtx. I already tried changing the graphic emulation, though that didn't change anything (currently it's at none.) And what do you mean by "which modes" ? Btw, thanks a lot for helping.
– Hypothesizethat's very strange. shader model 2.0 support must be enough to run this shader. your card supports up to shader model 5.0. did you edit this shader? try to use it as is. also, try to switch to fantastic mode in quality settings. if it still doesn't work, try to remove 'GrabPass {}' line. shader will not work properly then, but should draw an object (without transparency). it should help to detect what's wrong in shader. write response here.
– HarlandharleOk I tried all what you said. Changing the quality settings doesn't do anything. Removing the GrabPass removes the error, though as you said it's not transparant.
– Hypothesize