- Home /
How to add transparency to a waving shader?
I'm trying to get a 2D water effect and found a shader that gives me the wavy effect I'm looking for. Unfortunately, I can't seem to figure out how to add transparency to it. I am trying to emulate something like the water seen here:
https://youtu.be/z1rRuWBfc8k?t=6m6s
I don't need the dynamic waves at the top. I'm mainly just trying to apply this to a textured plane. I have looked through many similar posts about people trying to add transparency to certain shaders and have attempted to add several different lines to the wavy shader, but it seems like the answer is different depending on the type of shader you are using. Here is the shader I am trying to modify:
Shader "Custom/NewShader" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_SpeedX("SpeedX", float)=3.0
_SpeedY("SpeedY", float)=3.0
_Scale("Scale", range(0.005, 0.2))=0.03
_TileX("TileX", float)=5
_TileY("TileY", float)=5
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Lambert
sampler2D _MainTex;
float4 uv_MainTex_ST;
float _SpeedX;
float _SpeedY;
float _Scale;
float _TileX;
float _TileY;
struct Input {
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o)
{
float2 uv = IN.uv_MainTex;
uv.x += sin ((uv.x+uv.y)*_TileX+_Time.g *_SpeedX)*_Scale;
uv.x += cos (uv.y*_TileY+_Time.g *_SpeedY)*_Scale;
half4 c = tex2D (_MainTex, uv);
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
Answer by MakinStuffLookGood · Nov 17, 2015 at 08:00 PM
EDIT: My knowledge of surface shaders is terrible, here's the actual changes you need to make:
Tags
{
"Queue"="Transparent"
"RenderType"="Transparent"
}
...
#pragma surface surf Lambert alpha
...
Then, as you have it now you just use the Texture's alpha value, you may want to define another property _Alpha
and in your surf program do something like this:
o.Albedo = c.rgb * _Alpha;
o.Alpha = c.a * _Alpha;
Multiplying the output Albedo by the Alpha value seems to be necessary for an _Alpha value of zero to actually make the object entirely transparent.
I gave that a try, but it didn't seem to do anything yet. Would I need to change something in the Properties so that I could adjust the alpha value to tell it how translucent to be?
I was using my knowledge of CG shaders which wasn't applicable in the surface shader world! I've update my original answer with the correct information.
That's okay. You've still helped me quite a bit so far. It isn't quite working yet though. I added:
o.Albedo = c.rgb * _Alpha;
o.Alpha = c.a * _Alpha;
But that turns it completely pink.
Do I still need the other lines that you listed before then?:
ZWrite Off
Blend SrcAlpha One$$anonymous$$inusSrcAlpha
Ah. I was using an opaque texture before and trying to make it semi-transparent with the shader. But if I just make it semi-transparent in Photoshop, then it becomes translucent in Unity. Thanks!
I don't quite understand what to change for the o.Alpha though.
I edited my original answer and took out stuff that didn't need to be here. Here is the complete shader with the _Alpha
property implemented:
http://hastebin.com/ijuviteqiq.avrasm
If this solution works for you please mark the answer as correct! :D