Advertisement
Guest User

InteractiveWater.shader

a guest
Jan 25th, 2019
5,326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Unlit/Simple Water Interactive"
  2. {
  3.     Properties
  4.     {
  5.         _Color("Tint", Color) = (1, 1, 1, .5)
  6.         _FoamC("Foam", Color) = (1, 1, 1, .5)
  7.         _MainTex ("Main Texture", 2D) = "white" {}
  8.         _MaskInt ("RenderTexture Mask", 2D) = "white" {}
  9.         _TextureDistort("Texture Wobble", range(0,1)) = 0.1
  10.         _NoiseTex("Extra Wave Noise", 2D) = "white" {}
  11.         _Speed("Wave Speed", Range(0,1)) = 0.5
  12.         _Amount("Wave Amount", Range(0,1)) = 0.6
  13.         _Scale("Scale", Range(0,1)) = 0.5
  14.         _Height("Wave Height", Range(0,1)) = 0.1
  15.         _Foam("Foamline Thickness", Range(0,10)) = 8
  16.     }
  17.     SubShader
  18.     {
  19.         Tags { "RenderType"="Opaque"  "Queue" = "Transparent" }
  20.         LOD 100
  21.         Blend OneMinusDstColor One
  22.         Cull Off
  23.        
  24.         GrabPass{
  25.             Name "BASE"
  26.             Tags{ "LightMode" = "Always" }
  27.                 }
  28.         Pass
  29.         {
  30.             CGPROGRAM
  31.             #pragma vertex vert
  32.             #pragma fragment frag
  33.             // make fog work
  34.             #pragma multi_compile_fog
  35.            
  36.             #include "UnityCG.cginc"
  37.  
  38.             struct appdata
  39.             {
  40.                 float4 vertex : POSITION;
  41.                 float2 uv : TEXCOORD0;
  42.             };
  43.  
  44.             struct v2f
  45.             {
  46.                 float2 uv : TEXCOORD3;
  47.                 UNITY_FOG_COORDS(1)
  48.                 float4 vertex : SV_POSITION;
  49.                 float4 scrPos : TEXCOORD2;//
  50.                 float4 worldPos : TEXCOORD4;//
  51.             };
  52.             float _TextureDistort;
  53.             float4 _Color;
  54.             sampler2D _CameraDepthTexture; //Depth Texture
  55.             sampler2D _MainTex, _NoiseTex;//
  56.             float4 _MainTex_ST;
  57.             float _Speed, _Amount, _Height, _Foam, _Scale;//
  58.             float4 _FoamC;
  59.             sampler2D _MaskInt;
  60.  
  61.             uniform float3 _Position;
  62.             uniform sampler2D _GlobalEffectRT;
  63.             uniform float _OrthographicCamSize;
  64.  
  65.             v2f vert (appdata v)
  66.             {
  67.                 v2f o;
  68.                 UNITY_INITIALIZE_OUTPUT(v2f, o);
  69.                 float4 tex = tex2Dlod(_NoiseTex, float4(v.uv.xy, 0, 0));//extra noise tex
  70.                 v.vertex.y += sin(_Time.z * _Speed + (v.vertex.x * v.vertex.z * _Amount * tex)) * _Height;//movement
  71.                 o.vertex = UnityObjectToClipPos(v.vertex);
  72.                 o.worldPos = mul(unity_ObjectToWorld, v.vertex);
  73.                
  74.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
  75.                 o.scrPos = ComputeScreenPos(o.vertex);
  76.                 UNITY_TRANSFER_FOG(o,o.vertex);        
  77.                 return o;
  78.             }
  79.            
  80.             fixed4 frag (v2f i) : SV_Target
  81.             {
  82.                 // rendertexture UV
  83.                 float2 uv = i.worldPos.xz - _Position.xz;
  84.                 uv = uv/(_OrthographicCamSize *2);
  85.                 uv += 0.5;
  86.                 // Ripples
  87.                 float ripples = tex2D(_GlobalEffectRT, uv ).b;
  88.  
  89.                 // mask to prevent bleeding
  90.                 float4 mask = tex2D(_MaskInt, uv);             
  91.                 ripples *= mask.a;
  92.  
  93.  
  94.                 fixed distortx = tex2D(_NoiseTex, (i.worldPos.xz * _Scale)  + (_Time.x * 2)).r ;// distortion
  95.                 distortx +=  (ripples *2);
  96.            
  97.                 half4 col = tex2D(_MainTex, (i.worldPos.xz * _Scale) - (distortx * _TextureDistort));// texture times tint;        
  98.                 half depth = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.scrPos ))); // depth
  99.                 half4 foamLine =1 - saturate(_Foam* (depth - i.scrPos.w ) ) ;// foam line by comparing depth and screenposition
  100.                 col *= _Color;
  101.                 col += (step(0.4 * distortx,foamLine) * _FoamC); // add the foam line and tint to the texture
  102.                 col = saturate(col) * col.a ;
  103.                
  104.                ripples = step(0.99, ripples * 3);
  105.                float4 ripplesColored = ripples * _FoamC;
  106.                
  107.                return   saturate(col + ripplesColored);
  108.             }
  109.             ENDCG
  110.         }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement