
#include "metalShaderTypes.h"
#include "alpha.h"

vertex vertexOut vertexFunc(uint vertexID [[ vertex_id ]],const device txShaderVers* in[[ buffer(0) ]],constant txShaderUniforms& uniforms[[ buffer(3) ]])
{
    vertexOut out;

    out.normPos=in[vertexID].pos.xy;


    float zTimer=0.;
    float xTimer=0.;
    float xScale=1.;
    float rTimer=0.;
    if(uniforms.transitionState==0)
    {
        rTimer=3.1415926535897932384626433832795*-.2+uniforms.transitionProgress*3.1415926535897932384626433832795*.2;
        xTimer=3.1415926535897932384626433832795-uniforms.transitionProgress*3.1415926535897932384626433832795;
        zTimer=uniforms.transitionProgress*3.1415926535897932384626433832795;
        xScale=-1.;
    }
    else if(uniforms.transitionState==2)
    {
        rTimer=uniforms.transitionProgress*3.1415926535897932384626433832795*.2;
        xTimer=uniforms.transitionProgress*3.1415926535897932384626433832795;
        zTimer=3.1415926535897932384626433832795-uniforms.transitionProgress*3.1415926535897932384626433832795;
        xScale=1.;
    }

    mat4 rotation = mat4(
                         cos(rTimer),  0.0,-sin(rTimer), 0.0,
                         0.0,  1.0,        0.0, 0.0,
                         sin(rTimer),  0.0, cos(rTimer), 0.0,
                         0.0,  0.0,        0.0, 1.0);
    mat4 projection = mat4(
                           2.4140,0.0000,0.0000,0.0000,
                           0.0000,2.4140,0.0000,0.0000,
                           0.0000,0.0000,-1.020,-2.002,
                           0.0000,0.0000,-1.000,0.0000);

    float zvalue=cos(zTimer)*-2.5-4.9;
    float xvalue=sin(xTimer)*xScale;
    mat4 translation=mat4(
                          1.0, 0.0, 0.0, 0.00,
                          0.0, 1.0, 0.0, 0.00,
                          0.0, 0.0, 1.0, -0.014,
                          0.0, 0.0, 0.0, 1.00);


    vec4 tmpPos=vec4(in[vertexID].pos.xyz,1.0)*uniforms.worldViewProj;


    translation[0][3]+=xvalue;
    translation[2][3]+=zvalue;

    vec4 worldPos=tmpPos*rotation;
    worldPos*=translation;

    out.position=worldPos*projection;

    out.tex1=in[vertexID].tex1;
    out.tex2=in[vertexID].tex2;
    out.texMask=in[vertexID].texMask;
    return out;
}

fragment float4 fragmentFunc(vertexOut input [[stage_in]]
                             ,texture2d<half> inputTex0 [[ texture(0) ]]
                             ,texture2d<half> inputTex1 [[ texture(1) ]]
                             ,texture2d<half> inputTexMask [[ texture(2) ]]
                             ,constant txShaderUniforms& u[[ buffer(2) ]]
                             ,constant subFXVars& subVars[[buffer(3)]])
{
    float4 outColor = getColor(inputTex0, input.tex1);
    float alpha = u.baseAlpha;
#ifdef USE_MASK
    alpha *= float(inputTexMask.sample(linearSampler, input.texMask).a);
#endif
    multiplyOpacity(outColor, alpha);
    return outColor;
}
