@deck.gl/mapbox 8.8.11 → 8.9.0-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/deck-utils.ts CHANGED
@@ -3,13 +3,19 @@ import type {DeckProps, MapViewState, Layer} from '@deck.gl/core';
3
3
  import type MapboxLayer from './mapbox-layer';
4
4
  import type {Map} from 'mapbox-gl';
5
5
 
6
+ import {lngLatToWorld, unitsPerMeter} from '@math.gl/web-mercator';
7
+
6
8
  type UserData = {
7
9
  isExternal: boolean;
8
10
  currentViewport?: WebMercatorViewport | null;
9
11
  mapboxLayers: Set<MapboxLayer<any>>;
10
- mapboxVersion: {minor: number; major: number};
12
+ // mapboxVersion: {minor: number; major: number};
11
13
  };
12
14
 
15
+ // Mercator constants
16
+ const TILE_SIZE = 512;
17
+ const DEGREES_TO_RADIANS = Math.PI / 180;
18
+
13
19
  export function getDeckInstance({
14
20
  map,
15
21
  gl,
@@ -79,7 +85,7 @@ export function getDeckInstance({
79
85
  }
80
86
 
81
87
  (deckInstance.userData as UserData).mapboxLayers = new Set();
82
- (deckInstance.userData as UserData).mapboxVersion = getMapboxVersion(map);
88
+ // (deckInstance.userData as UserData).mapboxVersion = getMapboxVersion(map);
83
89
  map.__deck = deckInstance;
84
90
  map.on('render', () => {
85
91
  if (deckInstance.isInitialized) afterRender(deckInstance, map);
@@ -135,7 +141,16 @@ export function getViewState(map: Map): MapViewState & {
135
141
  };
136
142
  } {
137
143
  const {lng, lat} = map.getCenter();
138
- return {
144
+
145
+ const viewState: MapViewState & {
146
+ repeat: boolean;
147
+ padding: {
148
+ left: number;
149
+ right: number;
150
+ top: number;
151
+ bottom: number;
152
+ };
153
+ } = {
139
154
  // Longitude returned by getCenter can be outside of [-180, 180] when zooming near the anti meridian
140
155
  // https://github.com/visgl/deck.gl/issues/6894
141
156
  longitude: ((lng + 540) % 360) - 180,
@@ -146,49 +161,81 @@ export function getViewState(map: Map): MapViewState & {
146
161
  padding: map.getPadding(),
147
162
  repeat: map.getRenderWorldCopies()
148
163
  };
164
+
165
+ if (map.getTerrain?.()) {
166
+ // When the base map has terrain, we need to target the camera at the terrain surface
167
+ centerCameraOnTerrain(map, viewState);
168
+ }
169
+
170
+ return viewState;
149
171
  }
150
172
 
151
- function getMapboxVersion(map: Map): {minor: number; major: number} {
152
- // parse mapbox version string
153
- let major = 0;
154
- let minor = 0;
155
- // @ts-ignore (2339) undefined property
156
- const version: string = map.version;
157
- if (version) {
158
- [major, minor] = version.split('.').slice(0, 2).map(Number);
173
+ function centerCameraOnTerrain(map: Map, viewState: MapViewState) {
174
+ if (map.getFreeCameraOptions) {
175
+ // mapbox-gl v2
176
+ const {position} = map.getFreeCameraOptions();
177
+ if (!position || position.z === undefined) {
178
+ return;
179
+ }
180
+
181
+ // @ts-ignore transform is not typed
182
+ const height = map.transform.height;
183
+ const {longitude, latitude, pitch} = viewState;
184
+
185
+ // Convert mapbox mercator coordinate to deck common space
186
+ const cameraX = position.x * TILE_SIZE;
187
+ const cameraY = (1 - position.y) * TILE_SIZE;
188
+ const cameraZ = position.z * TILE_SIZE;
189
+
190
+ // Mapbox manipulates zoom in terrain mode, see discussion here: https://github.com/mapbox/mapbox-gl-js/issues/12040
191
+ const center = lngLatToWorld([longitude, latitude]);
192
+ const dx = cameraX - center[0];
193
+ const dy = cameraY - center[1];
194
+ const cameraToCenterDistanceGround = Math.sqrt(dx * dx + dy * dy);
195
+
196
+ const pitchRadians = pitch! * DEGREES_TO_RADIANS;
197
+ const altitudePixels = 1.5 * height;
198
+ const scale = (altitudePixels * Math.sin(pitchRadians)) / cameraToCenterDistanceGround;
199
+ viewState.zoom = Math.log2(scale);
200
+
201
+ const cameraZFromSurface = (altitudePixels * Math.cos(pitchRadians)) / scale;
202
+ const surfaceElevation = cameraZ - cameraZFromSurface;
203
+ viewState.position = [0, 0, surfaceElevation / unitsPerMeter(latitude)];
204
+ }
205
+ // @ts-ignore transform is not typed
206
+ else if (typeof map.transform.elevation === 'number') {
207
+ // maplibre-gl
208
+ // @ts-ignore transform is not typed
209
+ viewState.position = [0, 0, map.transform.elevation];
159
210
  }
160
- return {major, minor};
161
211
  }
162
212
 
213
+ // function getMapboxVersion(map: Map): {minor: number; major: number} {
214
+ // // parse mapbox version string
215
+ // let major = 0;
216
+ // let minor = 0;
217
+ // // @ts-ignore (2339) undefined property
218
+ // const version: string = map.version;
219
+ // if (version) {
220
+ // [major, minor] = version.split('.').slice(0, 2).map(Number);
221
+ // }
222
+ // return {major, minor};
223
+ // }
224
+
163
225
  function getViewport(deck: Deck, map: Map, useMapboxProjection = true): WebMercatorViewport {
164
- const {mapboxVersion} = deck.userData as UserData;
165
-
166
- return new WebMercatorViewport(
167
- Object.assign(
168
- {
169
- id: 'mapbox',
170
- x: 0,
171
- y: 0,
172
- width: deck.width,
173
- height: deck.height
174
- },
175
- getViewState(map),
176
- useMapboxProjection
177
- ? {
178
- // match mapbox's projection matrix
179
- // A change of near plane was made in 1.3.0
180
- // https://github.com/mapbox/mapbox-gl-js/pull/8502
181
- nearZMultiplier:
182
- (mapboxVersion.major === 1 && mapboxVersion.minor >= 3) || mapboxVersion.major >= 2
183
- ? 0.02
184
- : 1 / (deck.height || 1)
185
- }
186
- : {
187
- // use deck.gl's own default
188
- nearZMultiplier: 0.1
189
- }
190
- )
191
- );
226
+ return new WebMercatorViewport({
227
+ id: 'mapbox',
228
+ x: 0,
229
+ y: 0,
230
+ width: deck.width,
231
+ height: deck.height,
232
+ ...getViewState(map),
233
+ nearZMultiplier: useMapboxProjection
234
+ ? // match mapbox-gl@>=1.3.0's projection matrix
235
+ 0.02
236
+ : // use deck.gl's own default
237
+ 0.1
238
+ });
192
239
  }
193
240
 
194
241
  function afterRender(deck: Deck, map: Map): void {
@@ -1 +1 @@
1
- {"version":3,"file":"deck-utils.d.ts","sourceRoot":"","sources":["../src/deck-utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,IAAI,EAAoD,MAAM,eAAe,CAAC;AACt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