4
我對shader編程沒有任何意見,但現在我需要爲我想要使用的着色器添加alpha。其實我想淡入淡出我的精靈,但它不在我使用的着色器中。在Unity3D中添加alpha着色器
着色器:
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
我需要因爲性能移動設備優化的阿爾法。
回答
非常感謝阿納斯伊克巴爾。
這是一個有剪輯區+色調着色器:
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
}
}
}
的解決方案非常感謝,但我不知道怎麼回IN.color,當我在half4 FRAG(V2F IN)返回另一個東西'return colorTransparent'和'return tex2D(_MainTex,IN.texcoord)',我想在我的腳本中編輯材質的Alpha通道 – ATHellboy
'return colorTransparent;'將返回沒有透明度(透明/無顏色)的黑色。而'return tex2D(_MainTex,IN.texcoord)'將返回紋理的顏色。因此,如果你想玩紋理alpha;你可以做'half4 tex = tex2D(_MainTex,IN.texcoord); tex.a = IN.color.a; return tex;' 這會將紋理的alpha值更改爲您設置的顏色的alpha值。 –
關於這個着色器只是一件事,可以在移動設備上使用它嗎? – ATHellboy