Skip to content

Commit 6e456bd

Browse files
authored
fix gpupick when map devicePixelRatio!==window.devicePixelRatio (#567)
* fix gpupick when map devicePixelRatio!==window.devicePixelRatio * baseobject support cofnig pick weight
1 parent a024101 commit 6e456bd

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

src/BaseObject.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ const OPTIONS = {
1111
minZoom: 0,
1212
maxZoom: 30,
1313
asynchronous: false,
14-
bloom: false
14+
bloom: false,
15+
pickWeight: -1
1516
};
1617

1718
/**

src/GPUPick.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,11 @@ class GPUPick {
115115

116116
//read the pixel
117117
const { x, y } = pixel;
118-
const devicePixelRatio = window.devicePixelRatio;
118+
let devicePixelRatio = window.devicePixelRatio;
119+
const map = layer.getMap();
120+
if (map) {
121+
devicePixelRatio = map.getDevicePixelRatio ? map.getDevicePixelRatio() : map.options.devicePixelRatio;
122+
}
119123
const offsetX = (x * devicePixelRatio), offsetY = (pickingTexture.height - y * devicePixelRatio);
120124
renderer.readRenderTargetPixels(pickingTexture, Math.round(offsetX), Math.round(offsetY), 1, 1, pixelBuffer);
121125

src/index.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -933,9 +933,20 @@ class ThreeLayer extends maptalks.CanvasLayer {
933933
}
934934
}
935935
}
936+
let pickResult = baseObjects.filter(mesh => {
937+
return mesh instanceof BaseObject;
938+
});
939+
pickResult = pickResult.sort((a, b) => {
940+
return a['options'].pickWeight - b['options'].pickWeight;
941+
});
942+
baseObjects.forEach(mesh => {
943+
if (!(mesh instanceof BaseObject)) {
944+
pickResult.push(mesh);
945+
}
946+
})
936947
options = maptalks.Util.extend({}, options);
937948
const count = options['count'];
938-
return (maptalks.Util.isNumber(count) && count > 0 ? baseObjects.slice(0, count) : baseObjects);
949+
return (maptalks.Util.isNumber(count) && count > 0 ? pickResult.slice(0, count) : baseObjects);
939950
}
940951

941952
/**

src/type/BaseOption.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export type BaseLayerOptionType = {
1818
};
1919

2020
export type BaseObjectOptionType = {
21+
pickWeight?: number,
2122
interactive?: boolean,
2223
altitude?: number,
2324
minZoom?: number,

0 commit comments

Comments
 (0)