| 123456789101112131415161718192021222324252627282930313233343536 |
- /**
- * Represents a WebGL shader object and provides a mechanism to load shaders from HTML
- * script tags.
- */
- export default class ShaderCompiler {
- /**
- * @param {WebGLRenderingContext}gl
- * @param {{type: string, source: string}}script
- * @return {WebGLShader}
- */
- static compile (gl, script) {
- let shader
- // Now figure out what type of shader script we have, based on its MIME type.
- if (script.type === 'x-shader/x-fragment') {
- shader = gl.createShader(gl.FRAGMENT_SHADER)
- } else if (script.type === 'x-shader/x-vertex') {
- shader = gl.createShader(gl.VERTEX_SHADER)
- } else {
- throw new Error('Unknown shader type: ' + script.type)
- }
- // Send the source to the shader object.
- gl.shaderSource(shader, script.source)
- // Compile the shader program.
- gl.compileShader(shader)
- // See if it compiled successfully.
- if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
- throw new Error('An error occurred compiling the shaders: ' + gl.getShaderInfoLog(shader))
- }
- return shader
- }
- }
|