# Compare current depth with older depth

**URL:** <https://discourse.threejs.org/t/compare-current-depth-with-older-depth/2152>\
**Category:** Questions\
**Tags:** depth-texture\
**Created:** [March 16, 2018, 1:16pm UTC](https://discourse.threejs.org/t/compare-current-depth-with-older-depth/2152 "2018-03-16T13:16:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abdulsattar\_Mohammed](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/abdulsattar_mohammed/32/1344_2.png) [@Abdulsattar\_Mohammed](https://discourse.threejs.org/u/Abdulsattar_Mohammed)\
**Post date:** [March 16, 2018, 1:16pm UTC](https://discourse.threejs.org/t/compare-current-depth-with-older-depth/2152/1 "2018-03-16T13:16:43Z")

</div>

We have a scene rendered to a `WebGLRenderTarget` with a depth texture which is being passed to a `ShaderMaterial` in a different scene as `tDepth`. Following is the fragment shader for that `ShaderMaterial`. It does a depth check and then renders onto the canvas. (I can just disable autoclear of depth and have it working, but I want a custom depth function, hence the code).

```auto
varying vec2 vUv;
uniform sampler2D tDepth;
uniform float width;
uniform float height;
void main() {
  vec2 clipCoord = vec2(gl_FragCoord.x / width , gl_FragCoord.y / height)
  float oldDepth = texture2D(tDepth, clipCoord).r; // Gets wrong value.
  float currentDepth = gl_FragCoord.z;
  if (oldDepth > currentDepth) {
    gl_FragColor = vec4(1,0,0,0);
  } else {
    discard;
  }
}

```

Instead of using `clipCoord`, if I just use `vUv` it works only when the other scene is a full screen quad with ortho camera. How do I get the correct value of `oldDepth` in case of other scenes?

---

<div class="post-metadata">

**Author:** ![Mugen87](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/mugen87/32/5645_2.png) [@Mugen87](https://discourse.threejs.org/u/Mugen87)\
**Post date:** [March 16, 2018, 7:42pm UTC](https://discourse.threejs.org/t/compare-current-depth-with-older-depth/2152/2 "2018-03-16T19:42:40Z")

</div>

Do you mind providing a live demo?

BTW: Instead of using two uniforms `width` and `height`, i suggest you use `vec2 resolution`. The variable name `clipCoord` is wrong. You are not calculating clip coordinates but normalized pixel coordinates (with values from 0 to 1).

---

<div class="post-metadata">

**Author:** ![Mugen87](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/mugen87/32/5645_2.png) [@Mugen87](https://discourse.threejs.org/u/Mugen87)\
**Post date:** [March 17, 2018, 10:34am UTC](https://discourse.threejs.org/t/compare-current-depth-with-older-depth/2152/3 "2018-03-17T10:34:02Z")

</div>

Crossposting:

> <https://stackoverflow.com/questions/49321577/compare-current-depth-with-older-depth>

---

<div class="post-metadata">

**Author:** ![Abdulsattar\_Mohammed](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/abdulsattar_mohammed/32/1344_2.png) [@Abdulsattar\_Mohammed](https://discourse.threejs.org/u/Abdulsattar_Mohammed)\
**Post date:** [March 19, 2018, 5:03am UTC](https://discourse.threejs.org/t/compare-current-depth-with-older-depth/2152/4 "2018-03-19T05:03:18Z")

</div>

https://codepen.io/abdulsattar/embed/preview/YaGxRr?height=300&slug-hash=YaGxRr&default-tabs=js,result&host=https://codepen.io

The problem that I’m trying to solve is the losing anti aliasing the moment we render to WebGLRenderTarget. Edges look really bad without AA.

The idea is I draw the main object first, then do the post processing using EffectComposer (I need to use outline\_pass for somethings), then draw the edges directly to the canvas so that I don’t lose AA.

To be honest, when I posted this question I wasn’t getting the right value for tDepth, but when I tried to make the code simpler, I got the right values 🙂. I was seeing edges on the other side as well.

However, there’s still a problem: the lines are jagged which shouldn’t be the case.  
 ![02%20AM](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/2X/d/dc796db0c049f12866ce35906845e8412d7b2cca.png)

---

<div class="post-metadata">

**Author:** ![Mugen87](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/mugen87/32/5645_2.png) [@Mugen87](https://discourse.threejs.org/u/Mugen87)\
**Post date:** [March 19, 2018, 10:36am UTC](https://discourse.threejs.org/t/compare-current-depth-with-older-depth/2152/5 "2018-03-19T10:36:25Z")

</div>

That’s because the cube itself is not smoothed via antialiasing.

---

<div class="post-metadata">

**Author:** ![Abdulsattar\_Mohammed](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/abdulsattar_mohammed/32/1344_2.png) [@Abdulsattar\_Mohammed](https://discourse.threejs.org/u/Abdulsattar_Mohammed)\
**Post date:** [March 19, 2018, 10:57am UTC](https://discourse.threejs.org/t/compare-current-depth-with-older-depth/2152/6 "2018-03-19T10:57:06Z")

</div>

Okay, I tried not rendering the cube at all:

https://codepen.io/abdulsattar/embed/preview/BrQqJM?height=300&slug-hash=BrQqJM&default-tabs=js,result&host=https://codepen.io

I still see the jagged lines.

---

<div class="post-metadata">

**Author:** ![Mugen87](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/mugen87/32/5645_2.png) [@Mugen87](https://discourse.threejs.org/u/Mugen87)\
**Post date:** [March 19, 2018, 11:30am UTC](https://discourse.threejs.org/t/compare-current-depth-with-older-depth/2152/7 "2018-03-19T11:30:36Z")

</div>

I’ve refactored your codepen so the actual depth texture is rendered. You can still see the effect similar to missing antialiasing. You could try to blur the depth texture a bit. Maybe this helps to smooth your lines…

https://codepen.io/anon/embed/preview/YapRMz?height=300&slug-hash=YapRMz&default-tabs=js,result&host=https://codepen.io
