Avoid rendering outside WebGL layer and source extent

This commit is contained in:
Tim Schaub
2022-02-05 15:20:07 -07:00
parent 459cd51ae2
commit adbbc05159
8 changed files with 171 additions and 1 deletions
@@ -87,7 +87,11 @@ describe('ol/layer/WebGLTile', function () {
#else
precision mediump float;
#endif
varying vec2 v_textureCoord;
varying vec2 v_mapCoord;
uniform vec4 u_renderExtent;
uniform float u_transitionAlpha;
uniform float u_texturePixelWidth;
uniform float u_texturePixelHeight;
@@ -97,7 +101,16 @@ describe('ol/layer/WebGLTile', function () {
uniform float u_var_g;
uniform float u_var_b;
uniform sampler2D u_tileTextures[1];
void main() {
if (
v_mapCoord[0] < u_renderExtent[0] ||
v_mapCoord[1] < u_renderExtent[1] ||
v_mapCoord[0] > u_renderExtent[2] ||
v_mapCoord[1] > u_renderExtent[3]
) {
discard;
}
vec4 color = texture2D(u_tileTextures[0], v_textureCoord);
color = vec4(u_var_r / 255.0, u_var_g / 255.0, u_var_b / 255.0, 1.0);
if (color.a == 0.0) {
@@ -112,12 +125,24 @@ describe('ol/layer/WebGLTile', function () {
expect(compileShaderSpy.getCall(1).args[0].replace(/[ \n]+/g, ' ')).to.be(
`
attribute vec2 a_textureCoord;
uniform mat4 u_tileTransform;
uniform float u_texturePixelWidth;
uniform float u_texturePixelHeight;
uniform float u_textureResolution;
uniform float u_textureOriginX;
uniform float u_textureOriginY;
uniform float u_depth;
varying vec2 v_textureCoord;
varying vec2 v_mapCoord;
void main() {
v_textureCoord = a_textureCoord;
v_mapCoord = vec2(
u_textureOriginX + u_textureResolution * u_texturePixelWidth * v_textureCoord[0],
u_textureOriginY - u_textureResolution * u_texturePixelHeight * v_textureCoord[1]
);
gl_Position = u_tileTransform * vec4(a_textureCoord, u_depth, 1.0);
}
`.replace(/[ \n]+/g, ' ')
@@ -163,6 +188,10 @@ describe('ol/layer/WebGLTile', function () {
#else
precision mediump float;
#endif varying vec2 v_textureCoord;
varying vec2 v_mapCoord;
uniform vec4 u_renderExtent;
uniform float u_transitionAlpha;
uniform float u_texturePixelWidth;
uniform float u_texturePixelHeight;
@@ -188,6 +217,14 @@ describe('ol/layer/WebGLTile', function () {
}
void main() {
if (
v_mapCoord[0] < u_renderExtent[0] ||
v_mapCoord[1] < u_renderExtent[1] ||
v_mapCoord[0] > u_renderExtent[2] ||
v_mapCoord[1] > u_renderExtent[3]
) {
discard;
}
vec4 color = texture2D(u_tileTextures[0], v_textureCoord);
color = vec4((getBandValue(4.0, 0.0, 0.0) / 3000.0), (getBandValue(1.0, 0.0, 0.0) / 3000.0), (getBandValue(2.0, 0.0, 0.0) / 3000.0), 1.0);
if (color.a == 0.0) {
Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

@@ -0,0 +1,28 @@
import Map from '../../../../src/ol/Map.js';
import TileLayer from '../../../../src/ol/layer/WebGLTile.js';
import View from '../../../../src/ol/View.js';
import XYZ from '../../../../src/ol/source/XYZ.js';
import {useGeographic} from '../../../../src/ol/proj.js';
useGeographic();
new Map({
layers: [
new TileLayer({
extent: [-100, -30, 50, 50],
source: new XYZ({
url: '/data/tiles/satellite/{z}/{x}/{y}.jpg',
transition: 0,
}),
}),
],
target: 'map',
view: new View({
center: [0, 0],
zoom: 0,
rotation: Math.PI / 5,
}),
});
render({
message: 'data outside the layer extent is not rendered',
});
Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

@@ -0,0 +1,58 @@
import Map from '../../../../src/ol/Map.js';
import TileGrid from '../../../../src/ol/tilegrid/TileGrid.js';
import TileLayer from '../../../../src/ol/layer/WebGLTile.js';
import View from '../../../../src/ol/View.js';
import XYZ from '../../../../src/ol/source/XYZ.js';
import {getHeight, getWidth} from '../../../../src/ol/extent.js';
import {get as getProjection} from '../../../../src/ol/proj.js';
const fullExtent = getProjection('EPSG:3857').getExtent();
const width = getWidth(fullExtent);
const height = getHeight(fullExtent);
const partialExtent = [
fullExtent[0],
fullExtent[1] + 0.4 * height,
fullExtent[2] - 0.4 * width,
fullExtent[3],
];
function resolutionsFromExtent(extent, maxZoom) {
const height = getHeight(extent);
const width = getWidth(extent);
const maxResolution = Math.max(width / 256, height / 256);
const length = maxZoom + 1;
const resolutions = new Array(length);
for (let z = 0; z < length; ++z) {
resolutions[z] = maxResolution / Math.pow(2, z);
}
return resolutions;
}
new Map({
layers: [
new TileLayer({
source: new XYZ({
wrapX: false,
url: '/data/tiles/satellite/{z}/{x}/{y}.jpg',
transition: 0,
tileGrid: new TileGrid({
extent: partialExtent,
resolutions: resolutionsFromExtent(fullExtent, 10),
}),
}),
}),
],
target: 'map',
view: new View({
center: [0, 0],
zoom: 0,
rotation: -Math.PI / 8,
}),
});
render({
message: 'data outside the source tile grid extent is not rendered',
});