Skip to content

Commit

Permalink
Revert "Revert PickDepth"
Browse files Browse the repository at this point in the history
This reverts commit 9d46847.
  • Loading branch information
lilleyse committed Sep 20, 2019
1 parent 9d46847 commit a5f9457
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 8 deletions.
84 changes: 78 additions & 6 deletions Source/Scene/PickDepth.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ define([

this._depthTexture = undefined;
this._textureToCopy = undefined;
this._colorTextureMask = undefined;
this._copyDepthCommand = undefined;
this._copyDepthCommandRender = undefined;
this._copyDepthCommandPick = undefined;

this._useLogDepth = undefined;

Expand Down Expand Up @@ -112,9 +115,11 @@ define([
}
}

function updateCopyCommands(pickDepth, context, depthTexture) {
function updateCopyCommands(pickDepth, context, depthTexture, colorTextureMask) {
var fs;
if (!defined(pickDepth._copyDepthCommand)) {
var fs =
// Passthrough depth copy
fs =
'uniform sampler2D u_texture;\n' +
'varying vec2 v_textureCoordinates;\n' +
'void main()\n' +
Expand All @@ -132,17 +137,76 @@ define([
});
}

if (!defined(pickDepth._copyDepthCommandRender)) {
// If alpha is less than one, use globe depth instead of scene depth. Globe depth will overwrite areas where
// there is translucent geometry or no geometry (like the depth plane).
fs =
'uniform sampler2D u_texture;\n' +
'uniform sampler2D u_colorTextureMask;\n' +
'varying vec2 v_textureCoordinates;\n' +
'void main()\n' +
'{\n' +
' vec4 pickDepth = czm_packDepth(texture2D(u_texture, v_textureCoordinates).r);\n' +
' vec4 globeDepth = texture2D(czm_globeDepthTexture, v_textureCoordinates);\n' +
' bool mask = texture2D(u_colorTextureMask, v_textureCoordinates).a < 1.0;\n' +
' gl_FragColor = czm_branchFreeTernary(mask, globeDepth, pickDepth);\n' +
'}\n';
pickDepth._copyDepthCommandRender = context.createViewportQuadCommand(fs, {
renderState : RenderState.fromCache(),
uniformMap : {
u_texture : function() {
return pickDepth._textureToCopy;
},
u_colorTextureMask : function() {
return pickDepth._colorTextureMask;
}
},
owner : pickDepth
});
}

if (!defined(pickDepth._copyDepthCommandPick)) {
// If color is (0,0,0,0), use globe depth instead of scene depth. Globe depth will overwrite areas where
// there is no geometry (like the depth plane).
fs =
'uniform sampler2D u_texture;\n' +
'uniform sampler2D u_colorTextureMask;\n' +
'varying vec2 v_textureCoordinates;\n' +
'void main()\n' +
'{\n' +
' vec4 pickDepth = czm_packDepth(texture2D(u_texture, v_textureCoordinates).r);\n' +
' vec4 globeDepth = texture2D(czm_globeDepthTexture, v_textureCoordinates);\n' +
' bool mask = all(equal(texture2D(u_colorTextureMask, v_textureCoordinates), vec4(0.0)));\n' +
' gl_FragColor = czm_branchFreeTernary(mask, globeDepth, pickDepth);\n' +
'}\n';
pickDepth._copyDepthCommandPick = context.createViewportQuadCommand(fs, {
renderState : RenderState.fromCache(),
uniformMap : {
u_texture : function() {
return pickDepth._textureToCopy;
},
u_colorTextureMask : function() {
return pickDepth._colorTextureMask;
}
},
owner : pickDepth
});
}

pickDepth._textureToCopy = depthTexture;
pickDepth._colorTextureMask = colorTextureMask;
pickDepth._copyDepthCommand.framebuffer = pickDepth._framebuffer;
pickDepth._copyDepthCommandRender.framebuffer = pickDepth._framebuffer;
pickDepth._copyDepthCommandPick.framebuffer = pickDepth._framebuffer;
}

PickDepth.prototype.executeDebugPickDepth = function(context, passState, useLogDepth) {
executeDebugPickDepth(this, context, passState, useLogDepth);
};

PickDepth.prototype.update = function(context, depthTexture) {
PickDepth.prototype.update = function(context, depthTexture, colorTextureMask) {
updateFramebuffers(this, context, depthTexture);
updateCopyCommands(this, context, depthTexture);
updateCopyCommands(this, context, depthTexture, colorTextureMask);
};

var scratchPackedDepth = new Cartesian4();
Expand All @@ -162,8 +226,14 @@ define([
return Cartesian4.dot(packedDepth, packedDepthScale);
};

PickDepth.prototype.executeCopyDepth = function(context, passState) {
this._copyDepthCommand.execute(context, passState);
PickDepth.prototype.executeCopyDepth = function(context, passState, copyGlobeDepth, picking) {
if (!copyGlobeDepth) {
this._copyDepthCommand.execute(context, passState);
} else if (picking) {
this._copyDepthCommandPick.execute(context, passState);
} else {
this._copyDepthCommandRender.execute(context, passState);
}
};

PickDepth.prototype.isDestroyed = function() {
Expand All @@ -175,6 +245,8 @@ define([
destroyFramebuffers(this);

this._copyDepthCommand.shaderProgram = defined(this._copyDepthCommand.shaderProgram) && this._copyDepthCommand.shaderProgram.destroy();
this._copyDepthCommandRender.shaderProgram = defined(this._copyDepthCommandRender.shaderProgram) && this._copyDepthCommandRender.shaderProgram.destroy();
this._copyDepthCommandPick.shaderProgram = defined(this._copyDepthCommandPick.shaderProgram) && this._copyDepthCommandPick.shaderProgram.destroy();

return destroyObject(this);
};
Expand Down
5 changes: 3 additions & 2 deletions Source/Scene/Scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -2466,9 +2466,10 @@ define([
if (context.depthTexture && scene.useDepthPicking && (environmentState.useGlobeDepthFramebuffer || renderTranslucentDepthForPick)) {
// PERFORMANCE_IDEA: Use MRT to avoid the extra copy.
var depthStencilTexture = renderTranslucentDepthForPick ? passState.framebuffer.depthStencilTexture : globeDepth.framebuffer.depthStencilTexture;
var colorTexture = passState.framebuffer.getColorTexture(0);
var pickDepth = getPickDepth(scene, index);
pickDepth.update(context, depthStencilTexture);
pickDepth.executeCopyDepth(context, passState);
pickDepth.update(context, depthStencilTexture, colorTexture);
pickDepth.executeCopyDepth(context, passState, usePrimitiveFramebuffer, picking);
}

if (picking || !usePostProcessSelected) {
Expand Down

0 comments on commit a5f9457

Please sign in to comment.