sdf-2d/src/graphics/graphics-library/compiling/create-shader.ts
schmelczerandras 77bde04db3 Add files
2020-09-15 10:08:16 +02:00

22 lines
527 B
TypeScript

export const createShader = (
gl: WebGL2RenderingContext,
type: GLenum,
source: string,
substitutions: { [name: string]: string }
): WebGLShader => {
source = source.replace(/{(.+)}/gm, (_, name: string): string => {
const value = substitutions[name];
return Number.isInteger(value) ? `${value}.0` : value;
});
const shader = gl.createShader(type);
if (!shader) {
throw new Error('Could not create shader');
}
gl.shaderSource(shader, source);
gl.compileShader(shader);
return shader;
};