create static method

Texture create({
  1. required GL2 gl,
  2. Uint8List? pixelData,
  3. int width = 1,
  4. int height = 1,
  5. int textureFlags = TextureFlags.defaultFlags,
})

Creates a Texture from raw pixel data or as an empty texture.

  • gl: The WebGL2 rendering context.
  • pixelData: Optional Uint8List of pixel data (RGBA). If null, a 1x1 transparent black texture is created.
  • width: Width of the texture. Defaults to 1 if pixelData is null.
  • height: Height of the texture. Defaults to 1 if pixelData is null.
  • textureFlags: Bitmask of TextureFlags to apply.

Implementation

static Texture create({
  required GL2 gl,
  Uint8List? pixelData,
  int width = 1,
  int height = 1,
  int textureFlags = TextureFlags.defaultFlags,
}) {
  var flags = textureFlags;
  final tex = gl.createTexture();
  if (tex == null) {
    throw Exception("Could not create texture!");
  }

  gl.bindTexture(GL.TEXTURE_2D, tex);
  if (pixelData == null) {
    pixelData = _emptyTextureData;
    width = 1;
    height = 1;
  }
  gl.texImage2D(
    GL.TEXTURE_2D,
    0,
    GL.RGBA,
    width.toJS,
    height.toJS,
    0.toJS,
    GL.RGBA,
    GL.UNSIGNED_BYTE,
    pixelData.toJS,
  );

  if (flags.has(TextureFlags.filter)) {
    gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MAG_FILTER, GL.LINEAR);
  } else {
    gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MAG_FILTER, GL.NEAREST);
  }

  if (flags.hasAll(TextureFlags.mipmap | TextureFlags.filter)) {
    gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MIN_FILTER, GL.LINEAR_MIPMAP_LINEAR);
  } else if (flags.has(TextureFlags.mipmap)) {
    gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MIN_FILTER, GL.NEAREST_MIPMAP_NEAREST);
  } else if (flags.has(TextureFlags.filter)) {
    gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MIN_FILTER, GL.LINEAR);
  } else {
    gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MIN_FILTER, GL.NEAREST);
  }

  if (flags.has(TextureFlags.clampS)) {
    gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_WRAP_S, GL.CLAMP_TO_EDGE);
  } else if (flags.has(TextureFlags.repeatS)) {
    gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_WRAP_S, GL.REPEAT);
  } else if (flags.has(TextureFlags.mirroredRepeatS)) {
    gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_WRAP_S, GL.MIRRORED_REPEAT);
  } else {
    gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_WRAP_S, GL.CLAMP_TO_EDGE);
  }

  if (flags.has(TextureFlags.clampT)) {
    gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_WRAP_T, GL.CLAMP_TO_EDGE);
  } else if (flags.has(TextureFlags.repeatT)) {
    gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_WRAP_T, GL.REPEAT);
  } else if (flags.has(TextureFlags.mirroredRepeatT)) {
    gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_WRAP_T, GL.MIRRORED_REPEAT);
  } else {
    gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_WRAP_T, GL.CLAMP_TO_EDGE);
  }

  final texture = Texture(
    gl: gl,
    texture: tex,
    width: width,
    height: height,
    flags: textureFlags,
    pixelData: pixelData,
  );

  texture.isLoading = false;

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

  return texture;
}