Jump to content

Question about the depth shader


BlackMojito
 Share

Recommended Posts

I have some doubt about the depth shader. The output seems not to be the depth value in NDC space, nor the value like gl_FragCoord.z. So what it is about? Please correct me if I am wrong. :D

gl_Position = viewProjection * finalWorld * vec4(position, 1.0);
	
vDepthMetric = ((gl_Position.z + depthValues.x) / (depthValues.y));

 

Link to comment
Share on other sites

5 hours ago, Deltakosh said:

Hello!

The value is in linear space

 

See here an example on how to use it: https://github.com/BabylonJS/Babylon.js/blob/master/src/Shaders/ssao2.fragment.fx#L43

Thank you @Deltakosh. Hummm...I am really confused. As far as I understand,  the z component in projection space (some sort of 1/Zviewspace) is linear in screen space (for interpolation reason) but non linear as depth. So this is for convert it again to a linear depth but mapped in [0, 1]?

The value of vDepthMetric is 

1 - ((far - gl_Position.z) / (far + near)). I am not sure why it is linear again. Maybe it is a stupid question though...Thanks in advance for more explanation. :)

 

One more question, is gl_FragCoord.z the interpolation result of z in projection space?

Link to comment
Share on other sites

8 hours ago, Deltakosh said:

Hello!

The value is in linear space

 

See here an example on how to use it: https://github.com/BabylonJS/Babylon.js/blob/master/src/Shaders/ssao2.fragment.fx#L43

Hi @Deltakosh, the sample you gave was about the SSAO2, it uses the GeometryBufferRenderer which is based on the shader geometry.vertex.fx and geometry.fragment.fx.  Please see the links below. Here we output directly depth in view space (although I don't understand why we need to divide z by w because w should always be 1.0 here). It works perfectly with the SSAO2 sample as it construct a viewRay in View space.

https://github.com/BabylonJS/Babylon.js/blob/1dd71f9e125247a54c315d870b979ee0ec731433/src/Shaders/geometry.vertex.fx#L40

https://github.com/BabylonJS/Babylon.js/blob/1dd71f9e125247a54c315d870b979ee0ec731433/src/Shaders/geometry.fragment.fx#L30

 

My initial question is about depth.vertex.fx and depth.fragment.fx, which I believe are used by DepthRenderer. Then the output of DepthRenderer is used by SSAO effect. 

in depth.vertex.fx

gl_Position = viewProjection * finalWorld * vec4(position, 1.0);
	
vDepthMetric = ((gl_Position.z + depthValues.x) / (depthValues.y));
float depth = texture2D(textureSampler, vUV).r;
vec3 position = vec3(vUV, depth);

 

Link to comment
Share on other sites

8 hours ago, Deltakosh said:

Hello!

The value is in linear space

 

See here an example on how to use it: https://github.com/BabylonJS/Babylon.js/blob/master/src/Shaders/ssao2.fragment.fx#L43

Hi @Deltakosh, the sample you gave was about the SSAO2, it uses the GeometryBufferRenderer which is based on the shader geometry.vertex.fx and geometry.fragment.fx.  Please see the links below. Here we output directly depth in view space. This should be the linear depth. It works with the SSAO2 sample as it construct a viewRay in View space.

https://github.com/BabylonJS/Babylon.js/blob/1dd71f9e125247a54c315d870b979ee0ec731433/src/Shaders/geometry.vertex.fx#L40

https://github.com/BabylonJS/Babylon.js/blob/1dd71f9e125247a54c315d870b979ee0ec731433/src/Shaders/geometry.fragment.fx#L30

 

My initial question is about depth.vertex.fx and depth.fragment.fx, which I believe are used by DepthRenderer. Then the output of DepthRenderer is used by SSAO effect. 

in depth.vertex.fx

gl_Position = viewProjection * finalWorld * vec4(position, 1.0);
	
vDepthMetric = ((gl_Position.z + depthValues.x) / (depthValues.y));

in ssao.fragment.fx

float depth = texture2D(textureSampler, vUV).r;
vec3 position = vec3(vUV, depth);

As we know that vUV is [0, 1], so should be the "depth". My question is why we don't directly use gl_FragCoord.z? Why we use the "vDepthMetric"?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...