我对着色器编程一无所知,但现在我需要向要使用的着色器添加 alpha。实际上我想淡入淡出我的 Sprite ,但它不在我使用的着色器中。
着色器:
Shader "Sprites/ClipArea2Sides"
{
Properties
{
_MainTex ("Base (RGB), Alpha (A)", 2D) = "white" {}
_Length ("Length", Range(0.0, 1.0)) = 1.0
_Width ("Width", Range(0.0, 1.0)) = 1.0
}
SubShader
{
LOD 200
Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
}
Pass
{
Cull Off
Lighting Off
ZWrite Off
Offset -1, -1
Fog { Mode Off }
ColorMask RGB
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
float _Length;
float _Width;
struct appdata_t
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
};
v2f vert (appdata_t v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.texcoord = v.texcoord;
return o;
}
half4 frag (v2f IN) : COLOR
{
if ((IN.texcoord.x<0) || (IN.texcoord.x>_Width) || (IN.texcoord.y<0) || (IN.texcoord.y>_Length))
{
half4 colorTransparent = half4(0,0,0,0) ;
return colorTransparent ;
}
else
return tex2D(_MainTex, IN.texcoord) ;
}
ENDCG
}
}
}
像这样的着色器:http://wiki.unity3d.com/index.php/UnlitAlphaWithFade
由于性能原因,我需要针对移动设备优化 alpha。
答案
非常感谢阿纳斯·伊克巴尔。
这是一个具有剪辑区域 + 色调的着色器:
Shader "Sprites/TestShader"
{
Properties
{
_Color ("Color Tint", Color) = (1,1,1,1)
_MainTex ("Base (RGB), Alpha (A)", 2D) = "white" {}
_Length ("Length", Range(0.0, 1.0)) = 1.0
_Width ("Width", Range(0.0, 1.0)) = 1.0
}
SubShader
{
LOD 200
Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
}
Pass
{
Cull Off
Lighting Off
ZWrite Off
Offset -1, -1
Fog { Mode Off }
ColorMask RGB
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
float _Length;
float _Width;
half4 _Color;
struct appdata_t
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
half4 color : COLOR;
};
v2f vert (appdata_t v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.texcoord = v.texcoord;
o.color = _Color;
return o;
}
half4 frag (v2f IN) : COLOR
{
if ((IN.texcoord.x<0) || (IN.texcoord.x>_Width) || (IN.texcoord.y<0) || (IN.texcoord.y>_Length))
{
half4 colorTransparent = half4(0,0,0,0) ;
return colorTransparent;
}
else
{
half4 tex = tex2D(_MainTex, IN.texcoord);
tex.a = IN.color.a;
return tex;
}
}
ENDCG
}
}
}
最佳答案
将此行添加到您的 Properties
_Color ("Color Tint", Color) = (1,1,1,1)
然后在
float _Width;
正下方添加这一行half4 _Color;
更新您的
struct v2f
并在其中添加一个颜色变量。struct v2f
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
half4 color : COLOR;
};
那么你可以在你的
v2f vert
中使用它像这样:o.color = _Color
或者,如果您只想单独玩 rgb 和 alpha
o.color.rgb = _Color.rgb
o.color.a = _Color.a
或者
o.color.r = _Color.r
o.color.g = _Color.g
o.color.b = _Color.b
o.color.a = _Color.a
之后,您可以在
half4 frag (v2f IN) : COLOR
中返回颜色值。方法// do something with your color if you want
// you can also play with alpha here
return IN.color;
关于unity3d - 在 Unity3D 中将 alpha 添加到着色器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32330902/