ol 10.2.2-dev.1727715629258 → 10.2.2-dev.1727720556228
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ol.d.ts +24 -0
- package/dist/ol.d.ts.map +1 -1
- package/dist/ol.js +1 -1
- package/dist/ol.js.map +1 -1
- package/package.json +2 -2
- package/reproj/DataTile.d.ts +10 -0
- package/reproj/DataTile.d.ts.map +1 -1
- package/reproj/DataTile.js +106 -78
- package/reproj/glreproj.d.ts +70 -0
- package/reproj/glreproj.d.ts.map +1 -0
- package/reproj/glreproj.js +446 -0
- package/util.js +1 -1
- package/vec/mat4.d.ts +50 -5
- package/vec/mat4.d.ts.map +1 -1
- package/vec/mat4.js +157 -3
- package/webgl/Canvas.d.ts +58 -0
- package/webgl/Canvas.d.ts.map +1 -0
- package/webgl/Canvas.js +242 -0
|
@@ -0,0 +1,446 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module ol/reproj/glreproj
|
|
3
|
+
*/
|
|
4
|
+
import * as mat4 from '../vec/mat4.js';
|
|
5
|
+
import {WORKER_OFFSCREEN_CANVAS} from '../has.js';
|
|
6
|
+
import {Canvas as WebGLCanvas, createProgram} from '../webgl/Canvas.js';
|
|
7
|
+
import {
|
|
8
|
+
createEmpty,
|
|
9
|
+
extend,
|
|
10
|
+
getHeight,
|
|
11
|
+
getTopLeft,
|
|
12
|
+
getWidth,
|
|
13
|
+
} from '../extent.js';
|
|
14
|
+
|
|
15
|
+
const EDGE_VERTEX_SHADER = `
|
|
16
|
+
attribute vec4 a_position;
|
|
17
|
+
|
|
18
|
+
uniform mat4 u_matrix;
|
|
19
|
+
|
|
20
|
+
void main() {
|
|
21
|
+
gl_Position = u_matrix * a_position;
|
|
22
|
+
}
|
|
23
|
+
`;
|
|
24
|
+
const EDGE_FRAGMENT_SHADER = `
|
|
25
|
+
precision mediump float;
|
|
26
|
+
|
|
27
|
+
uniform vec4 u_val;
|
|
28
|
+
void main() {
|
|
29
|
+
gl_FragColor = u_val;
|
|
30
|
+
}
|
|
31
|
+
`;
|
|
32
|
+
|
|
33
|
+
const TRIANGLE_VERTEX_SHADER = `
|
|
34
|
+
attribute vec4 a_position;
|
|
35
|
+
attribute vec2 a_texcoord;
|
|
36
|
+
|
|
37
|
+
varying vec2 v_texcoord;
|
|
38
|
+
|
|
39
|
+
uniform mat4 u_matrix;
|
|
40
|
+
|
|
41
|
+
void main() {
|
|
42
|
+
gl_Position = u_matrix * a_position;
|
|
43
|
+
v_texcoord = a_texcoord;
|
|
44
|
+
}
|
|
45
|
+
`;
|
|
46
|
+
const TRIANGLE_FRAGMENT_SHADER = `
|
|
47
|
+
precision mediump float;
|
|
48
|
+
|
|
49
|
+
varying vec2 v_texcoord;
|
|
50
|
+
|
|
51
|
+
uniform sampler2D u_texture;
|
|
52
|
+
|
|
53
|
+
void main() {
|
|
54
|
+
if (v_texcoord.x < 0.0 || v_texcoord.x > 1.0 || v_texcoord.y < 0.0 || v_texcoord.y > 1.0) {
|
|
55
|
+
discard;
|
|
56
|
+
}
|
|
57
|
+
gl_FragColor = texture2D(u_texture, v_texcoord);
|
|
58
|
+
}
|
|
59
|
+
`;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Create an html canvas element and returns its webgl context.
|
|
63
|
+
* @param {number} [width] Canvas width.
|
|
64
|
+
* @param {number} [height] Canvas height.
|
|
65
|
+
* @param {Array<HTMLCanvasElement | OffscreenCanvas>} [canvasPool] Canvas pool to take existing canvas from.
|
|
66
|
+
* @param {WebGLContextAttributes} [settings] CanvasRenderingContext2DSettings
|
|
67
|
+
* @return {WebGLRenderingContext} The context.
|
|
68
|
+
*/
|
|
69
|
+
export function createCanvasContextWebGL(width, height, canvasPool, settings) {
|
|
70
|
+
/** @type {HTMLCanvasElement|OffscreenCanvas} */
|
|
71
|
+
let canvas;
|
|
72
|
+
if (canvasPool && canvasPool.length) {
|
|
73
|
+
canvas = /** @type {HTMLCanvasElement} */ (canvasPool.shift());
|
|
74
|
+
} else if (WORKER_OFFSCREEN_CANVAS) {
|
|
75
|
+
canvas = new OffscreenCanvas(width || 300, height || 300);
|
|
76
|
+
} else {
|
|
77
|
+
canvas = document.createElement('canvas');
|
|
78
|
+
}
|
|
79
|
+
if (width) {
|
|
80
|
+
canvas.width = width;
|
|
81
|
+
}
|
|
82
|
+
if (height) {
|
|
83
|
+
canvas.height = height;
|
|
84
|
+
}
|
|
85
|
+
//FIXME Allow OffscreenCanvasRenderingContext2D as return type
|
|
86
|
+
return /** @type {WebGLRenderingContext} */ (
|
|
87
|
+
canvas.getContext('webgl', settings)
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Releases canvas memory to avoid exceeding memory limits in Safari.
|
|
93
|
+
* See https://pqina.nl/blog/total-canvas-memory-use-exceeds-the-maximum-limit/
|
|
94
|
+
* @param {WebGLRenderingContext} gl Context.
|
|
95
|
+
*/
|
|
96
|
+
export function releaseGLCanvas(gl) {
|
|
97
|
+
const canvas = gl.canvas;
|
|
98
|
+
canvas.width = 1;
|
|
99
|
+
canvas.height = 1;
|
|
100
|
+
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* @type {Array<HTMLCanvasElement | OffscreenCanvas>}
|
|
105
|
+
*/
|
|
106
|
+
export const canvasGLPool = [];
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* @typedef {Object} ImageExtent
|
|
110
|
+
* @property {import("../extent.js").Extent} extent Extent.
|
|
111
|
+
* @property {WebGLTexture} texture Texture.
|
|
112
|
+
* @property {number} width Width of texture.
|
|
113
|
+
* @property {number} height Height of texture.
|
|
114
|
+
*/
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Renders the source data into new canvas based on the triangulation.
|
|
118
|
+
*
|
|
119
|
+
* @param {WebGLRenderingContext} gl the context to render in.
|
|
120
|
+
* @param {number} width_ Width of the canvas.
|
|
121
|
+
* @param {number} height_ Height of the canvas.
|
|
122
|
+
* @param {number} pixelRatio Pixel ratio.
|
|
123
|
+
* @param {number} sourceResolution Source resolution.
|
|
124
|
+
* @param {number} targetResolution Target resolution.
|
|
125
|
+
* @param {import("../extent.js").Extent} targetExtent Target extent (tile).
|
|
126
|
+
* @param {import("../reproj/Triangulation.js").default} triangulation Calculated triangulation.
|
|
127
|
+
* @param {Array<ImageExtent>} sources Array of sources.
|
|
128
|
+
* @param {number} gutter Gutter of the sources.
|
|
129
|
+
* @param {number} dataType What kind of data is the textures, must be gl.FLOAT or gl.UNSIGNED_BYTE
|
|
130
|
+
* TODO: Allow setting renderEdges value in the data as this is done in "data-space".
|
|
131
|
+
* @param {boolean | Array<number>} [renderEdges] Render reprojection edges.
|
|
132
|
+
* @param {boolean} [interpolate] Use linear interpolation when resampling.
|
|
133
|
+
* @return {{framebuffer: WebGLFramebuffer, width: number, height: number, texture: WebGLTexture}} Canvas with reprojected data.
|
|
134
|
+
*/
|
|
135
|
+
export function render(
|
|
136
|
+
gl,
|
|
137
|
+
width_,
|
|
138
|
+
height_,
|
|
139
|
+
pixelRatio,
|
|
140
|
+
sourceResolution,
|
|
141
|
+
targetResolution,
|
|
142
|
+
targetExtent,
|
|
143
|
+
triangulation,
|
|
144
|
+
sources,
|
|
145
|
+
gutter,
|
|
146
|
+
dataType,
|
|
147
|
+
renderEdges,
|
|
148
|
+
interpolate,
|
|
149
|
+
) {
|
|
150
|
+
const width = Math.round(pixelRatio * width_);
|
|
151
|
+
const height = Math.round(pixelRatio * height_);
|
|
152
|
+
gl.canvas.width = width;
|
|
153
|
+
gl.canvas.height = height;
|
|
154
|
+
|
|
155
|
+
/** @type {WebGLFramebuffer | null} */
|
|
156
|
+
let resultFrameBuffer;
|
|
157
|
+
/** @type {WebGLTexture | null} */
|
|
158
|
+
let resultTexture;
|
|
159
|
+
{
|
|
160
|
+
resultTexture = gl.createTexture();
|
|
161
|
+
gl.bindTexture(gl.TEXTURE_2D, resultTexture);
|
|
162
|
+
|
|
163
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
164
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
165
|
+
if (interpolate) {
|
|
166
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
|
|
167
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
|
|
168
|
+
} else {
|
|
169
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
|
|
170
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
|
|
171
|
+
}
|
|
172
|
+
gl.texImage2D(
|
|
173
|
+
gl.TEXTURE_2D,
|
|
174
|
+
0,
|
|
175
|
+
gl.RGBA,
|
|
176
|
+
width,
|
|
177
|
+
height,
|
|
178
|
+
0,
|
|
179
|
+
gl.RGBA,
|
|
180
|
+
dataType,
|
|
181
|
+
null,
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
resultFrameBuffer = gl.createFramebuffer();
|
|
185
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, resultFrameBuffer);
|
|
186
|
+
gl.framebufferTexture2D(
|
|
187
|
+
gl.FRAMEBUFFER,
|
|
188
|
+
gl.COLOR_ATTACHMENT0,
|
|
189
|
+
gl.TEXTURE_2D,
|
|
190
|
+
resultTexture,
|
|
191
|
+
0,
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (resultFrameBuffer === null) {
|
|
196
|
+
throw new Error('Could not create framebuffer');
|
|
197
|
+
}
|
|
198
|
+
if (resultTexture === null) {
|
|
199
|
+
throw new Error('Could not create texture');
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (sources.length === 0) {
|
|
203
|
+
return {
|
|
204
|
+
width,
|
|
205
|
+
height,
|
|
206
|
+
framebuffer: resultFrameBuffer,
|
|
207
|
+
texture: resultTexture,
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const sourceDataExtent = createEmpty();
|
|
212
|
+
sources.forEach(function (src, i, arr) {
|
|
213
|
+
extend(sourceDataExtent, src.extent);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
/** @type {WebGLTexture | null} */
|
|
217
|
+
let stitchTexture;
|
|
218
|
+
/** @type {number} */
|
|
219
|
+
let stitchWidth;
|
|
220
|
+
/** @type {number} */
|
|
221
|
+
let stitchHeight;
|
|
222
|
+
const stitchScale = 1 / sourceResolution;
|
|
223
|
+
|
|
224
|
+
if (sources.length !== 1 || gutter !== 0) {
|
|
225
|
+
stitchTexture = gl.createTexture();
|
|
226
|
+
if (resultTexture === null) {
|
|
227
|
+
throw new Error('Could not create texture');
|
|
228
|
+
}
|
|
229
|
+
stitchWidth = Math.round(getWidth(sourceDataExtent) * stitchScale);
|
|
230
|
+
stitchHeight = Math.round(getHeight(sourceDataExtent) * stitchScale);
|
|
231
|
+
|
|
232
|
+
// Make sure we do not exceed the max texture size by lowering the resolution for this image.
|
|
233
|
+
// https://github.com/openlayers/openlayers/pull/15860#issuecomment-2254123580
|
|
234
|
+
const maxTexSize = gl.getParameter(gl.MAX_TEXTURE_SIZE);
|
|
235
|
+
const largeSide = Math.max(stitchWidth, stitchHeight);
|
|
236
|
+
const scaleFactor = largeSide > maxTexSize ? maxTexSize / largeSide : 1;
|
|
237
|
+
const stitchWidthFixed = Math.round(stitchWidth * scaleFactor);
|
|
238
|
+
const stitchHeightFixed = Math.round(stitchHeight * scaleFactor);
|
|
239
|
+
|
|
240
|
+
gl.bindTexture(gl.TEXTURE_2D, stitchTexture);
|
|
241
|
+
|
|
242
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
243
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
244
|
+
if (interpolate) {
|
|
245
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
|
|
246
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
|
|
247
|
+
} else {
|
|
248
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
|
|
249
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
|
|
250
|
+
}
|
|
251
|
+
gl.texImage2D(
|
|
252
|
+
gl.TEXTURE_2D,
|
|
253
|
+
0,
|
|
254
|
+
gl.RGBA,
|
|
255
|
+
stitchWidthFixed,
|
|
256
|
+
stitchHeightFixed,
|
|
257
|
+
0,
|
|
258
|
+
gl.RGBA,
|
|
259
|
+
dataType,
|
|
260
|
+
null,
|
|
261
|
+
);
|
|
262
|
+
|
|
263
|
+
const fb = gl.createFramebuffer();
|
|
264
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
|
|
265
|
+
gl.framebufferTexture2D(
|
|
266
|
+
gl.FRAMEBUFFER,
|
|
267
|
+
gl.COLOR_ATTACHMENT0,
|
|
268
|
+
gl.TEXTURE_2D,
|
|
269
|
+
stitchTexture,
|
|
270
|
+
0,
|
|
271
|
+
);
|
|
272
|
+
const webGLCanvas = new WebGLCanvas(gl);
|
|
273
|
+
|
|
274
|
+
sources.forEach(function (src, i, arr) {
|
|
275
|
+
const xPos =
|
|
276
|
+
(src.extent[0] - sourceDataExtent[0]) * stitchScale * scaleFactor;
|
|
277
|
+
const yPos =
|
|
278
|
+
-(src.extent[3] - sourceDataExtent[3]) * stitchScale * scaleFactor;
|
|
279
|
+
const srcWidth = getWidth(src.extent) * stitchScale * scaleFactor;
|
|
280
|
+
const srcHeight = getHeight(src.extent) * stitchScale * scaleFactor;
|
|
281
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
|
|
282
|
+
gl.viewport(0, 0, stitchWidthFixed, stitchHeightFixed);
|
|
283
|
+
|
|
284
|
+
webGLCanvas.drawImage(
|
|
285
|
+
src.texture,
|
|
286
|
+
src.width,
|
|
287
|
+
src.height,
|
|
288
|
+
gutter,
|
|
289
|
+
gutter,
|
|
290
|
+
src.width - 2 * gutter,
|
|
291
|
+
src.height - 2 * gutter,
|
|
292
|
+
interpolate ? xPos : Math.round(xPos),
|
|
293
|
+
interpolate ? yPos : Math.round(yPos),
|
|
294
|
+
interpolate ? srcWidth : Math.round(xPos + srcWidth) - Math.round(xPos),
|
|
295
|
+
interpolate
|
|
296
|
+
? srcHeight
|
|
297
|
+
: Math.round(yPos + srcHeight) - Math.round(yPos),
|
|
298
|
+
stitchWidthFixed,
|
|
299
|
+
stitchHeightFixed,
|
|
300
|
+
);
|
|
301
|
+
});
|
|
302
|
+
gl.deleteFramebuffer(fb);
|
|
303
|
+
} else {
|
|
304
|
+
stitchTexture = sources[0].texture;
|
|
305
|
+
stitchWidth = sources[0].width;
|
|
306
|
+
stitchHeight = sources[0].width;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
const targetTopLeft = getTopLeft(targetExtent);
|
|
310
|
+
const sourceTopLeft = getTopLeft(sourceDataExtent);
|
|
311
|
+
|
|
312
|
+
const getUVs = (
|
|
313
|
+
/** @type {Array<import("../coordinate.js").Coordinate>} */ target,
|
|
314
|
+
) => {
|
|
315
|
+
const u0 =
|
|
316
|
+
((target[0][0] - targetTopLeft[0]) / targetResolution) * pixelRatio;
|
|
317
|
+
const v0 =
|
|
318
|
+
(-(target[0][1] - targetTopLeft[1]) / targetResolution) * pixelRatio;
|
|
319
|
+
const u1 =
|
|
320
|
+
((target[1][0] - targetTopLeft[0]) / targetResolution) * pixelRatio;
|
|
321
|
+
const v1 =
|
|
322
|
+
(-(target[1][1] - targetTopLeft[1]) / targetResolution) * pixelRatio;
|
|
323
|
+
const u2 =
|
|
324
|
+
((target[2][0] - targetTopLeft[0]) / targetResolution) * pixelRatio;
|
|
325
|
+
const v2 =
|
|
326
|
+
(-(target[2][1] - targetTopLeft[1]) / targetResolution) * pixelRatio;
|
|
327
|
+
return {u1, v1, u0, v0, u2, v2};
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, resultFrameBuffer);
|
|
331
|
+
gl.viewport(0, 0, width, height);
|
|
332
|
+
|
|
333
|
+
// Draw source to reprojtile
|
|
334
|
+
{
|
|
335
|
+
/** @type {Array<number>} */
|
|
336
|
+
const vertices = [];
|
|
337
|
+
/** @type {Array<number>} */
|
|
338
|
+
const texcoords = [];
|
|
339
|
+
|
|
340
|
+
const triProgram = createProgram(
|
|
341
|
+
gl,
|
|
342
|
+
TRIANGLE_FRAGMENT_SHADER,
|
|
343
|
+
TRIANGLE_VERTEX_SHADER,
|
|
344
|
+
);
|
|
345
|
+
gl.useProgram(triProgram);
|
|
346
|
+
|
|
347
|
+
// Bind image
|
|
348
|
+
const textureLocation = gl.getUniformLocation(triProgram, 'u_texture');
|
|
349
|
+
gl.bindTexture(gl.TEXTURE_2D, stitchTexture);
|
|
350
|
+
|
|
351
|
+
// Tell the shader to get the texture from texture unit 0
|
|
352
|
+
gl.uniform1i(textureLocation, 0);
|
|
353
|
+
|
|
354
|
+
// Calculate vert and tex coordinates.
|
|
355
|
+
triangulation.getTriangles().forEach(function (triangle, i, arr) {
|
|
356
|
+
const source = triangle.source;
|
|
357
|
+
const target = triangle.target;
|
|
358
|
+
// Make sure that everything is on pixel boundaries
|
|
359
|
+
const {u1, v1, u0, v0, u2, v2} = getUVs(target);
|
|
360
|
+
|
|
361
|
+
const su0 =
|
|
362
|
+
(source[0][0] - sourceTopLeft[0]) / sourceResolution / stitchWidth;
|
|
363
|
+
const sv0 =
|
|
364
|
+
-(source[0][1] - sourceTopLeft[1]) / sourceResolution / stitchHeight;
|
|
365
|
+
const su1 =
|
|
366
|
+
(source[1][0] - sourceTopLeft[0]) / sourceResolution / stitchWidth;
|
|
367
|
+
const sv1 =
|
|
368
|
+
-(source[1][1] - sourceTopLeft[1]) / sourceResolution / stitchHeight;
|
|
369
|
+
const su2 =
|
|
370
|
+
(source[2][0] - sourceTopLeft[0]) / sourceResolution / stitchWidth;
|
|
371
|
+
const sv2 =
|
|
372
|
+
-(source[2][1] - sourceTopLeft[1]) / sourceResolution / stitchHeight;
|
|
373
|
+
|
|
374
|
+
vertices.push(u1, v1, u0, v0, u2, v2);
|
|
375
|
+
texcoords.push(su1, sv1, su0, sv0, su2, sv2);
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
// Convert pixel space to clip space.
|
|
379
|
+
const matrix = mat4.orthographic(0, width, height, 0, -1, 1);
|
|
380
|
+
const matrixLocation = gl.getUniformLocation(triProgram, 'u_matrix');
|
|
381
|
+
gl.uniformMatrix4fv(matrixLocation, false, matrix);
|
|
382
|
+
|
|
383
|
+
const positionLocation = gl.getAttribLocation(triProgram, 'a_position');
|
|
384
|
+
const positionBuffer = gl.createBuffer();
|
|
385
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
|
|
386
|
+
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
|
|
387
|
+
gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0);
|
|
388
|
+
gl.enableVertexAttribArray(positionLocation);
|
|
389
|
+
|
|
390
|
+
const texcoordLocation = gl.getAttribLocation(triProgram, 'a_texcoord');
|
|
391
|
+
const texcoordBuffer = gl.createBuffer();
|
|
392
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, texcoordBuffer);
|
|
393
|
+
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(texcoords), gl.STATIC_DRAW);
|
|
394
|
+
gl.vertexAttribPointer(texcoordLocation, 2, gl.FLOAT, false, 0, 0);
|
|
395
|
+
gl.enableVertexAttribArray(texcoordLocation);
|
|
396
|
+
|
|
397
|
+
gl.drawArrays(gl.TRIANGLES, 0, vertices.length / 2);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
if (renderEdges) {
|
|
401
|
+
const edgeProgram = createProgram(
|
|
402
|
+
gl,
|
|
403
|
+
EDGE_FRAGMENT_SHADER,
|
|
404
|
+
EDGE_VERTEX_SHADER,
|
|
405
|
+
);
|
|
406
|
+
gl.useProgram(edgeProgram);
|
|
407
|
+
const matrix = mat4.orthographic(0, width, height, 0, -1, 1);
|
|
408
|
+
const matrixLocation = gl.getUniformLocation(edgeProgram, 'u_matrix');
|
|
409
|
+
gl.uniformMatrix4fv(matrixLocation, false, matrix);
|
|
410
|
+
|
|
411
|
+
const burnval = Array.isArray(renderEdges) ? renderEdges : [0, 0, 0, 255];
|
|
412
|
+
const burnvalLocation = gl.getUniformLocation(edgeProgram, 'u_val');
|
|
413
|
+
const isFloat = true;
|
|
414
|
+
if (isFloat) {
|
|
415
|
+
gl.uniform4fv(burnvalLocation, burnval);
|
|
416
|
+
} else {
|
|
417
|
+
gl.uniform4iv(burnvalLocation, burnval);
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
const positionLocation = gl.getAttribLocation(edgeProgram, 'a_position');
|
|
421
|
+
const positionBuffer = gl.createBuffer();
|
|
422
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
|
|
423
|
+
gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0);
|
|
424
|
+
gl.enableVertexAttribArray(positionLocation);
|
|
425
|
+
|
|
426
|
+
/** @type {Array<number>} */
|
|
427
|
+
const lines = triangulation.getTriangles().reduce(function (
|
|
428
|
+
/** @type {Array<number>} */ lines,
|
|
429
|
+
triangle,
|
|
430
|
+
) {
|
|
431
|
+
const target = triangle.target;
|
|
432
|
+
const {u1, v1, u0, v0, u2, v2} = getUVs(target);
|
|
433
|
+
|
|
434
|
+
return lines.concat([u1, v1, u0, v0, u0, v0, u2, v2, u2, v2, u1, v1]);
|
|
435
|
+
}, []);
|
|
436
|
+
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(lines), gl.STATIC_DRAW);
|
|
437
|
+
gl.drawArrays(gl.LINES, 0, lines.length / 2);
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
return {
|
|
441
|
+
width,
|
|
442
|
+
height,
|
|
443
|
+
framebuffer: resultFrameBuffer,
|
|
444
|
+
texture: resultTexture,
|
|
445
|
+
};
|
|
446
|
+
}
|
package/util.js
CHANGED
package/vec/mat4.d.ts
CHANGED
|
@@ -1,14 +1,59 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module ol/vec/mat4
|
|
3
3
|
*/
|
|
4
|
+
/** @typedef {Array<number>} Mat4 */
|
|
4
5
|
/**
|
|
5
|
-
* @return {
|
|
6
|
+
* @return {Mat4} "4x4 matrix representing a 3D identity transform."
|
|
6
7
|
*/
|
|
7
|
-
export function create():
|
|
8
|
+
export function create(): Mat4;
|
|
8
9
|
/**
|
|
9
|
-
* @param {
|
|
10
|
+
* @param {Mat4} mat4 Flattened 4x4 matrix receiving the result.
|
|
10
11
|
* @param {import("../transform.js").Transform} transform Transformation matrix.
|
|
11
|
-
* @return {
|
|
12
|
+
* @return {Mat4} "2D transformation matrix as flattened 4x4 matrix."
|
|
12
13
|
*/
|
|
13
|
-
export function fromTransform(mat4:
|
|
14
|
+
export function fromTransform(mat4: Mat4, transform: import("../transform.js").Transform): Mat4;
|
|
15
|
+
/**
|
|
16
|
+
* Generates a orthogonal projection matrix with the given bounds
|
|
17
|
+
*
|
|
18
|
+
* @param {number} left Left bound of the frustum
|
|
19
|
+
* @param {number} right Right bound of the frustum
|
|
20
|
+
* @param {number} bottom Bottom bound of the frustum
|
|
21
|
+
* @param {number} top Top bound of the frustum
|
|
22
|
+
* @param {number} near Near bound of the frustum
|
|
23
|
+
* @param {number} far Far bound of the frustum
|
|
24
|
+
* @param {Mat4} [out] mat4 frustum matrix will be written into
|
|
25
|
+
* @return {Mat4} out
|
|
26
|
+
*/
|
|
27
|
+
export function orthographic(left: number, right: number, bottom: number, top: number, near: number, far: number, out?: Mat4 | undefined): Mat4;
|
|
28
|
+
/**
|
|
29
|
+
* Scales the mat4 by the dimensions in the given vec3
|
|
30
|
+
*
|
|
31
|
+
* @param {Mat4} m The matrix to scale.
|
|
32
|
+
* @param {number} x How much to scale in the x direction.
|
|
33
|
+
* @param {number} y How much to scale in the y direction.
|
|
34
|
+
* @param {number} z How much to scale in the z direction.
|
|
35
|
+
* @param {Mat4} [out] The matrix to write to.
|
|
36
|
+
* @return {Mat4} out
|
|
37
|
+
**/
|
|
38
|
+
export function scale(m: Mat4, x: number, y: number, z: number, out?: Mat4 | undefined): Mat4;
|
|
39
|
+
/**
|
|
40
|
+
* Translate a matrix.
|
|
41
|
+
*
|
|
42
|
+
* @param {Mat4} m the matrix to translate
|
|
43
|
+
* @param {number} x How much to translate in the x direction.
|
|
44
|
+
* @param {number} y How much to translate in the y direction.
|
|
45
|
+
* @param {number} z How much to translate in the z direction.
|
|
46
|
+
* @param {Mat4} [out] the receiving matrix
|
|
47
|
+
* @return {Mat4} out
|
|
48
|
+
*/
|
|
49
|
+
export function translate(m: Mat4, x: number, y: number, z: number, out?: Mat4 | undefined): Mat4;
|
|
50
|
+
/**
|
|
51
|
+
* @param {number} x x translation.
|
|
52
|
+
* @param {number} y y translation.
|
|
53
|
+
* @param {number} z z translation.
|
|
54
|
+
* @param {Mat4} [out] optional matrix to store result
|
|
55
|
+
* @return {Mat4} out
|
|
56
|
+
*/
|
|
57
|
+
export function translation(x: number, y: number, z: number, out?: Mat4 | undefined): Mat4;
|
|
58
|
+
export type Mat4 = Array<number>;
|
|
14
59
|
//# sourceMappingURL=mat4.d.ts.map
|
package/vec/mat4.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mat4.d.ts","sourceRoot":"","sources":["mat4.js"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;GAEG;AACH,0BAFY,
|
|
1
|
+
{"version":3,"file":"mat4.d.ts","sourceRoot":"","sources":["mat4.js"],"names":[],"mappings":"AAAA;;GAEG;AAEH,oCAAoC;AAEpC;;GAEG;AACH,0BAFY,IAAI,CAIf;AAED;;;;GAIG;AACH,oCAJW,IAAI,aACJ,OAAO,iBAAiB,EAAE,SAAS,GAClC,IAAI,CAUf;AAED;;;;;;;;;;;GAWG;AACH,mCATW,MAAM,SACN,MAAM,UACN,MAAM,OACN,MAAM,QACN,MAAM,OACN,MAAM,2BAEL,IAAI,CAwBf;AAED;;;;;;;;;IASI;AACJ,yBAPW,IAAI,KACJ,MAAM,KACN,MAAM,KACN,MAAM,2BAEL,IAAI,CAqBf;AAED;;;;;;;;;GASG;AACH,6BAPW,IAAI,KACJ,MAAM,KACN,MAAM,KACN,MAAM,2BAEL,IAAI,CA6Cf;AAED;;;;;;GAMG;AACH,+BANW,MAAM,KACN,MAAM,KACN,MAAM,2BAEL,IAAI,CAuBf;mBA9Ka,KAAK,CAAC,MAAM,CAAC"}
|
package/vec/mat4.js
CHANGED
|
@@ -2,17 +2,19 @@
|
|
|
2
2
|
* @module ol/vec/mat4
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
+
/** @typedef {Array<number>} Mat4 */
|
|
6
|
+
|
|
5
7
|
/**
|
|
6
|
-
* @return {
|
|
8
|
+
* @return {Mat4} "4x4 matrix representing a 3D identity transform."
|
|
7
9
|
*/
|
|
8
10
|
export function create() {
|
|
9
11
|
return [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
|
|
10
12
|
}
|
|
11
13
|
|
|
12
14
|
/**
|
|
13
|
-
* @param {
|
|
15
|
+
* @param {Mat4} mat4 Flattened 4x4 matrix receiving the result.
|
|
14
16
|
* @param {import("../transform.js").Transform} transform Transformation matrix.
|
|
15
|
-
* @return {
|
|
17
|
+
* @return {Mat4} "2D transformation matrix as flattened 4x4 matrix."
|
|
16
18
|
*/
|
|
17
19
|
export function fromTransform(mat4, transform) {
|
|
18
20
|
mat4[0] = transform[0];
|
|
@@ -23,3 +25,155 @@ export function fromTransform(mat4, transform) {
|
|
|
23
25
|
mat4[13] = transform[5];
|
|
24
26
|
return mat4;
|
|
25
27
|
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Generates a orthogonal projection matrix with the given bounds
|
|
31
|
+
*
|
|
32
|
+
* @param {number} left Left bound of the frustum
|
|
33
|
+
* @param {number} right Right bound of the frustum
|
|
34
|
+
* @param {number} bottom Bottom bound of the frustum
|
|
35
|
+
* @param {number} top Top bound of the frustum
|
|
36
|
+
* @param {number} near Near bound of the frustum
|
|
37
|
+
* @param {number} far Far bound of the frustum
|
|
38
|
+
* @param {Mat4} [out] mat4 frustum matrix will be written into
|
|
39
|
+
* @return {Mat4} out
|
|
40
|
+
*/
|
|
41
|
+
export function orthographic(left, right, bottom, top, near, far, out) {
|
|
42
|
+
out = out ?? create();
|
|
43
|
+
const lr = 1 / (left - right),
|
|
44
|
+
bt = 1 / (bottom - top),
|
|
45
|
+
nf = 1 / (near - far);
|
|
46
|
+
out[0] = -2 * lr;
|
|
47
|
+
out[1] = 0;
|
|
48
|
+
out[2] = 0;
|
|
49
|
+
out[3] = 0;
|
|
50
|
+
out[4] = 0;
|
|
51
|
+
out[5] = -2 * bt;
|
|
52
|
+
out[6] = 0;
|
|
53
|
+
out[7] = 0;
|
|
54
|
+
out[8] = 0;
|
|
55
|
+
out[9] = 0;
|
|
56
|
+
out[10] = 2 * nf;
|
|
57
|
+
out[11] = 0;
|
|
58
|
+
out[12] = (left + right) * lr;
|
|
59
|
+
out[13] = (top + bottom) * bt;
|
|
60
|
+
out[14] = (far + near) * nf;
|
|
61
|
+
out[15] = 1;
|
|
62
|
+
return out;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Scales the mat4 by the dimensions in the given vec3
|
|
67
|
+
*
|
|
68
|
+
* @param {Mat4} m The matrix to scale.
|
|
69
|
+
* @param {number} x How much to scale in the x direction.
|
|
70
|
+
* @param {number} y How much to scale in the y direction.
|
|
71
|
+
* @param {number} z How much to scale in the z direction.
|
|
72
|
+
* @param {Mat4} [out] The matrix to write to.
|
|
73
|
+
* @return {Mat4} out
|
|
74
|
+
**/
|
|
75
|
+
export function scale(m, x, y, z, out) {
|
|
76
|
+
out = out ?? create();
|
|
77
|
+
out[0] = m[0] * x;
|
|
78
|
+
out[1] = m[1] * x;
|
|
79
|
+
out[2] = m[2] * x;
|
|
80
|
+
out[3] = m[3] * x;
|
|
81
|
+
out[4] = m[4] * y;
|
|
82
|
+
out[5] = m[5] * y;
|
|
83
|
+
out[6] = m[6] * y;
|
|
84
|
+
out[7] = m[7] * y;
|
|
85
|
+
out[8] = m[8] * z;
|
|
86
|
+
out[9] = m[9] * z;
|
|
87
|
+
out[10] = m[10] * z;
|
|
88
|
+
out[11] = m[11] * z;
|
|
89
|
+
out[12] = m[12];
|
|
90
|
+
out[13] = m[13];
|
|
91
|
+
out[14] = m[14];
|
|
92
|
+
out[15] = m[15];
|
|
93
|
+
return out;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Translate a matrix.
|
|
98
|
+
*
|
|
99
|
+
* @param {Mat4} m the matrix to translate
|
|
100
|
+
* @param {number} x How much to translate in the x direction.
|
|
101
|
+
* @param {number} y How much to translate in the y direction.
|
|
102
|
+
* @param {number} z How much to translate in the z direction.
|
|
103
|
+
* @param {Mat4} [out] the receiving matrix
|
|
104
|
+
* @return {Mat4} out
|
|
105
|
+
*/
|
|
106
|
+
export function translate(m, x, y, z, out) {
|
|
107
|
+
out = out ?? create();
|
|
108
|
+
let a00, a01, a02, a03, a10, a11, a12, a13, a20, a21, a22, a23;
|
|
109
|
+
|
|
110
|
+
if (m === out) {
|
|
111
|
+
out[12] = m[0] * x + m[4] * y + m[8] * z + m[12];
|
|
112
|
+
out[13] = m[1] * x + m[5] * y + m[9] * z + m[13];
|
|
113
|
+
out[14] = m[2] * x + m[6] * y + m[10] * z + m[14];
|
|
114
|
+
out[15] = m[3] * x + m[7] * y + m[11] * z + m[15];
|
|
115
|
+
} else {
|
|
116
|
+
a00 = m[0];
|
|
117
|
+
a01 = m[1];
|
|
118
|
+
a02 = m[2];
|
|
119
|
+
a03 = m[3];
|
|
120
|
+
a10 = m[4];
|
|
121
|
+
a11 = m[5];
|
|
122
|
+
a12 = m[6];
|
|
123
|
+
a13 = m[7];
|
|
124
|
+
a20 = m[8];
|
|
125
|
+
a21 = m[9];
|
|
126
|
+
a22 = m[10];
|
|
127
|
+
a23 = m[11];
|
|
128
|
+
|
|
129
|
+
out[0] = a00;
|
|
130
|
+
out[1] = a01;
|
|
131
|
+
out[2] = a02;
|
|
132
|
+
out[3] = a03;
|
|
133
|
+
out[4] = a10;
|
|
134
|
+
out[5] = a11;
|
|
135
|
+
out[6] = a12;
|
|
136
|
+
out[7] = a13;
|
|
137
|
+
out[8] = a20;
|
|
138
|
+
out[9] = a21;
|
|
139
|
+
out[10] = a22;
|
|
140
|
+
out[11] = a23;
|
|
141
|
+
|
|
142
|
+
out[12] = a00 * x + a10 * y + a20 * z + m[12];
|
|
143
|
+
out[13] = a01 * x + a11 * y + a21 * z + m[13];
|
|
144
|
+
out[14] = a02 * x + a12 * y + a22 * z + m[14];
|
|
145
|
+
out[15] = a03 * x + a13 * y + a23 * z + m[15];
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return out;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* @param {number} x x translation.
|
|
153
|
+
* @param {number} y y translation.
|
|
154
|
+
* @param {number} z z translation.
|
|
155
|
+
* @param {Mat4} [out] optional matrix to store result
|
|
156
|
+
* @return {Mat4} out
|
|
157
|
+
*/
|
|
158
|
+
export function translation(x, y, z, out) {
|
|
159
|
+
out = out ?? create();
|
|
160
|
+
|
|
161
|
+
out[0] = 1;
|
|
162
|
+
out[1] = 0;
|
|
163
|
+
out[2] = 0;
|
|
164
|
+
out[3] = 0;
|
|
165
|
+
out[4] = 0;
|
|
166
|
+
out[5] = 1;
|
|
167
|
+
out[6] = 0;
|
|
168
|
+
out[7] = 0;
|
|
169
|
+
out[8] = 0;
|
|
170
|
+
out[9] = 0;
|
|
171
|
+
out[10] = 1;
|
|
172
|
+
out[11] = 0;
|
|
173
|
+
out[12] = x;
|
|
174
|
+
out[13] = y;
|
|
175
|
+
out[14] = z;
|
|
176
|
+
out[15] = 1;
|
|
177
|
+
|
|
178
|
+
return out;
|
|
179
|
+
}
|