ol 10.2.2-dev.1727715662036 → 10.2.2-dev.1727768316442

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/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 {Array<number>} "4x4 matrix representing a 3D identity transform."
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 {Array<number>} mat4 Flattened 4x4 matrix receiving the result.
15
+ * @param {Mat4} mat4 Flattened 4x4 matrix receiving the result.
14
16
  * @param {import("../transform.js").Transform} transform Transformation matrix.
15
- * @return {Array<number>} "2D transformation matrix as flattened 4x4 matrix."
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
+ }
@@ -0,0 +1,58 @@
1
+ /**
2
+ * @param {WebGLRenderingContext} gl Rendering Context.
3
+ * @param {string} fragmentSource Fragment shader source.
4
+ * @param {string} vertexSource Vertex shader source.
5
+ * @return {WebGLProgram} [progam] The program.
6
+ */
7
+ export function createProgram(gl: WebGLRenderingContext, fragmentSource: string, vertexSource: string): WebGLProgram;
8
+ /** @typedef {import("../transform.js").Transform} Matrix */
9
+ /**
10
+ * Canvas-like operations implemented in webgl.
11
+ */
12
+ export class Canvas {
13
+ /**
14
+ * @param {WebGLRenderingContext} gl Context to render in.
15
+ */
16
+ constructor(gl: WebGLRenderingContext);
17
+ /**
18
+ * @private
19
+ * @type {WebGLRenderingContext}
20
+ */
21
+ private gl_;
22
+ /**
23
+ * @private
24
+ * @type {WebGLProgram}
25
+ */
26
+ private program_;
27
+ positionLocation: number;
28
+ texcoordLocation: number;
29
+ matrixLocation: WebGLUniformLocation | null;
30
+ textureMatrixLocation: WebGLUniformLocation | null;
31
+ textureLocation: WebGLUniformLocation | null;
32
+ positionBuffer: WebGLBuffer | null;
33
+ positions: number[];
34
+ texcoordBuffer: WebGLBuffer | null;
35
+ texcoords: number[];
36
+ /**
37
+ * 2dContext drawImage call implemented in webgl.
38
+ * Unlike images, textures do not have a width and height associated
39
+ * with them so we'll pass in the width and height of the texture.
40
+ *
41
+ * @param {WebGLTexture} tex Image to draw.
42
+ * @param {number} texWidth Image width.
43
+ * @param {number} texHeight Image height.
44
+ * @param {number} srcX Top-left x-point to read src image.
45
+ * @param {number} srcY Top-left y-point to read src image.
46
+ * @param {number} [srcWidth] Width of source to read.
47
+ * @param {number} [srcHeight] Height of source to read.
48
+ * @param {number} [dstX] Top-left x-point of destination.
49
+ * @param {number} [dstY] Top-left y-point of destination.
50
+ * @param {number} [dstWidth] Width of written image in destination.
51
+ * @param {number} [dstHeight] Height of written image in destination.
52
+ * @param {number} [width] Width of canvas.
53
+ * @param {number} [height] Height of canvas.
54
+ */
55
+ drawImage(tex: WebGLTexture, texWidth: number, texHeight: number, srcX: number, srcY: number, srcWidth?: number | undefined, srcHeight?: number | undefined, dstX?: number | undefined, dstY?: number | undefined, dstWidth?: number | undefined, dstHeight?: number | undefined, width?: number | undefined, height?: number | undefined): void;
56
+ }
57
+ export type Matrix = import("../transform.js").Transform;
58
+ //# sourceMappingURL=Canvas.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Canvas.d.ts","sourceRoot":"","sources":["Canvas.js"],"names":[],"mappings":"AAsNA;;;;;GAKG;AACH,kCALW,qBAAqB,kBACrB,MAAM,gBACN,MAAM,GACL,YAAY,CAuBvB;AAvMD,4DAA4D;AAE5D;;GAEG;AACH;IACE;;OAEG;IACH,gBAFW,qBAAqB,EA4C/B;IAzCC;;;OAGG;IACH,YAAa;IAEb;;;OAGG;IACH,iBAAiE;IAEjE,yBAAyE;IACzE,yBAAyE;IAEzE,4CAAsE;IACtE,mDAGC;IACD,6CAAwE;IAExE,mCAAuC;IAGvC,oBAAqD;IAOrD,mCAAuC;IAGvC,oBAAqD;IAQvD;;;;;;;;;;;;;;;;;;OAkBG;IACH,eAdW,YAAY,YACZ,MAAM,aACN,MAAM,QACN,MAAM,QACN,MAAM,qPAgFhB;CACF;qBA/Ia,OAAO,iBAAiB,EAAE,SAAS"}
@@ -0,0 +1,242 @@
1
+ import * as mat4 from '../vec/mat4.js';
2
+
3
+ /**
4
+ * @module ol/webgl/Canvas
5
+ */
6
+
7
+ const VERTEX_SHADER = `
8
+ attribute vec4 a_position;
9
+ attribute vec4 a_texcoord;
10
+
11
+ uniform mat4 u_matrix;
12
+ uniform mat4 u_textureMatrix;
13
+
14
+ varying vec2 v_texcoord;
15
+
16
+ void main() {
17
+ gl_Position = u_matrix * a_position;
18
+ vec2 texcoord = (u_textureMatrix * a_texcoord).xy;
19
+ v_texcoord = texcoord;
20
+ }
21
+ `;
22
+
23
+ const FRAGMENT_SHADER = `
24
+ precision mediump float;
25
+
26
+ varying vec2 v_texcoord;
27
+
28
+ uniform sampler2D u_texture;
29
+
30
+ void main() {
31
+ if (
32
+ v_texcoord.x < 0.0 ||
33
+ v_texcoord.y < 0.0 ||
34
+ v_texcoord.x > 1.0 ||
35
+ v_texcoord.y > 1.0
36
+ ) {
37
+ discard;
38
+ }
39
+ gl_FragColor = texture2D(u_texture, v_texcoord);
40
+ }
41
+ `;
42
+
43
+ /** @typedef {import("../transform.js").Transform} Matrix */
44
+
45
+ /**
46
+ * Canvas-like operations implemented in webgl.
47
+ */
48
+ export class Canvas {
49
+ /**
50
+ * @param {WebGLRenderingContext} gl Context to render in.
51
+ */
52
+ constructor(gl) {
53
+ /**
54
+ * @private
55
+ * @type {WebGLRenderingContext}
56
+ */
57
+ this.gl_ = gl;
58
+
59
+ /**
60
+ * @private
61
+ * @type {WebGLProgram}
62
+ */
63
+ this.program_ = createProgram(gl, FRAGMENT_SHADER, VERTEX_SHADER);
64
+
65
+ this.positionLocation = gl.getAttribLocation(this.program_, 'a_position');
66
+ this.texcoordLocation = gl.getAttribLocation(this.program_, 'a_texcoord');
67
+
68
+ this.matrixLocation = gl.getUniformLocation(this.program_, 'u_matrix');
69
+ this.textureMatrixLocation = gl.getUniformLocation(
70
+ this.program_,
71
+ 'u_textureMatrix',
72
+ );
73
+ this.textureLocation = gl.getUniformLocation(this.program_, 'u_texture');
74
+
75
+ this.positionBuffer = gl.createBuffer();
76
+ gl.bindBuffer(gl.ARRAY_BUFFER, this.positionBuffer);
77
+
78
+ this.positions = [0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1];
79
+ gl.bufferData(
80
+ gl.ARRAY_BUFFER,
81
+ new Float32Array(this.positions),
82
+ gl.STATIC_DRAW,
83
+ );
84
+
85
+ this.texcoordBuffer = gl.createBuffer();
86
+ gl.bindBuffer(gl.ARRAY_BUFFER, this.texcoordBuffer);
87
+
88
+ this.texcoords = [0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1];
89
+ gl.bufferData(
90
+ gl.ARRAY_BUFFER,
91
+ new Float32Array(this.texcoords),
92
+ gl.STATIC_DRAW,
93
+ );
94
+ }
95
+
96
+ /**
97
+ * 2dContext drawImage call implemented in webgl.
98
+ * Unlike images, textures do not have a width and height associated
99
+ * with them so we'll pass in the width and height of the texture.
100
+ *
101
+ * @param {WebGLTexture} tex Image to draw.
102
+ * @param {number} texWidth Image width.
103
+ * @param {number} texHeight Image height.
104
+ * @param {number} srcX Top-left x-point to read src image.
105
+ * @param {number} srcY Top-left y-point to read src image.
106
+ * @param {number} [srcWidth] Width of source to read.
107
+ * @param {number} [srcHeight] Height of source to read.
108
+ * @param {number} [dstX] Top-left x-point of destination.
109
+ * @param {number} [dstY] Top-left y-point of destination.
110
+ * @param {number} [dstWidth] Width of written image in destination.
111
+ * @param {number} [dstHeight] Height of written image in destination.
112
+ * @param {number} [width] Width of canvas.
113
+ * @param {number} [height] Height of canvas.
114
+ */
115
+ drawImage(
116
+ tex,
117
+ texWidth,
118
+ texHeight,
119
+ srcX,
120
+ srcY,
121
+ srcWidth,
122
+ srcHeight,
123
+ dstX,
124
+ dstY,
125
+ dstWidth,
126
+ dstHeight,
127
+ width,
128
+ height,
129
+ ) {
130
+ const gl = this.gl_;
131
+
132
+ if (dstX === undefined) {
133
+ dstX = srcX;
134
+ }
135
+ if (dstY === undefined) {
136
+ dstY = srcY;
137
+ }
138
+ if (srcWidth === undefined) {
139
+ srcWidth = texWidth;
140
+ }
141
+ if (srcHeight === undefined) {
142
+ srcHeight = texHeight;
143
+ }
144
+ if (dstWidth === undefined) {
145
+ dstWidth = srcWidth;
146
+ }
147
+ if (dstHeight === undefined) {
148
+ dstHeight = srcHeight;
149
+ }
150
+ if (width === undefined) {
151
+ width = gl.canvas.width;
152
+ }
153
+ if (height === undefined) {
154
+ height = gl.canvas.height;
155
+ }
156
+
157
+ gl.bindTexture(gl.TEXTURE_2D, tex);
158
+
159
+ gl.useProgram(this.program_);
160
+
161
+ gl.bindBuffer(gl.ARRAY_BUFFER, this.positionBuffer);
162
+ gl.enableVertexAttribArray(this.positionLocation);
163
+ gl.vertexAttribPointer(this.positionLocation, 2, gl.FLOAT, false, 0, 0);
164
+ gl.bindBuffer(gl.ARRAY_BUFFER, this.texcoordBuffer);
165
+ gl.enableVertexAttribArray(this.texcoordLocation);
166
+ gl.vertexAttribPointer(this.texcoordLocation, 2, gl.FLOAT, false, 0, 0);
167
+
168
+ // matrix for converting pixels to clip space
169
+ let matrix = mat4.orthographic(0, width, 0, height, -1, 1);
170
+ matrix = mat4.translate(matrix, dstX, dstY, 0);
171
+ matrix = mat4.scale(matrix, dstWidth, dstHeight, 1);
172
+ gl.uniformMatrix4fv(this.matrixLocation, false, matrix);
173
+
174
+ let texMatrix = mat4.translation(srcX / texWidth, srcY / texHeight, 0);
175
+ texMatrix = mat4.scale(
176
+ texMatrix,
177
+ srcWidth / texWidth,
178
+ srcHeight / texHeight,
179
+ 1,
180
+ );
181
+
182
+ gl.uniformMatrix4fv(this.textureMatrixLocation, false, texMatrix);
183
+ gl.uniform1i(this.textureLocation, 0);
184
+ gl.drawArrays(gl.TRIANGLES, 0, this.positions.length / 2);
185
+ }
186
+ }
187
+
188
+ /**
189
+ * @param {WebGLRenderingContext} gl Rendering Context.
190
+ * @param {GLenum} type Type of shader.
191
+ * @param {string} source source of shader.
192
+ * @return {WebGLShader} [progam] The program.
193
+ */
194
+ function createShader(gl, type, source) {
195
+ const shader = gl.createShader(type);
196
+
197
+ if (shader === null) {
198
+ throw new Error('Shader compilation failed');
199
+ }
200
+
201
+ gl.shaderSource(shader, source);
202
+
203
+ gl.compileShader(shader);
204
+ if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
205
+ const log = gl.getShaderInfoLog(shader);
206
+ if (log === null) {
207
+ throw new Error('Shader info log creation failed');
208
+ }
209
+ throw new Error(log);
210
+ }
211
+
212
+ return shader;
213
+ }
214
+
215
+ /**
216
+ * @param {WebGLRenderingContext} gl Rendering Context.
217
+ * @param {string} fragmentSource Fragment shader source.
218
+ * @param {string} vertexSource Vertex shader source.
219
+ * @return {WebGLProgram} [progam] The program.
220
+ */
221
+ export function createProgram(gl, fragmentSource, vertexSource) {
222
+ const program = gl.createProgram();
223
+
224
+ const vertexShader = createShader(gl, gl.VERTEX_SHADER, vertexSource);
225
+ const fragmentShader = createShader(gl, gl.FRAGMENT_SHADER, fragmentSource);
226
+ if (program === null) {
227
+ throw new Error('Program creation failed');
228
+ }
229
+
230
+ gl.attachShader(program, vertexShader);
231
+ gl.attachShader(program, fragmentShader);
232
+
233
+ gl.linkProgram(program);
234
+ if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
235
+ const log = gl.getProgramInfoLog(program);
236
+ if (log === null) {
237
+ throw new Error('Program info log creation failed');
238
+ }
239
+ throw new Error();
240
+ }
241
+ return program;
242
+ }