Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Shader "SnailGirl/Frag_Water"
- {
- Properties
- {
- _Tint("Tint", Color) = (1, 1, 1, .5) //A tint for all the water
- _MainTex ("Main Texture", 2D) = "white" {} //Texture for underlying water
- _NoiseTex("Wavy Noise", 2D) = "white" {} //Extra noise for the waves
- _Speed("Water Speed", Range(0,1)) = 0.5 //How fast the water moves
- _Amount("Wave Amount", Range(0,1)) = 0.5 //How many waves
- _Height("Wave Height", Range(0,1)) = 0.5 //How high the waves get
- _Foam("Foam Thickness", Range(0,3)) = 0.5 //how thick the foam is on the edges
- _TopTex ("Top Texture (RGB)", 2D) = "white" {} //top texture (painted foam lines for example)
- _Distort ("Distortion", Range(1, 30)) = 1 //How much the top texture will be distorted in the waves
- }
- SubShader
- {
- Tags { "RenderType"="Opaque" "Queue" = "Transparent" }
- LOD 100
- Blend SrcAlpha OneMinusSrcAlpha
- Pass
- {
- CGPROGRAM
- #pragma vertex vert
- #pragma fragment frag
- #pragma multi_compile_fog //Keep this to make sure the water gets foggy if you use fog
- #pragma target 3.0
- #include "UnityCG.cginc"
- struct appdata
- {
- 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
- float2 uv : TEXCOORD0;
- };
- struct v2f
- {
- float2 uv : TEXCOORD0;
- UNITY_FOG_COORDS(1) //To make sure fog works when vertices start to move
- float4 vertex : SV_POSITION; //find vertex position in screen space
- float4 scrPos : TEXCOORD2; //pixel cordinates on the screen (in x and y)
- };
- float4 _Tint;
- uniform sampler2D _CameraDepthTexture; //the depth from the camera, which together with the Screen position will tell us where to put foam
- sampler2D _MainTex, _NoiseTex, _TopTex;
- float4 _MainTex_ST; //Set Texture from the main texture. built in
- float _Speed, _Amount, _Height, _Foam, _Distort;
- v2f vert (appdata v)
- {
- //MOVING WAVES
- v2f o;
- float4 tex = tex2Dlod(_NoiseTex, float4(0, v.uv.xy, 0)); //Get the noise texture to be able to influence the vertices
- 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
- o.vertex = UnityObjectToClipPos(v.vertex); //We take the vertex point and convert in to a point in camera clip space
- o.uv = TRANSFORM_TEX(v.uv, _MainTex); //Scale and offset the texture (Which is why we needed the _MainTex_ST)
- o.scrPos = ComputeScreenPos(o.vertex); //get the pixels positions in the current screen position
- UNITY_TRANSFER_FOG(o,o.vertex); //again, just to make fog work when the vertices move
- return o;
- }
- fixed4 frag (v2f i) : SV_Target
- {
- //ANIMATED FOAM ON WATER SURFACE
- fixed2 animateUV = i.uv; //The uv we want animated
- half noiseVal = tex2D(_NoiseTex, i.uv).r; //Get the noise texture as a value
- animateUV.x = animateUV.x + noiseVal * sin(_Time.y * _Speed) / _Distort; //Distort the uv by adding the noise in a sinewave motion
- animateUV.y = animateUV.y + noiseVal * sin(_Time.y * _Speed) / _Distort;
- fixed4 topTex = tex2D(_TopTex, animateUV); //Apply the distorted uv to the top texture
- half4 col = tex2D(_MainTex, i.uv) * _Tint; //tint the underlying water texture
- //FOAMLINE ON EDGES OF INTERSECTING OBJECTS
- col.rgb = lerp (col.rgb, topTex.rgb, topTex.a); //make smooth color transition
- half depth = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.scrPos))); //Get the depth from the camera
- 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)
- col += foamLine * _Tint; //Add tint to foam
- UNITY_APPLY_FOG(i.fogCoord, col); //Again, just to make fog work
- return col ;
- }
- ENDCG
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement