Search Unity

Manual mipmapping in the shader

Discussion in 'Shaders' started by HonoraryBob, Apr 5, 2012.

  1. HonoraryBob

    HonoraryBob

    Joined:
    May 26, 2011
    Posts:
    1,214
    One of my shaders needs to draw textures from a custom texture atlas, hence I need to handle mipmapping "manually". Does anyone know of a good way to do this?
     
  2. Martin-Kraus

    Martin-Kraus

    Joined:
    Feb 18, 2011
    Posts:
    617
  3. HonoraryBob

    HonoraryBob

    Joined:
    May 26, 2011
    Posts:
    1,214
  4. Martin-Kraus

    Martin-Kraus

    Joined:
    Feb 18, 2011
    Posts:
    617
    You lost me. What do you mean by "handling mipmapping manually" if not "choosing the mipmap level explicitly"?
     
  5. HonoraryBob

    HonoraryBob

    Joined:
    May 26, 2011
    Posts:
    1,214
    I'm making my own mipmaps for my own texture atlas, which the shader reads by calculating the correct UV values, I need a way to determine which mipmap level should be used based on the distance to the camera. I've seen one method which uses ddx/ddy, but I don't know if Unity supports that.
     
  6. Martin-Kraus

    Martin-Kraus

    Joined:
    Feb 18, 2011
    Posts:
    617
  7. HonoraryBob

    HonoraryBob

    Joined:
    May 26, 2011
    Posts:
    1,214
    What sort of compatibility problems might there be with using ddx/ddy through Unity?
     
  8. Martin-Kraus

    Martin-Kraus

    Joined:
    Feb 18, 2011
    Posts:
    617
    The graphics hardware or driver might not support it.
     
  9. HonoraryBob

    HonoraryBob

    Joined:
    May 26, 2011
    Posts:
    1,214
    Is there another way to determine the distance to the camera within a fragment shader (or rather a combination of vertex and fragment shaders of course)?
     
  10. Martin-Kraus

    Martin-Kraus

    Joined:
    Feb 18, 2011
    Posts:
    617
    What is the first way? Determining this distance from ddx/ddy is not particularly easy.
    Usually you would use something like this:
    In the vertex position, you transform the vertex position to world coordinates and hand it to the fragment shader (or use the worldPos in the input structure of a SurfaceShader) and then compute the distance to the camera position: _WorldSpaceCameraPos ( http://unity3d.com/support/documentation/Components/SL-BuiltinValues.html )
     
  11. HonoraryBob

    HonoraryBob

    Joined:
    May 26, 2011
    Posts:
    1,214
    Thank you, that's what I was looking for.
     
  12. gjoel

    gjoel

    Joined:
    Apr 21, 2009
    Posts:
    9
    Just for future reference, another way of clamping mip-levels is to use texturesampling with explicit gradients, while clamping ddx/ddy. The function is called textureGrad in GLSL, and is an overload of tex2D in Cg.

    Code (csharp):
    1. float2 uv_dx = clamp( ddx( uv ), minval, maxval );
    2. float2 uv_dy = clamp( ddy( uv ), minval, maxval );
    3. half4 texsample = tex2D( _MainTex, uv0, uv_dx, uv_dy );
     
    Peter-Bailey, Tygman and ozonex like this.
  13. alex_is_my_name

    alex_is_my_name

    Joined:
    Sep 4, 2013
    Posts:
    2
    thanks! :D
     
  14. McDev02

    McDev02

    Joined:
    Nov 22, 2010
    Posts:
    664
    Do you really need a "custom" mipmap? A texture atlas is a common texture and works with mipmapping quite well, you only could face problems at the edges with low resolutions, but you could limit that, too. What do you want to achieve?