Advertisement
SnailGirlDev

Water Shader

Sep 12th, 2018
1,288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.60 KB | None | 0 0
  1. Shader "SnailGirl/Frag_Water"
  2. {
  3.     Properties
  4.     {
  5.         _Tint("Tint", Color) = (1, 1, 1, .5) //A tint for all the water
  6.         _MainTex ("Main Texture", 2D) = "white" {} //Texture for underlying water
  7.         _NoiseTex("Wavy Noise", 2D) = "white" {} //Extra noise for the waves
  8.         _Speed("Water Speed", Range(0,1)) = 0.5 //How fast the water moves
  9.         _Amount("Wave Amount", Range(0,1)) = 0.5 //How many waves
  10.         _Height("Wave Height", Range(0,1)) = 0.5 //How high the waves get
  11.         _Foam("Foam Thickness", Range(0,3)) = 0.5 //how thick the foam is on the edges
  12.        _TopTex ("Top Texture (RGB)", 2D) = "white" {} //top texture (painted foam lines for example)
  13.         _Distort ("Distortion", Range(1, 30)) = 1 //How much the top texture will be distorted in the waves
  14.     }
  15.     SubShader
  16.     {
  17.         Tags { "RenderType"="Opaque"  "Queue" = "Transparent" }
  18.         LOD 100
  19.         Blend SrcAlpha OneMinusSrcAlpha
  20.  
  21.         Pass
  22.         {
  23.             CGPROGRAM
  24.             #pragma vertex vert
  25.             #pragma fragment frag
  26.             #pragma multi_compile_fog //Keep this to make sure the water gets foggy if you use fog
  27.             #pragma target 3.0
  28.  
  29.             #include "UnityCG.cginc"
  30.  
  31.             struct appdata
  32.             {
  33.                 float4 vertex : POSITION; //We need to know the vertex position to be able to tell where things are in the mesh. That way we can make movement
  34.                 float2 uv : TEXCOORD0;
  35.             };
  36.  
  37.             struct v2f
  38.             {
  39.                 float2 uv : TEXCOORD0;
  40.                 UNITY_FOG_COORDS(1) //To make sure fog works when vertices start to move
  41.                 float4 vertex : SV_POSITION; //find vertex position in screen space
  42.                 float4 scrPos : TEXCOORD2; //pixel cordinates on the screen (in x and y)
  43.             };
  44.  
  45.             float4 _Tint;
  46.             uniform sampler2D _CameraDepthTexture; //the depth from the camera, which together with the Screen position will tell us where to put foam
  47.             sampler2D _MainTex, _NoiseTex, _TopTex;
  48.             float4 _MainTex_ST; //Set Texture from the main texture. built in
  49.             float _Speed, _Amount, _Height, _Foam, _Distort;
  50.            
  51.             v2f vert (appdata v)
  52.             {
  53.                 //MOVING WAVES
  54.                 v2f o;
  55.                 float4 tex = tex2Dlod(_NoiseTex, float4(0, v.uv.xy, 0)); //Get the noise texture to be able to influence the vertices
  56.                 v.vertex.y += sin(_Time.z * _Speed + (v.vertex.x * v.vertex.z * _Amount * tex)) * _Height; //SineWave, which is a curve back and forth, to move the vertices like a wave. We move in x and z, times the amount and the noise texture, for wobbly waves
  57.                 o.vertex = UnityObjectToClipPos(v.vertex); //We take the vertex point and convert in to a point in camera clip space
  58.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex); //Scale and offset the texture (Which is why we needed the _MainTex_ST)
  59.                 o.scrPos = ComputeScreenPos(o.vertex); //get the pixels positions in the current screen position
  60.                 UNITY_TRANSFER_FOG(o,o.vertex); //again, just to make fog work when the vertices move
  61.                
  62.                 return o;
  63.             }
  64.            
  65.             fixed4 frag (v2f i) : SV_Target
  66.             {
  67.                 //ANIMATED FOAM ON WATER SURFACE
  68.                 fixed2 animateUV = i.uv; //The uv we want animated
  69.                 half noiseVal = tex2D(_NoiseTex, i.uv).r; //Get the noise texture as a value
  70.                 animateUV.x = animateUV.x + noiseVal * sin(_Time.y * _Speed) / _Distort; //Distort the uv by adding the noise in a sinewave motion
  71.                 animateUV.y = animateUV.y + noiseVal * sin(_Time.y * _Speed) / _Distort;
  72.  
  73.                 fixed4 topTex = tex2D(_TopTex, animateUV); //Apply the distorted uv to the top texture
  74.                 half4 col = tex2D(_MainTex, i.uv) * _Tint; //tint the underlying water texture
  75.  
  76.                 //FOAMLINE ON EDGES OF INTERSECTING OBJECTS
  77.                 col.rgb = lerp (col.rgb, topTex.rgb, topTex.a); //make smooth color transition
  78.                 half depth = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.scrPos))); //Get the depth from the camera
  79.                 half4 foamLine = saturate( 1 -_Foam * (depth - i.scrPos.w)); //Compare the depth to the screen position to see where things intersect (1 - foam is to only put foam on the edges, so everything minus the foam leaves a nice line)
  80.                 col += foamLine * _Tint; //Add tint to foam
  81.  
  82.  
  83.                 UNITY_APPLY_FOG(i.fogCoord, col); //Again, just to make fog work
  84.                 return col ;
  85.             }
  86.             ENDCG
  87.         }
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement