Rendering glitch with GL_DEPTH_TEST and transparent textures

前端 未结 3 1891
北荒
北荒 2021-01-14 10:26

From one angle my shrubs look like this:

From the other, they look like this:

3条回答
  •  -上瘾入骨i
    2021-01-14 11:14

    If you're in WebGL or OpenGL ES 2.0 (iPhone/Android) there is no alpha testing. Instead you need to not draw transparent pixels. That way they won't affect the depth buffer since no pixel was written. To do that you need to discard pixels that are transparent in your fragment shader. You could hard code it

    ...
    void main() {
       vec4 color = texture2D(u_someSampler, v_someUVs);
       if (color.a == 0.0) {
         discard;
       }
       gl_FragColor = color;
    }
    

    or you could simulate the old style alpha testing where you can set the alpha value

    ...
    uniform float u_alphaTest;
    void main() {
       vec4 color = texture2D(u_someSampler, v_someUVs);
       if (color.a < u_alphaTest) {
         discard;
       }
       gl_FragColor = color;
    }
    

提交回复
热议问题