2015-09-01 70 views
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 
     } 
    } 
} 

回答

2

此行添加到您的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和阿尔法seperatly

o.color.rgb = _Color.rgb 
o.color.a = _Color.a 

OR

o.color.r = _Color.r 
o.color.g = _Color.g 
o.color.b = _Color.b 
o.color.a = _Color.a 
玩t后的

你看颜色:帽子,你可以在你的half4 frag (v2f IN) : COLOR方法返回的颜色值

// do something with your color if you want 
// you can also play with alpha here 
return IN.color; 
+0

的解决方案非常感谢,但我不知道怎么回IN.color,当我在half4 FRAG(V2F IN)返回另一个东西'return colorTransparent'和'return tex2D(_MainTex,IN.texcoord)',我想在我的脚本中编辑材质的Alpha通道 – ATHellboy

+1

'return colorTransparent;'将返回没有透明度(透明/无颜色)的黑色。而'return tex2D(_MainTex,IN.texcoord)'将返回纹理的颜色。因此,如果你想玩纹理alpha;你可以做'half4 tex = tex2D(_MainTex,IN.texcoord); tex.a = IN.color.a; return tex;' 这会将纹理的alpha值更改为您设置的颜色的alpha值。 –

+0

关于这个着色器只是一件事,可以在移动设备上使用它吗? – ATHellboy

相关问题