ol 7.3.1-dev.1681144772646 → 7.3.1-dev.1681303152721

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.
@@ -1,14 +1,17 @@
1
1
  /**
2
- * Classes and utilities for generating shaders from literal style objects
2
+ * Class for generating shaders from literal style objects
3
3
  * @module ol/webgl/ShaderBuilder
4
4
  */
5
5
 
6
- import {
7
- ValueTypes,
8
- expressionToGlsl,
9
- getStringNumberEquivalent,
10
- uniformNameForVariable,
11
- } from '../style/expressions.js';
6
+ const BASE_UNIFORMS = `uniform mat4 u_projectionMatrix;
7
+ uniform mat4 u_screenToWorldMatrix;
8
+ uniform vec2 u_viewportSizePx;
9
+ uniform float u_pixelRatio;
10
+ uniform float u_globalAlpha;
11
+ uniform float u_time;
12
+ uniform float u_zoom;
13
+ uniform float u_resolution;
14
+ uniform vec4 u_renderExtent;`;
12
15
 
13
16
  /**
14
17
  * @typedef {Object} VaryingDescription
@@ -28,7 +31,7 @@ import {
28
31
  * .addVarying('v_width', 'float', 'a_width')
29
32
  * .addUniform('u_time')
30
33
  * .setColorExpression('...')
31
- * .setSizeExpression('...')
34
+ * .setSymbolSizeExpression('...')
32
35
  * .outputSymbolFragmentShader();
33
36
  * ```
34
37
  */
@@ -59,25 +62,25 @@ export class ShaderBuilder {
59
62
  * @type {string}
60
63
  * @private
61
64
  */
62
- this.sizeExpression = 'vec2(1.0)';
65
+ this.symbolSizeExpression = 'vec2(1.0)';
63
66
 
64
67
  /**
65
68
  * @type {string}
66
69
  * @private
67
70
  */
68
- this.rotationExpression = '0.0';
71
+ this.symbolRotationExpression = '0.0';
69
72
 
70
73
  /**
71
74
  * @type {string}
72
75
  * @private
73
76
  */
74
- this.offsetExpression = 'vec2(0.0)';
77
+ this.symbolOffsetExpression = 'vec2(0.0)';
75
78
 
76
79
  /**
77
80
  * @type {string}
78
81
  * @private
79
82
  */
80
- this.colorExpression = 'vec4(1.0)';
83
+ this.symbolColorExpression = 'vec4(1.0)';
81
84
 
82
85
  /**
83
86
  * @type {string}
@@ -95,7 +98,25 @@ export class ShaderBuilder {
95
98
  * @type {boolean}
96
99
  * @private
97
100
  */
98
- this.rotateWithView = false;
101
+ this.symbolRotateWithView = false;
102
+
103
+ /**
104
+ * @type {string}
105
+ * @private
106
+ */
107
+ this.strokeWidthExpression = '1.0';
108
+
109
+ /**
110
+ * @type {string}
111
+ * @private
112
+ */
113
+ this.strokeColorExpression = 'vec4(1.0)';
114
+
115
+ /**
116
+ * @type {string}
117
+ * @private
118
+ */
119
+ this.fillColorExpression = 'vec4(1.0)';
99
120
  }
100
121
 
101
122
  /**
@@ -144,8 +165,8 @@ export class ShaderBuilder {
144
165
  * @param {string} expression Size expression
145
166
  * @return {ShaderBuilder} the builder object
146
167
  */
