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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Loading Crunched DXT at runtime

Discussion in 'Scripting' started by r3dwolf, Sep 28, 2017.

  1. r3dwolf

    r3dwolf

    Joined:
    Feb 16, 2012
    Posts:
    39
    Hello everyone,
    i need to load a DXT crunch texture downloaded with WWW (no bundle).
    Is there a way to load it?

    I tried with LoadRawTextureData but without success.
    Code (CSharp):
    1. Texture2D texture = new Texture2D(4096, 2048, TextureFormat.DXT1Crunched, true);
    2. texture.LoadRawTextureData(www.bytes);
     
    Last edited: Sep 28, 2017
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,077
    Are you getting any errors or is it just not working?

    You also need to call texture.Apply(); after you've loaded the texture data.
     
  3. r3dwolf

    r3dwolf

    Joined:
    Feb 16, 2012
    Posts:
    39
    Damn! you're right, I forgot it. :mad:
    thank you @tonemcbride
    Is there a way to load it without setting the width height and DXT(DXT1 or DXT5) ?
    This information is contained within the file; for DDS (not crunched) i can read the header bytes and get this informations in a fairly simple way but for crn...
     
  4. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,077
    I think crunched files just have the raw data without headers but I'm not certain, someone else here might know? You could always add a width/height to the file that you're downloading and read that in before creating the texture 2d?
     
  5. r3dwolf

    r3dwolf

    Joined:
    Feb 16, 2012
    Posts:
    39
    Looking inside c++ crunch lib I found I can get width height and format from byte stream.

    Code (CSharp):
    1.  
    2.         ushort width = System.BitConverter.ToUInt16(new byte[2] { www.bytes[13], www.bytes[12] }, 0);
    3.         ushort height = System.BitConverter.ToUInt16(new byte[2] { www.bytes[15], www.bytes[14] }, 0);
    4.         byte format = www.bytes[18];
    5.         TextureFormat textureFormat = TextureFormat.RGB24;
    6.         if(format == 0){
    7.             textureFormat = TextureFormat.DXT1Crunched;
    8.         }
    9.         else if (format == 2) {
    10.             textureFormat = TextureFormat.DXT5Crunched;
    11.         }
    12.         else{
    13.             throw new System.Exception("Not supported format");
    14.         }
    15.  
    Now I have a bigger problem. Load dxt1/5 crunch texture with LoadrawTexturedata works in the editor, in standalone PC version but not in WebGL.
    These are the errors in the console:
    Has anyone experienced and solved this problem?
     
    Last edited: Sep 29, 2017
  6. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,999
    OUT_OF_MEMORY, try to increase webgl memory in player settings,
    upload_2017-9-30_17-27-48.png
     
  7. tomazsaraiva

    tomazsaraiva

    Joined:
    Mar 19, 2009
    Posts:
    22
    Hey,

    I was able to get the image size and format using @r3dwolf solution.
    But the texture appears like this:
    https://gyazo.com/cc56b29003133f9d9025dff59d75eae6
    I'm compressing the image with the crunch library https://github.com/Unity-Technologies/crunch/tree/unity
    and using the following to load the texture from a WWW request:
    Code (CSharp):
    1.  Texture2D texture = new Texture2D(width, height, textureFormat, false);
    2. texture.LoadRawTextureData(www.bytes);
    3. texture.Apply();
    Anyone knows what's the problem?
     
  8. tylo

    tylo

    Joined:
    Dec 7, 2009
    Posts:
    154
    I am having a similar problem. When I try to (at runtime) load Crunch textures from Unity's fork of Crunch, they appear like this. (this is all in version 2017.2 or below, btw. As of 6/6/2018 crunch textures won't open at all with version 2017.3 or above.)

    upload_2018-6-6_23-22-22.png

    However, when I load crunch textures from the "vanilla" version of Crunch by Binomial, they appear just fine.

    It's unfortunate, because Unity creates Crunch textures roughly 5 to 6 times faster than vanilla.
     
  9. peetron

    peetron

    Joined:
    Jul 7, 2013
    Posts:
    7
    Did you file a bug report?