From bd9e73a5340a7a30bc1ba7016f951a8b32195f0f Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Thu, 21 Jul 2022 13:30:22 -0700 Subject: [PATCH] Renaming fill and stroke attributes and shaders --- examples/webgl-vector-layer.js | 4 +- src/ol/renderer/webgl/VectorLayer.js | 78 ++++++++++++++-------------- src/ol/renderer/webgl/shaders.js | 12 ++--- 3 files changed, 46 insertions(+), 48 deletions(-) diff --git a/examples/webgl-vector-layer.js b/examples/webgl-vector-layer.js index c1fee56403..e7e60bc762 100644 --- a/examples/webgl-vector-layer.js +++ b/examples/webgl-vector-layer.js @@ -16,7 +16,7 @@ class WebGLLayer extends Layer { createRenderer() { return new WebGLVectorLayerRenderer(this, { className: this.getClassName(), - polygonShader: { + fill: { attributes: { [DefaultAttributes.COLOR]: function (feature, properties) { const color = asArray(properties.COLOR || '#eee'); @@ -28,7 +28,7 @@ class WebGLLayer extends Layer { }, }, }, - lineStringShader: { + stroke: { attributes: { [DefaultAttributes.COLOR]: function (feature, properties) { const color = [...asArray(properties.COLOR || '#eee')]; diff --git a/src/ol/renderer/webgl/VectorLayer.js b/src/ol/renderer/webgl/VectorLayer.js index 2ce81e126a..aa2ff1c25f 100644 --- a/src/ol/renderer/webgl/VectorLayer.js +++ b/src/ol/renderer/webgl/VectorLayer.js @@ -10,13 +10,13 @@ import VectorEventType from '../../source/VectorEventType.js'; import ViewHint from '../../ViewHint.js'; import WebGLLayerRenderer from './Layer.js'; import { - DEFAULT_LINESTRING_FRAGMENT, - DEFAULT_LINESTRING_VERTEX, - DEFAULT_POINT_FRAGMENT, - DEFAULT_POINT_VERTEX, - DEFAULT_POLYGON_FRAGMENT, - DEFAULT_POLYGON_VERTEX, DefaultAttributes, + FILL_FRAGMENT_SHADER, + FILL_VERTEX_SHADER, + POINT_FRAGMENT_SHADER, + POINT_VERTEX_SHADER, + STROKE_FRAGMENT_SHADER, + STROKE_VERTEX_SHADER, packColor, } from './shaders.js'; import {DefaultUniform} from '../../webgl/Helper.js'; @@ -43,9 +43,9 @@ import {listen, unlistenByKey} from '../../events.js'; /** * @typedef {Object} Options * @property {string} [className='ol-layer'] A CSS class name to set to the canvas element. - * @property {ShaderProgram} [polygonShader] Vertex shaders for polygons; using default shader if unspecified - * @property {ShaderProgram} [lineStringShader] Vertex shaders for line strings; using default shader if unspecified - * @property {ShaderProgram} [pointShader] Vertex shaders for points; using default shader if unspecified + * @property {ShaderProgram} [fill] Attributes and shaders for filling polygons. + * @property {ShaderProgram} [stroke] Attributes and shaders for line strings and polygon strokes. + * @property {ShaderProgram} [point] Attributes and shaders for points. * @property {Object} [uniforms] Uniform definitions. * @property {Array} [postProcesses] Post-processes definitions */ @@ -96,16 +96,17 @@ class WebGLVectorLayerRenderer extends WebGLLayerRenderer { */ this.currentTransform_ = projectionMatrixTransform; - const polygonAttributesWithDefault = { + const fillAttributes = { [DefaultAttributes.COLOR]: function () { return packColor('#ddd'); }, [DefaultAttributes.OPACITY]: function () { return 1; }, - ...(options.polygonShader && options.polygonShader.attributes), + ...(options.fill && options.fill.attributes), }; - const lineAttributesWithDefault = { + + const strokeAttributes = { [DefaultAttributes.COLOR]: function () { return packColor('#eee'); }, @@ -115,44 +116,41 @@ class WebGLVectorLayerRenderer extends WebGLLayerRenderer { [DefaultAttributes.WIDTH]: function () { return 1.5; }, - ...(options.lineStringShader && options.lineStringShader.attributes), + ...(options.stroke && options.stroke.attributes), }; - const pointAttributesWithDefault = { + + const pointAttributes = { [DefaultAttributes.COLOR]: function () { return packColor('#eee'); }, [DefaultAttributes.OPACITY]: function () { return 1; }, - ...(options.pointShader && options.pointShader.attributes), + ...(options.point && options.point.attributes), }; + function toAttributesArray(obj) { return Object.keys(obj).map((key) => ({name: key, callback: obj[key]})); } - this.polygonVertexShader_ = - (options.polygonShader && options.polygonShader.vertexShader) || - DEFAULT_POLYGON_VERTEX; - this.polygonFragmentShader_ = - (options.polygonShader && options.polygonShader.fragmentShader) || - DEFAULT_POLYGON_FRAGMENT; - this.polygonAttributes_ = toAttributesArray(polygonAttributesWithDefault); + this.fillVertexShader_ = + (options.fill && options.fill.vertexShader) || FILL_VERTEX_SHADER; + this.fillFragmentShader_ = + (options.fill && options.fill.fragmentShader) || FILL_FRAGMENT_SHADER; + this.fillAttributes_ = toAttributesArray(fillAttributes); - this.lineStringVertexShader_ = - (options.lineStringShader && options.lineStringShader.vertexShader) || - DEFAULT_LINESTRING_VERTEX; - this.lineStringFragmentShader_ = - (options.lineStringShader && options.lineStringShader.fragmentShader) || - DEFAULT_LINESTRING_FRAGMENT; - this.lineStringAttributes_ = toAttributesArray(lineAttributesWithDefault); + this.strokeVertexShader_ = + (options.stroke && options.stroke.vertexShader) || STROKE_VERTEX_SHADER; + this.strokeFragmentShader_ = + (options.stroke && options.stroke.fragmentShader) || + STROKE_FRAGMENT_SHADER; + this.strokeAttributes_ = toAttributesArray(strokeAttributes); this.pointVertexShader_ = - (options.pointShader && options.pointShader.vertexShader) || - DEFAULT_POINT_VERTEX; + (options.point && options.point.vertexShader) || POINT_VERTEX_SHADER; this.pointFragmentShader_ = - (options.pointShader && options.pointShader.fragmentShader) || - DEFAULT_POINT_FRAGMENT; - this.pointAttributes_ = toAttributesArray(pointAttributesWithDefault); + (options.point && options.point.fragmentShader) || POINT_FRAGMENT_SHADER; + this.pointAttributes_ = toAttributesArray(pointAttributes); /** * @private @@ -198,9 +196,9 @@ class WebGLVectorLayerRenderer extends WebGLLayerRenderer { this.polygonRenderer_ = new PolygonBatchRenderer( this.helper, this.worker_, - this.polygonVertexShader_, - this.polygonFragmentShader_, - this.polygonAttributes_ + this.fillVertexShader_, + this.fillFragmentShader_, + this.fillAttributes_ ); this.pointRenderer_ = new PointBatchRenderer( this.helper, @@ -212,9 +210,9 @@ class WebGLVectorLayerRenderer extends WebGLLayerRenderer { this.lineStringRenderer_ = new LineStringBatchRenderer( this.helper, this.worker_, - this.lineStringVertexShader_, - this.lineStringFragmentShader_, - this.lineStringAttributes_ + this.strokeVertexShader_, + this.strokeFragmentShader_, + this.strokeAttributes_ ); } diff --git a/src/ol/renderer/webgl/shaders.js b/src/ol/renderer/webgl/shaders.js index f8150cb379..4494cbc74f 100644 --- a/src/ol/renderer/webgl/shaders.js +++ b/src/ol/renderer/webgl/shaders.js @@ -38,7 +38,7 @@ const DECODE_COLOR_EXPRESSION = `vec3( * Relies on DefaultAttributes.COLOR and DefaultAttributes.OPACITY. * @type {string} */ -export const DEFAULT_POLYGON_VERTEX = ` +export const FILL_VERTEX_SHADER = ` precision mediump float; uniform mat4 u_projectionMatrix; attribute vec2 a_position; @@ -57,7 +57,7 @@ export const DEFAULT_POLYGON_VERTEX = ` * Default polygon fragment shader. * @type {string} */ -export const DEFAULT_POLYGON_FRAGMENT = ` +export const FILL_FRAGMENT_SHADER = ` precision mediump float; varying vec3 v_color; varying float v_opacity; @@ -71,7 +71,7 @@ export const DEFAULT_POLYGON_FRAGMENT = ` * Relies on DefaultAttributes.COLOR, DefaultAttributes.OPACITY and DefaultAttributes.WIDTH. * @type {string} */ -export const DEFAULT_LINESTRING_VERTEX = ` +export const STROKE_VERTEX_SHADER = ` precision mediump float; uniform mat4 u_projectionMatrix; uniform vec2 u_sizePx; @@ -136,7 +136,7 @@ export const DEFAULT_LINESTRING_VERTEX = ` * Default linestring fragment shader. * @type {string} */ -export const DEFAULT_LINESTRING_FRAGMENT = ` +export const STROKE_FRAGMENT_SHADER = ` precision mediump float; uniform float u_pixelRatio; varying vec2 v_segmentStart; @@ -166,7 +166,7 @@ export const DEFAULT_LINESTRING_FRAGMENT = ` * Relies on DefaultAttributes.COLOR and DefaultAttributes.OPACITY. * @type {string} */ -export const DEFAULT_POINT_VERTEX = ` +export const POINT_VERTEX_SHADER = ` precision mediump float; uniform mat4 u_projectionMatrix; uniform mat4 u_offsetScaleMatrix; @@ -196,7 +196,7 @@ export const DEFAULT_POINT_VERTEX = ` * Default point fragment shader. * @type {string} */ -export const DEFAULT_POINT_FRAGMENT = ` +export const POINT_FRAGMENT_SHADER = ` precision mediump float; varying vec3 v_color; varying float v_opacity;