Using the WebGL API, how can I get a value from the depth buffer, or in any other way determine 3D coordinates from screen coordinates (i.e. to find a location clicked on),
Several years have passed, these days the WEBGL_depth_texture
extension is widely available... unless you need to support IE.
General usage:
Preparation:
gl.DEPTH_COMPONENT
)gl.COLOR_ATTACHMENT0
, gl.DEPTH_ATTACHMENT
)Rendering:
Unbind the framebuffer, pass the depth texture to your shaders and read it like any other texture:
texPos.xyz = (gl_Position.xyz / gl_Position.w) * 0.5 + 0.5;
float depthFromZBuffer = texture2D(uTexDepthBuffer, texPos.xy).x;