Skip to content

Distant object rendered incorrectly. #60

@KokeCacao

Description

@KokeCacao

Before Fix:
Image

After Fix:
Image

Why it Happens: Incorrect MipMap level blends alpha channel.

The Fix:

  protected createAtlasTexture(image: ImageData) {

    const texture = this.gl.createTexture()!
    this.gl.bindTexture(this.gl.TEXTURE_2D, texture)

    const ext = this.gl.getExtension('EXT_texture_filter_anisotropic')
    || this.gl.getExtension('MOZ_EXT_texture_filter_anisotropic')
    || this.gl.getExtension('WEBKIT_EXT_texture_filter_anisotropic');
    if (ext) {
      this.gl.texParameterf(
        this.gl.TEXTURE_2D,
        ext.TEXTURE_MAX_ANISOTROPY_EXT,
        4  // try 2, 4, 8, or 16 depending on performance/quality balance
      );
    }

    this.gl.texImage2D(this.gl.TEXTURE_2D, 0, this.gl.RGBA, this.gl.RGBA, this.gl.UNSIGNED_BYTE, image)

    // only sample up to mip level 4 because Minecraft block texture usually 16x16 pixels
    this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_BASE_LEVEL, 0);
    this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MAX_LEVEL, 4);

    // linear blend between the two closest mipmap levels, nearest texel in each.
    this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MIN_FILTER, this.gl.NEAREST_MIPMAP_LINEAR)
    this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MAG_FILTER, this.gl.NEAREST)

    this.gl.generateMipmap(this.gl.TEXTURE_2D)

    return texture
  }

I can draft a PR if you wish.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions