2D Water Shader

This shader distorts textures behind it according to a noise map and also creates a nice blue/green color gradient. Requires a noise map but not a base texture.

iexplore_2018-02-02_17-15-34

// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

 Shader "Custom/WaterShader2D" 
 {
     Properties 
     {
         _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
         _Colour ("Colour", Color) = (1,1,1,1)
 
         _BumpMap ("Noise text", 2D) = "bump" {}
         _Magnitude ("Magnitude", Range(0,1)) = 0.5
		 _Blue ("Blue", Range(0,7)) = 2
		 _Green ("Green", Range(0,7)) = 2
		 _MaxAlpha("Alpha Clamp", Range(0,1)) = 0.5
     }
     
     SubShader
     {
         Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Opaque"}
         ZWrite On Lighting Off Cull Off Fog { Mode Off } Blend One Zero
 
         GrabPass { "_GrabTexture" }
         
         Pass 
         {
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #include "UnityCG.cginc"
 
             sampler2D _GrabTexture;
 
             sampler2D _MainTex;
             fixed4 _Colour;
 
             sampler2D _BumpMap;
             float  _Magnitude;
			 float _Green;
			 float _Blue;
			 float _MaxAlpha;
			 
 
             struct vin_vct
             {
                 float4 vertex : POSITION;
                 float4 color : COLOR;
                 float2 texcoord : TEXCOORD0;
				 //float2 uv : TEXCOORD0;
             };
 
             struct v2f_vct
             {
                 float4 vertex : POSITION;
                 fixed4 color : COLOR;
                 float2 texcoord : TEXCOORD0;
				 //float2 uv : TEXCOORD0;
                 float4 uvgrab : TEXCOORD1;
             };
 
             // Vertex function 
             v2f_vct vert (vin_vct v)
             {
                 v2f_vct o;
                 o.vertex = UnityObjectToClipPos(v.vertex);
                 o.color = v.color;
 
                 o.texcoord = v.texcoord;
				 //o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                 o.uvgrab = ComputeGrabScreenPos(o.vertex);
                 return o;
             }
 
             // Fragment function
             half4 frag (v2f_vct i) : COLOR
             {
                 half4 mainColour;
                 mainColour.a = 1;
				 mainColour.b = 1*_Blue*i.texcoord.y;
				 mainColour.g = 1*_Green*i.texcoord.y*i.texcoord.y;
				 mainColour.r = 0;
                 half4 bump = tex2D(_BumpMap, i.texcoord);
                 half2 distortion = UnpackNormal(bump).rg;
 
                 i.uvgrab.xy += distortion * _Magnitude*.02;
 
                 fixed4 col = tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(i.uvgrab));
				 col.a = 0;
                 return col * mainColour ;
             }
         
             ENDCG
         } 
     }
 }

 

Leave a Reply