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

Fixed resolution scale for globe lighting and labels #8351

Merged
merged 3 commits into from
Nov 6, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 4 additions & 5 deletions Source/Scene/Label.js
Original file line number Diff line number Diff line change
Expand Up @@ -1133,12 +1133,11 @@ import VerticalOrigin from './VerticalOrigin.js';
var width = 0;
var height = 0;
var scale = label.totalScale;
var resolutionScale = label._labelCollection._resolutionScale;

var backgroundBillboard = label._backgroundBillboard;
if (defined(backgroundBillboard)) {
x = screenSpacePosition.x + (backgroundBillboard._translate.x / resolutionScale);
y = screenSpacePosition.y - (backgroundBillboard._translate.y / resolutionScale);
x = screenSpacePosition.x + (backgroundBillboard._translate.x);
y = screenSpacePosition.y - (backgroundBillboard._translate.y);
width = backgroundBillboard.width * scale;
height = backgroundBillboard.height * scale;

Expand All @@ -1161,8 +1160,8 @@ import VerticalOrigin from './VerticalOrigin.js';
continue;
}

var glyphX = screenSpacePosition.x + (billboard._translate.x / resolutionScale);
var glyphY = screenSpacePosition.y - (billboard._translate.y / resolutionScale);
var glyphX = screenSpacePosition.x + (billboard._translate.x);
var glyphY = screenSpacePosition.y - (billboard._translate.y);
var glyphWidth = glyph.dimensions.width * scale;
var glyphHeight = glyph.dimensions.height * scale;

Expand Down
37 changes: 12 additions & 25 deletions Source/Scene/LabelCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ import GraphemeSplitter from '../ThirdParty/graphemesplitter.js';
var glyphPixelOffset = new Cartesian2();
var scratchBackgroundPadding = new Cartesian2();

function repositionAllGlyphs(label, resolutionScale) {
function repositionAllGlyphs(label) {
var glyphs = label._glyphs;
var text = label._renderedText;
var glyph;
Expand Down Expand Up @@ -378,7 +378,7 @@ import GraphemeSplitter from '../ThirdParty/graphemesplitter.js';
backgroundBillboard._labelHorizontalOrigin = horizontalOrigin;
}

glyphPixelOffset.x = widthOffset * scale * resolutionScale;
glyphPixelOffset.x = widthOffset * scale;
glyphPixelOffset.y = 0;

var firstCharOfLine = true;
Expand All @@ -390,7 +390,7 @@ import GraphemeSplitter from '../ThirdParty/graphemesplitter.js';
lineOffsetY += lineSpacing;
lineWidth = lineWidths[lineIndex];
widthOffset = calculateWidthOffset(lineWidth, horizontalOrigin, backgroundPadding);
glyphPixelOffset.x = widthOffset * scale * resolutionScale;
glyphPixelOffset.x = widthOffset * scale;
firstCharOfLine = true;
} else {
glyph = glyphs[glyphIndex];
Expand All @@ -409,12 +409,12 @@ import GraphemeSplitter from '../ThirdParty/graphemesplitter.js';
glyphPixelOffset.y = otherLinesHeight + maxGlyphDescent + backgroundPadding.y;
glyphPixelOffset.y -= SDFSettings.PADDING;
}
glyphPixelOffset.y = (glyphPixelOffset.y - dimensions.descent - lineOffsetY) * scale * resolutionScale;
glyphPixelOffset.y = (glyphPixelOffset.y - dimensions.descent - lineOffsetY) * scale;

// Handle any offsets for the first character of the line since the bounds might not be right on the bottom left pixel.
if (firstCharOfLine)
{
glyphPixelOffset.x -= SDFSettings.PADDING * scale * resolutionScale;
glyphPixelOffset.x -= SDFSettings.PADDING * scale;
firstCharOfLine = false;
}

Expand All @@ -430,7 +430,7 @@ import GraphemeSplitter from '../ThirdParty/graphemesplitter.js';
//as well as any applied scale.
if (glyphIndex < glyphLength - 1) {
var nextGlyph = glyphs[glyphIndex + 1];
glyphPixelOffset.x += ((dimensions.width - dimensions.bounds.minx) + nextGlyph.dimensions.bounds.minx) * scale * resolutionScale;
glyphPixelOffset.x += ((dimensions.width - dimensions.bounds.minx) + nextGlyph.dimensions.bounds.minx) * scale;
}
}
}
Expand All @@ -443,7 +443,7 @@ import GraphemeSplitter from '../ThirdParty/graphemesplitter.js';
} else {
widthOffset = 0;
}
glyphPixelOffset.x = widthOffset * scale * resolutionScale;
glyphPixelOffset.x = widthOffset * scale;

if (verticalOrigin === VerticalOrigin.TOP) {
glyphPixelOffset.y = maxLineHeight - maxGlyphY - maxGlyphDescent;
Expand All @@ -455,7 +455,7 @@ import GraphemeSplitter from '../ThirdParty/graphemesplitter.js';
// VerticalOrigin.BOTTOM
glyphPixelOffset.y = 0;
}
glyphPixelOffset.y = glyphPixelOffset.y * scale * resolutionScale;
glyphPixelOffset.y = glyphPixelOffset.y * scale;

backgroundBillboard.width = totalLineWidth;
backgroundBillboard.height = totalLineHeight;
Expand Down Expand Up @@ -566,7 +566,6 @@ import GraphemeSplitter from '../ThirdParty/graphemesplitter.js';
this._labels = [];
this._labelsToUpdate = [];
this._totalGlyphCount = 0;
this._resolutionScale = undefined;

