Framebuffer Depth Texture

If we require the depth test when rendering to a texture, then the framebuffer will require a depth buffer. This functionality should be enabled before use.

gl.getExtension("webgl_depth_texture");

Like the color buffer, the depth buffer is an ordinary texture. It must have the same width and height as the color buffer, and the format must be DEPTH_COMPONENT.

depthTexturegl.createTexture()
gl.bindTexture(gl.TEXTURE_2D, depthTexture)
gl.texImage2D (gl.TEXTURE_2D, 0, gl.DEPTH_COMPONENT, framebufferWidth, framebufferHeight, 0, gl.DEPTH_COMPONENT, gl.UNSIGNED_SHORT, null)

The depth texture's parameters need not be identical to the color buffer's, but under most circumstances the reasonable defaults are the same.

gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)