2015-09-01 67 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

相關問題