How can I fix aliasing or enable mipmaps on Android?

Symptoms

  • Aliasing artifacts appear when the camera is moved farther from a webview.
  • Text becomes blurry or hard to read when the camera is moved farther from a webview.

Cause

Mipmaps aren't enabled for the OES textures used by 3D WebView's Android plugins.

Resolution

In order to achieve efficient rendering, 3D WebView's Android plugins use the GL_OES_EGL_image_external OpenGL extension. Normally, aliasing for a texture can be reduced by enabling mipmaps, however the specification for GL_OES_EGL_image_external declares that the extension doesn't support mipmaps:

Calling GenerateMipmaps with set to TEXTURE_EXTERNAL_OES results in an INVALID_ENUM.

However, there are still a couple of options for reducing aliasing:

  1. If your application uses XR, you can reduce aliasing by increasing XRSettings.eyeTextureResolutionScale, like this:
XRSettings.eyeTextureResolutionScale = 2;
  1. You can blit the contents of the webview's texture to a RenderTexture that has mipmaps enabled, as demonstrated in this example:
using UnityEngine;
using Vuplex.WebView;

class MipmappedTextureUpdater : MonoBehaviour {

    WebViewPrefab _webViewPrefab;
    RenderTexture _renderTexture;
    Material _webMaterial;

    void Start() {

        // Get a reference to the prefab in the scene.
        _webViewPrefab = GameObject.Find("WebViewPrefab").GetComponent<WebViewPrefab>();
        _webViewPrefab.Initialized += async (sender, e) => {
            // Create a RenderTexture with mipmaps enabled.
            var textureSize = _webViewPrefab.WebView.SizeInPixels;
            _renderTexture = new RenderTexture((int)textureSize.x, (int)textureSize.y, 0, RenderTextureFormat.Default, RenderTextureReadWrite.Default);
            _renderTexture.filterMode = FilterMode.Trilinear;
            _renderTexture.anisoLevel = 8;
            _renderTexture.useMipMap = true;
            _renderTexture.wrapMode = TextureWrapMode.Repeat;
            // Set the RenderTexture with mimaps enabled as the prefab's material.
            _webViewPrefab.Material = new Material(Shader.Find("Standard"));
            _webViewPrefab.Material.mainTexture = _renderTexture;
            // Create an instance of the web material for blitting.
            _webMaterial = await Web.CreateMaterial();
        };
    }

    void Update() {

        // Blit the contents of the webview's texture to the RenderTexture
        // that has mipmaps enabled.
        if (_webMaterial != null) {
            Graphics.Blit(
                _webViewPrefab.WebView.Texture,
                _renderTexture,
                _webMaterial
            );
        }
    }
}