AlphaFunctions in WebGL?

时光总嘲笑我的痴心妄想 提交于 2019-12-22 08:29:29

问题


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.


回答1:


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;



回答2:


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



来源:https://stackoverflow.com/questions/7277047/alphafunctions-in-webgl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!