Expressions / introduce the match operator

This operator allows mapping from input values to output values of different
kinds and handles variable arguments count.
This commit is contained in:
Olivier Guyot
2019-10-24 16:52:58 +02:00
parent e3f7d29bb2
commit 27c530ec64
2 changed files with 125 additions and 0 deletions

View File

@@ -503,5 +503,33 @@ export const Operators = {
const end = expressionToGlsl(newContext, args[2], ValueTypes.COLOR);
return `mix(${start}, ${end}, ${expressionToGlsl(context, args[0])})`;
}
},
'match': {
getReturnType: function(args) {
let type = ValueTypes.ANY;
for (let i = 2; i < args.length; i += 2) {
type = type & getValueType(args[i]);
}
type = type & getValueType(args[args.length - 1]);
return type;
},
toGlsl: function(context, args, opt_typeHint) {
assertArgsEven(args);
// compute input/output types
const typeHint = opt_typeHint !== undefined ? opt_typeHint : ValueTypes.ANY;
const outputType = Operators['match'].getReturnType(args) & typeHint;
assertUniqueInferredType(args, outputType);
const input = expressionToGlsl(context, args[0]);
const fallback = expressionToGlsl(context, args[args.length - 1], outputType);
let result = null;
for (let i = args.length - 3; i >= 1; i -= 2) {
const match = expressionToGlsl(context, args[i]);
const output = expressionToGlsl(context, args[i + 1], outputType);
result = `(${input} == ${match} ? ${output} : ${result || fallback})`;
}
return result;
}
}
};