- Home /
button discarding pixels in shader
hi all,
i'm working with buttons for which i'm using my own shader. this works fine, except unity seems to be discarding/clipping pixels before they ever get to my shader.
my image has the top and bottom thirds transparent. if i do something like color=fixed4(1,0,0,1); only the middle section will be red. both the button and the texture are square, and i can see in the scene view that both are positioned properly. any ideas on how i can prevent the button from clipping these transparent pixels?
shader is below:
Shader "Custom/BlackTimerImage"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
[PerRendererData] _PercentComplete("Percent Complete", Float) = .2
_ColorMask ("Color Mask", Float) = 15
[Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
}
SubShader
{
Tags
{
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}
Cull Off
Lighting Off
ZWrite Off
ZTest [unity_GUIZTestMode]
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
Name "Default"
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "UnityUI.cginc"
//#pragma multi_compile __ UNITY_UI_ALPHACLIP
struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
float2 texcoord : TEXCOORD0;
float4 worldPosition : TEXCOORD1;
};
fixed4 _Color;
fixed4 _TextureSampleAdd;
float _PercentComplete;
v2f vert(appdata_t v)
{
v2f OUT;
OUT.worldPosition = v.vertex;
OUT.vertex = UnityObjectToClipPos(OUT.worldPosition);
OUT.texcoord = v.texcoord;
OUT.color = v.color * _Color;
return OUT;
}
sampler2D _MainTex;
fixed4 frag(v2f IN) : SV_Target
{
half4 color = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd) * IN.color;
half2 up = float2(0, 1);
half2 pixelRay = normalize(float2(.5-IN.texcoord.x, IN.texcoord.y-.5));
float angle = (1-saturate((dot(up, pixelRay)+1) *.5))*.5;
if (IN.texcoord.x < .5)
{
angle = .5 + (.5 - angle);
}
if (angle > _PercentComplete)
{
color.rgb *= .5f;
if (color.a <= .10)
{
color.a = .5f;
}
}
return color;
}
ENDCG
}
}
}
,Hi all,
I'm working with Unity buttons for which I'm writing my own shader. It seems the button is discarding pixels before it ever gets to my (fragment) shader, which I'd love to prevent.
the image i'm using is square, my button is square. however, in my image the top third is transparent, the bottom third is transparent, and the middle has colored pixels. the unity button is discard the top and bottom pixels. i know this because if i do color=fixed4(1,0,0,1) only the middle portion is red. does anybody know how to stop unity from discarding/clipping these pixels? shader is below.
Shader "Custom/BlackTimerImage"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
[PerRendererData] _PercentComplete("Percent Complete", Float) = .2
_ColorMask ("Color Mask", Float) = 15
[Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
}
SubShader
{
Tags
{
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}
Cull Off
Lighting Off
ZWrite Off
ZTest [unity_GUIZTestMode]
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
Name "Default"
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "UnityUI.cginc"
//#pragma multi_compile __ UNITY_UI_ALPHACLIP
struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
float2 texcoord : TEXCOORD0;
float4 worldPosition : TEXCOORD1;
};
fixed4 _Color;
fixed4 _TextureSampleAdd;
float _PercentComplete;
v2f vert(appdata_t v)
{
v2f OUT;
OUT.worldPosition = v.vertex;
OUT.vertex = UnityObjectToClipPos(OUT.worldPosition);
OUT.texcoord = v.texcoord;
OUT.color = v.color * _Color;
return OUT;
}
sampler2D _MainTex;
fixed4 frag(v2f IN) : SV_Target
{
half4 color = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd) * IN.color;
half2 up = float2(0, 1);
half2 pixelRay = normalize(float2(.5-IN.texcoord.x, IN.texcoord.y-.5));
float angle = (1-saturate((dot(up, pixelRay)+1) *.5))*.5;
if (IN.texcoord.x < .5)
{
angle = .5 + (.5 - angle);
}
if (angle > _PercentComplete)
{
color.rgb *= .5f;
if (color.a <= .10)
{
color.a = .5f;
}
}
return color;
}
ENDCG
}
}
}
Answer by cronus98 · Feb 16, 2019 at 11:47 PM
To anyone with a similar problem, I ended up removing the Image component and adding a RawImage instead. No clipping!
It works! Is there anyone who can explain why exactly this is?
Your answer
Follow this Question
Related Questions
Generic clipsafe fading ✿ 0 Answers
custom shader error. copy of "Nature/Tree Soft Occlusion Leaves" 0 Answers
Shader that does per Vert alpha with co 0 Answers
shader problem 0 Answers
Z-priming alpha-per-vertex 0 Answers