# Some Trouble with depth texture

**URL:** <https://forum.babylonjs.com/t/some-trouble-with-depth-texture/24474>\
**Category:** Questions\
**Created:** [October 11, 2021, 7:09am UTC](https://forum.babylonjs.com/t/some-trouble-with-depth-texture/24474 "2021-10-11T07:09:54Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Moriy](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/moriy/32/30563_2.png) [@Moriy](https://forum.babylonjs.com/u/Moriy)\
**Post date:** [October 11, 2021, 7:09am UTC](https://forum.babylonjs.com/t/some-trouble-with-depth-texture/24474/1 "2021-10-11T07:09:54Z")

</div>

Hi everyone!!!  
I have some trouble with depth texture.  
I achieve a light scan func with screen space postprocess like this, it samples depthMap:

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/d/4/d441a4686036d466cbf590188a606f377d35f7c2.jpeg)  
I achieve a build split effect with PBRCustomMaterial like this:  
 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/1/1/110721c7ebb20e6766d128586863a74affd1bdc3.jpeg)  
 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/6/2/62de8a6d81d78016a0b4d095811315008ee2c02f.png)  
When I make them working together, bo---------om, ghost shadow appears:  
 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/9/1/91027b5eb3e9d437bf13f0caa0b0ef855b95d796.jpeg)

---

<div class="post-metadata">

**Author:** ![Moriy](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/moriy/32/30563_2.png) [@Moriy](https://forum.babylonjs.com/u/Moriy)\
**Post date:** [October 11, 2021, 7:27am UTC](https://forum.babylonjs.com/t/some-trouble-with-depth-texture/24474/2 "2021-10-11T07:27:59Z")

</div>

This shows “world position” calculated by ScreenSpaceDepth (mod 100).

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/7/0/70b933cf434fc2de0d91c6def33a52b6ef2b1637.jpeg)  
It seems that custom uniforms splitStep,splitPercent,splitMaxHeight didn’t effect the depth vertex shader.

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/9/d/9d9dadcc8c48c0a658622a9cbb4163a51d94f2d6.png)  
👇  
 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/5/f/5faf9f7a10604592cebca8430c6f2c156a07c551.png)  
