Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable improved depth testing for all billboards #6802

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Source/DataSources/BillboardVisualizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ define([
var defaultHorizontalOrigin = HorizontalOrigin.CENTER;
var defaultVerticalOrigin = VerticalOrigin.CENTER;
var defaultSizeInMeters = false;
var defaultDisableDepthTestDistance = 0.0;

var positionScratch = new Cartesian3();
var colorScratch = new Color();
Expand Down Expand Up @@ -156,7 +155,7 @@ define([
billboard.pixelOffsetScaleByDistance = Property.getValueOrUndefined(billboardGraphics._pixelOffsetScaleByDistance, time, pixelOffsetScaleByDistanceScratch);
billboard.sizeInMeters = Property.getValueOrDefault(billboardGraphics._sizeInMeters, time, defaultSizeInMeters);
billboard.distanceDisplayCondition = Property.getValueOrUndefined(billboardGraphics._distanceDisplayCondition, time, distanceDisplayConditionScratch);
billboard.disableDepthTestDistance = Property.getValueOrDefault(billboardGraphics._disableDepthTestDistance, time, defaultDisableDepthTestDistance);
billboard.disableDepthTestDistance = Property.getValueOrUndefined(billboardGraphics._disableDepthTestDistance, time);

var subRegion = Property.getValueOrUndefined(billboardGraphics._imageSubRegion, time, boundingRectangleScratch);
if (defined(subRegion)) {
Expand Down
3 changes: 1 addition & 2 deletions Source/DataSources/LabelVisualizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ define([
var defaultHeightReference = HeightReference.NONE;
var defaultHorizontalOrigin = HorizontalOrigin.CENTER;
var defaultVerticalOrigin = VerticalOrigin.CENTER;
var defaultDisableDepthTestDistance = 0.0;

var positionScratch = new Cartesian3();
var fillColorScratch = new Color();
Expand Down Expand Up @@ -164,7 +163,7 @@ define([
label.pixelOffsetScaleByDistance = Property.getValueOrUndefined(labelGraphics._pixelOffsetScaleByDistance, time, pixelOffsetScaleByDistanceScratch);
label.scaleByDistance = Property.getValueOrUndefined(labelGraphics._scaleByDistance, time, scaleByDistanceScratch);
label.distanceDisplayCondition = Property.getValueOrUndefined(labelGraphics._distanceDisplayCondition, time, distanceDisplayConditionScratch);
label.disableDepthTestDistance = Property.getValueOrDefault(labelGraphics._disableDepthTestDistance, time, defaultDisableDepthTestDistance);
label.disableDepthTestDistance = Property.getValueOrUndefined(labelGraphics._disableDepthTestDistance, time);
}
return true;
};
Expand Down
5 changes: 2 additions & 3 deletions Source/Scene/Billboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ define([
this._pixelOffsetScaleByDistance = pixelOffsetScaleByDistance;
this._sizeInMeters = defaultValue(options.sizeInMeters, false);
this._distanceDisplayCondition = distanceDisplayCondition;
this._disableDepthTestDistance = defaultValue(options.disableDepthTestDistance, 0.0);
this._disableDepthTestDistance = options.disableDepthTestDistance;
this._id = options.id;
this._collection = defaultValue(options.collection, billboardCollection);

Expand Down Expand Up @@ -789,7 +789,6 @@ define([
* When set to zero, the depth test is always applied. When set to Number.POSITIVE_INFINITY, the depth test is never applied.
* @memberof Billboard.prototype
* @type {Number}
* @default 0.0
*/
disableDepthTestDistance : {
get : function() {
Expand All @@ -798,7 +797,7 @@ define([
set : function(value) {
if (this._disableDepthTestDistance !== value) {
//>>includeStart('debug', pragmas.debug);
if (!defined(value) || value < 0.0) {
if (defined(value) && value < 0.0) {
throw new DeveloperError('disableDepthTestDistance must be greater than or equal to 0.0.');
}
//>>includeEnd('debug');
Expand Down
72 changes: 26 additions & 46 deletions Source/Scene/BillboardCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,6 @@ define([
this._shaderDisableDepthDistance = false;
this._compiledShaderDisableDepthDistance = false;

this._shaderClampToGround = false;
this._compiledShaderClampToGround = false;

this._propertiesChanged = new Uint32Array(NUMBER_OF_PROPERTIES);

this._maxSize = 0.0;
Expand Down Expand Up @@ -1194,16 +1191,14 @@ define([
}

var disableDepthTestDistance = billboard.disableDepthTestDistance;
if (billboard.heightReference === HeightReference.CLAMP_TO_GROUND && disableDepthTestDistance === 0.0 && billboardCollection._scene.context.depthTexture) {
disableDepthTestDistance = 2000.0;
if (!defined(disableDepthTestDistance)) {
disableDepthTestDistance = context.depthTexture ? 5000.0 : 0.0;
}

disableDepthTestDistance *= disableDepthTestDistance;
if (disableDepthTestDistance > 0.0) {
billboardCollection._shaderDisableDepthDistance = true;
if (disableDepthTestDistance === Number.POSITIVE_INFINITY) {
disableDepthTestDistance = -1.0;
}
billboardCollection._shaderDisableDepthDistance = true;
if (disableDepthTestDistance === Number.POSITIVE_INFINITY) {
disableDepthTestDistance = -1.0;
}

var imageHeight;
Expand Down Expand Up @@ -1252,9 +1247,6 @@ define([
}

function writeTextureCoordinateBoundsOrLabelTranslate(billboardCollection, context, textureAtlasCoordinates, vafWriters, billboard) {
if (billboard.heightReference === HeightReference.CLAMP_TO_GROUND) {
billboardCollection._shaderClampToGround = billboardCollection._scene.context.depthTexture;
}
var i;
var writer = vafWriters[attributeLocations.textureCoordinateBoundsOrLabelTranslate];

Expand Down Expand Up @@ -1665,8 +1657,7 @@ define([
(this._shaderTranslucencyByDistance !== this._compiledShaderTranslucencyByDistance) ||
(this._shaderPixelOffsetScaleByDistance !== this._compiledShaderPixelOffsetScaleByDistance) ||
(this._shaderDistanceDisplayCondition !== this._compiledShaderDistanceDisplayCondition) ||
(this._shaderDisableDepthDistance !== this._compiledShaderDisableDepthDistance) ||
(this._shaderClampToGround !== this._compiledShaderClampToGround)) {
(this._shaderDisableDepthDistance !== this._compiledShaderDisableDepthDistance)) {

vsSource = BillboardCollectionVS;
fsSource = BillboardCollectionFS;
Expand Down Expand Up @@ -1706,12 +1697,10 @@ define([
if (this._shaderDisableDepthDistance) {
vs.defines.push('DISABLE_DEPTH_DISTANCE');
}
if (this._shaderClampToGround) {
if (supportVSTextureReads) {
vs.defines.push('VERTEX_DEPTH_CHECK');
} else {
vs.defines.push('FRAGMENT_DEPTH_CHECK');
}
if (supportVSTextureReads) {
vs.defines.push('VERTEX_DEPTH_CHECK');
} else {
vs.defines.push('FRAGMENT_DEPTH_CHECK');
}

var vectorFragDefine = defined(this._batchTable) ? 'VECTOR_TILE' : '';
Expand All @@ -1721,12 +1710,10 @@ define([
defines : ['OPAQUE', vectorFragDefine],
sources : [fsSource]
});
if (this._shaderClampToGround) {
if (supportVSTextureReads) {
fs.defines.push('VERTEX_DEPTH_CHECK');
} else {
fs.defines.push('FRAGMENT_DEPTH_CHECK');
}
if (supportVSTextureReads) {
fs.defines.push('VERTEX_DEPTH_CHECK');
} else {
fs.defines.push('FRAGMENT_DEPTH_CHECK');
}
this._sp = ShaderProgram.replaceCache({
context : context,
Expand All @@ -1740,12 +1727,10 @@ define([
defines : ['TRANSLUCENT', vectorFragDefine],
sources : [fsSource]
});
if (this._shaderClampToGround) {
if (supportVSTextureReads) {
fs.defines.push('VERTEX_DEPTH_CHECK');
} else {
fs.defines.push('FRAGMENT_DEPTH_CHECK');
}
if (supportVSTextureReads) {
fs.defines.push('VERTEX_DEPTH_CHECK');
} else {
fs.defines.push('FRAGMENT_DEPTH_CHECK');
}
this._spTranslucent = ShaderProgram.replaceCache({
context : context,
Expand All @@ -1761,12 +1746,10 @@ define([
defines : [vectorFragDefine],
sources : [fsSource]
});
if (this._shaderClampToGround) {
if (supportVSTextureReads) {
fs.defines.push('VERTEX_DEPTH_CHECK');
} else {
fs.defines.push('FRAGMENT_DEPTH_CHECK');
}
if (supportVSTextureReads) {
fs.defines.push('VERTEX_DEPTH_CHECK');
} else {
fs.defines.push('FRAGMENT_DEPTH_CHECK');
}
this._sp = ShaderProgram.replaceCache({
context : context,
Expand All @@ -1782,12 +1765,10 @@ define([
defines : [vectorFragDefine],
sources : [fsSource]
});
if (this._shaderClampToGround) {
if (supportVSTextureReads) {
fs.defines.push('VERTEX_DEPTH_CHECK');
} else {
fs.defines.push('FRAGMENT_DEPTH_CHECK');
}
if (supportVSTextureReads) {
fs.defines.push('VERTEX_DEPTH_CHECK');
} else {
fs.defines.push('FRAGMENT_DEPTH_CHECK');
}
this._spTranslucent = ShaderProgram.replaceCache({
context : context,
Expand All @@ -1805,7 +1786,6 @@ define([
this._compiledShaderPixelOffsetScaleByDistance = this._shaderPixelOffsetScaleByDistance;
this._compiledShaderDistanceDisplayCondition = this._shaderDistanceDisplayCondition;
this._compiledShaderDisableDepthDistance = this._shaderDisableDepthDistance;
this._compiledShaderClampToGround = this._shaderClampToGround;
}

var commandList = frameState.commandList;
Expand Down
4 changes: 2 additions & 2 deletions Source/Scene/Label.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ define([
this._scaleByDistance = scaleByDistance;
this._heightReference = defaultValue(options.heightReference, HeightReference.NONE);
this._distanceDisplayCondition = distanceDisplayCondition;
this._disableDepthTestDistance = defaultValue(options.disableDepthTestDistance, 0.0);
this._disableDepthTestDistance = options.disableDepthTestDistance;

this._labelCollection = labelCollection;
this._glyphs = [];
Expand Down Expand Up @@ -938,7 +938,7 @@ define([
set : function(value) {
if (this._disableDepthTestDistance !== value) {
//>>includeStart('debug', pragmas.debug);
if (!defined(value) || value < 0.0) {
if (defined(value) && value < 0.0) {
throw new DeveloperError('disableDepthTestDistance must be greater than 0.0.');
}
//>>includeEnd('debug');
Expand Down
25 changes: 14 additions & 11 deletions Source/Shaders/BillboardCollectionVS.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,6 @@ void main()

depthOrigin = vec2(1.0) - (depthOrigin * 0.5);
#endif
#if defined(VERTEX_DEPTH_CHECK) || defined(FRAGMENT_DEPTH_CHECK)
temp = compressedAttribute3.w;
temp = temp * SHIFT_RIGHT12;

vec2 dimensions;
dimensions.y = (temp - floor(temp)) * SHIFT_LEFT12;
dimensions.x = floor(temp);
#endif

#ifdef EYE_DISTANCE_TRANSLUCENCY
vec4 translucencyByDistance;
Expand All @@ -216,6 +208,15 @@ void main()
translucencyByDistance.w = ((temp - floor(temp)) * SHIFT_LEFT8) / 255.0;
#endif

#if defined(VERTEX_DEPTH_CHECK) || defined(FRAGMENT_DEPTH_CHECK)
temp = compressedAttribute3.w;
temp = temp * SHIFT_RIGHT12;

vec2 dimensions;
dimensions.y = (temp - floor(temp)) * SHIFT_LEFT12;
dimensions.x = floor(temp);
#endif

#ifdef ALIGNED_AXIS
vec3 alignedAxis = czm_octDecode(floor(compressedAttribute1.y * SHIFT_RIGHT8));
temp = compressedAttribute2.z * SHIFT_RIGHT5;
Expand Down Expand Up @@ -328,16 +329,18 @@ if (lengthSq < disableDepthTestDistance) {
vec4 pEC1 = addScreenSpaceOffset(positionEC, dimensions, scale, vec2(0.0), origin, labelTranslate, pixelOffset, alignedAxis, validAlignedAxis, rotation, sizeInMeters, rotationMatrix, mpp);
float globeDepth1 = getGlobeDepth(pEC1);

if (globeDepth1 != 0.0 && pEC1.z < globeDepth1)
float depthsilon = 10.0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where did you come up with 10.0? Have you tested other areas of terrain?

Great variable name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I tested on both flat areas and mountainy areas. 10 was the lowest value that mostly got rid of the flickering. Other values I tested like 5 and 7 still had a lot of flickering when the terrain had more drastic slopes


if (globeDepth1 != 0.0 && pEC1.z + depthsilon < globeDepth1)
{
vec4 pEC2 = addScreenSpaceOffset(positionEC, dimensions, scale, vec2(0.0, 1.0), origin, labelTranslate, pixelOffset, alignedAxis, validAlignedAxis, rotation, sizeInMeters, rotationMatrix, mpp);
float globeDepth2 = getGlobeDepth(pEC2);

if (globeDepth2 != 0.0 && pEC2.z < globeDepth2)
if (globeDepth2 != 0.0 && pEC2.z + depthsilon < globeDepth2)
{
vec4 pEC3 = addScreenSpaceOffset(positionEC, dimensions, scale, vec2(1.0), origin, labelTranslate, pixelOffset, alignedAxis, validAlignedAxis, rotation, sizeInMeters, rotationMatrix, mpp);
float globeDepth3 = getGlobeDepth(pEC3);
if (globeDepth3 != 0.0 && pEC3.z < globeDepth3)
if (globeDepth3 != 0.0 && pEC3.z + depthsilon < globeDepth3)
{
positionEC.xyz = vec3(0.0);
}
Expand Down
Loading