@woosh/meep-engine
Version:
Pure JavaScript game engine. Fully featured and production ready.
40 lines (33 loc) • 1.05 kB
JavaScript
/**
*
* @param {string} input name of the variable on which components are being accessed
* @param {(string|number)[]} swizzle
* @returns {string} swizzled vector
*/
export function glsl_gen_swizzled_read(input, swizzle) {
const swizzle_read = [];
const n = swizzle.length;
for (let i = 0; i < n; i++) {
const token = swizzle[i];
if (typeof token === "string") {
swizzle_read[i] = `${input}.${token}`;
} else if (typeof token === "number") {
swizzle_read[i] = Number(token).toFixed(4);
} else {
throw new Error(`Unsupported swizzle token '${token}'`);
}
}
const arg_string = swizzle_read.join(', ');
const ctors = [
undefined,
'float',
'vec2',
'vec3',
'vec4'
];
const ctor = ctors[swizzle.length];
if (ctor === undefined) {
throw new Error(`No constructor for input length ${swizzle.length}`);
}
return `${ctor}(${arg_string})`;
}