0

Sorry for such a long title.

This is a followup on my old question, which led me to drawing a beveled circle looking like this:

Old dodgy circle

If you look closely at this image, you can see the pixelation around the edge.

I wanted to smooth this circle with anti-aliasing, and I ended up with this:

New dodgy circle

If you look closely at this image, you can see a white border around the edge.

I'd like to remove this white border if possible, but my shader is a mess at this point:

#extension GL_OES_standard_derivatives : enable

void main(void) {

    lowp vec2 cxy = 2.0 * gl_PointCoord - 1.0;
    lowp float radius = dot(cxy, cxy);

    const lowp vec3 ambient = vec3(0.5, 0.2, 0.1);
    const lowp vec3 lightDiffuse = vec3(1, 0.5, 0.2);

    lowp vec3 normal = vec3(cxy, sqrt(1.0 - radius));
    lowp vec3 lightDir = normalize(vec3(0, -1, -0.5));
    lowp float color = max(dot(normal, lightDir), 0.0);

    lowp float delta = fwidth(radius);
    lowp float alpha = 1.0 - smoothstep(1.0 - delta, 1.0 + delta, radius);

    gl_FragColor = vec4(ambient + lightDiffuse * color, alpha);
}

If anyone could figure out how to remove the white border, that'd be fantastic!

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
MysteryPancake
  • 1,365
  • 1
  • 18
  • 47
  • 1
    @Rabbid76 After looking at the links you posted for a while, I found out the problem was I had to set alpha to false when creating the context. Could you add this as the answer? I added it myself but I can't accept it for a few days. – MysteryPancake Oct 02 '17 at 05:21
  • Just FYI: https://webglstats.com/webgl/parameter/ALIASED_POINT_SIZE_RANGE – gman Oct 04 '17 at 03:53

1 Answers1

1

In WebGL the default is to use pre-multiplied alpha. (see WebGL and Alpha)

When initializing the canvas, I has to be specified no alpha:

gl = document.getElementById("canvas").getContext("webgl", { alpha: false }); 

see further the answers to the questions:

Rabbid76
  • 202,892
  • 27
  • 131
  • 174