I am very sorry, I will reply you so late. I made a simple example. By changing the source code, before the depth map is used in postStage, my code is used first. The depth of the result is negative. You can run my example to see it.
I don’t know why I can’t send attachments, I can only paste the code in the reply, I am sorry to give you trouble.
Here is where I am modifying the source code.It’s in the Scene.js file, the testDepthTexture method is what I added.
if (usePostProcess) {
var inputFramebuffer = sceneFramebuffer;
if (useGlobeDepthFramebuffer && !useOIT) {
inputFramebuffer = globeFramebuffer;
}
var postProcess = scene.postProcessStages;
var colorTexture = inputFramebuffer.getColorTexture(0);
var idTexture = idFramebuffer.getColorTexture(0);
var depthTexture = defaultValue(globeFramebuffer, sceneFramebuffer).depthStencilTexture;
if(Cesium.testDepthTexture){
Cesium.testDepthTexture.execute(context, colorTexture, depthTexture, idTexture);
}
postProcess.execute(context, colorTexture, depthTexture, idTexture);
postProcess.copy(context, defaultFramebuffer);
}
var fragmentShader = `
uniform sampler2D colorTexture;
uniform sampler2D depthTexture;
varying vec2 v_textureCoordinates;
vec4 toEye(in vec2 uv, in float depth){
vec2 xy = vec2((uv.x * 2.0 - 1.0),(uv.y * 2.0 - 1.0));
vec4 posInCamera =czm_inverseProjection * vec4(xy, depth, 1.0);
posInCamera =posInCamera / posInCamera.w;
return posInCamera;
}
float getDepth(in vec4 depth){
float z_window = czm_unpackDepth(depth);
z_window = czm_reverseLogDepth(z_window);
float n_range = czm_depthRange.near;
float f_range = czm_depthRange.far;
return (2.0 * z_window - n_range - f_range) / (f_range - n_range);
}
void main()
{
vec4 currD = texture2D(depthTexture, v_textureCoordinates);
if(currD.r>=1.0){
gl_FragColor = texture2D(colorTexture, v_textureCoordinates);
return;
}
float depth = getDepth(currD);
gl_FragColor = vec4(depth,0.0,0.0,1.0);
}`;
function testDepthTexture(viewer){
this.viewer = viewer;
this.init();
}
testDepthTexture.prototype.init = function(){
var _this = this;
this.viewer.scene.logarithmicDepthBuffer = true;
this.viewer.scene.globe.depthTestAgainstTerrain = true;
this.createFBO();
this.viewer.scene._postRender.addEventListener(function(){
_this.clearFBOCD.execute(_this.viewer.scene.context);
});
this.addPostProcess();
}
testDepthTexture.prototype.execute = function(context, colorTexture, depthTexture, idTexture){
this._defaultColorTexture = colorTexture;
this._defaultDepth = depthTexture;
this.showDepth();
}
testDepthTexture.prototype.createFBO = function(){
let context = this.viewer.scene.context;
let size = this.getWinSize();
let width = size.width;
let height = size.height;
this.mergeFBO = this.createMergeFBO(context,width,height);
this.clearFBOCD = this.cretaeClearFBOCD(this.mergeFBO);
}
testDepthTexture.prototype.createMergeFBO = function(context,width,height){
let tt = new Cesium.Texture({
context: context,
width: width,
height: height
});
return new Cesium.Framebuffer({
context: context,
colorTextures: [tt]
});
}
testDepthTexture.prototype.cretaeClearFBOCD = function(fbo){
if(!fbo)return;
var cc = new Cesium.ClearCommand({
color: new Cesium.Color(0.0, 0.0, 0.0, 0.0),
framebuffer: fbo
});
cc.pass = Cesium.Pass.OVERLAY;
return cc;
}
testDepthTexture.prototype.getWinSize = function(){
return {
width:this.viewer.scene.drawingBufferWidth,
height:this.viewer.scene.drawingBufferHeight
}
}
testDepthTexture.prototype.showDepth = function(){
var _this = this;
var context = this.viewer.scene.context;
if(!this.drawDepthCommand){
var uniformMap = {
colorTexture:function(){
return _this._defaultColorTexture
},
depthTexture:function(){
return _this._defaultDepth
}
}
var fbo = this.mergeFBO;
var rs = new Cesium.RenderState();
rs.depthTest.enabled = false;
var pass = Cesium.Pass.OVERLAY;
this.drawDepthCommand = context.createViewportQuadCommand(fragmentShader,{
renderState:rs,
uniformMap:uniformMap,
framebuffer:fbo,
pass:pass
});
}
this.drawDepthCommand.execute(this.viewer.scene.context);;
}
testDepthTexture.prototype.addPostProcess = function(){
var _this = this;
var fs =
“uniform sampler2D colorTexture;”+
“uniform sampler2D depthTexture;”+
“varying vec2 v_textureCoordinates;”+
“vec4 toEye(in vec2 uv, in float depth){”+
“vec2 xy = vec2((uv.x * 2.0 - 1.0),(uv.y * 2.0 - 1.0));”+
“vec4 posInCamera =czm_inverseProjection * vec4(xy, depth, 1.0);”+
“posInCamera =posInCamera / posInCamera.w;”+
“return posInCamera;”+
“}”+
“float getDepth(in vec4 depth){”+
“float z_window = czm_unpackDepth(depth);”+
“z_window = czm_reverseLogDepth(z_window);”+
“float n_range = czm_depthRange.near;”+
“float f_range = czm_depthRange.far;”+
“return (2.0 * z_window - n_range - f_range) / (f_range - n_range);”+
“}”+
"void main() "+
“{”+
“vec4 currD = texture2D(depthTexture, v_textureCoordinates);”+
“vec4 oldc = texture2D(colorTexture, v_textureCoordinates);”+
“if(currD.r>=1.0){”+
" gl_FragColor = texture2D(colorTexture, v_textureCoordinates);"+
“return;”+
“}”+
“float depth = getDepth(currD);”+
// “gl_FragColor = currD;”+
“gl_FragColor = oldc;”+
“}”;
this.depthProcess = new Cesium.PostProcessStage({
fragmentShader: fs,
uniforms: {
colorTexture: function () {
return _this.mergeFBO._colorTextures[0];
}
}
});
this.viewer.scene.postProcessStages.add(this.depthProcess);
}