4

Is it possible to achieve an transparency effect where fragments with alpha lower than 0.5 are discarded and fragments with alpha higher than 0.5 are rendered rendered opaque? From what I've read,

glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0.5); 

would be what I'm looking for but unfortunately, AlphaFunction is not defined in WebGL. Is there a workaround?

My problem is, that transparent fragments write into the depth buffer and thus prevent farther fragments from beeing rendered: alpha_error http://gebackene-ente.at/nudelsalat/sonstiges/pointcloud_alphaerror.jpg

Sorting is not an option because there are way too much points.

2 Answers 2

15

Use your shader to do it. At the bottom of your fragment shader, after setting the output color:

if(gl_FragColor.a < 0.5)
  discard;
1
  • 1
    Note, you must put the "discard" at the bottom after setting output colour as Nicol Bolas says. Placing "discard" earlier seems to be ignored in some situations. Mar 31, 2015 at 10:33
4

You can completely discard fragments in the fragment shader using the discard statement. So just look up there alpha from the texture (or somewhere else) and then just call

if(alpha < 0.5)
    discard;

So you don't even need to propagate the alpha to the color or you can make the test dependent on something else. That is the modern way of doing alpha test, as it's also deprecated in desktop GL 3+.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.