Skip to content
Snippets Groups Projects
WaterShader.shader 3.27 KiB
Newer Older
Liam Robinson's avatar
Liam Robinson committed
Shader "Custom/WaterShader"
{
    Properties
    {
        _DeepColor ("Deep Color", Color) = (1,1,1,1)
        _ShallowColor("Shallow Color", Color) = (1,1,1,1)
Liam Robinson's avatar
Liam Robinson committed
        _CausticsTex ("Caustics Texture", 2D) = "white" {}
        _WavesTex("Waves Texture", 2D) = "white" {}
        _WaveSize("Wave Size", Range(-5,5)) = 0.5
        _WaveSpeed("Wave Speed", Vector) = (0.5, 0.5, 0 ,0)
        _WaveSpeed2("Wave Speed 2", Vector) = (0.5, 0.5, 0 ,0)
        [HideInInspector]_ReflectionTex("Reflection", 2D) = "white" {}
    }
        Tags { "Queue" = "Transparent" "RenderType" = "Transparent" }
Liam Robinson's avatar
Liam Robinson committed
        LOD 2
        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard fullforwardshadows vertex:vert alpha:fade

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        fixed4 _DeepColor;
        fixed4 _ShallowColor;
Liam Robinson's avatar
Liam Robinson committed
        sampler2D _CausticsTex;
        sampler2D _WavesTex;
		sampler2D _ReflectionTex;
        sampler2D _CameraDepthTexture;
Liam Robinson's avatar
Liam Robinson committed
        float _WaveSize;
        fixed2 _WaveSpeed;
        fixed4 _WaveSpeed2;

        //surf input
        struct Input
        {
            float2 uv_CausticsTex;
			float4 screenPos;
            float4 screenPosition : TEXCOORD2;
        void vert (inout appdata_full v, out Input o) {
            UNITY_INITIALIZE_OUTPUT(Input, o);
Liam Robinson's avatar
Liam Robinson committed
            float4 texUV = v.texcoord;
            texUV.xy = (texUV.xy + _WaveSpeed * _Time.y);
            float4 texUV2 = v.texcoord;
            texUV.xy = (texUV.xy + _WaveSpeed2.xy * _Time.y);
            float height = min(tex2Dlod(_WavesTex, texUV).r, tex2Dlod(_WavesTex, texUV2).r);
            v.vertex.xyz += v.normal * (_WaveSize * height) - (_WaveSize / 2) + 0.02;
            o.screenPosition = ComputeScreenPos(UnityObjectToClipPos(v.vertex));
Liam Robinson's avatar
Liam Robinson committed
        }

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            // Albedo comes from a texture tinted by color
            //fixed4 albC = _DeepColor;
            //o.Albedo = albC.rgb;
Liam Robinson's avatar
Liam Robinson committed

            // Caustics
            float2 texUV = IN.uv_CausticsTex;
Liam Robinson's avatar
Liam Robinson committed
            texUV.xy = (texUV.xy + _WaveSpeed * _Time.y);
            float2 texUV2 = IN.uv_CausticsTex;
Liam Robinson's avatar
Liam Robinson committed
            fixed2 uv = IN.uv_CausticsTex * _WaveSpeed * _Time.y;
            texUV2.xy = (texUV2.xy + _WaveSpeed2 * _Time.y);
            fixed3 caustic1 = tex2D(_CausticsTex, texUV).rgb;
            fixed3 caustic2 = tex2D(_CausticsTex, texUV2).rgb;;
            
Liam Robinson's avatar
Liam Robinson committed

            // Metallic and smoothness come from slider variables
            o.Metallic = 0;
            o.Smoothness = 1;
            float2 screenUV = IN.screenPos.xy / IN.screenPos.w;
            screenUV.x = 1 - screenUV.x;
            float existingDepth01 = tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(IN.screenPosition)).r;
            float existingDepthLinear = LinearEyeDepth(existingDepth01);
            float depthDifference = existingDepthLinear - IN.screenPosition.w;
            fixed3 causticrgb = caustic1 * caustic2 * depthDifference;
            o.Albedo = (_DeepColor + causticrgb)/2;
            o.Alpha = .6;
Liam Robinson's avatar
Liam Robinson committed
            //Add reflections
			//o.Albedo *= (tex2D(_ReflectionTex, screenUV).rgb);
Liam Robinson's avatar
Liam Robinson committed
        }
        ENDCG
    }
    FallBack "Diffuse"
}