Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

how to get mipmap level in application playing

Discussion in 'General Graphics' started by jianyingmengcan, May 24, 2019.

  1. jianyingmengcan

    jianyingmengcan

    Joined:
    Feb 17, 2017
    Posts:
    3
    in unity 2017.4,i cannot find this api,have any idea to get it?thanks
     
  2. lyndon_unity

    lyndon_unity

    Unity Technologies

    Joined:
    Nov 2, 2017
    Posts:
    64
    Do you mean the mip level uploaded to the GPU by the texture mip streaming system?
    https://docs.unity3d.com/ScriptReference/Texture2D-loadedMipmapLevel.html
    This is a new API related to texture mip map streaming and was only added in 2018.2
    https://docs.unity3d.com/Manual/TextureStreaming.html
    Prior to this all the mipmap levels would be loaded to the GPU.

    The number of mip levels in the original texture can be retrieved with:
    https://docs.unity3d.com/ScriptReference/Texture2D-mipmapCount.html

    If you want to get the mip level that the GPU has selected based on the position on screen, you would have to do that per pixel in the shader. This post might be helpful.
    https://forum.unity.com/threads/calculate-used-mipmap-level-ddx-ddy.255237/
    If you want to query the number of mip levels a GPU texture has in the shader, see GetMipCount in the code here:
    https://docs.unity3d.com/Manual/TextureStreaming-API.html
     
    Omaek likes this.
  3. jianyingmengcan

    jianyingmengcan

    Joined:
    Feb 17, 2017
    Posts:
    3
    hi,my unity version is 2017.4,cannot use this api.i read those url,try use maintex_texelsize and ddx.but it is unuseful.i want to get the texture current mimap level,thanks
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    You're going to have to describe where and how you intend to use the mip level. The options @lyndon_unity listed are kind of the only options available.

    If you want the largest available mipmap level of a texture in C# for Unity 2017, if the texture is loaded then it's always effectively 0, or perhaps more specifically the mip level set in the quality settings, since 2017 doesn't support mip streaming.

    If you want the current per pixel mipmap level in a shader you have to use the techniques mentioned in the thread linked above.

    If what you want is to get the mipmap level for an object on screen in c#, that's not easily possible. It's not information the CPU knows, nor is it a constant value across the entire mesh as it's determined on the GPU on each pixel the object renders independent* of the rest of the mesh. You'd have to write out the mip levels to a render texture, then iterate over all the pixels with a compute shader or copy the contents back to the CPU to iterate over in script.
    * Actually it's determined for each 2x2 quad of pixels.