build can be splitted correctly, gl\_Position shouldn’t been wrong. Maybe BBL draws color and depth at different time. When BBL draws depth, it didn’t bind custom uniforms for submesh.

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [October 11, 2021, 10:47am UTC](https://forum.babylonjs.com/t/some-trouble-with-depth-texture/24474/3 "2021-10-11T10:47:21Z")

</div>

Ping @nasimiasl who owns the custom material.

In your case I would recommend going the NME road instead as we can not guarantee back compat of our shaders internally and those are the kind of things which will be impossible to foresee.

---

<div class="post-metadata">

**Author:** ![nasimiasl](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/nasimiasl/32/34_2.png) [@nasimiasl](https://forum.babylonjs.com/u/nasimiasl)\
**Post date:** [October 11, 2021, 11:09am UTC](https://forum.babylonjs.com/t/some-trouble-with-depth-texture/24474/4 "2021-10-11T11:09:14Z")

</div>

let me read that first 🙂  
in progress

---

<div class="post-metadata">

**Author:** ![nasimiasl](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/nasimiasl/32/34_2.png) [@nasimiasl](https://forum.babylonjs.com/u/nasimiasl)\
**Post date:** [October 11, 2021, 12:07pm UTC](https://forum.babylonjs.com/t/some-trouble-with-depth-texture/24474/5 "2021-10-11T12:07:33Z")

</div>

@Moriy hey

i use the different depth parameter that not use LOG  
\*\* notice that mathematical function not always make same result in the different GPU specially when you talk about sensitive data

so i prefer use “gl\_FragCoord.z”

by this calculate ((2.0 \* near \* far) / (far + near - (gl\_FragCoord.z\*2.-1.) \* (far - near)))/far

[https://www.babylonjs-playground.com/#TBG0FA#2](https://www.babylonjs-playground.com/#TBG0FA#2)

also if you need more smooth use can use this  
[https://www.babylonjs-playground.com/#TBG0FA#1](https://www.babylonjs-playground.com/#TBG0FA#1)

\*\* i know you need them in post process i need try that

---

<div class="post-metadata">

**Author:** ![nasimiasl](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/nasimiasl/32/34_2.png) [@nasimiasl](https://forum.babylonjs.com/u/nasimiasl)\
**Post date:** [October 11, 2021, 12:48pm UTC](https://forum.babylonjs.com/t/some-trouble-with-depth-texture/24474/6 "2021-10-11T12:48:47Z")

</div>

[https://www.babylonjs-playground.com/#TBG0FA#3](https://www.babylonjs-playground.com/#TBG0FA#3)

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/e/8/e820d37804327595e03ead87458a0c65084aac0c.jpeg)

---

<div class="post-metadata">

**Author:** ![Moriy](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/moriy/32/30563_2.png) [@Moriy](https://forum.babylonjs.com/u/Moriy)\
**Post date:** [October 12, 2021, 2:04am UTC](https://forum.babylonjs.com/t/some-trouble-with-depth-texture/24474/7 "2021-10-12T02:04:03Z")

</div>

I find that nodeMaterial can be created by uniqueURL or file, could it created by a string field? My project will work at local area network , and automatic build program do not support to move each xxxxx.xxx.fx/.json file into public folder, it is very cumbersome to push NME file into built package by hand. These are why I don’t willing to use NME ,maybe I just need static way to create nodeMaterial, once built , never change

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [October 12, 2021, 2:07am UTC](https://forum.babylonjs.com/t/some-trouble-with-depth-texture/24474/8 "2021-10-12T02:07:32Z")

</div>

if you store the json in code you can use:

```auto
const material = new NodeMaterial("AwesomeMat", scene);
material.loadFromSerialization(json);

```

---

<div class="post-metadata">

**Author:** ![Moriy](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/moriy/32/30563_2.png) [@Moriy](https://forum.babylonjs.com/u/Moriy)\
**Post date:** [October 12, 2021, 2:23am UTC](https://forum.babylonjs.com/t/some-trouble-with-depth-texture/24474/9 "2021-10-12T02:23:16Z")

</div>

Thank you for your help!!! Maybe my explanation is not clear enough (my English is very bad), I will try my best to eaplan it clearly 😁 😁 😁  
My main problem is that when I change vertexShader of PBRCustomMaterial (like I move a mesh upward with an uniform **upwardValue** in vertexShader ----- positionUpdated.y += upwardValue;) In Color Space the mesh has been moved up 100 unit, but in Depth Space, the mesh is stand still. When light scaning, it looks like a ghost shadow in original location. It seems that uniforms wasn’t setted into submesh when scene rendered this mesh into depth.  
 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/b/3/b3c204d347979c95af0b04e6fec2f2f1c58280af.jpeg)

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/5/c/5c09dd3c981fb70342b1682316db135553435aa2.jpeg)  
 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/7/5/75b0bd8a3a79ad3da3fe68d47cd8398428455f52.jpeg)

---

<div class="post-metadata">

**Author:** ![Moriy](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/moriy/32/30563_2.png) [@Moriy](https://forum.babylonjs.com/u/Moriy)\
**Post date:** [October 12, 2021, 2:27am UTC](https://forum.babylonjs.com/t/some-trouble-with-depth-texture/24474/11 "2021-10-12T02:27:55Z")

</div>

Is json exported like picture?

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/8/7/87e7950bc43e932a9a38a81345a45d4d73280dc2.png)

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [October 12, 2021, 2:34am UTC](https://forum.babylonjs.com/t/some-trouble-with-depth-texture/24474/12 "2021-10-12T02:34:53Z")

</div>

Yup it will provide you with the json you can then embed where you want.

---

<div class="post-metadata">

**Author:** ![Moriy](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/moriy/32/30563_2.png) [@Moriy](https://forum.babylonjs.com/u/Moriy)\
**Post date:** [October 12, 2021, 2:38am UTC](https://forum.babylonjs.com/t/some-trouble-with-depth-texture/24474/13 "2021-10-12T02:38:37Z")

</div>

Get√ , have a try 😁 😁 😁

---

<div class="post-metadata">

**Author:** ![Moriy](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/moriy/32/30563_2.png) [@Moriy](https://forum.babylonjs.com/u/Moriy)\
**Post date:** [October 12, 2021, 3:25am UTC](https://forum.babylonjs.com/t/some-trouble-with-depth-texture/24474/14 "2021-10-12T03:25:56Z")

</div>

Hello sebavan~  
It has some mistakes [https://playground.babylonjs.com/#F6Q8K2#2](https://playground.babylonjs.com/#F6Q8K2#2)

---

<div class="post-metadata">

**Author:** ![Moriy](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/moriy/32/30563_2.png) [@Moriy](https://forum.babylonjs.com/u/Moriy)\
**Post date:** [October 12, 2021, 3:29am UTC](https://forum.babylonjs.com/t/some-trouble-with-depth-texture/24474/15 "2021-10-12T03:29:13Z")

</div>

this is NME [Babylon.js Node Material Editor](https://nme.babylonjs.com/#7TY9PJ)

---

<div class="post-metadata">

**Author:** ![nasimiasl](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/nasimiasl/32/34_2.png) [@nasimiasl](https://forum.babylonjs.com/u/nasimiasl)\
**Post date:** [October 12, 2021, 8:54am UTC](https://forum.babylonjs.com/t/some-trouble-with-depth-texture/24474/16 "2021-10-12T08:54:55Z")

</div>

i see

the world position most not calculate the vertex changes you need use the varying and passed that from vertexshader

your code mode  
[https://cyos.babylonjs.com/#CNG205](https://cyos.babylonjs.com/#CNG205)

correct mode  
[https://cyos.babylonjs.com/#CNG205#1](https://cyos.babylonjs.com/#CNG205#1)

but you most init that on Cusom material  
u need  
**Fragment\_MainBegin** and **Fragment\_Definitions** and **Vertex\_Definitions**

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [October 12, 2021, 12:57pm UTC](https://forum.babylonjs.com/t/some-trouble-with-depth-texture/24474/17 "2021-10-12T12:57:28Z")

</div>

You are not passing a json object but a string containing json in the function.

[https://playground.babylonjs.com/#F6Q8K2#4](https://playground.babylonjs.com/#F6Q8K2#4)

---

<div class="post-metadata">

**Author:** ![Moriy](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/moriy/32/30563_2.png) [@Moriy](https://forum.babylonjs.com/u/Moriy)\
**Post date:** [October 12, 2021, 1:25pm UTC](https://forum.babylonjs.com/t/some-trouble-with-depth-texture/24474/18 "2021-10-12T13:25:12Z")

</div>

Ohhhhh, my mistake!!! I always thought JSON is equal to string 😭 😭 😭

---

<div class="post-metadata">

**Author:** ![Moriy](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/moriy/32/30563_2.png) [@Moriy](https://forum.babylonjs.com/u/Moriy)\
**Post date:** [October 12, 2021, 2:12pm UTC](https://forum.babylonjs.com/t/some-trouble-with-depth-texture/24474/19 "2021-10-12T14:12:20Z")

</div>

yes, I read the pbr.vertex.fx, customPBRMaterial use gl\_Position.w to calculate depth, gl\_Position is calculated by worldPos, worldPos is calculated by positionUpdated. I add code in Vertex\_Before\_PositionUpdated where positionUpdated is defined but worldPos isn’t defined.

In this order, I change positionUpdated, gl\_Position is changed correctly, but depth not change.

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/5/e/5e25d54f30ed3035e62b249758b0680d113f57e5.jpeg)

This picture is draw with a screen space postprocess Effect, It samplers depth map, calculate world position for each fragment, then output worldposition mod 100.(I think it is best way to show depthMap than linear gray 0-1)  
 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/9/a/9afb40c1205e225f23ffff07d4cd219b9eea1df9.jpeg)

---

<div class="post-metadata">

**Author:** ![Moriy](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/moriy/32/30563_2.png) [@Moriy](https://forum.babylonjs.com/u/Moriy)\
**Post date:** [October 12, 2021, 2:17pm UTC](https://forum.babylonjs.com/t/some-trouble-with-depth-texture/24474/20 "2021-10-12T14:17:17Z")

</div>

I have a good idea to show this phenomenon.  
I will give you a clearly picture to show this error tomorrow.

---

<div class="post-metadata">

**Author:** ![nasimiasl](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/nasimiasl/32/34_2.png) [@nasimiasl](https://forum.babylonjs.com/u/nasimiasl)\
**Post date:** [October 12, 2021, 4:17pm UTC](https://forum.babylonjs.com/t/some-trouble-with-depth-texture/24474/21 "2021-10-12T16:17:22Z")

</div>

any playground can help

[Next page](https://forum.babylonjs.com/t/some-trouble-with-depth-texture/24474.md?page=2)
