precision highp int;
precision highp float;
 #include<OgreUnifiedShader.h>;
float calculateHalfWidth_(float lineWidth, float lineGapWidth)
{
    return lineGapWidth == 0.0 ? lineWidth / 2.0 : lineGapWidth / 2.0 + lineWidth;
}
float calculateHalfWidth(float lineWidth, float lineGapWidth)
{
    return 1.0 + calculateHalfWidth_(lineWidth, lineGapWidth);
}
varying float vUV_x;
varying float vUV_y;
varying vec3 vLineData;
varying float vHalfWidth;
varying vec4 vHazeColor;
float antialiasedBorder(float value, float border)
{
    float epsilon = 0.2;
    float f = (value - border + epsilon) / (epsilon * 2.0);
    f = clamp(f, 0.0, 1.0);
    return f;
}
float calculateFactor(float distanceFromCenter, float lineGapWidth, float lineWidth)
{
    return lineGapWidth == 0.0
               ? antialiasedBorder(lineWidth / 2.0, distanceFromCenter)
               : antialiasedBorder(distanceFromCenter, lineGapWidth / 2.0) *
                     antialiasedBorder(lineGapWidth / 2.0 + lineWidth, distanceFromCenter);
}
varying float vDistance;
varying float vUV_yScaled;
float calculateDashFactor(float lineDash, float lineDashGap, float lineWidth)
{
    float dashPatternLength = (lineDash + lineDashGap);
    float depth = log2(max(1e-6, vDistance * lineWidth));
    float logDepth = pow(2.0, floor(depth));
    float uvy = vUV_yScaled / logDepth;
    uvy += (dashPatternLength * 0.5);
    float normalizedPatternPosition = fract(uvy / dashPatternLength);
    return step(lineDashGap / dashPatternLength, normalizedPatternPosition);
}
vec4 calculateColor(float distanceFromCenter, vec3 lineData, vec4 lineColor, float lineDash,
                    float lineDashGap, float opacityMultiplier)
{
    float lineWidth = lineData[0];
    float lineGapWidth = lineData[1];
    float lineOpacity = lineData[2];
    vec4 color = lineColor;
    color.a *= lineOpacity * opacityMultiplier;
    float lineFactor = calculateFactor(distanceFromCenter, lineGapWidth, lineWidth);
    color.a *= lineFactor;
    float lineDashFactor = calculateDashFactor(lineDash, lineDashGap, lineWidth);
    color.a *= lineDashFactor;
    return color;
}
uniform vec4 lineColor;
uniform float lineDash;
uniform float lineDashGap;
uniform float opacityMultiplier;
uniform float visibleDistance;
uniform float fadeOutLength;
void main()
{
    float distanceFromCenter = abs(vUV_x - 0.5) / 0.5 * vHalfWidth;
    float vertexDistance = vUV_y - 1.0;
    if ((visibleDistance >= 0.0) && (vertexDistance >= visibleDistance))
    {
        gl_FragColor = vec4(0.0);
        return;
    }
    vec4 baseColor = lineColor;
    vec4 color = calculateColor(distanceFromCenter, vLineData, baseColor, lineDash, lineDashGap,
                                opacityMultiplier);
    color.rgb = mix(color.rgb, vHazeColor.rgb, vHazeColor.a);
    color.rgb *= color.a;
    gl_FragColor = color;
}
