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

View File

@@ -96,12 +96,22 @@ function parseStyle(style, bandCount) {
const vertexShader = `
attribute vec2 ${Attributes.TEXTURE_COORD};
uniform mat4 ${Uniforms.TILE_TRANSFORM};
uniform float ${Uniforms.TEXTURE_PIXEL_WIDTH};
uniform float ${Uniforms.TEXTURE_PIXEL_HEIGHT};
uniform float ${Uniforms.TEXTURE_RESOLUTION};
uniform float ${Uniforms.TEXTURE_ORIGIN_X};
uniform float ${Uniforms.TEXTURE_ORIGIN_Y};
uniform float ${Uniforms.DEPTH};
varying vec2 v_textureCoord;
varying vec2 v_mapCoord;
void main() {
v_textureCoord = ${Attributes.TEXTURE_COORD};
v_mapCoord = vec2(
${Uniforms.TEXTURE_ORIGIN_X} + ${Uniforms.TEXTURE_RESOLUTION} * ${Uniforms.TEXTURE_PIXEL_WIDTH} * v_textureCoord[0],
${Uniforms.TEXTURE_ORIGIN_Y} - ${Uniforms.TEXTURE_RESOLUTION} * ${Uniforms.TEXTURE_PIXEL_HEIGHT} * v_textureCoord[1]
);
gl_Position = ${Uniforms.TILE_TRANSFORM} * vec4(${Attributes.TEXTURE_COORD}, ${Uniforms.DEPTH}, 1.0);
}
`;
@@ -237,6 +247,8 @@ function parseStyle(style, bandCount) {
#endif
varying vec2 v_textureCoord;
varying vec2 v_mapCoord;
uniform vec4 ${Uniforms.RENDER_EXTENT};
uniform float ${Uniforms.TRANSITION_ALPHA};
uniform float ${Uniforms.TEXTURE_PIXEL_WIDTH};
uniform float ${Uniforms.TEXTURE_PIXEL_HEIGHT};
@@ -248,6 +260,15 @@ function parseStyle(style, bandCount) {
${functionDefintions.join('\n')}
void main() {
if (
v_mapCoord[0] < ${Uniforms.RENDER_EXTENT}[0] ||
v_mapCoord[1] < ${Uniforms.RENDER_EXTENT}[1] ||
v_mapCoord[0] > ${Uniforms.RENDER_EXTENT}[2] ||
v_mapCoord[1] > ${Uniforms.RENDER_EXTENT}[3]
) {
discard;
}
vec4 color = texture2D(${
Uniforms.TILE_TEXTURE_ARRAY
}[0], v_textureCoord);

View File

@@ -35,6 +35,10 @@ export const Uniforms = {
DEPTH: 'u_depth',
TEXTURE_PIXEL_WIDTH: 'u_texturePixelWidth',
TEXTURE_PIXEL_HEIGHT: 'u_texturePixelHeight',
TEXTURE_RESOLUTION: 'u_textureResolution', // map units per texture pixel
TEXTURE_ORIGIN_X: 'u_textureOriginX', // map x coordinate of left edge of texture
TEXTURE_ORIGIN_Y: 'u_textureOriginY', // map y coordinate of top edge of texture
RENDER_EXTENT: 'u_renderExtent', // intersection of layer, source, and view extent
RESOLUTION: 'u_resolution',
ZOOM: 'u_zoom',
};
@@ -150,7 +154,7 @@ class WebGLTileLayerRenderer extends WebGLLayerRenderer {
this.renderComplete = false;
/**
* This transform converts tile i, j coordinates to screen coordinates.
* This transform converts texture coordinates to screen coordinates.
* @type {import("../../transform.js").Transform}
* @private
*/
@@ -581,6 +585,19 @@ class WebGLTileLayerRenderer extends WebGLLayerRenderer {
Uniforms.TEXTURE_PIXEL_HEIGHT,
tileSize[1]
);
this.helper.setUniformFloatValue(
Uniforms.TEXTURE_RESOLUTION,
tileResolution
);
this.helper.setUniformFloatValue(
Uniforms.TEXTURE_ORIGIN_X,
tileOrigin[0] + tileCenterI * tileSize[0] * tileResolution
);
this.helper.setUniformFloatValue(
Uniforms.TEXTURE_ORIGIN_Y,
tileOrigin[1] - tileCenterJ * tileSize[1] * tileResolution
);
this.helper.setUniformFloatVec4(Uniforms.RENDER_EXTENT, extent);
this.helper.setUniformFloatValue(
Uniforms.RESOLUTION,
viewState.resolution

View File

@@ -962,6 +962,15 @@ class WebGLHelper extends Disposable {
this.getGL().uniform1f(this.getUniformLocation(uniform), value);
}
/**
* Give a value for a vec4 uniform
* @param {string} uniform Uniform name
* @param {Array<number>} value Array of length 4.
*/
setUniformFloatVec4(uniform, value) {
this.getGL().uniform4fv(this.getUniformLocation(uniform), value);
}
/**
* Give a value for a standard matrix4 uniform
* @param {string} uniform Uniform name