load static method

Texture load(
  1. GL2 gl,
  2. Loader loadingInfo,
  3. dynamic path, [
  4. int textureFlags = TextureFlags.defaultFlags,
])

Asynchronously loads a Texture from the specified path.

  • gl: The WebGL2 rendering context.
  • loadingInfo: A Loader instance to track loading progress.
  • path: The URL or path to the image file.
  • textureFlags: Optional TextureFlags to apply.

Returns a Texture instance. Use onLoad to register a callback for when loading completes.

Implementation

static Texture load(GL2 gl, Loader loadingInfo, path, [int textureFlags = TextureFlags.defaultFlags]) {
  final texture = create(gl: gl, textureFlags: textureFlags);
  texture.isLoading = true;

  loadImageData(path, loadingInfo)
      .then((img) {
        if (img != null && texture.texture != null) {
          texture.width = img.width;
          texture.height = img.height;
          gl.bindTexture(GL.TEXTURE_2D, texture.texture);
          final canvas =
              HTMLCanvasElement()
                ..width = texture.width
                ..height = texture.height;
          final ctx = canvas.getContext('2d') as CanvasRenderingContext2D;
          ctx.drawImage(img, 0, 0);
          final pixelData = ctx.getImageData(0, 0, texture.width, texture.height).data.toDart;
          gl.texImage2D(
            GL.TEXTURE_2D,
            0,
            GL.RGBA,
            texture.width.toJS,
            texture.height.toJS,
            0.toJS,
            GL.RGBA,
            GL.UNSIGNED_BYTE,
            pixelData.buffer.asUint8List().toJS,
          );
          texture.pixelData = pixelData.buffer.asUint8List();
          texture.isLoading = false;

          if (texture.flags.has(TextureFlags.mipmap)) {
            gl.generateMipmap(GL.TEXTURE_2D);
          }

          texture.triggerOnLoadCallback();
        }
      })
      .catchError((e) {
        error("catchError", e);
        texture.isLoading = false;
      });

  return texture;
}