# Sphere Depth in Post Process

**URL:** <https://forum.babylonjs.com/t/sphere-depth-in-post-process/15795>\
**Category:** Questions\
**Tags:** material, shader\
**Created:** [November 18, 2020, 12:37am UTC](https://forum.babylonjs.com/t/sphere-depth-in-post-process/15795 "2020-11-18T00:37:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheNosiriN](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/thenosirin/32/11803_2.png) [@TheNosiriN](https://forum.babylonjs.com/u/TheNosiriN)\
**Post date:** [November 18, 2020, 12:37am UTC](https://forum.babylonjs.com/t/sphere-depth-in-post-process/15795/1 "2020-11-18T00:37:02Z")

</div>

I created a sphere in the scene (left) and created a sphere in a postprocess (right). I then got the depth of both of them yet they don’t look the same, they are slightly off, why?

https://playground.babylonjs.com/#BH3MRV#1

[https://playground.babylonjs.com/#BH3MRV#1](https://playground.babylonjs.com/#BH3MRV#1)

How do I make them similar?  
I’m doing something wrong but I don’t know what

---

<div class="post-metadata">

**Author:** ![Pryme8](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/pryme8/32/7212_2.png) [@Pryme8](https://forum.babylonjs.com/u/Pryme8)\
**Post date:** [November 18, 2020, 12:59am UTC](https://forum.babylonjs.com/t/sphere-depth-in-post-process/15795/2 "2020-11-18T00:59:05Z")

</div>

> [@Depth Sorted Ray Marching PostProccess](https://forum.babylonjs.com/t/depth-sorted-ray-marching-postproccess/7030):
>
> Thanks to a little help from @Evgeni_Popov with my math and depth calculations, we were able to create a depth sorted post effect that will allow raymarched scenes to be rendered with your normal scene. [https://playground.babylonjs.com/#6Z7SIZ#16](https://playground.babylonjs.com/#6Z7SIZ#16) Lots of work to go into it still, but here eventually I plan on passing all of BJS standard material stuff to the ray marched surfaces and you will be able to hopefully not tell the difference for general purposes. -TODO: Volumetric Absorption Ref…

---

<div class="post-metadata">

**Author:** ![TheNosiriN](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/thenosirin/32/11803_2.png) [@TheNosiriN](https://forum.babylonjs.com/u/TheNosiriN)\
**Post date:** [November 18, 2020, 1:07am UTC](https://forum.babylonjs.com/t/sphere-depth-in-post-process/15795/3 "2020-11-18T01:07:58Z")

</div>

I saw this recently but haven’t used much code from it yet. I’m going to try using `getRayFromScreenSpace`

---

<div class="post-metadata">

**Author:** ![TheNosiriN](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/thenosirin/32/11803_2.png) [@TheNosiriN](https://forum.babylonjs.com/u/TheNosiriN)\
**Post date:** [November 18, 2020, 1:16am UTC](https://forum.babylonjs.com/t/sphere-depth-in-post-process/15795/4 "2020-11-18T01:16:48Z")

</div>

using `getRayFromScreenSpace` produced the same results, but when I increased the radius to 600 the post process sphere started jittering.

[https://playground.babylonjs.com/#BH3MRV#2](https://playground.babylonjs.com/#BH3MRV#2)

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/2/2556eb4470f675dae7861ab932ccd9623f9709a7.png)

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [November 18, 2020, 11:29pm UTC](https://forum.babylonjs.com/t/sphere-depth-in-post-process/15795/5 "2020-11-18T23:29:55Z")

</div>

The `intersect.x` value is not a z, it is a parameter that will let you compute the 3D coordinates of the intersecting point with the ray origin + ray direction.

When you have the intersection point, it is in world space so you need to convert it to view space to get the z value you are after:

[https://playground.babylonjs.com/#BH3MRV#6](https://playground.babylonjs.com/#BH3MRV#6)

The right 1/2 sphere starts jittering because you set a too low value for minZ. Just try 0.1 as in the PG above.

---

<div class="post-metadata">

**Author:** ![TheNosiriN](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/thenosirin/32/11803_2.png) [@TheNosiriN](https://forum.babylonjs.com/u/TheNosiriN)\
**Post date:** [November 19, 2020, 5:04am UTC](https://forum.babylonjs.com/t/sphere-depth-in-post-process/15795/6 "2020-11-19T05:04:16Z")

</div>

Oh, thanks a ton, I really appreciate it. You just made my day 😄

---

<div class="post-metadata">

**Author:** ![TheNosiriN](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/thenosirin/32/11803_2.png) [@TheNosiriN](https://forum.babylonjs.com/u/TheNosiriN)\
**Post date:** [November 20, 2020, 6:19pm UTC](https://forum.babylonjs.com/t/sphere-depth-in-post-process/15795/7 "2020-11-20T18:19:10Z")

</div>

Wait then how would I convert it back to world space from view space do I just multiply by inverse of view matrix?

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [November 20, 2020, 11:06pm UTC](https://forum.babylonjs.com/t/sphere-depth-in-post-process/15795/8 "2020-11-20T23:06:39Z")

</div>

Yes, just use the inverse view matrix.
