// MiniGun UI
#version 450


// Vk3DVision (http://3dsurroundgaming.com/) - Stereo Injection
layout(set = 0, binding = 30, std140) uniform Vk3DParams
{
    vec4 stereo;
    vec4 custom_params;
} vk3d_params;

layout(set = 0, binding = 0, std140) uniform freqHigh_vertexUniforms_ubo
{
    vec4 mvpmatrixx;
    vec4 mvpmatrixy;
    vec4 mvpmatrixz;
    vec4 mvpmatrixw;
    vec4 modelmatrixx;
    vec4 modelmatrixy;
    vec4 modelmatrixz;
    vec4 particleuberparms1;
} freqHigh_vertexUniforms;

layout(set = 0, binding = 1, std140) uniform freqLow_vertexUniforms_ubo
{
    vec4 fogparms;
    vec4 fogparms2;
    vec4 globalvieworigin;
    vec4 fogparms1;
    vec4 foglightinscatterdirection;
    vec4 particlestscalebias;
} freqLow_vertexUniforms;

layout(location = 0) in vec4 in_Position;
layout(location = 2) in vec4 in_Normal;
layout(location = 9) in vec4 in_Tangent;
layout(location = 8) out vec4 vofi_TexCoord6;
layout(location = 2) out vec4 vofi_TexCoord0;
layout(location = 1) in vec2 in_TexCoord;
layout(location = 4) in vec2 in_TexCoord1;
layout(location = 3) out vec4 vofi_TexCoord1;
layout(location = 3) in vec4 in_Color;
layout(location = 0) out vec4 vofi_Color;
layout(location = 4) out vec4 vofi_TexCoord2;
layout(location = 12) in vec2 in_VmtrTC;
layout(location = 6) out vec4 vofi_TexCoord4;

float _65;

float dot4(vec4 a, vec4 b)
{
    return dot(a, b);
}

float saturate(float v)
{
    return clamp(v, 0.0, 1.0);
}

float SRGBlinear(float value)
{
    if (value <= 0.040449999272823333740234375)
    {
        return value / 12.9200000762939453125;
    }
    else
    {
        return pow((value / 1.05499994754791259765625) + 0.0521326996386051177978515625, 2.400000095367431640625);
    }
}

vec3 SRGBlinear(vec3 sRGB)
{
    float param = sRGB.x;
    vec3 outLinear;
    outLinear.x = SRGBlinear(param);
    float param_1 = sRGB.y;
    outLinear.y = SRGBlinear(param_1);
    float param_2 = sRGB.z;
    outLinear.z = SRGBlinear(param_2);
    return outLinear;
}

vec4 SRGBlinear(vec4 sRGBA)
{
    vec3 param = sRGBA.xyz;
    vec4 outLinear = vec4(SRGBlinear(param), 1.0);
    float param_1 = sRGBA.w;
    outLinear.w = SRGBlinear(param_1);
    return outLinear;
}

vec4 DeGamma(vec4 color)
{
    vec4 param = color;
    return SRGBlinear(param);
}

