- Home /
Question by
Truest · Mar 21, 2020 at 06:39 PM ·
shadershader programminguvs
I tried to flip the UVs of a sprite with a shader but it's glitching
Here's my shader code:
Shader "Custom/Reverse"
{
Properties
{
[PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
_Color("Tint", Color) = (1,1,1,1)
[MaterialToggle] PixelSnap("Pixel snap", Float) = 0
}
SubShader
{
Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
"PreviewType" = "Plane"
"CanUseSpriteAtlas" = "True"
}
// No culling or depth
Cull Off
ZWrite Off
ZTest Always
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
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 = UnityObjectToClipPos(v.vertex);
o.uv.x = 1 - v.uv.x;
o.uv.y = v.uv.y;
return o;
}
sampler2D _MainTex;
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
// just invert the colors
//col.rgb = 1 - col.rgb;
return col;
}
ENDCG
}
}
}
It did manage to flip her, but as you can see everything around her is transferred her colors everywhere. Any idea as to why that's happening?
unity-rrqdvb7oqa.png
(11.5 kB)
Comment
Best Answer
Answer by Truest · Mar 21, 2020 at 11:48 PM
So.... it turns out there's a shader command that goes as follows:
Blend SrcAlpha OneMinusSrcAlpha
This command tells the shader how each pixel will blend with the pixels beneath it. I don't know too much of details, but if you put this the other commands like Cull off, Zwrite Off, etc., transparency will now work with your shader! I discovered this from this youtube video: https://youtu.be/T-HXmQAMhG0