src/programs.js
function loadShader(gl, shaderSource, shaderType, opt_errorCallback) {
  const errFn = opt_errorCallback || error;
  // Create the shader object
  const shader = gl.createShader(shaderType);

  // Remove the first end of line because WebGL 2.0 requires
  // #version 300 es
  // as the first line. No whitespace allowed before that line
  // so
  //
  // <script>
  // #version 300 es
  // </script>
  //
  // Has one line before it which is invalid according to GLSL ES 3.00
  //
  let lineOffset = 0;
  if (spaceRE.test(shaderSource)) {
    lineOffset = 1;
    shaderSource = shaderSource.replace(spaceRE, '');
  }

  // Load the shader source
  gl.shaderSource(shader, shaderSource);

  // Compile the shader
  gl.compileShader(shader);

  // Check the compile status
  const compiled = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
  if (!compiled) {
    // Something went wrong during compilation; get the error
    const lastError = gl.getShaderInfoLog(shader);
    errFn(addLineNumbers(shaderSource, lineOffset) + "\n*** Error compiling shader: " + lastError);
    gl.deleteShader(shader);
    return null;
  }

  return shader;
}
function loadShader(gl, shaderSource, shaderType, opt_errorCallback) {
  const errFn = opt_errorCallback || error;
  // Create the shader object
  const shader = gl.createShader(shaderType);

  // Remove the first end of line because WebGL 2.0 requires
  // #version 300 es
  // as the first line. No whitespace allowed before that line
  // so
  //
  // <script>
  // #version 300 es
  // </script>
  //
  // Has one line before it which is invalid according to GLSL ES 3.00
  //
  let lineOffset = 0;
  if (spaceRE.test(shaderSource)) {
    lineOffset = 1;
    shaderSource = shaderSource.replace(spaceRE, '');
  }

  // Load the shader source
  gl.shaderSource(shader, shaderSource);

  // Compile the shader
  gl.compileShader(shader);

  // Check the compile status
  const compiled = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
  if (!compiled) {
    // Something went wrong during compilation; get the error
    const lastError = gl.getShaderInfoLog(shader);
    errFn(addLineNumbers(shaderSource, lineOffset) + "\n*** Error compiling shader: " + lastError);
    gl.deleteShader(shader);
    return null;
  }

  return shader;
}
function loadShader ( gl , shaderSource , shaderType , opt_errorCallback ) { const errFn = opt_errorCallback || error ; // Create the shader object const shader = gl . createShader ( shaderType ) ; // Remove the first end of line because WebGL 2.0 requires // #version 300 es // as the first line. No whitespace allowed before that line // so // // <script> // #version 300 es // </script> // // Has one line before it which is invalid according to GLSL ES 3.00 // let lineOffset = 0 ; if ( spaceRE . test ( shaderSource ) ) { lineOffset = 1 ; shaderSource = shaderSource . replace ( spaceRE , '' ) ; } // Load the shader source gl . shaderSource ( shader , shaderSource ) ; // Compile the shader gl . compileShader ( shader ) ; // Check the compile status const compiled = gl . getShaderParameter ( shader , gl . COMPILE_STATUS ) ; if ( ! compiled ) { // Something went wrong during compilation; get the error const lastError = gl . getShaderInfoLog ( shader ) ; errFn ( addLineNumbers ( shaderSource , lineOffset ) + "\n*** Error compiling shader: " + lastError ) ; gl . deleteShader ( shader ) ; return null ; } return shader ; }
Loads a shader.
@param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
@param {string} shaderSource The shader source.
@param {number} shaderType The type of shader.
@param {module:twgl.ErrorCallback} opt_errorCallback callback for errors.
@return {WebGLShader} The created shader.
@private
Loads a shader .