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

[MapboxLayer] integrate mapbox-gl's near plane fix #3490

Merged
merged 2 commits into from
Aug 26, 2019
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
26 changes: 23 additions & 3 deletions modules/mapbox/src/deck-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export function getDeckInstance({map, gl, deck}) {
map.__deck = null;
});
}
deck.props.userData.mapboxVersion = getMapboxVersion(map);
map.__deck = deck;
map.on('render', () => afterRender(deck, map));

Expand Down Expand Up @@ -101,7 +102,22 @@ function getViewState(map) {
};
}

function getMapboxVersion(map) {
// parse mapbox version string
let major = 0;
let minor = 0;
if (map.version) {
[major, minor] = map.version
.split('.')
.slice(0, 2)
.map(Number);
}
return {major, minor};
}

function getViewport(deck, map, useMapboxProjection = true) {
const {mapboxVersion} = deck.props.userData;

return new WebMercatorViewport(
Object.assign(
{
Expand All @@ -111,12 +127,16 @@ function getViewport(deck, map, useMapboxProjection = true) {
height: deck.height
},
getViewState(map),
// https://github.com/mapbox/mapbox-gl-js/issues/7573
useMapboxProjection
? {
// match mapbox's projection matrix
nearZMultiplier: deck.height ? 1 / deck.height : 1,
farZMultiplier: 1
// A change of near plane was made in 1.3.0
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: These nested conditionals are starting to look a little messy, maybe time to write a helper function for this.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Should some of this be codified in viewport-mercator-project? One of its main purposes has been to contain the knowledge of how to generate mapbox compatible matrices.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Mapbox has changed its projection matrix multiple times. We added nearZMultiplier and farZMultiplier to viewport-mercator-project so that we could accommodate all these use cases.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, sounds like we should just add a mapboxVersion parameter instead ;)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I would keep it for now. deck.gl is currently using the settings from mapbox 0.29. We have internal use cases where apps need to tweak the near/far planes to achieve 180 degree pitch rotation. We might want to further decouple the defaults of WebMercatorViewport from mapbox in v8.

// https://github.com/mapbox/mapbox-gl-js/pull/8502
nearZMultiplier:
(mapboxVersion.major === 1 && mapboxVersion.minor >= 3) || mapboxVersion.major >= 2
? 0.02
: 1 / (deck.height || 1),
farZMultiplier: 1.01
}
: {
// use deck.gl's projection matrix
Expand Down
4 changes: 4 additions & 0 deletions test/modules/mapbox/mapbox-layer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {gl} from '@deck.gl/test-utils';
class MockMapboxMap {
constructor(opts) {
this.opts = opts;
this.version = opts.version;
this._callbacks = {};
this._layers = {};
}
Expand Down Expand Up @@ -71,6 +72,7 @@ test('MapboxLayer#onAdd, onRemove, setProps', t => {
);

const map = new MockMapboxMap({
version: '1.10.0-beta.1',
center: {lng: -122.45, lat: 37.78},
zoom: 12
});
Expand All @@ -87,6 +89,7 @@ test('MapboxLayer#onAdd, onRemove, setProps', t => {
),
'Layer is added to deck'
);
t.deepEqual(deck.props.userData.mapboxVersion, {major: 1, minor: 10}, 'Mapbox version is parsed');

t.deepEqual(
deck.props.viewState,
Expand Down Expand Up @@ -157,6 +160,7 @@ test('MapboxLayer#external Deck', t => {
deck.props.onLoad = () => {
map.addLayer(layer);
t.is(layer.deck, deck, 'Used external Deck instance');
t.ok(deck.props.userData.mapboxVersion, 'Mapbox version is parsed');

map.emit('render');
t.pass('Map render does not throw');
Expand Down