Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Textures loaded via UnityWebRequestTexture are sometimes rotated 90 degrees

Discussion in 'Scripting' started by nilsdr, Mar 12, 2021.

  1. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    374
    Hello,

    Our application downloads textures to the filesystem/persistent data (via DownloadHandlerFile) and then later displays them in a RawImage component by loading them from there like so:

    Code (CSharp):
    1. UnityWebRequest request = UnityWebRequestTexture.GetTexture("file://" + localfilePath));
    2.  
    3. request.SendWebRequest().completed += (AsyncOperation op) => {
    4.     if(!request.isFaulty())
    5.     {
    6.         Texture2D texture = DownloadHandlerTexture.GetContent(request);
    7.         Debug.Log(texture.width + " / " + texture.height);
    8.     }
    9. };
    This works just fine, except on some phones / in some cases the pictures are rotated after they're loaded. What I mean by that is that the width and height are inverted, and the UI displays the image as rotated 90 ccw or cw.

    Attached below is an image that does this:

    - It has a width of 1844 pixels
    - And a height of 4000 pixels
    - After loading, the created texture has a width of 4000 and a height of 1844

    What may be important to note is that he photo is taken with a smartphone camera. I thought, perhaps theres something in the EXIF giving it an alternate rotation (I have no idea :p), but I can't find anything strange in there.

    Is there something really obvious I'm missing?

     
  2. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    374
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    This is actually probably the case. To avoid decompressing and recompressing the image, certain platforms actually just store some extra rotational information in the image (not really sure where) in order to do the final rotation for display. The actual image data is not rotated. Google might be of help here, but I doubt anything in Unity will know specifically about a rotate marker in some random platform's file metadata. I'm not sure if that metadata would even be served to your program, unless it was embedded in the image itself.
     
  4. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    374
    Thanks! Perhaps I misunderstand, but if the actual image data is not rated (it doesn't appear to be, opening the file on my PC displays the image in the correct orientation) and the metadata is not exposed to Unity, then what is causing it to flip the texture?
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Think of it more as, "what is not causing it to unflip."

    I would speculate the image data is actually rotated but viewed on PC /Mac, the data gets un-rotated properly.
     
    nilsdr likes this.
  6. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    374
    You were correct, used this tool to display complete exif:

    http://exif.regex.info/

    And it had a rotation applied to it. Googled and found this:

    https://forum.unity.com/threads/tex...d-cant-be-fixed-because-of-sandboxing.216531/

    Which points to this:

    https://www.codeproject.com/Articles/47486/Understanding-and-Reading-Exif-Data

    Managed to apply it to parse the image EXIF, check the rotation, and then rotate the texture using this:

    https://answers.unity.com/questions/951835/rotate-the-contents-of-a-texture.html

    Hope it helps someone
     
  7. osheaprogramming

    osheaprogramming

    Joined:
    Oct 10, 2017
    Posts:
    1
    @nilsdr You are an absolute lifesaver! been struggling with this for a couple days now and thanks to you have resolved my issue as well.

    Cheers
     
    nilsdr likes this.
  8. minthiha

    minthiha

    Joined:
    Dec 4, 2013
    Posts:
    9
    @nilsdr Thank you so much, you save my life.
     
    nilsdr likes this.