#include "IOParam/IODefine_VS.hls"
#include "../FogOfWar/FogOfWar_VS.hls"
#include "../Lighting/Lighting.hls"
#include "unpack.hls"
#include "skinning.hls"


/////////////////////////////////////////////////////////////////////////////////////
///
///
///
/////////////////////////////////////////////////////////////////////////////////////
void main
(
    float3		vPos : X3D_VSIN_POSITION,
    float3		vNormal : X3D_VSIN_NORMAL,
    float2		vUV1 : X3D_VSIN_TEXCOORD0,
    blendindex4	vBlendIndices : X3D_VSIN_BLENDINDICES,
    float4		vBlendWeight : X3D_VSIN_BLENDWEIGHT,
    out float4	vPosOut : POSITION,
    out float4	vColorOut : COLOR0,
    out float4	vColorOut1 : COLOR1,
    out float2	vTexOut1 : TEXCOORD0,
    out float2  out_texcoord4 : TEXCOORD1
#ifndef DISABLE_SHADOWS
    , out float3	vTexSM : TEXCOORD2
#endif
#ifdef	SEPARATE_ALPHA_UV
    , out float2	vAlphaUV : TEXCOORD3
#endif
#ifdef REFLECTIVE
    , out float4	out_reflectRay : TEXCOORD4
    , out float3	out_worldNormal : TEXCOORD5
    , out float3	vReflTint : TEXCOORD6
#endif

)
{
#if EXPERIMENTAL_UNPACK_VERTS
    UnpackAttributes(vBlendWeight, vNormal, vUV1);     // unpack fixed point attributes
#endif

    float3 worldPosition;
    float3 worldNormal;
    SkinPositionAndNormal(vPos, vNormal, vBlendIndices, vBlendWeight, worldPosition, worldNormal);

    // Output homogeneous position
    vPosOut = mul(float4(worldPosition.xyz, 1.0f), mProj);

    // Output texture coordinate using texture transform
    vTexOut1.xy = vUV1 * vUVTransform.xy + vUVTransform.zw;

#ifdef SEPARATE_ALPHA_UV
    vAlphaUV = vUV1;
#endif

#ifndef DISABLE_SHADOWS
    // Output shadow position
    vTexSM = mul(float4(worldPosition.xyz, 1.0f), mShadowProj).xyz;
#endif

    Lighting(worldPosition, worldNormal, vColorOut, vColorOut1);

    // Fog of war
    out_texcoord4.xy = FogOfWarTextureCoordinates(worldPosition.xyz).xy;


#ifdef REFLECTIVE
    float fresnelPower = vReflection.x;
    float3 eyeRay = normalize(worldPosition.xyz - vCamera.xyz);
    out_worldNormal = worldNormal.xyz;


    out_reflectRay.xyz = reflect(eyeRay, out_worldNormal);

    //store the fresnel factor in the w component of the reflectRay. This is done in the vertex shader, so a proper power can be used.
    //base fresnel is the 0 to 1 reflection intensity based on the fresnel power.
    float baseFresnel = 1.0f - pow(saturate(dot(-eyeRay, worldNormal.xyz)), fresnelPower);
    //the final scaled result factors in the minimum and maximum reflection intensities supplied by the artists.
    out_reflectRay.w = lerp(vReflection.y, vReflection.z, baseFresnel);
    //the view dependent reflection color is also keyed off the fresnel.
    vReflTint = lerp(1.0f, vReflectionFColor, out_reflectRay.w);
#endif

}
