Search code examples
webglfragment-shaderalpha-transparency

WebGL: Will zero-alpha pixels in a texture necessarily update the depth buffer?


I'm trying to render a texture which has a mix of fully-transparent pixels, and fully-opaque pixels. It seems like my only option is to render them distance-wise back-to-front (after all fully opaque polygons), because even the fully-transparent pixels update the depth buffer, but I'm not sure that's accurate. Is it?

It seems like the fragment shader could be told to leave the depth buffer alone for transparent pixels, but apparently not. Am I missing anything?

Is there some other sensible way to render highly-irregularly-shaped images other than lots of polygons that I'm not thinking of?


Solution

  • Now that I've asked the question, I seem to have found at least one answer. The discard statement in the fragment shader seems to do what I need:

    void main(void) {
      vec4 textureColor = texture2D(uSampler, vTextureCoord);
      if (textureColor.a < 0.5) 
        discard;
      else
        gl_FragColor = vec4(textureColor.rgb * vLighting, textureColor.a);
    }