<?xml version="1.0" encoding="utf-8"?>

<CustomShader version="1.0" classRequirement="">
    <LodLevel startDistance="0" subsequentPassForceMode="ignore">
        <Parameters>
            <Parameter name="bumpSpeedScale" target="In.mat.bumpSpeedScale" type="float3" />
            <Parameter name="reflectionAmount" target="In.mat.reflectionAmount" type="float3" />
            <Parameter name="underwaterFogColor" target="In.mat.underwaterFogColor" type="float3" />
            <Parameter name="distanceFogInfo" target="In.mat.distanceFogInfo" type="float4" />
        </Parameters>
        <CodeInjections>
            <CodeInjection position="NUM_TEXCOORDS">
<![CDATA[
// only allow directional lights, specular only
#define NUM_DIFFUSE_LIGHTS 0
#define NUM_POINT_LIGHTS 0
#define NUM_SPOT_LIGHTS 0

// for low spec no lights at all
//#ifdef LOWSPEC
#define NUM_DIRECTIONAL_LIGHTS 0
//#endif


#if (defined(REFLECTION_MAP_2D) || defined(REFLECTION_MAP_CUBE)) && defined(REFRACTION_MAP) && defined(NORMAL_MAP)
#define WATER_RENDERING
#endif
#if defined(WATER_RENDERING)
//#defeval WATER_NORMALMAP_TEXCOORD1 NEXT_TEXCOORD
#defeval WATER_NORMALMAP_TEXCOORD1 0
#defeval WATER_NORMALMAP_TEXCOORD2 NEXT_TEXCOORD
#defeval WATER_NORMALMAP_TEXCOORD3 NEXT_TEXCOORD

#if defined(DEPTH_BLEND_MAP)
#define WATER_DEPTH tex2D(In.samplers.depthBlendMap, In.vs.waterDistanceWaterPos.xy).x
#define WATER_DEPTH_VEC tex2D(In.samplers.depthBlendMap, In.vs.waterDistanceWaterPos.xy)
#else
#define WATER_DEPTH 1.0
#define WATER_DEPTH_VEC float4(0,0,0,0)
#endif
#endif
]]>
            </CodeInjection>
            <CodeInjection position="NEED_FS_EYE_VECTOR">
<![CDATA[
#if defined(WATER_RENDERING)
    #if !defined(_NEED_FS_EYE_VECTOR)
    #define _NEED_FS_EYE_VECTOR
    #endif
#endif
]]>
            </CodeInjection>
            <CodeInjection position="CONFIG_DEFINES">
<![CDATA[
#if defined(WATER_RENDERING)
#define _NEED_FS_EYE_VECTOR
#undef _NO_TANGENTSPACE
#define _HAS_TEXTURES
#endif
]]>
            </CodeInjection>
            <CodeInjection position="VS_OUTPUT">
<![CDATA[
#if defined(WATER_RENDERING)
    float2 waterDistanceWaterPos;
    //float distanceFogFactor;
#endif
]]>
            </CodeInjection>
            <CodeInjection position="MATERIALINFO">
<![CDATA[
#if defined(WATER_RENDERING)
    uniform float4 bumpSpeedScale;
    uniform float4 reflectionAmount;
    uniform float4 underwaterFogColor;
    uniform float4 distanceFogInfo;
#endif
]]>
            </CodeInjection>
            <CodeInjection position="POST_SET_TEXCOORDS_VS">
<![CDATA[
#if defined(WATER_RENDERING)
float textureScale = In.mat.bumpSpeedScale.z;
float2 bumpSpeed = In.mat.bumpSpeedScale.xy*textureScale;

//float2 translation=float2(fmod(In.time.y, 1.0/bumpSpeed.x)*bumpSpeed.x, 0);
float2 translation=float2(In.time.y*bumpSpeed.x, 0);
float2 translation2=float2(translation.x, In.time.y*bumpSpeed.y);

float2 texCoords=In.position.xz*textureScale;

setTexCoords(Out.texCoords[WATER_NORMALMAP_TEXCOORD1/2], WATER_NORMALMAP_TEXCOORD1, texCoords.xy+translation);
setTexCoords(Out.texCoords[WATER_NORMALMAP_TEXCOORD2/2], WATER_NORMALMAP_TEXCOORD2, texCoords.xy*2.0+translation*4.0);
setTexCoords(Out.texCoords[WATER_NORMALMAP_TEXCOORD3/2], WATER_NORMALMAP_TEXCOORD3, texCoords.xy*4.0-translation2);

//Out.waterDistanceWaterPos.x = Out.position.w;
//Out.waterDistanceWaterPos.yz = getTexCoords(In, NORMALMAP_INPUT_TEXCOORD).xy; //In.position.xz;
Out.waterDistanceWaterPos.xy = getTexCoords(In, NORMALMAP_INPUT_TEXCOORD).xy; //In.position.xz;

//Out.distanceFogFactor = calcFogFactor(length(viewDir.xyz), In.mat.distanceFogInfo.w, 2); // 2 = exp fog type
#endif
]]>
            </CodeInjection>
            <CodeInjection position="POST_ALBEDO_FS">
<![CDATA[
#if defined(WATER_RENDERING)
albedo = float4(0, 0, 0, 1);
diffuseLighting = float3(0,0,0);
#endif
]]>
            </CodeInjection>
            <CodeInjection position="GET_NORMAL_FS">
<![CDATA[
#if defined(WATER_RENDERING)
// sum normal maps
// sample from 3 different points so no texture repetition is noticeable
float3 t0 = tex2D(In.samplers.normalMap, getTexCoords(In, globals, WATER_NORMALMAP_TEXCOORD1)).xyz * 2.0 - 1.0;
float3 t1 = tex2D(In.samplers.normalMap, getTexCoords(In, globals, WATER_NORMALMAP_TEXCOORD2)).xyz * 2.0 - 1.0;
float3 t2 = tex2D(In.samplers.normalMap, getTexCoords(In, globals, WATER_NORMALMAP_TEXCOORD3)).xyz * 2.0 - 1.0;
return normalize((t0.xyz + t1.xyz + t2.xyz));
#endif
]]>
            </CodeInjection>
            <CodeInjection position="GET_REFRACTION_COLOR_TEX_FS">
<![CDATA[
#if defined(WATER_RENDERING)
    float depth = WATER_DEPTH;
    float2 finalCoord2 = In.vs.reflRefrCoord.xy/In.vs.reflRefrCoord.w;
    finalCoord2 += normal.xy*In.mat.reflectionRefractionCoeffScale[3]*depth;
    return tex2D(In.samplers.refractionMap, finalCoord2).xyz;
#endif
]]>
            </CodeInjection>
            <CodeInjection position="GET_REFRACTION_COLOR_FS">
<![CDATA[
#if defined(WATER_RENDERING)
    float softIntersect = WATER_DEPTH_VEC.x;

    float fresnel = getFresnel(facing, In.mat.reflectionRefractionCoeffScale[0], 5.0);
    fresnel = saturate(fresnel * In.mat.reflectionAmount.x);

    float3 refractionColor = getRefractionColorTex(In, globals, normal);
    // add some underwater fog
    //refractionColor = lerp(refractionColor, In.mat.underwaterFogColor.xyz, WATER_DEPTH_VEC.y);

    float3 reflectionColor = getReflectionColorTex(In, globals, normal);

    float3 specularLighting = oColor.xyz;
    oColor.xyz = lerp(refractionColor, reflectionColor, fresnel);

    oColor.xyz += specularLighting;

    oColor.xyz = lerp(refractionColor, oColor.xyz, softIntersect); //* saturate(In.vs.reflRefrCoord.w*0.8));

    return oColor.xyz;
#endif
]]>
            </CodeInjection>
            <CodeInjection position="GET_REFLECTION_COLOR_TEX_FS">
<![CDATA[
#if defined(WATER_RENDERING)
    float2 finalCoord2 = In.vs.reflRefrCoord.xy/In.vs.reflRefrCoord.w;
    finalCoord2 += normal.xy*In.mat.reflectionRefractionCoeffScale[1];
    return tex2D(In.samplers.reflectionMap, finalCoord2).xyz;
#endif
]]>
            </CodeInjection>
            <CodeInjection position="GET_REFLECTION_COLOR_FS">
<![CDATA[
#if defined(WATER_RENDERING)
    // reflection is already done in the refraction part
    return oColor;
#endif
]]>
            </CodeInjection>
            <CodeInjection position="FINAL_POS_FS">
<![CDATA[
#if defined(WATER_RENDERING)
    //oColor.xyz = lerp(In.mat.distanceFogInfo.xyz, oColor.xyz, In.vs.distanceFogFactor);
#endif

/*#if defined(WATER_RENDERING)
    oColor.xyz = WATER_DEPTH_VEC.x;
#endif*/
/*#if defined(WATER_RENDERING)
    oColor.xyz = In.vs.reflRefrCoord.w;
#endif*/
]]>
            </CodeInjection>
        </CodeInjections>
    </LodLevel>
</CustomShader>