Skip to content

Instantly share code, notes, and snippets.

@pencilking2002
Created February 17, 2020 00:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pencilking2002/cfb45a0274ce02987114441b4cf5983c to your computer and use it in GitHub Desktop.
Save pencilking2002/cfb45a0274ce02987114441b4cf5983c to your computer and use it in GitHub Desktop.
Shader "Custom/DynamicWater"
{
Properties
{
_Tint("Tint", Color) = (1, 1, 1, .5)
_MainTex("Main Texture", 2D) = "white" {}
_HighlightTex ("Highlight Tex", 2D) = "black" {}
_NoiseTex("Extra Wave Noise", 2D) = "white" {}
_FoamColor("Foam Color", Color) = (1,1,1,1)
_Foam("Foamline Thickness", Range(0,3)) = 0.5
_HighlightStrength("HighlightSrength", Range(0,10)) = 1
_HighlightScale ("HighlightScale", Range(0.1,10)) = 1
_ScrollSpeed1 ("ScrolSpeed1", Range(0,1)) = 1
_ScrollSpeed2 ("ScrolSpeed2", Range(0,1)) = 1
_Speed("Wave Speed", Range(0,10)) = 0.5
_Amount("Wave Amount", Range(0,1)) = 0.5
_Height("Wave Height", Range(0,1)) = 0.5
_DepthPower("Depth Power", Float) = 8
_DepthFactor("Depth Factor", Float) = 60
}
SubShader
{
Tags { "RenderType"="Opaque" "Queue" = "Transparent" }
LOD 100
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog alpha:on
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float2 uv2 : TEXCOORD3;
};
struct v2f
{
float2 uv : TEXCOORD0;
float2 uv2 : TEXCOORD3;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
float4 scrPos : TEXCOORD1;//
};
float4 _Tint;
uniform sampler2D _CameraDepthTexture; //Depth Texture
sampler2D _MainTex, _NoiseTex;//
sampler2D _HighlightTex;
half _HighlightStrength;
float4 _MainTex_ST;
float _Speed, _Amount, _Height, _Foam;//
half _ScrollSpeed1;
half _ScrollSpeed2;
half _HighlightScale;
half4 _FoamColor;
half _DepthPower;
half _DepthFactor;
v2f vert (appdata v)
{
v2f o;
UNITY_INITIALIZE_OUTPUT(v2f, o);
float4 tex = tex2Dlod(_NoiseTex, float4(v.uv.xy, 0, 0));//extra noise tex
v.vertex.y += sin(_Time.z * _Speed + (v.vertex.x * v.vertex.z * _Amount * tex)) * _Height;//movement
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.scrPos = ComputeScreenPos(o.vertex); // grab position on screen
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
half4 noiseTex = tex2D(_NoiseTex, i.uv -_Time.x * _ScrollSpeed2); //extra noise tex
half4 col = tex2D(_MainTex, i.uv - _Time.x * _ScrollSpeed1) * _Tint;// texture times tint;
col *= _Tint;
half2 hUV = float2((i.uv.x + _Time.x * _ScrollSpeed2), i.uv.y);
half4 highlight = tex2D(_HighlightTex, hUV * _HighlightScale);
highlight += noiseTex;
highlight = lerp(highlight, highlight * noiseTex, 0.5);
half depth = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.scrPos))); // depth
half4 foamLine = 1-saturate(_Foam * (depth - i.scrPos.w));// foam line by comparing depth and screenposition
//foamLine *= lerp(foamLine, highlight * 5.0, highlight);
//foamLine = lerp(foamLine, step(0.1, foamLine.r), 0.1);
foamLine *= (_FoamColor * 1.1) * highlight * 5.0;
foamLine = pow(foamLine, 15.0);
highlight.rgb = lerp(highlight.rgb, step(0.99, highlight.r), noiseTex.r);
col += foamLine; // add the foam line and tint to the texture
col += highlight * _HighlightStrength;
half waterDepth = pow((depth - i.scrPos.w) / _DepthFactor, _DepthPower);
col.a = saturate(waterDepth);
return col;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment