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

Align projection matrix to pixel grids to draw crisp raster tiles #6026

Merged
merged 1 commit into from
Jan 22, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
48 changes: 48 additions & 0 deletions debug/raster-streets.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!DOCTYPE html>
<html>
<head>
<title>Mapbox GL JS debug page</title>
<meta charset='utf-8'>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<link rel='stylesheet' href='/dist/mapbox-gl.css' />
<style>
body { margin: 0; padding: 0; }
html, body, #map { height: 100%; }
</style>
</head>

<body>
<div id='map'></div>

<script src='/dist/mapbox-gl-dev.js'></script>
<script src='/debug/access_token_generated.js'></script>
<script>

var map = window.map = new mapboxgl.Map({
container: 'map',
zoom: 12.5,
center: [-77.01866, 38.888],
style: {
"version": 8,
"sources": {
"raster-tiles": {
"type": "raster",
"url": "mapbox://mapbox.streets",
"tileSize": 256
}
},
"layers": [{
"id": "simple-tiles",
"type": "raster",
"source": "raster-tiles",
"minzoom": 0,
"maxzoom": 22
}]
},
hash: true
});


</script>
</body>
</html>
34 changes: 27 additions & 7 deletions src/geo/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Transform {
pixelsToGLUnits: Array<number>;
cameraToCenterDistance: number;
projMatrix: Float64Array;
alignedProjMatrix: Float64Array;
pixelMatrix: Float64Array;
pixelMatrixInverse: Float64Array;
_fov: number;
Expand All @@ -45,6 +46,7 @@ class Transform {
_center: LngLat;
_constraining: boolean;
_posMatrixCache: {[number]: Float32Array};
_alignedPosMatrixCache: {[number]: Float32Array};

constructor(minZoom: ?number, maxZoom: ?number, renderWorldCopies: boolean | void) {
this.tileSize = 512; // constant
Expand All @@ -64,6 +66,7 @@ class Transform {
this._pitch = 0;
this._unmodified = true;
this._posMatrixCache = {};
this._alignedPosMatrixCache = {};
}

clone(): Transform {
Expand Down Expand Up @@ -398,10 +401,11 @@ class Transform {
* Calculate the posMatrix that, given a tile coordinate, would be used to display the tile on a map.
* @param {UnwrappedTileID} unwrappedTileID;
*/
calculatePosMatrix(unwrappedTileID: UnwrappedTileID): Float32Array {
calculatePosMatrix(unwrappedTileID: UnwrappedTileID, aligned: boolean = false): Float32Array {
const posMatrixKey = unwrappedTileID.key;
if (this._posMatrixCache[posMatrixKey]) {
return this._posMatrixCache[posMatrixKey];
const cache = aligned ? this._alignedPosMatrixCache : this._posMatrixCache;
if (cache[posMatrixKey]) {
return cache[posMatrixKey];
}

const canonical = unwrappedTileID.canonical;
Expand All @@ -411,10 +415,10 @@ class Transform {
const posMatrix = mat4.identity(new Float64Array(16));
mat4.translate(posMatrix, posMatrix, [unwrappedX * scale, canonical.y * scale, 0]);
mat4.scale(posMatrix, posMatrix, [scale / EXTENT, scale / EXTENT, 1]);
mat4.multiply(posMatrix, this.projMatrix, posMatrix);
mat4.multiply(posMatrix, aligned ? this.alignedProjMatrix : this.projMatrix, posMatrix);

this._posMatrixCache[posMatrixKey] = new Float32Array(posMatrix);
return this._posMatrixCache[posMatrixKey];
cache[posMatrixKey] = new Float32Array(posMatrix);
return cache[posMatrixKey];
}

_constrain() {
Expand Down Expand Up @@ -496,6 +500,7 @@ class Transform {
const halfFov = this._fov / 2;
const groundAngle = Math.PI / 2 + this._pitch;
const topHalfSurfaceDistance = Math.sin(halfFov) * this.cameraToCenterDistance / Math.sin(Math.PI - groundAngle - halfFov);
const x = this.x, y = this.y;

// Calculate z distance of the farthest fragment that should be rendered.
const furthestDistance = Math.cos(Math.PI / 2 - this._pitch) * topHalfSurfaceDistance + this.cameraToCenterDistance;
Expand All @@ -510,7 +515,7 @@ class Transform {
mat4.translate(m, m, [0, 0, -this.cameraToCenterDistance]);
mat4.rotateX(m, m, this._pitch);
mat4.rotateZ(m, m, this.angle);
mat4.translate(m, m, [-this.x, -this.y, 0]);
mat4.translate(m, m, [-x, -y, 0]);

// scale vertically to meters per pixel (inverse of ground resolution):
// worldSize / (circumferenceOfEarth * cos(lat * π / 180))
Expand All @@ -519,6 +524,20 @@ class Transform {

this.projMatrix = m;

// Make a second projection matrix that is aligned to a pixel grid for rendering raster tiles.
// We're rounding the (floating point) x/y values to achieve to avoid rendering raster images to fractional
// coordinates. Additionally, we adjust by half a pixel in either direction in case that viewport dimension
// is an odd integer to preserve rendering to the pixel grid. We're rotating this shift based on the angle
// of the transformation so that 0°, 90°, 180°, and 270° rasters are crisp, and adjust the shift so that
// it is always <= 0.5 pixels.
const xShift = (this.width % 2) / 2, yShift = (this.height % 2) / 2,
angleCos = Math.cos(this.angle), angleSin = Math.sin(this.angle),
dx = x - Math.round(x) + angleCos * xShift + angleSin * yShift,
dy = y - Math.round(y) + angleCos * yShift + angleSin * xShift;
const alignedM = new Float64Array(m);
mat4.translate(alignedM, alignedM, [ dx > 0.5 ? dx - 1 : dx, dy > 0.5 ? dy - 1 : dy, 0 ]);
this.alignedProjMatrix = alignedM;

// matrix for conversion from location to screen coordinates
m = mat4.create();
mat4.scale(m, m, [this.width / 2, -this.height / 2, 1]);
Expand All @@ -531,6 +550,7 @@ class Transform {
this.pixelMatrixInverse = m;

this._posMatrixCache = {};
this._alignedPosMatrixCache = {};
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/render/draw_raster.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function drawRaster(painter: Painter, sourceCache: SourceCache, layer: RasterSty
layer.paint.get('raster-opacity') === 1 ? DepthMode.ReadWrite : DepthMode.ReadOnly, gl.LESS));

const tile = sourceCache.getTile(coord);
const posMatrix = painter.transform.calculatePosMatrix(coord.toUnwrapped());
const posMatrix = painter.transform.calculatePosMatrix(coord.toUnwrapped(), true);

tile.registerFadeDuration(layer.paint.get('raster-fade-duration'));

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/integration/render-tests/image/default/expected.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/integration/render-tests/image/pitched/expected.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/integration/render-tests/image/raster-brightness/expected.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/integration/render-tests/image/raster-contrast/expected.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/integration/render-tests/image/raster-hue-rotate/expected.png
Binary file modified test/integration/render-tests/image/raster-opacity/expected.png
Binary file modified test/integration/render-tests/image/raster-saturation/expected.png
Binary file modified test/integration/render-tests/image/raster-visibility/expected.png
Binary file modified test/integration/render-tests/raster-alpha/default/expected.png
34 changes: 34 additions & 0 deletions test/integration/render-tests/raster-rotation/0/style.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"version": 8,
"metadata": {
"test": {
"height": 256
}
},
"center": [
13.418056,
52.499167
],
"zoom": 16,
"bearing": 0,
"sources": {
"satellite": {
"type": "raster",
"tiles": [
"local://tiles/{z}-{x}-{y}.satellite.png"
],
"maxzoom": 17,
"tileSize": 256
}
},
"layers": [
{
"id": "raster",
"type": "raster",
"source": "satellite",
"paint": {
"raster-fade-duration": 0
}
}
]
}
34 changes: 34 additions & 0 deletions test/integration/render-tests/raster-rotation/180/style.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"version": 8,
"metadata": {
"test": {
"height": 256
}
},
"center": [
13.418056,
52.499167
],
"zoom": 16,
"bearing": 180,
"sources": {
"satellite": {
"type": "raster",
"tiles": [
"local://tiles/{z}-{x}-{y}.satellite.png"
],
"maxzoom": 17,
"tileSize": 256
}
},
"layers": [
{
"id": "raster",
"type": "raster",
"source": "satellite",
"paint": {
"raster-fade-duration": 0
}
}
]
}
34 changes: 34 additions & 0 deletions test/integration/render-tests/raster-rotation/270/style.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"version": 8,
"metadata": {
"test": {
"height": 256
}
},
"center": [
13.418056,
52.499167
],
"zoom": 16,
"bearing": 270,
"sources": {
"satellite": {
"type": "raster",
"tiles": [
"local://tiles/{z}-{x}-{y}.satellite.png"
],
"maxzoom": 17,
"tileSize": 256
}
},
"layers": [
{
"id": "raster",
"type": "raster",
"source": "satellite",
"paint": {
"raster-fade-duration": 0
}
}
]
}
34 changes: 34 additions & 0 deletions test/integration/render-tests/raster-rotation/45/style.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"version": 8,
"metadata": {
"test": {
"height": 256
}
},
"center": [
13.418056,
52.499167
],
"zoom": 16,
"bearing": 45,
"sources": {
"satellite": {
"type": "raster",
"tiles": [
"local://tiles/{z}-{x}-{y}.satellite.png"
],
"maxzoom": 17,
"tileSize": 256
}
},
"layers": [
{
"id": "raster",
"type": "raster",
"source": "satellite",
"paint": {
"raster-fade-duration": 0
}
}
]
}
34 changes: 34 additions & 0 deletions test/integration/render-tests/raster-rotation/90/style.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"version": 8,
"metadata": {
"test": {
"height": 256
}
},
"center": [
13.418056,
52.499167
],
"zoom": 16,
"bearing": 90,
"sources": {
"satellite": {
"type": "raster",
"tiles": [
"local://tiles/{z}-{x}-{y}.satellite.png"
],
"maxzoom": 17,
"tileSize": 256
}
},
"layers": [
{
"id": "raster",
"type": "raster",
"source": "satellite",
"paint": {
"raster-fade-duration": 0
}
}
]
}
Binary file modified test/integration/render-tests/video/default/expected.png
Binary file modified test/integration/render-tests/zoomed-raster/fractional/expected.png