Unity | 记一次置灰shader问题
起因在使用置灰shader的时候,发现图片透明的部分有黑边
而且IOS没这个问题,只有安卓会有这个问题。
后来发现这张图被打进了图集,而且图集对应安卓的压缩格式如下
有分离alpha通道,于是修改了下shader代码,显示就正常了
shader代码如下
Shader "FullSpeedFlight/Gray"
{
Properties
{
//Image上自带的Texture
_MainTex("Base (RGB), Alpha (A)", 2D) = "white" {}
//合并图像时传入的Texture
_AlphaTex("Sprite Alpha Texture", 2D) = "white" {}
}
SubShader
{
Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
}
Pass
{
Cull Off
Lighting Off
ZWrite Off
Fog{ Mode Off }
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
sampler2D _AlphaTex;
struct a2v
{
float4 vertex : POSITION;
half4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 vertex : POSITION;
half4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
void vert(a2v v, out v2f o)
{
o.vertex = UnityObjectToClipPos(v.vertex);
o.color = v.color;
o.texcoord = v.texcoord;
}
half4 frag(v2f i) : SV_Target
{
// Sample the texture
half4 col = tex2D(_MainTex, i.texcoord) * i.color;
fixed texAlpha = tex2D(_AlphaTex, i.texcoord).r;
fixed c = 0.299 * col.r + 0.587 * col.g + 0.184 * col.b;
col = float4(c, c, c, col.a * texAlpha);
return col;
}
ENDCG
}
}
}
嗯不用怀疑,就是这么简单......
页:
[1]