- Home /
Making a color transparent in shaders
I have this shader that I have written. I have a 2d rectangular sprite, no textures. If the left side of the sprite is 0 and the right end of the sprite is 1, I am interpolating through and calculating its position within the sprite, and color it accordingly. If it goes out the range, I want to make it transparent. This is what I currently have for the pixel shader.
Shader "Custom/rangeColorShader"
{
Properties
{
_RangeColor3("RangeColor3", Color) = (1, 0, 0, 1)
_RangeColor2("RangeColor2", Color) = (1, 0, 0, 1)
_RangeColor1("RangeColor1", Color) = (1, 0, 0, 1)
_StartXPos("StartXPos", Float) = 1.5
_EndXPos("EndXPos", Float) = 1.5
_Bad("Bad", Range(0.0, 1.0)) = 0.5
_Good("Good", Range(0.0, 1.0)) = 0.5
}
SubShader
{
Tags{ "Queue" = "Transparent" "RenderType" = "Transparent" }
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma enable_d3d11_debug_symbols
#pragma vertex vert
#pragma fragment frag
struct vertInput
{
float4 pos : POSITION;
};
struct vertOutput
{
float4 pos : SV_POSITION;
float3 wPos : TEXCOORD1;
};
vertOutput vert(vertInput input)
{
vertOutput o;
o.pos = mul(UNITY_MATRIX_MVP, input.pos);
o.wPos = mul(_Object2World, input.pos).xyz;
return o;
}
float _StartXPos;
float _EndXPos;
float4 _RangeColor3;
float4 _RangeColor2;
float4 _RangeColor1;
float _Good;
float _Bad;
float4 frag(vertOutput output) : COLOR
{
float pos = (output.wPos.x - _StartXPos) / (_EndXPos - _StartXPos);
if (pos < _Bad)
return half4(_RangeColor1.r, _RangeColor1.g, _RangeColor1.b, 1.0f);
else if (pos < _Good)
return half4(_RangeColor2.r, _RangeColor2.g, _RangeColor2.b, 1.0f);
else if (pos <= 1.0f)
return half4(_RangeColor3.r, _RangeColor3.g, _RangeColor3.b, 1.0f);
else //for debugging purpose
return half4(1.0, 1.0, 1.0, 1.0);
}
ENDCG
}
}
FallBack "Diffuse"
}
I thought setting the alpha to 0 would make it transparent, but when it hits the else statement, it just goes black. It does not show the color behind the sprite (which is purple). Is my approach to making a section go transparent incorrect?
Edit: Posted my entire shader code. Edit #2: Anyone??
There's nothing obviously wrong with your code - it'll be related to blending. If you could post your whole shader then I'm sure we can fix it up.
I've just uploaded my entire shader. Thank you in advance!
Hmmm. Are you aware that in that particular shader you aren't setting the alpha to 0 anywhere? All of the colour values you return have a final component (w or 'alpha') of 1. What happens if you set those to 0. Or even just return half4(1,1,1,0) at the top of the fragment shader?
I'll post a transparent shader when I get home tonight if you're still stuck!
Answer by Jessespike · Dec 09, 2015 at 08:02 PM
You didn't include the shader's Tags and Render State Setup in the question, which is important to know for transparency. Did you update the tags and enable alpha blending?
Tags { "Queue"="Transparent" "RenderType"="Transparent" }
Blend SrcAlpha OneMinusSrcAlpha
Sorry I was away for a while. Yes I do have the tags. Tags{ "RenderType" = "Transparent" "Queue" = "Transparent" }
I did some googling on the 2nd line, the Blend stuff. $$anonymous$$ost of the example uses Pass and defines the 2nd line in it.
So I added the 2nd line and it still doesnt seem to work.
Edit: After adding in the Blend code my sprite is just pink. I tried commenting that piece of code out, and then my sprite was appearing as intended. And I'm getting this error in Unity saying that "unrecognized identifier 'Blend' ". I'm still able to run the game though.