void main()
{
    float modeLitParticle = 1.0;
    vec4 param = in_Position;
    vec4 param_1 = freqHigh_vertexUniforms.mvpmatrixx;
    gl_Position.x = dot4(param, param_1);
    vec4 param_2 = in_Position;
    vec4 param_3 = freqHigh_vertexUniforms.mvpmatrixy;
    gl_Position.y = dot4(param_2, param_3);
    vec4 param_4 = in_Position;
    vec4 param_5 = freqHigh_vertexUniforms.mvpmatrixz;
    gl_Position.z = dot4(param_4, param_5);
    vec4 param_6 = in_Position;
    vec4 param_7 = freqHigh_vertexUniforms.mvpmatrixw;
    gl_Position.w = dot4(param_6, param_7);
    vec4 param_8 = vec4(in_Position.xyz, 1.0);
    vec4 param_9 = freqHigh_vertexUniforms.modelmatrixx;
    vec3 worldPos;
    worldPos.x = dot4(param_8, param_9);
    vec4 param_10 = vec4(in_Position.xyz, 1.0);
    vec4 param_11 = freqHigh_vertexUniforms.modelmatrixy;
    worldPos.y = dot4(param_10, param_11);
    vec4 param_12 = vec4(in_Position.xyz, 1.0);
    vec4 param_13 = freqHigh_vertexUniforms.modelmatrixz;
    worldPos.z = dot4(param_12, param_13);
    float emissiveMult = (in_Normal.x * 10.0) + 1.0;
    float alphaMul = (in_Normal.y * 10.0) + 1.0;
    float alphaFade = in_Normal.w;
    float eyeDist = gl_Position.w;
    bool facingCamera = in_Tangent.y > 0.0;
    float frameFrac = in_Tangent.z;
    float depthAlphaMul = in_Tangent.w;
    float FLT_EPSILON = 9.9999997473787516355514526367188e-05;
    float heightFogAmount = 0.0;
    float inscatterAlpha = 0.0;
    float param_14 = exp2(-(clamp(gl_Position.w - freqLow_vertexUniforms.fogparms.x, 0.0, freqLow_vertexUniforms.fogparms.y) * freqLow_vertexUniforms.fogparms.z));
    float distanceFogAmount = 1.0 - saturate(param_14);
    if (freqLow_vertexUniforms.fogparms2.z < 1.0)
    {
        vec3 cameraToPixel = worldPos - freqLow_vertexUniforms.globalvieworigin.xyz;
        float distanceToPixel = length(cameraToPixel);
        float be = freqLow_vertexUniforms.fogparms2.x * exp2((-freqLow_vertexUniforms.fogparms1.w) * (freqLow_vertexUniforms.globalvieworigin.z - freqLow_vertexUniforms.fogparms1.x));
        float distanceInFog = max(distanceToPixel - freqLow_vertexUniforms.fogparms2.y, 0.0);
        float effectiveZ = max(cameraToPixel.z, FLT_EPSILON);
        if (cameraToPixel.z < (-FLT_EPSILON))
        {
            effectiveZ = cameraToPixel.z;
        }
        float lineIntegral = be * distanceInFog;
        lineIntegral *= ((1.0 - exp2((-freqLow_vertexUniforms.fogparms1.w) * effectiveZ)) / (freqLow_vertexUniforms.fogparms1.w * effectiveZ));
        float param_15 = exp2(-lineIntegral);
        heightFogAmount = 1.0 - max(saturate(param_15), freqLow_vertexUniforms.fogparms2.z);
        float cosL = dot(freqLow_vertexUniforms.foglightinscatterdirection.xyz, normalize(cameraToPixel));
        float param_16 = (cosL - freqLow_vertexUniforms.fogparms1.y) * freqLow_vertexUniforms.fogparms1.z;
        float blendValue = saturate(param_16);
        inscatterAlpha = 1.0 - (blendValue * blendValue);
    }
    vofi_TexCoord6.x = distanceFogAmount;
    vofi_TexCoord6.y = heightFogAmount;
    vofi_TexCoord6.z = inscatterAlpha;
    vofi_TexCoord6.w = 0.0;
    float thickness = ((1.0 - depthAlphaMul) * 127.0) / eyeDist;
    vec2 _376 = (in_TexCoord * freqLow_vertexUniforms.particlestscalebias.x) + vec2(freqLow_vertexUniforms.particlestscalebias.y);
    vofi_TexCoord0 = vec4(_376.x, _376.y, vofi_TexCoord0.z, vofi_TexCoord0.w);
    vec2 _387 = (in_TexCoord1 * freqLow_vertexUniforms.particlestscalebias.x) + vec2(freqLow_vertexUniforms.particlestscalebias.y);
    vofi_TexCoord0 = vec4(vofi_TexCoord0.x, vofi_TexCoord0.y, _387.x, _387.y);
    vofi_TexCoord1.x = emissiveMult;
    vofi_TexCoord1.y = depthAlphaMul;
    vofi_TexCoord1.z = frameFrac;
    vofi_TexCoord1.w = alphaFade;
    float param_17 = abs(eyeDist) / ((1.0 - depthAlphaMul) * 127.0);
    float particleFade = saturate(param_17);
    vec4 param_18 = in_Color;
    vec4 vtxcol = DeGamma(param_18);
    vofi_Color = vec4(vtxcol.xyz.x, vtxcol.xyz.y, vtxcol.xyz.z, vofi_Color.w);
    float _423;
    if (freqHigh_vertexUniforms.particleuberparms1.x == modeLitParticle)
    {
        _423 = 1.0;
    }
    else
    {
        _423 = 0.0;
    }
    vofi_Color.w = (particleFade * vtxcol.w) * mix(1.0, alphaMul, _423);
    if (facingCamera == true)
    {
        float opacityMin = 0.0039215688593685626983642578125;
        float opacityScale = 1.0 / (1.0 - opacityMin);
        if (vofi_Color.w < opacityMin)
        {
            gl_Position = vec4(vec3(0.0).x, vec3(0.0).y, vec3(0.0).z, gl_Position.w);
        }
        vofi_Color.w = (vofi_Color.w - opacityMin) * opacityScale;
    }
    vofi_TexCoord2 = vec4(worldPos, max(emissiveMult - 1.0, 0.0));
    vec2 tc_atlas = vec2(in_VmtrTC.x, in_VmtrTC.y);
    float atlasY = 1.0 - tc_atlas.y;
    vofi_TexCoord4 = vec4(tc_atlas.x, atlasY, alphaMul, 0.0);
    gl_Position.y = -gl_Position.y;

// Vk3DVision (http://3dsurroundgaming.com/) - Stereo Injection
if (vk3d_params.custom_params.z != 0)
		{
		  if(gl_Position.w > vk3d_params.custom_params.z)
		      gl_Position.x += vk3d_params.stereo.x * (gl_Position.w - vk3d_params.stereo.y);
		  else
		      gl_Position.x += vk3d_params.stereo.x * (gl_Position.w - vk3d_params.stereo.y * 0.65) * 0.75;
		}
		else
		{
		    gl_Position.x += vk3d_params.stereo.x * (gl_Position.w - vk3d_params.stereo.y);
		}
}


