Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AlphaFunctions in WebGL?

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.

like image 929
Markus Avatar asked Dec 03 '22 01:12

Markus


2 Answers

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;
like image 71
Nicol Bolas Avatar answered Dec 07 '22 23:12

Nicol Bolas


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+.

like image 39
Christian Rau Avatar answered Dec 07 '22 23:12

Christian Rau