Search code examples
transparencywebglalphathree.js

WebGL z-buffer artifacts?


We are working on a Three.js based WebGL project, and have trouble understanding how transparency is handled in WebGL. The image shows a doublesided surface drawn with alpha = 0.7, which behaves correctly on its right side. However closer to the middle strange artifacts appear, and on the left side the transparency does not seem to work at all.

example screenshot

The problem can also be seen here: http://emilaxelsson.se/sandbox/vis1/ Has anyone seen anything similar before? What could the reason be?


Solution

  • Your problem is that transparent objects needs to be sorted and rendered in a back-to-front order (if you try to change the opacity of your mesh from 0.7 (transparent) to 1.0 (opaque), you can see that the z-buffer works just fine).

    See:

    In your case it might be less trivial to solve, since I assume that you only have one mesh.

    Edit: Just to summarize the discussion below. It is possible to achieve correct rendering of such a double-sided transparent mesh. To do this, you need to create 6 versions of the mesh, corresponding to 6 sides of a cube. Each version needs to be sorted in a back-to-front order based on the 'side of the cube' (front, back, left, right, top, bottom). When rendering choose the correct mesh (based on the camera viewing direction) and render that single mesh.