this._highlightColor = Color.clone(Color.WHITE); // Only used by Vector3DTilePoints

Expand Down Expand Up @@ -844,21 +843,9 @@ import GraphemeSplitter from '../ThirdParty/graphemesplitter.js';
addWhitePixelCanvas(this._backgroundTextureAtlas, this);
}

var uniformState = context.uniformState;
var resolutionScale = uniformState.pixelRatio;
var resolutionChanged = this._resolutionScale !== resolutionScale;
this._resolutionScale = resolutionScale;

var labelsToUpdate;
if (resolutionChanged) {
labelsToUpdate = this._labels;
} else {
labelsToUpdate = this._labelsToUpdate;
}

var len = labelsToUpdate.length;
var len = this._labelsToUpdate.length;
for (var i = 0; i < len; ++i) {
var label = labelsToUpdate[i];
var label = this._labelsToUpdate[i];
if (label.isDestroyed()) {
continue;
}
Expand All @@ -870,8 +857,8 @@ import GraphemeSplitter from '../ThirdParty/graphemesplitter.js';
label._rebindAllGlyphs = false;
}

if (resolutionChanged || label._repositionAllGlyphs) {
repositionAllGlyphs(label, resolutionScale);
if (label._repositionAllGlyphs) {
repositionAllGlyphs(label);
label._repositionAllGlyphs = false;
}

Expand Down
20 changes: 2 additions & 18 deletions Source/Shaders/BillboardCollectionVS.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -83,26 +83,10 @@ vec4 addScreenSpaceOffset(vec4 positionEC, vec2 imageSize, float scale, vec2 dir
}
#endif

if (sizeInMeters)
{
positionEC.xy += halfSize;
}

mpp = czm_metersPerPixel(positionEC);
positionEC.xy += (originTranslate + halfSize) * (sizeInMeters ? 1.0 : mpp);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
positionEC.xy += (originTranslate + halfSize) * (sizeInMeters ? 1.0 : mpp);
positionEC.xy += (originTranslate + halfSize) * (czm_branchFreeTernary(sizeInMeters, 1.0, mpp));

positionEC.xy += (translate + pixelOffset) * mpp;

if (!sizeInMeters)
{
originTranslate *= mpp;
}

positionEC.xy += originTranslate;
if (!sizeInMeters)
{
positionEC.xy += halfSize * mpp;
}

positionEC.xy += translate * mpp;
positionEC.xy += pixelOffset * mpp;
return positionEC;
}

Expand Down
26 changes: 23 additions & 3 deletions Source/Shaders/Builtin/Functions/metersPerPixel.glsl
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
/**
* Computes the size of a pixel in meters at a distance from the eye.

* <p>
* Use this version when passing in a custom pixel ratio. For example, passing in 1.0 will return meters per native device pixel.
* </p>
* @name czm_metersPerPixel
* @glslFunction
*
* @param {vec3} positionEC The position to get the meters per pixel in eye coordinates.
* @param {float} pixelRatio The scaling factor from pixel space to coordinate space
*
* @returns {float} The meters per pixel at positionEC.
*/
float czm_metersPerPixel(vec4 positionEC)
float czm_metersPerPixel(vec4 positionEC, float pixelRatio)
{
float width = czm_viewport.z;
float height = czm_viewport.w;
Expand Down Expand Up @@ -37,5 +40,22 @@ float czm_metersPerPixel(vec4 positionEC)
pixelWidth = 2.0 * distanceToPixel * tanTheta / width;
}

return max(pixelWidth, pixelHeight) * czm_pixelRatio;
return max(pixelWidth, pixelHeight) * pixelRatio;
}

/**
* Computes the size of a pixel in meters at a distance from the eye.
* <p>
* Use this version when scaling by pixel ratio.
* </p>
* @name czm_metersPerPixel
* @glslFunction
*
* @param {vec3} positionEC The position to get the meters per pixel in eye coordinates.
*
* @returns {float} The meters per pixel at positionEC.
*/
float czm_metersPerPixel(vec4 positionEC)
{
return czm_metersPerPixel(positionEC, czm_pixelRatio);
}
2 changes: 1 addition & 1 deletion Source/Shaders/Builtin/Functions/writeLogDepth.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ varying float v_logZ;
/**
* Writes the fragment depth to the logarithmic depth buffer.
* <p>
* Use this when the vertex shader does not calls {@link czm_vertexlogDepth}, for example, when
* Use this when the vertex shader does not call {@link czm_vertexlogDepth}, for example, when
* ray-casting geometry using a full screen quad.
* </p>
* @name czm_writeLogDepth
Expand Down
2 changes: 1 addition & 1 deletion Source/Shaders/GlobeFS.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ void main()
}

#if defined(PER_FRAGMENT_GROUND_ATMOSPHERE) && (defined(ENABLE_DAYNIGHT_SHADING) || defined(ENABLE_VERTEX_LIGHTING))
float mpp = czm_metersPerPixel(vec4(0.0, 0.0, -czm_currentFrustum.x, 1.0));
float mpp = czm_metersPerPixel(vec4(0.0, 0.0, -czm_currentFrustum.x, 1.0), 1.0);
vec2 xy = gl_FragCoord.xy / czm_viewport.zw * 2.0 - vec2(1.0);
xy *= czm_viewport.zw * mpp * 0.5;

Expand Down