From 2a2783c086fb38fa577d7e7a50fa1868e007c13c Mon Sep 17 00:00:00 2001 From: Olivier Guyot Date: Mon, 28 Oct 2019 15:17:08 +0100 Subject: [PATCH] ShaderBuilder / better handling of strings variables/attributes Now values which are not mentioned in the style are still added to the string literals mapping. Also an error will be thrown if a style references a missing variable. --- src/ol/webgl/ShaderBuilder.js | 14 +++++-- test/spec/ol/webgl/shaderbuilder.test.js | 50 ++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 4 deletions(-) diff --git a/src/ol/webgl/ShaderBuilder.js b/src/ol/webgl/ShaderBuilder.js index e3a141e187..bea9a05926 100644 --- a/src/ol/webgl/ShaderBuilder.js +++ b/src/ol/webgl/ShaderBuilder.js @@ -3,7 +3,7 @@ * @module ol/webgl/ShaderBuilder */ -import {expressionToGlsl, ValueTypes} from '../style/expressions.js'; +import {expressionToGlsl, stringToGlsl, ValueTypes} from '../style/expressions.js'; /** * @typedef {Object} VaryingDescription @@ -445,8 +445,14 @@ export function parseLiteralStyle(style) { fragContext.variables.forEach(function(varName) { builder.addUniform(`float u_${varName}`); uniforms[`u_${varName}`] = function() { - return style.variables && style.variables[varName] !== undefined ? - style.variables[varName] : 0; + if (!style.variables || style.variables[varName] === undefined) { + throw new Error(`The following variable is missing from the style: ${varName}`); + } + let value = style.variables[varName]; + if (typeof value === 'string') { + value = parseFloat(stringToGlsl(vertContext, value)); + } + return value !== undefined ? value : -9999999; // to avoid matching with the first string literal }; }); @@ -481,7 +487,7 @@ export function parseLiteralStyle(style) { callback: function(feature) { let value = feature.get(attributeName); if (typeof value === 'string') { - value = vertContext.stringLiteralsMap[value]; + value = parseFloat(stringToGlsl(vertContext, value)); } return value !== undefined ? value : -9999999; // to avoid matching with the first string literal } diff --git a/test/spec/ol/webgl/shaderbuilder.test.js b/test/spec/ol/webgl/shaderbuilder.test.js index f2b9145c96..4b08699ebb 100644 --- a/test/spec/ol/webgl/shaderbuilder.test.js +++ b/test/spec/ol/webgl/shaderbuilder.test.js @@ -402,6 +402,56 @@ void main(void) { expect(result.attributes).to.eql([]); expect(result.uniforms).to.have.property('u_ratio'); }); + + it('correctly adds string variables to the string literals mapping', function() { + const result = parseLiteralStyle({ + variables: { + mySize: 'abcdef' + }, + symbol: { + symbolType: 'square', + size: ['match', ['var', 'mySize'], 'abc', 10, 'def', 20, 30], + color: 'red' + } + }); + + expect(result.uniforms['u_mySize']()).to.be.greaterThan(0); + }); + + it('throws when a variable is requested but not present in the style', function(done) { + const result = parseLiteralStyle({ + variables: {}, + symbol: { + symbolType: 'square', + size: ['var', 'mySize'], + color: 'red' + } + }); + + try { + result.uniforms['u_mySize'](); + } catch (e) { + done(); + } + done(true); + }); + + it('throws when a variable is requested but the style does not have a variables dict', function(done) { + const result = parseLiteralStyle({ + symbol: { + symbolType: 'square', + size: ['var', 'mySize'], + color: 'red' + } + }); + + try { + result.uniforms['u_mySize'](); + } catch (e) { + done(); + } + done(true); + }); }); });