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

Set desired mip map count when using `Texture2D.LoadRawTextureData`

Discussion in 'General Graphics' started by NanoBob, Oct 15, 2019.

  1. NanoBob

    NanoBob

    Joined:
    Oct 15, 2019
    Posts:
    3
    In my project I'm loading a texture using `Texture2D.LoadRawTextureData` The texture I'm loading has a mipmap chain where its smallest texture is a 4x4 texture (with a total of 9 mipmaps)


    Code (CSharp):
    1.  
    2. Texture2D unityTexture = new Texture2D(texture.Data.Width, texture.Data.Height, format, texture.Data.MipMapCount > 1)
    3. {
    4.     name = texture.Data.TextureName,
    5. };
    6. unityTexture.LoadRawTextureData(DdsHelper.StripDdsHeader(texture.GetDds(true)));
    7. unityTexture.Apply(true);
    8.  
    9. UnityEngine.Material material = new UnityEngine.Material(Shader.Find("Standard"));
    10. material.SetTexture("_MainTex", unityTexture);
    11. material.name = texture.Data.TextureName;
    12.  
    The issue is that `unityTexture.Apply();` throws an exception regarding an over-read. Since the file size is not sufficient for the entire mipmap chain. This exception is not thrown if the texture has mipmaps going all the way down to a 1x1 texture (11 mipmaps in this case, instead of 9).

    The exception is as follows:
    UnityException: LoadRawTextureData: not enough data provided (will result in overread).

    Is it possible to specify the amount of mipmaps to be loaded by `LoadRawTextureData` and `Apply()`?
     
  2. Shane_Michael

    Shane_Michael

    Joined:
    Jul 8, 2013
    Posts:
    158
    Texture2D in 2019.2 got a couple new constructors that take in a mipCount parameter. If you create your texture with a matching mipCount, I would expect it to load correctly though I have not had a reason to try it yet. It isn't in the documentation yet, but Intellisense should pop-up all the constructor signatures for you.
     
  3. NanoBob

    NanoBob

    Joined:
    Oct 15, 2019
    Posts:
    3
    Great, I'll update my unity for the project and try it out, thanks!
    (Will post an update with results)
     
  4. NanoBob

    NanoBob

    Joined:
    Oct 15, 2019
    Posts:
    3
    I've tried it out and it works like a charm, thanks!

    Code (CSharp):
    1. Texture2D unityTexture = new Texture2D(texture.Data.Width, texture.Data.Height, format, mipMapCount, false);