I managed to do what I wanted, more or less. I post it here, so maybe someone will find it useful.
Since GLES/WebGL do not support depth textures out of box (extensions exist, but I don’t use them), I did it in fragment shader by storing depth value in alpha channel.
while rendering to FBO
color.a = gl_FragCoord.z;
and while rendering that FBO to the screen
gl_FragDepth = color.a;
color.a = 1.0;
Also, to avoid doing that with completely transparent pixels, in both shaders add
if(color.a == 0) discard;
before doing anything.
That wouldn’t work well with semi transparent tiles, as devades mentioned.
Also
[quote]For older devices that don’t support depth textures, another idea is to use a “multi texture” batch like so:
[/quote]
I also though of that, but as I also mentioned earlier, I want to be able to zoom out ver far, so I have to prerender some chunks of terrain into texture anyway. (one could argue that with such zoom out I shouldn’t also be displaying any objects, so depth stops being relevant, but whatever
)