src/textures.js
function createTexture(gl, options, callback) {
  callback = callback || noop;
  options = options || defaults.textureOptions;
  const tex = gl.createTexture();
  const target = options.target || gl.TEXTURE_2D;
  let width  = options.width  || 1;
  let height = options.height || 1;
  const internalFormat = options.internalFormat || gl.RGBA;
  const formatType = getFormatAndTypeForInternalFormat(internalFormat);
  let type = options.type || formatType.type;
  gl.bindTexture(target, tex);
  if (target === gl.TEXTURE_CUBE_MAP) {
    // this should have been the default for CUBEMAPS :(
    gl.texParameteri(target, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
    gl.texParameteri(target, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  }
  let src = options.src;
  if (src) {
    if (typeof src === "function") {
      src = src(gl, options);
    }
    if (typeof (src) === "string") {
      loadTextureFromUrl(gl, tex, options, callback);
    } else if (isArrayBuffer(src) ||
               (Array.isArray(src) && (
                    typeof src[0] === 'number' ||
                    Array.isArray(src[0]) ||
                    isArrayBuffer(src[0]))
               )
              ) {
      const dimensions = setTextureFromArray(gl, tex, src, options);
      width  = dimensions.width;
      height = dimensions.height;
      type   = dimensions.type;
    } else if (Array.isArray(src) && (typeof (src[0]) === 'string' || isTexImageSource(src[0]))) {
      if (target === gl.TEXTURE_CUBE_MAP) {
        loadCubemapFromUrls(gl, tex, options, callback);
      } else {
        loadSlicesFromUrls(gl, tex, options, callback);
      }
    } else if (isTexImageSource(src)) {
      setTextureFromElement(gl, tex, src, options);
      width  = src.width;
      height = src.height;
    } else {
      throw "unsupported src type";
    }
  } else {
    setEmptyTexture(gl, tex, options);
  }
  if (shouldAutomaticallySetTextureFilteringForSize(options)) {
    setTextureFilteringForSize(gl, tex, options, width, height, internalFormat, type);
  }
  setTextureParameters(gl, tex, options);
  return tex;
}
function createTexture(gl, options, callback) {
  callback = callback || noop;
  options = options || defaults.textureOptions;
  const tex = gl.createTexture();
  const target = options.target || gl.TEXTURE_2D;
  let width  = options.width  || 1;
  let height = options.height || 1;
  const internalFormat = options.internalFormat || gl.RGBA;
  const formatType = getFormatAndTypeForInternalFormat(internalFormat);
  let type = options.type || formatType.type;
  gl.bindTexture(target, tex);
  if (target === gl.TEXTURE_CUBE_MAP) {
    // this should have been the default for CUBEMAPS :(
    gl.texParameteri(target, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
    gl.texParameteri(target, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  }
  let src = options.src;
  if (src) {
    if (typeof src === "function") {
      src = src(gl, options);
    }
    if (typeof (src) === "string") {
      loadTextureFromUrl(gl, tex, options, callback);
    } else if (isArrayBuffer(src) ||
               (Array.isArray(src) && (
                    typeof src[0] === 'number' ||
                    Array.isArray(src[0]) ||
                    isArrayBuffer(src[0]))
               )
              ) {
      const dimensions = setTextureFromArray(gl, tex, src, options);
      width  = dimensions.width;
      height = dimensions.height;
      type   = dimensions.type;
    } else if (Array.isArray(src) && (typeof (src[0]) === 'string' || isTexImageSource(src[0]))) {
      if (target === gl.TEXTURE_CUBE_MAP) {
        loadCubemapFromUrls(gl, tex, options, callback);
      } else {
        loadSlicesFromUrls(gl, tex, options, callback);
      }
    } else if (isTexImageSource(src)) {
      setTextureFromElement(gl, tex, src, options);
      width  = src.width;
      height = src.height;
    } else {
      throw "unsupported src type";
    }
  } else {
    setEmptyTexture(gl, tex, options);
  }
  if (shouldAutomaticallySetTextureFilteringForSize(options)) {
    setTextureFilteringForSize(gl, tex, options, width, height, internalFormat, type);
  }
  setTextureParameters(gl, tex, options);
  return tex;
}
function createTexture ( gl , options , callback ) { callback = callback || noop ; options = options || defaults . textureOptions ; const tex = gl . createTexture ( ) ; const target = options . target || gl . TEXTURE_2D ; let width = options . width || 1 ; let height = options . height || 1 ; const internalFormat = options . internalFormat || gl . RGBA ; const formatType = getFormatAndTypeForInternalFormat ( internalFormat ) ; let type = options . type || formatType . type ; gl . bindTexture ( target , tex ) ; if ( target === gl . TEXTURE_CUBE_MAP ) { // this should have been the default for CUBEMAPS :( gl . texParameteri ( target , gl . TEXTURE_WRAP_S , gl . CLAMP_TO_EDGE ) ; gl . texParameteri ( target , gl . TEXTURE_WRAP_T , gl . CLAMP_TO_EDGE ) ; } let src = options . src ; if ( src ) { if ( typeof src === "function" ) { src = src ( gl , options ) ; } if ( typeof ( src ) === "string" ) { loadTextureFromUrl ( gl , tex , options , callback ) ; } else if ( isArrayBuffer ( src ) || ( Array . isArray ( src ) && ( typeof src [ 0 ] === 'number' || Array . isArray ( src [ 0 ] ) || isArrayBuffer ( src [ 0 ] ) ) ) ) { const dimensions = setTextureFromArray ( gl , tex , src , options ) ; width = dimensions . width ; height = dimensions . height ; type = dimensions . type ; } else if ( Array . isArray ( src ) && ( typeof ( src [ 0 ] ) === 'string' || isTexImageSource ( src [ 0 ] ) ) ) { if ( target === gl . TEXTURE_CUBE_MAP ) { loadCubemapFromUrls ( gl , tex , options , callback ) ; } else { loadSlicesFromUrls ( gl , tex , options , callback ) ; } } else if ( isTexImageSource ( src ) ) { setTextureFromElement ( gl , tex , src , options ) ; width = src . width ; height = src . height ; } else { throw "unsupported src type" ; } } else { setEmptyTexture ( gl , tex , options ) ; } if ( shouldAutomaticallySetTextureFilteringForSize ( options ) ) { setTextureFilteringForSize ( gl , tex , options , width , height , internalFormat , type ) ; } setTextureParameters ( gl , tex , options ) ; return tex ; }
Creates a texture based on the options passed in.
@param {WebGLRenderingContext} gl the WebGLRenderingContext
@param {module:twgl.TextureOptions} [options] A TextureOptions object with whatever parameters you want set.
@param {module:twgl.TextureReadyCallback} [callback] A callback called when an image has been downloaded and uploaded to the texture.
@return {WebGLTexture} the created texture.
@memberOf module:twgl/textures
Creates a texture based on the options passed in .