# Sampling the depth texture always returns 0

**URL:** <https://discourse.threejs.org/t/sampling-the-depth-texture-always-returns-0/15123>\
**Category:** Questions\
**Tags:** depth-texture\
**Created:** [May 12, 2020, 12:32pm UTC](https://discourse.threejs.org/t/sampling-the-depth-texture-always-returns-0/15123 "2020-05-12T12:32:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![notvalid](https://avatars.discourse-cdn.com/v4/letter/n/49beb7/32.png) [@notvalid](https://discourse.threejs.org/u/notvalid)\
**Post date:** [May 12, 2020, 12:32pm UTC](https://discourse.threejs.org/t/sampling-the-depth-texture-always-returns-0/15123/1 "2020-05-12T12:32:04Z")

</div>

I have looked at [this](https://threejs.org/examples/webgl_depth_texture.html) example and tried to do the same: set up a separate render target with a depth texture, then pass this depth texture as a uniform to a shader. Problem is, when I try to sample its values, all I ever get is 0!

So I create a target:

```auto
let depthTarget = new THREE.WebGLRenderTarget(canvas.width, canvas.height, {depthBuffer: true, depthTexture: new THREE.DepthTexture(canvas.width, canvas.height)});

```

I render to this target (the scene is a view of a terrain btw, and this terrain is just a plane with some vertex shader transformations):

```auto
renderer.setRenderTarget(depthTarget);
renderer.render(this.scene, this.camera);

```

Then I pass `depthTarget.depthTexture` as a uniform to a shader for another object from another scene. There doesn’t seem to be any error with how I set up the uniforms, seeing as passing any other texture, like an external image, works just fine.

I sample the texture in the fragment shader:

```auto
vec2 screenPos = vec2((gl_FragCoord.x) / width, (gl_FragCoord.y) / height);
gl_FragColor = vec4(texture2D(depthTexture, screenPos).x, 0, 0, 1);

```

(actually I plan to use it in a different manner – I need to reconstruct world position from depth – but when I started to suspect there was something wrong with the depth texture, this was how I decided to check)

If I now render this (separate) scene with an object that uses this shader that samples the depth texture, and read pixels from the render target, then I find that the value is always 0, which it really shouldn’t be, seeing as I have the object (a plane with vertices transformed from height map data on its vertex shader) visible… `depthWrite` is enabled for the material used to render the plane. Am I missing something else?

---

<div class="post-metadata">

**Author:** ![li\_danyang](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/li_danyang/32/24521_2.png) [@li\_danyang](https://discourse.threejs.org/u/li_danyang)\
**Post date:** [February 25, 2022, 6:59am UTC](https://discourse.threejs.org/t/sampling-the-depth-texture-always-returns-0/15123/2 "2022-02-25T06:59:06Z")

</div>

Hello, have you solved this problem now. I have the same problem.
