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:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user