Shader "Ocean/OceanMain_v1" 
{
	Properties 
	{
		_SurfaceColor ("SurfaceColor", Color) = (1,1,1,1)
		_WaterColor ("WaterColor", Color) = (1,1,1,1)
		_Color ("Main Color", Color) = (1,1,1,1)
		_SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
		_SunColor ("Sun Color", Color) = (0.5, 0.5, 0.5, 1)
		_SunPower ("Sun Intensity", Range (0,2)) = 0.25
		_Refraction ("Refraction (RGB)", 2D) = "white" {}
		_Reflection ("Reflection (RGB)", 2D) = "white" {}
		_Fresnel ("Fresnel (A) ", 2D) = "gray" {}
		_Bump ("Bump (RGB)", 2D) = "bump" {}
		_Foam ("Foam (RGB)", 2D) = "white" {}
		_Size ("Size", Vector) = (1, 1, 1, 1)
		_SunPosition ("Sun Position", Vector) = (1, 1, 1,1)
		_Transparency ("Transparency", Color) = (0.5, 0.5, 0.5, 1)
		_MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {}
		_Caustics ("Caustics", 2D) = "caustics" {}
		_RefractionLevel ("RefractionLevel", Float) = 0.05
		_BendA ("BendA", Float) = 0.05
		_BendB ("BendB", Float) = 0.05
}
	SubShader {
    Pass {

CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_fog_exp2
#pragma fragmentoption ARB_precision_hint_fastest 

#include "UnityCG.cginc"

uniform float3 _SunPosition;
uniform float4 _Bump_ST;

struct v2f 
{
    V2F_POS_FOG;
    float2  projTexCoord;
    float2  bumpTexCoord;
    float3  viewDir;
    float3  lightDir;
    float3  objSpaceNormal;
    float   foamStrength;
    float3  sampleOffset;
};

float4 _Size;

v2f vert (appdata_tan v)
{
    v2f o;
    
    //o.bumpTexCoord.xy = v.vertex.xz/float2(_Size.x, _Size.z);
    o.bumpTexCoord.xy = v.texcoord;
    o.foamStrength = v.tangent.w;    
    
    PositionFog( v.vertex, o.pos, o.fog );
    
    float3 cappedPos = mul( glstate.matrix.mvp, float4(v.vertex.x, max(v.vertex.y, 0.0), v.vertex.z, 1.0)).xyz;
    float3 clipPlanePos = mul( glstate.matrix.mvp, float4(v.vertex.x, 0.0, v.vertex.z, 1.0)).xyz;

	float4 tmpProj = mul( glstate.matrix.mvp, float4(v.vertex.x, 0.0, v.vertex.z, 1.0));
   
    //Bias matrix for converting clip-space vertex positions
    //to texture coordinates.
    float3x4 mat = float3x4(
    0.5, 0.0, 0.0, 0.5,
    0.0, 0.5 * _ProjectionParams.x, 0.0, 0.5,
    0.0, 0.0, 0.0, 1.0
    );
   
    o.projTexCoord.xy = mul(mat, tmpProj).xy;
    o.projTexCoord.xy /= tmpProj.w; 
	
    float3 objSpaceViewDir = ObjSpaceViewDir(v.vertex);
    
    //o.normal = v.normal;
    float3 binormal = cross( normalize(v.normal), normalize(v.tangent.xyz) );
	float3x3 rotation = float3x3( v.tangent.xyz, binormal, v.normal );
    
    o.objSpaceNormal = v.normal;
    o.viewDir = mul(rotation, objSpaceViewDir);
	
    o.lightDir = mul(rotation, -_SunPosition) ;
    //o.lightDir = mul(rotation, float3(0.3,-0.5,-1.0));
    
    return o;
}

sampler2D _Refraction;
sampler2D _Reflection;
sampler2D _Fresnel;
sampler2D _Bump;
sampler2D _MainTex;
sampler2D _Foam;
sampler2D _Caustics;
half4 _SurfaceColor;
half4 _WaterColor;
half4 _Transparency ;
half4 _SpecColor ;
half4 _Color ;
Float _RefractionLevel ;
half4 _SunColor ;
Float _SunPower;
Float _BendA;
Float _BendB;


half4 frag (v2f i) : COLOR
{
	half3 normViewDir = normalize(i.viewDir);
	half3 normViewDir2 = normViewDir*0.01;
	
	half4 buv = half4(i.bumpTexCoord.x + _Time.x * 0.03, i.bumpTexCoord.y + _SinTime.x * 0.2, i.bumpTexCoord.x + _Time.y * 0.04, i.bumpTexCoord.y + _SinTime.y * 0.5);

	half3 tangentNormal0 = (tex2D(_Bump, buv.xy).rgb *2) - 1;
	half3 tangentNormal1 = (tex2D(_Bump, buv.zw ).rgb *2) - 1;
	half3 tangentNormal = normalize(tangentNormal0 + tangentNormal1);
	
	
	half4 result = half4(0, 0, 0, 1);
	
	float2 bumpSampleOffset = i.objSpaceNormal.xz * 0.5 + tangentNormal.xy * _RefractionLevel;
	
	
	half3 reflection = tex2D(_Reflection, i.projTexCoord.xy + bumpSampleOffset - i.sampleOffset.xy).rgb * _SurfaceColor.rgb;
	half3	refraction = tex2D(_Refraction, i.projTexCoord.xy + bumpSampleOffset).rgb * float3(0.8, 0.9, 0.8) * _WaterColor.rgb;
	half3 diffuse = tex2D(_MainTex, buv.zw)*_Color;
	//half3 diffuse = 1;
	half3 caustics = tex2D(_Caustics, buv.xy)*_Color * _Transparency;
	float fresnelLookup = dot(tangentNormal, normViewDir);
	
	float fresnelTerm = tex2D(_Fresnel, float2(fresnelLookup, 0.5)).a*_SpecColor;
	half4 foam = clamp(tex2D(_Foam, i.bumpTexCoord.xy)  - 0.5, 0.0, 1.0) *  i.foamStrength * 2.8;

	
	float3 halfVec = normalize(normViewDir - normalize(i.lightDir));
    float sun = pow(max(dot(halfVec, tangentNormal ), 0.2), 200.0)*_SunPower*_SunPower;
    //float sun = 1.0;
    
	
	half3 ref= lerp(refraction , reflection , fresnelTerm)+ clamp(foam.r, 0.0, 1.0) ;
	half3 wcol = (diffuse * caustics );

	result.rgb=  wcol + ref  + (sun*_SunColor);
//	result.rgb=  wcol + ref;
	result.rgb = result.rgb;

    return result;
}
ENDCG

    }
}
FallBack "Self-Illumin/Specular", 1
}


