diff --git a/src/render/terrain.test.ts b/src/render/terrain.test.ts index fb212688922..22c4d4250a7 100644 --- a/src/render/terrain.test.ts +++ b/src/render/terrain.test.ts @@ -35,6 +35,7 @@ describe('Terrain', () => { context: new Context(gl), width: 1, height: 1, + pixelRatio: 1, transform: {center: {lng: 0}}, maybeDrawDepthAndCoords: jest.fn(), } as any as Painter; @@ -70,6 +71,7 @@ describe('Terrain', () => { context: new Context(gl), width: WORLD_WIDTH, height: 1, + pixelRatio: 1, maybeDrawDepthAndCoords: jest.fn(), } as any as Painter; const sourceCache = {} as SourceCache; diff --git a/src/render/terrain.ts b/src/render/terrain.ts index 7d1b420b475..289fece81a2 100644 --- a/src/render/terrain.ts +++ b/src/render/terrain.ts @@ -332,9 +332,12 @@ export class Terrain { const rgba = new Uint8Array(4); const context = this.painter.context, gl = context.gl; + const px = Math.round(p.x * this.painter.pixelRatio / devicePixelRatio); + const py = Math.round(p.y * this.painter.pixelRatio / devicePixelRatio); + const textureHeight = Math.round(this.painter.height / devicePixelRatio); // grab coordinate pixel from coordinates framebuffer context.bindFramebuffer.set(this.getFramebuffer('coords').framebuffer); - gl.readPixels(p.x, this.painter.height / devicePixelRatio - p.y - 1, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, rgba); + gl.readPixels(px, textureHeight - py - 1, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, rgba); context.bindFramebuffer.set(null); // decode coordinates (encoding see getCoordsTexture) const x = rgba[0] + ((rgba[2] >> 4) << 8);