Skip to content

Commit

Permalink
Fix rendering bug using terrain (#1841)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pessimistress committed Apr 21, 2022
1 parent 2e56787 commit 1927497
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/mapbox/mapbox.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {transformToViewState, applyViewStateToTransform} from '../utils/transform';
import {transformToViewState, applyViewStateToTransform, cloneTransform} from '../utils/transform';
import {normalizeStyle} from '../utils/style-utils';
import {deepEqual} from '../utils/deep-equal';

Expand Down Expand Up @@ -451,7 +451,7 @@ export default class Mapbox {

const settingsChanged = this._updateSettings(props, oldProps);
if (settingsChanged) {
this._renderTransform = this._map.transform.clone();
this._renderTransform = cloneTransform(this._map.transform);
}
const sizeChanged = this._updateSize(props);
const viewStateChanged = this._updateViewState(props, true);
Expand Down Expand Up @@ -542,7 +542,7 @@ export default class Mapbox {
if (props.cursor) {
map.getCanvas().style.cursor = props.cursor;
}
this._renderTransform = map.transform.clone();
this._renderTransform = cloneTransform(map.transform);

// Hack
// Insert code into map's render cycle
Expand Down Expand Up @@ -723,11 +723,10 @@ export default class Mapbox {
if (!nextProps.terrain || map.getSource(nextProps.terrain.source)) {
changed = true;
map.setTerrain(nextProps.terrain);
// Copy changes to the transform
// @ts-ignore
this._renderTransform.elevation = map.transform.elevation;
}
}
// Copy changes to the transform
this._renderTransform.elevation = map.transform.elevation;
}
return changed;
}
Expand Down Expand Up @@ -878,6 +877,7 @@ export default class Mapbox {
const tr = this._map.transform;
// Make sure camera matches the current props
this._map.transform = this._renderTransform;
this._map.painter.transform = this._renderTransform;

this._onAfterRepaint = () => {
// Restores camera state before render/load events are fired
Expand Down
2 changes: 2 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export type Transform = {
bearing: number;
pitch: number;
padding: PaddingOptions;
elevation: any;
pixelsToGLUnits: [number, number];

clone: () => Transform;
resize: (width: number, height: number) => void;
Expand Down
11 changes: 11 additions & 0 deletions src/utils/transform.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import type {MapboxProps} from '../mapbox/mapbox';
import type {Transform, ViewState} from '../types';

/**
* Make a copy of a transform
* @param tr
*/
export function cloneTransform(tr: Transform): Transform {
const newTransform = tr.clone();
// Work around mapbox bug - this value is not assigned in clone(), only in resize()
newTransform.pixelsToGLUnits = tr.pixelsToGLUnits;
return newTransform;
}

/**
* Capture a transform's current state
* @param transform
Expand Down
1 change: 1 addition & 0 deletions test/src/utils/mapbox-gl-mock/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export default class Map extends Evented {
this.transform.zoom = this.options.zoom || 0;
this.transform.pitch = this.options.pitch || 0;
this.transform.bearing = this.options.bearing || 0;
this.painter = {transform: this.transform};

setTimeout(() => {
this.style._loaded = true;
Expand Down

0 comments on commit 1927497

Please sign in to comment.