统一按钮丢弃着色器中的像素
unity button discarding pixels in shader
我正在使用我自己的着色器处理按钮。这工作正常,除了 unity 在到达我的着色器之前似乎是 discarding/clipping 像素。
我的图片上下三分之一透明。如果我做类似 color=fixed4(1,0,0,1) 的事情;只有中间部分是红色的。按钮和纹理都是正方形的,我可以在场景视图中看到它们的位置都正确。关于如何防止按钮剪切这些透明像素的任何想法?
着色器如下:
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
}
}
}
对于有类似问题的任何人,我最终删除了 Image 组件并添加了 RawImage。没有剪裁!
我正在使用我自己的着色器处理按钮。这工作正常,除了 unity 在到达我的着色器之前似乎是 discarding/clipping 像素。
我的图片上下三分之一透明。如果我做类似 color=fixed4(1,0,0,1) 的事情;只有中间部分是红色的。按钮和纹理都是正方形的,我可以在场景视图中看到它们的位置都正确。关于如何防止按钮剪切这些透明像素的任何想法?
着色器如下:
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
}
}
}
对于有类似问题的任何人,我最终删除了 Image 组件并添加了 RawImage。没有剪裁!