question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Enable Anisotropic filtering for textures where available

See original GitHub issue

Using standard bilinear filtering for sprite textures in the manner that PIXI currently does, often leads to slightly blurry sprites when a sprite is scaled to a size that is inbetween two mipmaps.

Since part of our app is a WebGL powered photo editor, using PIXI, this is not ideal. We have previously worked around this issue by modifying PIXI’s sprite shader generator to set a negative mipmap LOD bias:

vec4 sample = texture2D(uSampler, vTextureCoord, -0.5);

This ensures that the sprite is always using a slightly higher resolution mipmap.

While this works fine, we have recently realised that WebGL now has good support for anisotropic filtering, which enables us to remove the negative LOD bias and always get perfectly sharp sprites. Furthermore, performance is significantly better than our previous method since on modern GPU’s, anisotropic filtering is pretty much free, or if not, the performance impact is absolutely miniscule.

Support for anisotropic filtering can be checked like so:

 var ext = (
                gl.getExtension('EXT_texture_filter_anisotropic') ||
                gl.getExtension('MOZ_EXT_texture_filter_anisotropic') ||
                gl.getExtension('WEBKIT_EXT_texture_filter_anisotropic')
            );

And can then be set on a per texture basis in the same manner as PIXI currently sets its current linear/nearest filtering in the Texture.minFilter function:

if(ext){
     var max = gl.getParameter(ext.MAX_TEXTURE_MAX_ANISOTROPY_EXT);
     var anisotropy_level = Math.min(this.anisotropic, max);
     gl.texParameterf(gl.TEXTURE_2D, ext.TEXTURE_MAX_ANISOTROPY_EXT, anisotropy_level );
}

Please note than in this example we have simply added an anisotropic property to the Texture object, which accepts values from 0-16. We then check to see the maximum supported anisotropy level, and apply the chosen level or the highest supported value.

Also, in our hacked function, we only apply this technique if linear filtering is selected for a texture, and if mipmapping is enabled and allowed for said texture (since it wouldn’t make any difference without mipmaps anyway).

It would be great to see this added into PIXI as the improvement in scaled sprites is hugely significant.

More info can be found here: https://developer.mozilla.org/en-US/docs/Web/API/EXT_texture_filter_anisotropic

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
teoxoycommented, Mar 7, 2019

I’ve just realized that this issue has been opened 2 years ago. I will go ahead and start a PR for this.

0reactions
vvashishtcommented, Jul 5, 2019

Cool. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Glossary:Anisotropic filtering (AF)
Anisotropic filtering is a type of texture filtering which increases the visual quality of textures at steep angles to the camera, ...
Read more >
Enable Anisotropic filtering for textures where available
This ensures that the sprite is always using a slightly higher resolution mipmap. While this works fine, we have recently realised that WebGL ......
Read more >
Advanced VR Graphics Techniques
To set the texture filtering settings for a texture in Unit: Go to Edit. Choose Project Settings.
Read more >
EXT_texture_filter_anisotropic - Web APIs | MDN
The EXT_texture_filter_anisotropic extension is part of the WebGL API and exposes two constants for anisotropic filtering (AF).
Read more >
tex2Dlod and anisotropic filtering
However, anisotropic filtering also enables trilinear filtering as it is an extension of the later. It's possible that when you disable ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found