#include "dxShaderIncludes.h"
#include "alpha.h"
#include "textAnimationHelper.h"

typedef struct
{
    int direction;
    float easeInOut;
    float motionRamp;
    float motionSpeed;
} fxVars;

cbuffer variableBuffer : register(b2)
{
    fxVars vars;
}

vertexOut vertexShader(txShaderVerts input)
{
    vertexOut output;

    output.normPos = input.position.xy;

    float4 tmpPos = mul(u.worldViewProj, float4(input.position.xyz, 1.0));

    if (u.transitionState == 0)
    {
        float scale = mix(.16, 1.0, segmentStart[vars.direction]);
        tmpPos.xy *= scale;
    }
    else if (u.transitionState == 2)
    {
        tmpPos.xy *= (smoothstep(0.0, 1.0, 1.0 - segmentStart[vars.direction]) * 4.0 + 1.0);
    }

    tmpPos = handleContinuedMotion(tmpPos, u.localTime, vars.motionRamp, vars.motionSpeed);

    output.position = tmpPos;

    output.tex1 = input.tex1;
    output.tex2 = input.tex2;
    output.texMask = input.texMask;

    return output;
}

float4 pixelShader(vertexOut input) : SV_TARGET
{
    float4 outColor = getColor(inputTex0, input.tex1);
    float distance = segmentEnd[vars.direction] - segmentStart[vars.direction];
    float progress = segmentStart[vars.direction] + distance * input.normPos.x;
    float alpha = u.baseAlpha * progress;

#ifdef IS_MEDIA_FILL
    alpha *= outColor.a;
    outColor = float4(inputTex1.sample(linearSampler, input.tex2));
#endif

#ifdef USE_MASK
    alpha *= inputTexMask.Sample(linearSampler, input.texMask).a;
#endif

    multiplyOpacity(outColor, alpha);

#ifdef SHOW_SEGMENT_OUTLINE
    showSegmentOutline(outColor);
#endif

    return outColor;
}
