Webgl / return 0 if doing renderTarget.read outside of data width/height
This would happen when WebGLPointsLayerRenderer.forEachFeatureAtCoordinate is called on "warped" worlds at -360/+360 degrees, and may produce false positives.
This commit is contained in:
@@ -115,12 +115,18 @@ class WebGLRenderTarget {
|
||||
/**
|
||||
* Reads one pixel of the frame buffer as an array of r, g, b, a components
|
||||
* in the 0-255 range (unsigned byte).
|
||||
* If x and/or y are outside of existing data, an array filled with 0 is returned.
|
||||
* @param {number} x Pixel coordinate
|
||||
* @param {number} y Pixel coordinate
|
||||
* @returns {Uint8Array} Integer array with one color value (4 components)
|
||||
* @api
|
||||
*/
|
||||
readPixel(x, y) {
|
||||
if (x < 0 || y < 0 || x > this.size_[0] || y >= this.size_[1]) {
|
||||
tmpArray4[0] = tmpArray4[1] = tmpArray4[2] = tmpArray4[3] = 0;
|
||||
return tmpArray4;
|
||||
}
|
||||
|
||||
this.readAll();
|
||||
const index = Math.floor(x) + (this.size_[1] - Math.floor(y) - 1) * this.size_[0];
|
||||
tmpArray4[0] = this.data_[index * 4];
|
||||
|
||||
@@ -123,6 +123,26 @@ describe('ol.webgl.RenderTarget', function() {
|
||||
expect(spy.callCount).to.eql(2);
|
||||
});
|
||||
|
||||
it('returns an array filled with 0 if outside of range', function() {
|
||||
const rt = new WebGLRenderTarget(helper, [4, 4]);
|
||||
helper.createTexture([4, 4], testImage_4x4, rt.getTexture());
|
||||
|
||||
let data = rt.readPixel(-1, 0);
|
||||
expect(data).to.eql([0, 0, 0, 0]);
|
||||
|
||||
data = rt.readPixel(3, -1);
|
||||
expect(data).to.eql([0, 0, 0, 0]);
|
||||
|
||||
data = rt.readPixel(6, 2);
|
||||
expect(data).to.eql([0, 0, 0, 0]);
|
||||
|
||||
data = rt.readPixel(2, 7);
|
||||
expect(data).to.eql([0, 0, 0, 0]);
|
||||
|
||||
data = rt.readPixel(2, 3);
|
||||
expect(data).not.to.eql([0, 0, 0, 0]);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user