2014-02-23 97 views
4

我是着色器新手,但我一直在關注一些教程,並且在這個特定着色器上嘗試了很多東西......無濟於事。爲片段着色器(GLSL)添加alpha支持

我從here得到了密碼。該着色器隨着時間的推移循環遍歷2個紋理,具有「擦除」效果(請參閱源代碼演示)。現在我已經學會了,並且瞭解了它的大部分內容,我試圖將其更改爲可以使用帶有alpha的紋理。

這裏的shader代碼:

Shader "Wipe" { 

Properties{ 
    _tex0 ("Texture1", 2D) = "white" {} 
    _tex1 ("Texture2", 2D) = "white" {} 
} 

SubShader{ 
    Tags {"Queue"="Geometry"} 

    Pass{ 
    CGPROGRAM 
    #pragma target 3.0 
    #pragma vertex vert 
    #pragma fragment frag 
    #include "UnityCG.cginc" 

    sampler2D _tex0; 
    sampler2D _tex1; 
    float4 _tex0_ST; 

    struct v2f { 
     float4 pos : POSITION; 
     float4 color : COLOR0; 
     float4 fragPos : COLOR1; 
     float2 uv : TEXCOORD0; 
    }; 

    v2f vert (appdata_base v){ 
     v2f o; 
     o.pos = mul (UNITY_MATRIX_MVP, v.vertex); 
     o.fragPos = o.pos; 
     o.uv = TRANSFORM_TEX (v.texcoord, _tex0); 
     o.color = float4 (1.0, 1.0, 1.0, 1); 
     return o; 
    } 

    half4 frag (v2f i) : COLOR{ 
     float animtime = _Time*10.0; 
     float2 q = i.uv.xy/float2(1,1); 
     float3 oricol = tex2D (_tex0,float2(q.x,q.y)).xyz; 
     float3 col = tex2D (_tex1,float2(i.uv.x,i.uv.y)).xyz; 
     float comp = smoothstep(0.2, 0.7, sin(animtime)); 
     col = lerp(col,oricol, clamp(-2.0+2.0*q.x+3.0*comp,0.0,1.0)); 

     // custom alpha bit 
     float4 textureColor = float4(col,1); 
     if(textureColor.a < 1.0){ 
      discard; 
     } 

     return textureColor; 
    } 
    ENDCG 
    } 
} 
//FallBack "VertexLit" 
} 

我試圖實現在阿爾法支持的最後一個功能的一小段代碼。我試着改變Tags爲{「Queue」=「Transparent」「IgnoreProjector」=「True」「RenderType」=「Transparent」},將Cull和ZWrite設置爲Off,添加Blend SrcAlpha OneMinusSrcAlpha ...基本上嘗試丟棄透明片段,alpha測試和混合,但我一定在做錯事。

我把我的兩個紋理(這是PSD,Photoshop文件格式與Alpha)設置爲「高級」紋理類型,關閉「生成Mip貼圖」,打開「Alpha is Transparency」,「格式」 RGBA 32位「。有了這些設置和上面的着色器,我的紋理預覽,因爲他們應該,但我的紋理飛機是黑色的...

任何人都可以幫助我或正確的方向給我提示?

在此先感謝!


編輯: (抱歉的聯繫,我沒有足夠的聲譽還沒有發佈他們全部)

這是我和修改前的着色得到here + PSD設置爲正常(沒有先進的紋理設置): http://answers.unity3d.com/storage/temp/22441-unity_base.jpg

這是我用當前着色器(代碼上面)+ PSD作爲紋理獲得,設置到RGBA 32位,沒有Mip地圖,並且'Alpha is Transparency'開啓: http://answers.unity3d這是我試圖實現的目標(但不是與靜止圖像,我想保持原來的「擦除」效果): http://storage/temp/22442-unity_black.jpg

/nicolasraspail.com/unity_achieve.png


編輯:(!感謝@nwellnhof)這裏的着色器工作正常

Shader "Wipe" { 

     // Editor controllers 
     Properties{ 
     _tex0 ("Texture1", 2D) = "white" {} 
     _tex1 ("Texture2", 2D) = "white" {} 
     } 

    SubShader{ 
     Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"} 
     ZWrite Off 
     Blend SrcAlpha OneMinusSrcAlpha 

     Pass{ 
     CGPROGRAM 
      #pragma target 3.0 
      #pragma vertex vert 
      #pragma fragment frag 
      #include "UnityCG.cginc" 

      sampler2D _tex0; 
      sampler2D _tex1; 
      float4 _tex0_ST; 

      struct v2f { 
       float4 pos : POSITION; 
       float4 color : COLOR0; 
       float4 fragPos : COLOR1; 
       float2 uv : TEXCOORD0; 
      }; 

      v2f vert (appdata_base v){ 
       v2f o; 
       o.pos = mul (UNITY_MATRIX_MVP, v.vertex); 
       o.fragPos = o.pos; 
       o.uv = TRANSFORM_TEX (v.texcoord, _tex0); 
       o.color = float4 (1.0, 1.0, 1.0, 1); 
       return o; 
      } 

      half4 frag (v2f i) : COLOR{ 
       float4 oricol = tex2D(_tex0, i.uv); 
       float4 col  = tex2D(_tex1, i.uv); 
       float animtime = _Time * 10.0; 
       float comp  = smoothstep(0.2, 0.7, sin(animtime)); 
       float coeff = clamp(-2.0 + 2.0 * i.uv.x + 3.0 * comp, 0.0, 1.0); 
       float4 result = lerp(col, oricol, coeff); 

       return result; 
      } 
     ENDCG 
} 
} 
//FallBack "VertexLit" 
} 

回答

4

好了,你的變化片斷程序丟棄如果整個片段顏色在_tex1是透明的。這不會產生預期的效果。你應該做的是退出你的更改,只需使用float4而不是float3作爲顏色變量,並在作業結束時刪除.xyz。那就是:

float4 oricol = tex2D (_tex0,float2(q.x,q.y)); 
    float4 col = tex2D (_tex1,float2(i.uv.x,i.uv.y)); 

但整個程序可以簡化很多。像下面的內容應該是等價的(未經測試):

half4 frag (v2f i) : COLOR { 
    float4 oricol = tex2D(_tex0, i.uv); 
    float4 col  = tex2D(_tex1, i.uv); 
    float animtime = _Time * 10.0; 
    float comp  = smoothstep(0.2, 0.7, sin(animtime)); 
    float coeff = clamp(-2.0 + 2.0 * i.uv.x + 3.0 * comp, 0.0, 1.0); 
    float4 result = lerp(col, oricol, coeff); 

    return result; 
} 
+0

謝謝您的回答。你的frag函數實際上正在工作,但我得到了和以前完全相同的結果([this](http://answers.unity3d.com/storage/temp/22441-unity_base.jpg))。它可能是一個圖像格式或導入問題?我試過PSD(Photoshop CS6),PNG(不按預期導入),TGA(與PSD相同的結果)。對於它們中的每一個,透明度都如同我的屏幕截圖一樣顯示... – Posthoc

+0

如果紋理與其中一個內置透明着色器正常工作(例如* Transparent Diffuse *),那麼這不是導入問題。另外請注意,您仍然需要對您提到的* Tags *,* ZWrite *和* Blend *進行更改。 – nwellnhof

+0

非常感謝!我沒有意識到我必須包含以前的標籤,zwrite和混合選項。 – Posthoc