- Home /
Tweak Alpha on custom fragment shader
Hi guys,
I'm new to shaders but I've been following some tutorials and I tried a LOT of things on this particular shader... to no avail.
I got the code from here. This shader cycles through 2 textures over time, with a "wipe" effect (see source for a demo). Now that I've learned and kind of understand most of it, I'm trying to change it to be able to use textures with alpha.
Here's the shader code :
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"
}
I tried to implement a small code snippet in the last function for alpha support. I tried changing Tags to {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
, setting Cull
and ZWrite
to Off
, adding Blend SrcAlpha OneMinusSrcAlpha
... basically tried discarding transparent fragments, alpha testing, and blending, but I must be doing something wrong.
I set my two textures (which are PSDs, Photoshop file format with Alpha) to "Advanced" Texture Type, with "Generate Mip Maps" turned off, "Alpha is Transparency" turned on, and "Format" to "RGBA 32 bit". With these settings and the shader above, my textures preview as they should, but my textured plane is black...
Anyone could either help me out or tip me in the right direction please ?
Thanks in advance !
EDIT
This is what I get with the unedited shader from here + PSD set to normal (no advanced texture settings) :
This is what I get with current shader (code above) + PSD set to RGBA 32-bit, no Mip Maps, and Alpha is Transparency turned on :
And this is what I'm trying to achieve (but not with a still image, I want to keep the "Wipe" effect).
Answer by Posthoc · Feb 23, 2014 at 04:56 PM
Got my answer from http://stackoverflow.com/questions/21967969/add-alpha-support-to-fragment-shader-glsl
Here's the proper code for what I was trying to achieve :
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;
}
Your answer
Follow this Question
Related Questions
CG Shader: Setting output alpha changes RGB too 0 Answers
Getting alpha information in shader 1 Answer
My shader stops working if my texture colours are all low-alpha 0 Answers
Changing material color´s alpha in self iluminated shader 1 Answer
Scripting Shader: How to control Alpha with slider? 1 Answer