Hey everyone!
I'm trying to make a custom 2D sprite shader that would make the bottom half of my sprite semi-transparent. So far, this is what I've been able to come up with:
Shader "Custom/SinkInTile"
{
Properties
{
_MainTex("Texture", 2D) = "white" {}
}
SubShader
{
Tags
{
"Queue" = "Transparent+1"
}
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 spriteUV : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.spriteUV = v.uv;
return o;
}
fixed4 frag(v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.spriteUV);
if (i.spriteUV.y < 0.5f) { //set alpha to 0.5 for pixels on the lower half of the sprite frame
col.a *= 0.5f;
}
return col;
}
ENDCG
}
}
}
However, the problem with this code is that it derives the y coordinate relative to the height of the whole sprite sheet, meaning that it won't work as intended with sprite sheets that contain multiple rows of frames.
Is there any way to get the y coordinate of a pixel relative to the individual sprite frame?
↧