-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
TileLayer.js
424 lines (377 loc) · 11.1 KB
/
TileLayer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/**
* @module ol/renderer/webgl/TileLayer
*/
import ReprojDataTile from '../../reproj/DataTile.js';
import ReprojTile from '../../reproj/Tile.js';
import TileState from '../../TileState.js';
import TileTexture from '../../webgl/TileTexture.js';
import WebGLArrayBuffer from '../../webgl/Buffer.js';
import WebGLBaseTileLayerRenderer, {
Uniforms as BaseUniforms,
getCacheKey,
} from './TileLayerBase.js';
import {AttributeType} from '../../webgl/Helper.js';
import {ELEMENT_ARRAY_BUFFER, STATIC_DRAW} from '../../webgl.js';
import {apply as applyTransform} from '../../transform.js';
import {
boundingExtent,
containsCoordinate,
getIntersection,
} from '../../extent.js';
import {fromUserExtent} from '../../proj.js';
import {fromTransform as mat4FromTransform} from '../../vec/mat4.js';
import {toSize} from '../../size.js';
export const Uniforms = {
...BaseUniforms,
TILE_TEXTURE_ARRAY: 'u_tileTextures',
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
};
export const Attributes = {
TEXTURE_COORD: 'a_textureCoord',
};
/**
* @type {Array<import('../../webgl/Helper.js').AttributeDescription>}
*/
const attributeDescriptions = [
{
name: Attributes.TEXTURE_COORD,
size: 2,
type: AttributeType.FLOAT,
},
];
/**
* @typedef {Object} Options
* @property {string} vertexShader Vertex shader source.
* @property {string} fragmentShader Fragment shader source.
* @property {Object<string, import("../../webgl/Helper").UniformValue>} [uniforms] Additional uniforms
* made available to shaders.
* @property {Array<import("../../webgl/PaletteTexture.js").default>} [paletteTextures] Palette textures.
* @property {number} [cacheSize=512] The texture cache size.
* @property {Array<import('./Layer.js').PostProcessesOptions>} [postProcesses] Post-processes definitions.
*/
/**
* @typedef {import("../../webgl/TileTexture.js").TileType} TileTextureType
*/
/**
* @typedef {import("../../webgl/TileTexture.js").default} TileTextureRepresentation
*/
/**
* @classdesc
* WebGL renderer for tile layers.
* @template {import("../../layer/WebGLTile.js").default|import("../../layer/Flow.js").default} LayerType
* @extends {WebGLBaseTileLayerRenderer<LayerType, TileTextureType, TileTextureRepresentation>}
* @api
*/
class WebGLTileLayerRenderer extends WebGLBaseTileLayerRenderer {
/**
* @param {LayerType} tileLayer Tile layer.
* @param {Options} options Options.
*/
constructor(tileLayer, options) {
super(tileLayer, options);
/**
* @type {WebGLProgram}
* @private
*/
this.program_;
/**
* @private
*/
this.vertexShader_ = options.vertexShader;
/**
* @private
*/
this.fragmentShader_ = options.fragmentShader;
/**
* Tiles are rendered as a quad with the following structure:
*
* [P3]---------[P2]
* |` |
* | ` B |
* | ` |
* | ` |
* | A ` |
* | ` |
* [P0]---------[P1]
*
* Triangle A: P0, P1, P3
* Triangle B: P1, P2, P3
*
* @private
*/
this.indices_ = new WebGLArrayBuffer(ELEMENT_ARRAY_BUFFER, STATIC_DRAW);
this.indices_.fromArray([0, 1, 3, 1, 2, 3]);
/**
* @type {Array<import("../../webgl/PaletteTexture.js").default>}
* @private
*/
this.paletteTextures_ = options.paletteTextures || [];
}
/**
* @param {Options} options Options.
* @override
*/
reset(options) {
super.reset(options);
if (this.helper) {
const gl = this.helper.getGL();
for (const paletteTexture of this.paletteTextures_) {
paletteTexture.delete(gl);
}
}
this.vertexShader_ = options.vertexShader;
this.fragmentShader_ = options.fragmentShader;
this.paletteTextures_ = options.paletteTextures || [];
if (this.helper) {
this.program_ = this.helper.getProgram(
this.fragmentShader_,
this.vertexShader_,
);
const gl = this.helper.getGL();
for (const paletteTexture of this.paletteTextures_) {
// upload the texture data
paletteTexture.getTexture(gl);
}
}
}
/**
* @override
*/
afterHelperCreated() {
super.afterHelperCreated();
const gl = this.helper.getGL();
for (const paletteTexture of this.paletteTextures_) {
// upload the texture data
paletteTexture.getTexture(gl);
}
this.program_ = this.helper.getProgram(
this.fragmentShader_,
this.vertexShader_,
);
this.helper.flushBufferData(this.indices_);
}
/**
* @override
*/
removeHelper() {
if (this.helper) {
const gl = this.helper.getGL();
for (const paletteTexture of this.paletteTextures_) {
paletteTexture.delete(gl);
}
}
super.removeHelper();
}
/**
* @override
*/
createTileRepresentation(options) {
return new TileTexture(options);
}
/**
* @override
*/
beforeTilesRender(frameState, tilesWithAlpha) {
super.beforeTilesRender(frameState, tilesWithAlpha);
this.helper.useProgram(this.program_, frameState);
}
/**
* @override
*/
renderTile(
tileTexture,
tileTransform,
frameState,
renderExtent,
tileResolution,
tileSize,
tileOrigin,
tileExtent,
depth,
gutter,
alpha,
) {
const gl = this.helper.getGL();
this.helper.bindBuffer(tileTexture.coords);
this.helper.bindBuffer(this.indices_);
this.helper.enableAttributes(attributeDescriptions);
let textureSlot = 0;
while (textureSlot < tileTexture.textures.length) {
const uniformName = `${Uniforms.TILE_TEXTURE_ARRAY}[${textureSlot}]`;
this.helper.bindTexture(
tileTexture.textures[textureSlot],
textureSlot,
uniformName,
);
++textureSlot;
}
for (
let paletteIndex = 0;
paletteIndex < this.paletteTextures_.length;
++paletteIndex
) {
const paletteTexture = this.paletteTextures_[paletteIndex];
const texture = paletteTexture.getTexture(gl);
this.helper.bindTexture(texture, textureSlot, paletteTexture.name);
++textureSlot;
}
const viewState = frameState.viewState;
const tileWidthWithGutter = tileSize[0] + 2 * gutter;
const tileHeightWithGutter = tileSize[1] + 2 * gutter;
const tile = tileTexture.tile;
const tileCoord = tile.tileCoord;
const tileCenterI = tileCoord[1];
const tileCenterJ = tileCoord[2];
this.helper.setUniformMatrixValue(
Uniforms.TILE_TRANSFORM,
mat4FromTransform(this.tempMat4, tileTransform),
);
this.helper.setUniformFloatValue(Uniforms.TRANSITION_ALPHA, alpha);
this.helper.setUniformFloatValue(Uniforms.DEPTH, depth);
let gutterExtent = renderExtent;
if (gutter > 0) {
gutterExtent = tileExtent;
getIntersection(gutterExtent, renderExtent, gutterExtent);
}
this.helper.setUniformFloatVec4(Uniforms.RENDER_EXTENT, gutterExtent);
this.helper.setUniformFloatValue(Uniforms.RESOLUTION, viewState.resolution);
this.helper.setUniformFloatValue(Uniforms.ZOOM, viewState.zoom);
this.helper.setUniformFloatValue(
Uniforms.TEXTURE_PIXEL_WIDTH,
tileWidthWithGutter,
);
this.helper.setUniformFloatValue(
Uniforms.TEXTURE_PIXEL_HEIGHT,
tileHeightWithGutter,
);
this.helper.setUniformFloatValue(
Uniforms.TEXTURE_RESOLUTION,
tileResolution,
);
this.helper.setUniformFloatValue(
Uniforms.TEXTURE_ORIGIN_X,
tileOrigin[0] +
tileCenterI * tileSize[0] * tileResolution -
gutter * tileResolution,
);
this.helper.setUniformFloatValue(
Uniforms.TEXTURE_ORIGIN_Y,
tileOrigin[1] -
tileCenterJ * tileSize[1] * tileResolution +
gutter * tileResolution,
);
this.helper.drawElements(0, this.indices_.getSize());
}
/**
* @param {import("../../pixel.js").Pixel} pixel Pixel.
* @return {Uint8ClampedArray|Uint8Array|Float32Array|DataView} Data at the pixel location.
* @override
*/
getData(pixel) {
const gl = this.helper.getGL();
if (!gl) {
return null;
}
const frameState = this.frameState;
if (!frameState) {
return null;
}
const layer = this.getLayer();
const coordinate = applyTransform(
frameState.pixelToCoordinateTransform,
pixel.slice(),
);
const viewState = frameState.viewState;
const layerExtent = layer.getExtent();
if (layerExtent) {
if (
!containsCoordinate(
fromUserExtent(layerExtent, viewState.projection),
coordinate,
)
) {
return null;
}
}
// determine last source suitable for rendering at coordinate
const sources = layer.getSources(
boundingExtent([coordinate]),
viewState.resolution,
);
let i, source, tileGrid;
for (i = sources.length - 1; i >= 0; --i) {
source = sources[i];
if (source.getState() === 'ready') {
tileGrid = source.getTileGridForProjection(viewState.projection);
if (source.getWrapX()) {
break;
}
const gridExtent = tileGrid.getExtent();
if (!gridExtent || containsCoordinate(gridExtent, coordinate)) {
break;
}
}
}
if (i < 0) {
return null;
}
const tileTextureCache = this.tileRepresentationCache;
for (
let z = tileGrid.getZForResolution(viewState.resolution);
z >= tileGrid.getMinZoom();
--z
) {
const tileCoord = tileGrid.getTileCoordForCoordAndZ(coordinate, z);
const cacheKey = getCacheKey(source, tileCoord);
if (!tileTextureCache.containsKey(cacheKey)) {
continue;
}
const tileTexture = tileTextureCache.get(cacheKey);
const tile = tileTexture.tile;
if (
(tile instanceof ReprojTile || tile instanceof ReprojDataTile) &&
tile.getState() === TileState.EMPTY
) {
return null;
}
if (!tileTexture.loaded) {
continue;
}
const tileOrigin = tileGrid.getOrigin(z);
const tileSize = toSize(tileGrid.getTileSize(z));
const tileResolution = tileGrid.getResolution(z);
const col =
(coordinate[0] - tileOrigin[0]) / tileResolution -
tileCoord[1] * tileSize[0];
const row =
(tileOrigin[1] - coordinate[1]) / tileResolution -
tileCoord[2] * tileSize[1];
return tileTexture.getPixelData(col, row);
}
return null;
}
/**
* Clean up.
* @override
*/
disposeInternal() {
const helper = this.helper;
if (helper) {
const gl = helper.getGL();
for (const paletteTexture of this.paletteTextures_) {
paletteTexture.delete(gl);
}
this.paletteTextures_.length = 0;
gl.deleteProgram(this.program_);
delete this.program_;
helper.deleteBuffer(this.indices_);
}
super.disposeInternal();
delete this.indices_;
}
}
export default WebGLTileLayerRenderer;