Skip to content

Commit

Permalink
Bug Fix: updatePatternPositions fix (#3339)
Browse files Browse the repository at this point in the history
* updatePatternPositions fix

* spelling

* add testing

* adjust comments; better typescript

* jest fn; cleanup empty lines

* add changelog

* add changelog

---------

Co-authored-by: Craig O'Connor <craigoconnor@Craigs-MacBook-Air.local>
  • Loading branch information
CraigglesO and Craig O'Connor authored Nov 21, 2023
1 parent 32cd9fe commit 684b314
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
- _...Add new stuff here..._

### 🐞 Bug fixes
- _...Add new stuff here..._

- Fix mapbox-gl-draw example ([#2601](https://github.com/maplibre/maplibre-gl-js/issues/2601), [#3394](https://github.com/maplibre/maplibre-gl-js/pull/3394))
- Fix fill patterns sometimes not rendering at all ([#3339](https://github.com/maplibre/maplibre-gl-js/pull/3339))
- _...Add new stuff here..._

## 3.6.1

Expand Down
74 changes: 74 additions & 0 deletions src/render/update_pattern_positions_in_program.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import {Tile} from '../source/tile';
import {OverscaledTileID} from '../source/tile_id';
import {updatePatternPositionsInProgram} from './update_pattern_positions_in_program';
import {FillStyleLayer} from '../style/style_layer/fill_style_layer';
import type {CrossFaded} from '../style/properties';
import type {FillLayerSpecification, ResolvedImage} from '@maplibre/maplibre-gl-style-spec';
import type {ProgramConfiguration} from '../data/program_configuration';
import type {ImagePosition} from './image_atlas';
import type {Rect} from './glyph_atlas';

interface MockProgramConfiguration extends ProgramConfiguration {
patternPositions: {
posFrom: Rect;
posTo: Rect;
};
}

function constructMockProgramConfiguration(): MockProgramConfiguration {
const mockProgramConfiguration: MockProgramConfiguration = {patternPositions: {}} as any;
mockProgramConfiguration.updatePaintBuffers = jest.fn();
mockProgramConfiguration.setConstantPatternPositions = (posFrom: ImagePosition, posTo: ImagePosition) => {
// this does not exist on ProgramConfiguration but we want to test the resulting output
mockProgramConfiguration.patternPositions = {posFrom: posFrom.paddedRect, posTo: posTo.paddedRect};
};

return mockProgramConfiguration;
}

function constructMockFillStyleLayer(): FillStyleLayer {
const layerSpec = {
id: 'mock-layer',
source: 'empty-source',
type: 'fill',
layout: {},
'paint': {
'fill-pattern': [
'step',
['zoom'],
'zoo_11',
4,
'volcano_11'
]
}
} as FillLayerSpecification;
const layer = new FillStyleLayer(layerSpec);
return layer;
}

describe('updatePatternPositionsInProgram', () => {
test('geojson tile', () => {
const config = constructMockProgramConfiguration();
const tile = new Tile(new OverscaledTileID(3, 0, 2, 1, 2), undefined);
tile.imageAtlas = {} as any;
tile.imageAtlas.patternPositions = {
'volcano_11': {paddedRect: {x: 0, y: 0, w: 0, h: 0}, version: 0, tl: [0, 0], pixelRatio: 1, br: [0, 0], tlbr: [0, 0, 0, 0], displaySize: [0, 0], stretchX: [], stretchY: [], content: [0, 0, 0, 0]},
};
const crossFadeResolveImage: CrossFaded<ResolvedImage> = {
from: {name: 'zoo_11', available: false, toString: () => 'zoo_11'},
to: {name: 'volcano_11', available: false, toString: () => 'volcano_11'}
};
updatePatternPositionsInProgram(
config,
'fill-pattern',
crossFadeResolveImage,
tile,
constructMockFillStyleLayer()
);
// we added this property to just see what the update looks like
expect(config.patternPositions).toEqual({
posFrom: {x: 0, y: 0, w: 0, h: 0},
posTo: {x: 0, y: 0, w: 0, h: 0}
});
});
});
4 changes: 4 additions & 0 deletions src/render/update_pattern_positions_in_program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export function updatePatternPositionsInProgram(
let posTo = patternPositions[constantPattern.to.toString()];
let posFrom = patternPositions[constantPattern.from.toString()];

// https://github.com/maplibre/maplibre-gl-js/issues/3377
if (!posTo && posFrom) posTo = posFrom;
if (!posFrom && posTo) posFrom = posTo;

// try again in case patternPositions has been updated by worker
if (!posTo || !posFrom) {
const transitioned = layer.getPaintProperty(propertyName) as string;
Expand Down

0 comments on commit 684b314

Please sign in to comment.