 varying vec4 v_color;
 varying float v_width;
+
+float getPointOnLine(vec2 p0, vec2 p1, float x)
+{
+    float slope = (p0.y - p1.y) / (p0.x - p1.x);
+    return slope * (x - p0.x) + p0.y;
+}
 
 czm_material czm_getMaterial(czm_materialInput materialInput)
 {

    vec2 st = materialInput.st;
    
    float base = 1.0 - dFdx(st.s) * 10.0;
    vec2 upperBase = vec2(base, 1.0);
    vec2 lowerBase = vec2(base, 0.0);
    vec2 tip = vec2(1.0, 0.5);
    float upperSlope = (upperBase.y - tip.y) / (upperBase.x - tip.x);
    float lowerSlope = (lowerBase.y - tip.y) / (lowerBase.x - tip.x);
    
    float halfWidth = 0.15;
    float s = step(0.5 - halfWidth, st.t);

    s *= 1.0 - step(base, st.s);
    
    float t = step(base, materialInput.st.s);
    t *= 1.0 - step(ptOnUpperLine, st.t);
    t *= step(ptOnLowerLine, st.t);
    
    // Find the distance from the closest separator (region between two colors)
    float value;
    if (st.s < base)
    {
        float d1 = abs(st.t - (0.5 - halfWidth));
        float d2 = abs(st.t - (0.5 + halfWidth));
        value = min(d1, d2);
    }
    else
    {
        float d1 = czm_infinity;
        if (st.t < 0.5 - halfWidth && st.t > 0.5 + halfWidth)
        {
            d1 = abs(st.s - base);
        }
        float d2 = abs(st.t - ptOnUpperLine);
        float d3 = abs(st.t - ptOnLowerLine);
        value = min(min(d1, d2), d3);
    }
    
    //anti-aliasing
    const float fuzz = 0.1;
    float val1 = clamp(value / fuzz, 0.0, 1.0);
    float val2 = clamp((value - 0.5) / fuzz, 0.0, 1.0);
    val1 = val1 * (1.0 - val2);
    val1 = val1 * val1 * (3.0 - (2.0 * val1));
    val1 = pow(val1, 0.5); //makes the transition nicer
    
    vec4 outsideColor = vec4(0.0);
    vec4 midColor = (outsideColor + v_color) * 0.5;
    vec4 currentColor = mix(outsideColor, v_color, clamp(s + t, 0.0, 1.0));
    vec4 color = mix(midColor, currentColor, val1);
    
    material.diffuse = color.rgb;
    material.alpha = color.a;
    return material;
}
