Hello,I want to customize the depth of the object through the material plugin.
I have completed a PG, but the actual application seems that the depth has not reached the expected effect, I would like to ask if my implementation method has really changed the depth.
PG:https://playground.babylonjs.com/#9QIS9V#3
The situation you mentioned is quite strange.
My Playground (PG) in my browser doesn’t throw any errors, but your PG in my browser gives an error, saying: ‘nearFar’ : redefinition.
However, what I really want to ask is whether I can modify only gl_Position.z
to affect the actual depth calculation of the object, or do I also need to do any additional work in the fragment stage or other phases?
There are no errors in my browser console either
When using material.useLogarithmicDepth = true;
with a PBR material, I’ve found the way depth is calculated in both the vertex shader and fragment shader.
I’m not quite sure why it’s necessary to recalculate the depth in the fragment shader, and why the calculation methods are different.
In Vertex:
vFragmentDepth=1.0+gl_Position.w;
gl_Position.z=log2(max(0.000001,vFragmentDepth))*logarithmicDepthConstant;
In Fragment:
gl_FragDepth=log2(vFragmentDepth) * logarithmicDepthConstant * 0.5;
If I use my own depth calculation method, I’m not sure how to modify it in the fragment shader.
In Vertex:
float v_depth=(1.0/log2(nearFar.y-nearFar.x+1.0))*log2((0.5 * (gl_Position.z /gl_Position.w) + 0.5) * gl_Position.w+1.0);
float tempz = (gl_Position.w+gl_Position.w)*v_depth-gl_Position.w;
gl_Position.z=tempz;
In Fragment:
I don’t konw what I can do?
What’s the expected effect? Let’s start by clarifying this question and continue with finding the solution.
Calculate depth based on Cesium’s method, and modify it in Babylon accordingly to synchronize and integrate with Cesium’s depth.
Could you help me with this problem?
Technically speaking you want to alter the Z coordinates of the vertices?
I have already modified gl_Position.z
in the vertex shader. I want to know how to correspondingly modify it in the fragment shader. What is the relationship between the depth outputs of the vertex shader and the fragment shader?
Can this page help?
https://doc.babylonjs.com/communityExtensions/Babylon.js+ExternalLibraries/BabylonJS_and_CesiumJS
I don’t know anything about Cesium. However theoretically you could pass the tempz
value to the fragment shader using a varying
:
vertex
:
varying float v_depth;
.
.
.
v_depth = tempz;
fragment
:
varying float v_depth;
.
.
.
gl_FragDepth = v_depth;
or just dive into the solution suggested by @Evgeni_Popov
Thank you , I’ve solved this question.
Thank you , I’ve solved this question.