147
- setSizeExpression(expression) {
148
- this.sizeExpression = expression;
168
+ setSymbolSizeExpression(expression) {
169
+ this.symbolSizeExpression = expression;
149
170
  return this;
150
171
  }
151
172
 
@@ -156,8 +177,8 @@ export class ShaderBuilder {
156
177
  * @param {string} expression Size expression
157
178
  * @return {ShaderBuilder} the builder object
158
179
  */
159
- setRotationExpression(expression) {
160
- this.rotationExpression = expression;
180
+ setSymbolRotationExpression(expression) {
181
+ this.symbolRotationExpression = expression;
161
182
  return this;
162
183
  }
163
184
 
@@ -170,7 +191,7 @@ export class ShaderBuilder {
170
191
  * @return {ShaderBuilder} the builder object
171
192
  */
172
193
  setSymbolOffsetExpression(expression) {
173
- this.offsetExpression = expression;
194
+ this.symbolOffsetExpression = expression;
174
195
  return this;
175
196
  }
176
197
 
@@ -181,8 +202,8 @@ export class ShaderBuilder {
181
202
  * @param {string} expression Color expression
182
203
  * @return {ShaderBuilder} the builder object
183
204
  */
184
- setColorExpression(expression) {
185
- this.colorExpression = expression;
205
+ setSymbolColorExpression(expression) {
206
+ this.symbolColorExpression = expression;
186
207
  return this;
187
208
  }
188
209
 
@@ -219,48 +240,31 @@ export class ShaderBuilder {
219
240
  * @return {ShaderBuilder} the builder object
220
241
  */
221
242
  setSymbolRotateWithView(rotateWithView) {
222
- this.rotateWithView = rotateWithView;
243
+ this.symbolRotateWithView = rotateWithView;
223
244
  return this;
224
245
  }
225
246
 
226
247
  /**
227
- * @return {string} Previously set size expression
228
- */
229
- getSizeExpression() {
230
- return this.sizeExpression;
231
- }
232
-
233
- /**
234
- * @return {string} Previously set symbol offset expression
235
- */
236
- getOffsetExpression() {
237
- return this.offsetExpression;
238
- }
239
-
240
- /**
241
- * @return {string} Previously set color expression
248
+ * @param {string} expression Stroke width expression, returning value in pixels
249
+ * @return {ShaderBuilder} the builder object
242
250
  */
243
- getColorExpression() {
244
- return this.colorExpression;
251
+ setStrokeWidthExpression(expression) {
252
+ this.strokeWidthExpression = expression;
253
+ return this;
245
254
  }
246
255
 
247
- /**
248
- * @return {string} Previously set texture coordinate expression
249
- */
250
- getTextureCoordinateExpression() {
251
- return this.texCoordExpression;
256
+ setStrokeColorExpression(expression) {
257
+ this.strokeColorExpression = expression;
258
+ return this;
252
259
  }
253
260
 
254
- /**
255
- * @return {string} Previously set fragment discard expression
256
- */
257
- getFragmentDiscardExpression() {
258
- return this.discardExpression;
261
+ setFillColorExpression(expression) {
262
+ this.fillColorExpression = expression;
263
+ return this;
259
264
  }
260
265
 
261
266
  /**
262
- * Generates a symbol vertex shader from the builder parameters,
263
- * intended to be used on point geometries.
267
+ * Generates a symbol vertex shader from the builder parameters
264
268
  *
265
269
  * Four uniforms are hardcoded in all shaders: `u_projectionMatrix`, `u_offsetScaleMatrix`,
266
270
  * `u_offsetRotateMatrix`, `u_time`.
@@ -276,7 +280,7 @@ export class ShaderBuilder {
276
280
  * @return {string} The full shader as a string.
277
281
  */
278
282
  getSymbolVertexShader(forHitDetection) {
279
- const offsetMatrix = this.rotateWithView
283
+ const offsetMatrix = this.symbolRotateWithView
280
284
  ? 'u_offsetScaleMatrix * u_offsetRotateMatrix'
281
285
  : 'u_offsetScaleMatrix';
282
286
 
@@ -320,9 +324,9 @@ ${varyings
320
324
  .join('\n')}
321
325
  void main(void) {
322
326
  mat4 offsetMatrix = ${offsetMatrix};
323
- vec2 halfSize = ${this.sizeExpression} * 0.5;
324
- vec2 offset = ${this.offsetExpression};
325
- float angle = ${this.rotationExpression};
327
+ vec2 halfSize = ${this.symbolSizeExpression} * 0.5;
328
+ vec2 offset = ${this.symbolOffsetExpression};
329
+ float angle = ${this.symbolRotationExpression};
326
330
  float offsetX;
327
331
  float offsetY;
328
332
  if (a_index == 0.0) {
@@ -356,8 +360,7 @@ ${varyings
356
360
  }
357
361
 
358
362
  /**
359
- * Generates a symbol fragment shader from the builder parameters,
360
- * intended to be used on point geometries.
363
+ * Generates a symbol fragment shader from the builder parameters
361
364
  *
362
365
  * Expects the following varyings to be transmitted by the vertex shader:
363
366
  * `vec2 v_quadCoord`, `vec2 v_texCoord`
@@ -399,194 +402,305 @@ ${varyings
399
402
  .join('\n')}
400
403
  void main(void) {
401
404
  if (${this.discardExpression}) { discard; }
402
- gl_FragColor = ${this.colorExpression};
405
+ gl_FragColor = ${this.symbolColorExpression};
403
406
  gl_FragColor.rgb *= gl_FragColor.a;
404
407
  ${hitDetectionBypass}
405
408
  }`;
406
409
  }
410
+
411
+ /**
412
+ * Generates a stroke vertex shader from the builder parameters
413
+ *
414
+ * @param {boolean} [forHitDetection] If true, the shader will be modified to include hit detection variables
415
+ * (namely, hit color with encoded feature id).
416
+ * @return {string} The full shader as a string.
417
+ */
418
+ getStrokeVertexShader(forHitDetection) {
419
+ let attributes = this.attributes;
420
+ let varyings = this.varyings;
421
+
422
+ if (forHitDetection) {
423
+ attributes = attributes.concat('vec4 a_hitColor');
424
+ varyings = varyings.concat({
425
+ name: 'v_hitColor',
426
+ type: 'vec4',
427
+ expression: 'a_hitColor',
428
+ });
429
+ }
430
+
431
+ return `#ifdef GL_FRAGMENT_PRECISION_HIGH
432
+ precision highp float;
433
+ #else
434
+ precision mediump float;
435
+ #endif
436
+ ${BASE_UNIFORMS}
437
+ ${this.uniforms
438
+ .map(function (uniform) {
439
+ return 'uniform ' + uniform + ';';
440
+ })
441
+ .join('\n')}
442
+ attribute vec2 a_position;
443
+ attribute float a_index;
444
+ attribute vec2 a_segmentStart;
445
+ attribute vec2 a_segmentEnd;
446
+ attribute float a_parameters;
447
+ ${attributes
448
+ .map(function (attribute) {
449
+ return 'attribute ' + attribute + ';';
450
+ })
451
+ .join('\n')}
452
+ varying vec2 v_segmentStart;
453
+ varying vec2 v_segmentEnd;
454
+ varying float v_angleStart;
455
+ varying float v_angleEnd;
456
+ varying float v_width;
457
+ ${varyings
458
+ .map(function (varying) {
459
+ return 'varying ' + varying.type + ' ' + varying.name + ';';
460
+ })
461
+ .join('\n')}
462
+
463
+ vec2 worldToPx(vec2 worldPos) {
464
+ vec4 screenPos = u_projectionMatrix * vec4(worldPos, 0.0, 1.0);
465
+ return (0.5 * screenPos.xy + 0.5) * u_viewportSizePx;
407
466
  }
408
467
 
409
- /**
410
- * @typedef {Object} StyleParseResult
411
- * @property {ShaderBuilder} builder Shader builder pre-configured according to a given style
412
- * @property {Object<string,import("./Helper").UniformValue>} uniforms Uniform definitions.
413
- * @property {Array<import("../renderer/webgl/PointsLayer").CustomAttribute>} attributes Attribute descriptions.
414
- */
468
+ vec4 pxToScreen(vec2 pxPos) {
469
+ vec2 screenPos = pxPos * 4.0 / u_viewportSizePx;
470
+ return vec4(screenPos.xy, 0.0, 0.0);
471
+ }
415
472
 
416
- /**
417
- * Parses a {@link import("../style/literal").LiteralStyle} object and returns a {@link ShaderBuilder}
418
- * object that has been configured according to the given style, as well as `attributes` and `uniforms`
419
- * arrays to be fed to the `WebGLPointsRenderer` class.
420
- *
421
- * Also returns `uniforms` and `attributes` properties as expected by the
422
- * {@link module:ol/renderer/webgl/PointsLayer~WebGLPointsLayerRenderer}.
423
- *
424
- * @param {import("../style/literal").LiteralStyle} style Literal style.
425
- * @return {StyleParseResult} Result containing shader params, attributes and uniforms.
426
- */
427
- export function parseLiteralStyle(style) {
428
- const symbStyle = style.symbol;
429
- const size = symbStyle.size !== undefined ? symbStyle.size : 1;
430
- const color = symbStyle.color || 'white';
431
- const texCoord = symbStyle.textureCoord || [0, 0, 1, 1];
432
- const offset = symbStyle.offset || [0, 0];
433
- const opacity = symbStyle.opacity !== undefined ? symbStyle.opacity : 1;
434
- const rotation = symbStyle.rotation !== undefined ? symbStyle.rotation : 0;
473
+ vec2 getOffsetDirection(vec2 normalPx, vec2 tangentPx, float joinAngle) {
474
+ if (cos(joinAngle) > 0.93) return normalPx - tangentPx;
475
+ float halfAngle = joinAngle / 2.0;
476
+ vec2 angleBisectorNormal = vec2(
477
+ sin(halfAngle) * normalPx.x + cos(halfAngle) * normalPx.y,
478
+ -cos(halfAngle) * normalPx.x + sin(halfAngle) * normalPx.y
479
+ );
480
+ float length = 1.0 / sin(halfAngle);
481
+ return angleBisectorNormal * length;
482
+ }
483
+
484
+ void main(void) {
485
+ float lineWidth = ${this.strokeWidthExpression};
486
+ float anglePrecision = 1500.0;
487
+ float paramShift = 10000.0;
488
+ v_angleStart = fract(a_parameters / paramShift) * paramShift / anglePrecision;
489
+ v_angleEnd = fract(floor(a_parameters / paramShift + 0.5) / paramShift) * paramShift / anglePrecision;
490
+ float vertexNumber = floor(a_parameters / paramShift / paramShift + 0.0001);
491
+ vec2 tangentPx = worldToPx(a_segmentEnd) - worldToPx(a_segmentStart);
492
+ tangentPx = normalize(tangentPx);
493
+ vec2 normalPx = vec2(-tangentPx.y, tangentPx.x);
494
+ float normalDir = vertexNumber < 0.5 || (vertexNumber > 1.5 && vertexNumber < 2.5) ? 1.0 : -1.0;
495
+ float tangentDir = vertexNumber < 1.5 ? 1.0 : -1.0;
496
+ float angle = vertexNumber < 1.5 ? v_angleStart : v_angleEnd;
497
+ vec2 offsetPx = getOffsetDirection(normalPx * normalDir, tangentDir * tangentPx, angle) * lineWidth * 0.5;
498
+ vec2 position = vertexNumber < 1.5 ? a_segmentStart : a_segmentEnd;
499
+ gl_Position = u_projectionMatrix * vec4(position, 0.0, 1.0) + pxToScreen(offsetPx);
500
+ v_segmentStart = worldToPx(a_segmentStart);
501
+ v_segmentEnd = worldToPx(a_segmentEnd);
502
+ v_width = lineWidth;
503
+ ${varyings
504
+ .map(function (varying) {
505
+ return ' ' + varying.name + ' = ' + varying.expression + ';';
506
+ })
507
+ .join('\n')}
508
+ }`;
509
+ }
435
510
 
436
511
  /**
437
- * @type {import("../style/expressions.js").ParsingContext}
512
+ * Generates a stroke fragment shader from the builder parameters
513
+ *
514
+ * @param {boolean} [forHitDetection] If true, the shader will be modified to include hit detection variables
515
+ * (namely, hit color with encoded feature id).
516
+ * @return {string} The full shader as a string.
438
517
  */
439
- const vertContext = {
440
- inFragmentShader: false,
441
- variables: [],
442
- attributes: [],
443
- stringLiteralsMap: {},
444
- functions: {},
445
- };
446
- const parsedSize = expressionToGlsl(
447
- vertContext,
448
- size,
449
- ValueTypes.NUMBER_ARRAY | ValueTypes.NUMBER
450
- );
451
- const parsedOffset = expressionToGlsl(
452
- vertContext,
453
- offset,
454
- ValueTypes.NUMBER_ARRAY
455
- );
456
- const parsedTexCoord = expressionToGlsl(
457
- vertContext,
458
- texCoord,
459
- ValueTypes.NUMBER_ARRAY
460
- );
461
- const parsedRotation = expressionToGlsl(
462
- vertContext,
463
- rotation,
464
- ValueTypes.NUMBER
465
- );
518
+ getStrokeFragmentShader(forHitDetection) {
519
+ const hitDetectionBypass = forHitDetection
520
+ ? ' if (gl_FragColor.a < 0.1) { discard; } gl_FragColor = v_hitColor;'
521
+ : '';
522
+
523
+ let varyings = this.varyings;
524
+
525
+ if (forHitDetection) {
526
+ varyings = varyings.concat({
527
+ name: 'v_hitColor',
528
+ type: 'vec4',
529
+ expression: 'a_hitColor',
530
+ });
531
+ }
532
+
533
+ return `#ifdef GL_FRAGMENT_PRECISION_HIGH
534
+ precision highp float;
535
+ #else
536
+ precision mediump float;
537
+ #endif
538
+ ${BASE_UNIFORMS}
539
+ ${this.uniforms
540
+ .map(function (uniform) {
541
+ return 'uniform ' + uniform + ';';
542
+ })
543
+ .join('\n')}
544
+ varying vec2 v_segmentStart;
545
+ varying vec2 v_segmentEnd;
546
+ varying float v_angleStart;
547
+ varying float v_angleEnd;
548
+ varying float v_width;
549
+ ${varyings
550
+ .map(function (varying) {
551
+ return 'varying ' + varying.type + ' ' + varying.name + ';';
552
+ })
553
+ .join('\n')}
554
+
555
+ vec2 pxToWorld(vec2 pxPos) {
556
+ vec2 screenPos = 2.0 * pxPos / u_viewportSizePx - 1.0;
557
+ return (u_screenToWorldMatrix * vec4(screenPos, 0.0, 1.0)).xy;
558
+ }
559
+
560
+ float segmentDistanceField(vec2 point, vec2 start, vec2 end, float radius) {
561
+ vec2 startToPoint = point - start;
562
+ vec2 startToEnd = end - start;
563
+ float ratio = clamp(dot(startToPoint, startToEnd) / dot(startToEnd, startToEnd), 0.0, 1.0);
564
+ float dist = length(startToPoint - ratio * startToEnd);
565
+ return 1.0 - smoothstep(radius - 1.0, radius, dist);
566
+ }
567
+
568
+ void main(void) {
569
+ vec2 v_currentPoint = gl_FragCoord.xy / u_pixelRatio;
570
+ #ifdef GL_FRAGMENT_PRECISION_HIGH
571
+ vec2 v_worldPos = pxToWorld(v_currentPoint);
572
+ if (
573
+ abs(u_renderExtent[0] - u_renderExtent[2]) > 0.0 && (
574
+ v_worldPos[0] < u_renderExtent[0] ||
575
+ v_worldPos[1] < u_renderExtent[1] ||
576
+ v_worldPos[0] > u_renderExtent[2] ||
577
+ v_worldPos[1] > u_renderExtent[3]
578
+ )
579
+ ) {
580
+ discard;
581
+ }
582
+ #endif
583
+ if (${this.discardExpression}) { discard; }
584
+ gl_FragColor = ${this.strokeColorExpression} * u_globalAlpha;
585
+ gl_FragColor *= segmentDistanceField(v_currentPoint, v_segmentStart, v_segmentEnd, v_width);
586
+ ${hitDetectionBypass}
587
+ }`;
588
+ }
466
589
 
467
590
  /**
468
- * @type {import("../style/expressions.js").ParsingContext}
591
+ * Generates a fill vertex shader from the builder parameters
592
+ *
593
+ * @param {boolean} [forHitDetection] If true, the shader will be modified to include hit detection variables
594
+ * (namely, hit color with encoded feature id).
595
+ * @return {string} The full shader as a string.
469
596
  */
470
- const fragContext = {
471
- inFragmentShader: true,
472
- variables: vertContext.variables,
473
- attributes: [],
474
- stringLiteralsMap: vertContext.stringLiteralsMap,
475
- functions: {},
476
- };
477
- const parsedColor = expressionToGlsl(fragContext, color, ValueTypes.COLOR);
478
- const parsedOpacity = expressionToGlsl(
479
- fragContext,
480
- opacity,
481
- ValueTypes.NUMBER
482
- );
597
+ getFillVertexShader(forHitDetection) {
598
+ let attributes = this.attributes;
599
+ let varyings = this.varyings;
483
600
 
484
- let opacityFilter = '1.0';
485
- const visibleSize = `vec2(${expressionToGlsl(
486
- fragContext,
487
- size,
488
- ValueTypes.NUMBER_ARRAY | ValueTypes.NUMBER
489
- )}).x`;
490
- switch (symbStyle.symbolType) {
491
- case 'square':
492
- break;
493
- case 'image':
494
- break;
495
- // taken from https://thebookofshaders.com/07/
496
- case 'circle':
497
- opacityFilter = `(1.0-smoothstep(1.-4./${visibleSize},1.,dot(v_quadCoord-.5,v_quadCoord-.5)*4.))`;
498
- break;
499
- case 'triangle':
500
- const st = '(v_quadCoord*2.-1.)';
501
- const a = `(atan(${st}.x,${st}.y))`;
502
- opacityFilter = `(1.0-smoothstep(.5-3./${visibleSize},.5,cos(floor(.5+${a}/2.094395102)*2.094395102-${a})*length(${st})))`;
503
- break;
504
-
505
- default:
506
- throw new Error('Unexpected symbol type: ' + symbStyle.symbolType);
507
- }
601
+ if (forHitDetection) {
602
+ attributes = attributes.concat('vec4 a_hitColor');
603
+ varyings = varyings.concat({
604
+ name: 'v_hitColor',
605
+ type: 'vec4',
606
+ expression: 'a_hitColor',
607
+ });
608
+ }
508
609
 
509
- const builder = new ShaderBuilder()
510
- .setSizeExpression(`vec2(${parsedSize})`)
511
- .setRotationExpression(parsedRotation)
512
- .setSymbolOffsetExpression(parsedOffset)
513
- .setTextureCoordinateExpression(parsedTexCoord)
514
- .setSymbolRotateWithView(!!symbStyle.rotateWithView)
515
- .setColorExpression(
516
- `vec4(${parsedColor}.rgb, ${parsedColor}.a * ${parsedOpacity} * ${opacityFilter})`
517
- );
518
-
519
- if (style.filter) {
520
- const parsedFilter = expressionToGlsl(
521
- fragContext,
522
- style.filter,
523
- ValueTypes.BOOLEAN
524
- );
525
- builder.setFragmentDiscardExpression(`!${parsedFilter}`);
526
- }
610
+ return `#ifdef GL_FRAGMENT_PRECISION_HIGH
611
+ precision highp float;
612
+ #else
613
+ precision mediump float;
614
+ #endif
615
+ ${BASE_UNIFORMS}
616
+ ${this.uniforms
617
+ .map(function (uniform) {
618
+ return 'uniform ' + uniform + ';';
619
+ })
620
+ .join('\n')}
621
+ attribute vec2 a_position;
622
+ ${attributes
623
+ .map(function (attribute) {
624
+ return 'attribute ' + attribute + ';';
625
+ })
626
+ .join('\n')}
627
+ ${varyings
628
+ .map(function (varying) {
629
+ return 'varying ' + varying.type + ' ' + varying.name + ';';
630
+ })
631
+ .join('\n')}
527
632
 
528
- /** @type {Object<string,import("../webgl/Helper").UniformValue>} */
529
- const uniforms = {};
530
-
531
- // define one uniform per variable
532
- fragContext.variables.forEach(function (varName) {
533
- const uniformName = uniformNameForVariable(varName);
534
- builder.addUniform(`float ${uniformName}`);
535
- uniforms[uniformName] = function () {
536
- if (!style.variables || style.variables[varName] === undefined) {
537
- throw new Error(
538
- `The following variable is missing from the style: ${varName}`
539
- );
540
- }
541
- let value = style.variables[varName];
542
- if (typeof value === 'string') {
543
- value = getStringNumberEquivalent(vertContext, value);
544
- }
545
- return value !== undefined ? value : -9999999; // to avoid matching with the first string literal
546
- };
547
- });
548
-
549
- if (symbStyle.symbolType === 'image' && symbStyle.src) {
550
- const texture = new Image();
551
- texture.crossOrigin =
552
- symbStyle.crossOrigin === undefined ? 'anonymous' : symbStyle.crossOrigin;
553
- texture.src = symbStyle.src;
554
- builder
555
- .addUniform('sampler2D u_texture')
556
- .setColorExpression(
557
- builder.getColorExpression() + ' * texture2D(u_texture, v_texCoord)'
558
- );
559
- uniforms['u_texture'] = texture;
633
+ void main(void) {
634
+ gl_Position = u_projectionMatrix * vec4(a_position, 0.0, 1.0);
635
+ ${varyings
636
+ .map(function (varying) {
637
+ return ' ' + varying.name + ' = ' + varying.expression + ';';
638
+ })
639
+ .join('\n')}
640
+ }`;
560
641
  }
561
642
 
562
- // for each feature attribute used in the fragment shader, define a varying that will be used to pass data
563
- // from the vertex to the fragment shader, as well as an attribute in the vertex shader (if not already present)
564
- fragContext.attributes.forEach(function (attrName) {
565
- if (!vertContext.attributes.includes(attrName)) {
566
- vertContext.attributes.push(attrName);
643
+ /**
644
+ * Generates a fill fragment shader from the builder parameters
645
+ *
646
+ * @param {boolean} [forHitDetection] If true, the shader will be modified to include hit detection variables
647
+ * (namely, hit color with encoded feature id).
648
+ * @return {string} The full shader as a string.
649
+ */
650
+ getFillFragmentShader(forHitDetection) {
651
+ const hitDetectionBypass = forHitDetection
652
+ ? ' if (gl_FragColor.a < 0.1) { discard; } gl_FragColor = v_hitColor;'
653
+ : '';
654
+
655
+ let varyings = this.varyings;
656
+
657
+ if (forHitDetection) {
658
+ varyings = varyings.concat({
659
+ name: 'v_hitColor',
660
+ type: 'vec4',
661
+ expression: 'a_hitColor',
662
+ });
567
663
  }
568
- builder.addVarying(`v_${attrName}`, 'float', `a_${attrName}`);
569
- });
570
-
571
- // for each feature attribute used in the vertex shader, define an attribute in the vertex shader.
572
- vertContext.attributes.forEach(function (attrName) {
573
- builder.addAttribute(`float a_${attrName}`);
574
- });
575
-
576
- return {
577
- builder: builder,
578
- attributes: vertContext.attributes.map(function (attributeName) {
579
- return {
580
- name: attributeName,
581
- callback: function (feature, props) {
582
- let value = props[attributeName];
583
- if (typeof value === 'string') {
584
- value = getStringNumberEquivalent(vertContext, value);
585
- }
586
- return value !== undefined ? value : -9999999; // to avoid matching with the first string literal
587
- },
588
- };
589
- }),
590
- uniforms: uniforms,
591
- };
664
+
665
+ return `#ifdef GL_FRAGMENT_PRECISION_HIGH
666
+ precision highp float;
667
+ #else
668
+ precision mediump float;
669
+ #endif
670
+ ${BASE_UNIFORMS}
671
+ ${this.uniforms
672
+ .map(function (uniform) {
673
+ return 'uniform ' + uniform + ';';
674
+ })
675
+ .join('\n')}
676
+ ${varyings
677
+ .map(function (varying) {
678
+ return 'varying ' + varying.type + ' ' + varying.name + ';';
679
+ })
680
+ .join('\n')}
681
+
682
+ vec2 pxToWorld(vec2 pxPos) {
683
+ vec2 screenPos = 2.0 * pxPos / u_viewportSizePx - 1.0;
684
+ return (u_screenToWorldMatrix * vec4(screenPos, 0.0, 1.0)).xy;
685
+ }
686
+
687
+ void main(void) {
688
+ #ifdef GL_FRAGMENT_PRECISION_HIGH
689
+ vec2 v_worldPos = pxToWorld(gl_FragCoord.xy / u_pixelRatio);
690
+ if (
691
+ abs(u_renderExtent[0] - u_renderExtent[2]) > 0.0 && (
692
+ v_worldPos[0] < u_renderExtent[0] ||
693
+ v_worldPos[1] < u_renderExtent[1] ||
694
+ v_worldPos[0] > u_renderExtent[2] ||
695
+ v_worldPos[1] > u_renderExtent[3]
696
+ )
697
+ ) {
698
+ discard;
699
+ }
700
+ #endif
701
+ if (${this.discardExpression}) { discard; }
702
+ gl_FragColor = ${this.fillColorExpression} * u_globalAlpha;
703
+ ${hitDetectionBypass}
704
+ }`;
705
+ }
592
706
  }