- Home /
Slice-9-grid gradient shader
Hi. I want to create UI gradient shader and i have some trouble. My gradient shader based on UI/Default shader And in Editor-mode his works fine : https://i.imgur.com/wENG5Hi.png But if i click "Play" - uv coord changed and my lerp functions work incorrectly : https://i.imgur.com/1EEG5fy.png My picture is packed in atlas and i think it's problem... I try to use ComputeScreenPos for calculation OUT.uv in vert function but in this case gradient changes at screen position... it's bad =) Please help!)
Short shader code :
struct appdata_t
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f
{
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
float4 worldPosition : TEXCOORD1;
UNITY_VERTEX_OUTPUT_STEREO
fixed texcoordRatio : TEXCOORD_RATIO;
};
sampler2D _MainTex;
fixed4 _Color;
fixed4 _Color2;
fixed4 _TextureSampleAdd;
float4 _ClipRect;
float4 _MainTex_ST;
v2f vert(appdata_t v)
{
v2f OUT;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
OUT.worldPosition = v.vertex;
OUT.vertex = UnityObjectToClipPos(OUT.worldPosition);
OUT.uv = TRANSFORM_TEX(v.uv, _MainTex); // my problem is here! How to get real tex coord in play mode
return OUT;
}
fixed4 frag(v2f IN) : SV_Target
{
half4 color = (tex2D(_MainTex, IN.uv) + _TextureSampleAdd);
color *= lerp(_Color2, _Color , IN.uv.y ); // !!!Gradient
#ifdef UNITY_UI_CLIP_RECT
color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
#endif
#ifdef UNITY_UI_ALPHACLIP
clip (color.a - 0.001);
#endif
return color;
}
Comment