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.
This commit is contained in:
Olivier Guyot
2019-10-28 15:17:08 +01:00
parent 2f49876180
commit 2a2783c086
2 changed files with 60 additions and 4 deletions

View File

@@ -3,7 +3,7 @@
* @module ol/webgl/ShaderBuilder * @module ol/webgl/ShaderBuilder
*/ */
import {expressionToGlsl, ValueTypes} from '../style/expressions.js'; import {expressionToGlsl, stringToGlsl, ValueTypes} from '../style/expressions.js';
/** /**
* @typedef {Object} VaryingDescription * @typedef {Object} VaryingDescription
@@ -445,8 +445,14 @@ export function parseLiteralStyle(style) {
fragContext.variables.forEach(function(varName) { fragContext.variables.forEach(function(varName) {
builder.addUniform(`float u_${varName}`); builder.addUniform(`float u_${varName}`);
uniforms[`u_${varName}`] = function() { uniforms[`u_${varName}`] = function() {
return style.variables && style.variables[varName] !== undefined ? if (!style.variables || style.variables[varName] === undefined) {
style.variables[varName] : 0; 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) { callback: function(feature) {
let value = feature.get(attributeName); let value = feature.get(attributeName);
if (typeof value === 'string') { 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 return value !== undefined ? value : -9999999; // to avoid matching with the first string literal
} }

View File

@@ -402,6 +402,56 @@ void main(void) {
expect(result.attributes).to.eql([]); expect(result.attributes).to.eql([]);
expect(result.uniforms).to.have.property('u_ratio'); 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);
});
}); });
}); });