- Home /
Transparent sprite color change shader
Hello! I've got a transparent Sprite from a png sprite sheet, that is rendered by SpriteRenderer. I want to be able to switch it's fully transparent alpha with semi-transparent black color. Simply changing color doesn't work, since it doesn't change alpha color. I know there any many ways to do this - such as simply put another square sprite on top - or use two different versions of the same sprite, but I'm curios to do this using a shader. Here is what I've got so far:
Shader "Custom/Darkened"
{
Properties
{
_MainTex("Base (RGB), Alpha (A)", 2D) = "black" {}
}
SubShader
{
LOD 200
Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
}
Pass
{
ColorMask RGBA
Cull Off
Lighting Off
ZWrite Off
Fog { Mode Off }
Offset -1,-1
Blend SrcAlpha OneMinusDstAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
struct appdata_t
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
fixed4 color : COLOR;
};
struct v2f
{
float4 vertex : SV_POSITION;
half2 texcoord : TEXCOORD0;
fixed4 color : COLOR;
};
v2f o;
v2f vert (appdata_t v)
{
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.texcoord = v.texcoord;
o.color = v.color
return o;
}
fixed4 frag (v2f IN) : COLOR
{
IN.color.a=0.5;
return tex2D(_MainTex, IN.texcoord) * IN.color;
}
ENDCG
}
}
}
Alpha is removed, but it's completely black, not semi-transparent, I cannot see through it. What's the right way to do this?
Your answer
![](https://koobas.hobune.stream/wayback/20220613170412im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Allow 2D sprite to receive light from any direction and show on both sides 0 Answers
Shader doesn't work when object is inverted by axis 1 Answer
Shadows looks different in editor than in build 1 Answer
My shader doesn't work if I use the same material for multiple objects with the same texture. 1 Answer