Search Unity

Question RenderTexture Mip Map Bias not working in WebGL

Discussion in 'General Graphics' started by Foreman_Dev, Nov 13, 2022.

  1. Foreman_Dev

    Foreman_Dev

    Joined:
    Feb 4, 2018
    Posts:
    82
    Hi there!

    Setting RenderTexture.mipMapBias does not work at all in a WebGL build.

    In my scene I am trying to make a presentation screen look a bit sharper from a distance by setting the mipMapBias to -0.5. I see the desired result for this in the Unity editor, but there is no difference in a WebGL build. To verify, I set the mipMapBias to 2.0, which makes the texture look very blurry in the Unity editor, but in a WebGL build the texture is not affected at all.

    How can I enable support for mip map bias in a WebGL build? Do I need to use a custom shader of some kind? If so, where could I find an example of such a shader?
     
  2. Foreman_Dev

    Foreman_Dev

    Joined:
    Feb 4, 2018
    Posts:
    82
    Figured it out, with a little help from a friend of mine.

    Rather than using
    RenderTexture.mipMapBias
    I had to make a custom shader that samples the texture with mipmap bias via
    tex2Dbias
    . This should also work for other OpenGL ES based platforms such as Android (which could be useful particularly for standalone Android VR devices like the Meta Quest).

    Here's my shader. It's an Unlit shader, but the mipmap bias parts should be able to be incorporated into other types of shaders that need it:

    Code (csharp):
    1. Shader "Unlit/UnlitMipMapBias"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.         // Mipmap bias Unity ref: https://docs.unity3d.com/ScriptReference/Texture-mipMapBias.html
    7.         // ^Says we need to use a custom shader for WebGL (OpenGL ES), so here we are.
    8.         _MainTexBias("Mipmap Bias", Range(-1.0,1.0)) = -0.5
    9.     }
    10.     SubShader
    11.     {
    12.         Tags { "RenderType"="Opaque" }
    13.         LOD 100
    14.  
    15.         Pass
    16.         {
    17.             CGPROGRAM
    18.             #pragma vertex vert
    19.             #pragma fragment frag
    20.             // Commenting out fog because it isn't needed for this shader.
    21.             // #pragma multi_compile_fog
    22.  
    23.             #include "UnityCG.cginc"
    24.  
    25.             struct appdata
    26.             {
    27.                 float4 vertex : POSITION;
    28.                 float2 uv : TEXCOORD0;
    29.             };
    30.  
    31.             struct v2f
    32.             {
    33.                 float2 uv : TEXCOORD0;
    34.                 UNITY_FOG_COORDS(1)
    35.                 float4 vertex : SV_POSITION;
    36.             };
    37.  
    38.             sampler2D _MainTex;
    39.             float4 _MainTex_ST;
    40.             half _MainTexBias;
    41.  
    42.             v2f vert (appdata v)
    43.             {
    44.                 v2f o;
    45.                 o.vertex = UnityObjectToClipPos(v.vertex);
    46.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    47.                 UNITY_TRANSFER_FOG(o,o.vertex);
    48.                 return o;
    49.             }
    50.  
    51.             fixed4 frag (v2f i) : SV_Target
    52.             {
    53.                 // Sample the texture with mipmap bias via tex2Dbias. Ref: https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-tex2dbias
    54.                 fixed4 col = tex2Dbias(_MainTex, half4(i.uv.x, i.uv.y, 0.0, _MainTexBias));
    55.                 // Commenting out fog because it isn't needed for this shader.
    56.                 // UNITY_APPLY_FOG(i.fogCoord, col);
    57.                 return col;
    58.             }
    59.             ENDCG
    60.         }
    61.     }
    62. }