Search Unity

Problem generating mipmaps on externally loaded textures

Discussion in 'Editor & General Support' started by highlyinteractive, Mar 9, 2015.

  1. highlyinteractive

    highlyinteractive

    Joined:
    Sep 6, 2012
    Posts:
    116
    I'm having an odd problem with some external files. The files themselves seem to be the cause, but I don't know why.

    Here's a code snippet:
    Code (CSharp):
    1. //Load file
    2. string uri = "file://" + Application.dataPath + "/Assets/" + imgName + ".jpg";
    3.  
    4. Texture2D tex = new Texture2D(256, 256, TextureFormat.DXT1, true);
    5.  
    6. WWW www = new WWW(uri);
    7. yield return www;
    8.  
    9. www.LoadImageIntoTexture(tex);
    10. www.Dispose();
    11. www = null;
    12.  
    13. Sprite spr = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), Vector2.zero);
    14. Image img = gameObject.GetComponent<Image>();
    15.  
    16. img.sprite = spr;
    17.  
    This code works fine some of the time, but some files are loaded with blank, black mipmaps. I've done a few tests now & it's definitely an issue with the file, but I'd really like to understand why.

    I've uploaded two sample images. Both were taken with the same camera. Both have the same compression settings. For some reason, Unity takes issue with a.jpg & fails to create mipmaps. I've also created a test project with a few different loading methods.

    Test jpegs: https://www.dropbox.com/s/gjmjknqaxa727mo/mipmap_images.zip?dl=0
    Full project: https://www.dropbox.com/s/rakfcvlxybzb8da/MipMapTest.zip?dl=0

    If anyone can help me get to the bottom of this issue, I'd really appreciate it. Thanks!

    Edit: This is in Unity 4.6.3
     
    Last edited: Mar 9, 2015
  2. Carpe-Denius

    Carpe-Denius

    Joined:
    May 17, 2013
    Posts:
    842
  3. highlyinteractive

    highlyinteractive

    Joined:
    Sep 6, 2012
    Posts:
    116
    Thanks, I was actually just looking at exactly that. Unfortunately it didn't work because I'm compressing the texture. It turns out it's the compression that causes the problem.

    Works fine:
    Code (CSharp):
    1. tex = new Texture2D(256, 256, TextureFormat.RGB, true);
    Doesn't work:
    Code (CSharp):
    1. tex = new Texture2D(256, 256, TextureFormat.DXT1, true);
    Doesn't work:
    Code (CSharp):
    1. tex = new Texture2D(256, 256, TextureFormat.DXT1, true);
    2. tex.SetPixels( tex.GetPixels(0,0,tex.width,tex.height) );
    3. tex.Apply();
    4.  
    Doesn't work:
    Code (CSharp):
    1. tex = new Texture2D(256, 256, TextureFormat.RGB, true);
    2. tex.Compress();
    3.  
    It's really annoying as I need to compress the textures.

    I've no idea why the error only occurs on certain files.
     
  4. highlyinteractive

    highlyinteractive

    Joined:
    Sep 6, 2012
    Posts:
    116
    I just tried creating some brand new jpeg's in Photoshop. Same problem.

    Also getting the same issue with PNGs (although mipmaps are transparent instead of black).

    Starting to think this might be a Unity bug...
     
  5. highlyinteractive

    highlyinteractive

    Joined:
    Sep 6, 2012
    Posts:
    116
    Tested in Unity 5 - bug still exists.

    Even worse, the images that were working in U4 are throwing this error:
    GetPixels32 failed: insufficent pixel buffer size (2722560), must be at least 1420 x 1920
    UnityEngine.Sprite:Create(Texture2D, Rect, Vector2)
    <CreateImageFromExternalTexture>c__Iterator0:MoveNext() (at Assets/LoadAssets.cs:49)

    Submitted a bug report to Unity
     
  6. primitiveType

    primitiveType

    Joined:
    Oct 25, 2012
    Posts:
    22
    @emsee , I know this is a necro, but have you tried changing the project to use either DX9 or openGL? changing the rendering engine made a difference for me.