Search Unity

Texture2d.LoadRawTextureData()????

Discussion in 'Scripting' started by mmwizard, Jan 22, 2014.

  1. mmwizard

    mmwizard

    Joined:
    Mar 27, 2009
    Posts:
    65
    Hello.

    Does anyone know how LoadRawTextureData() works?
    I have a pre formatted pvr texture wich does load in untiy. But i dont know how to load it from a external source.
    this is my code so far.
    Code (csharp):
    1.  
    2. IEnumerator Start() {
    3.         DirectoryInfo dir = Directory.GetParent(Application.dataPath);
    4.         WWW w = new WWW("file://" + dir.ToString() + "/pvrtestimg.pvr");
    5.         yield return w;
    6.         Debug.Log(w.size);
    7.         Texture2D tex = new Texture2D(512,256,TextureFormat.PVRTC_RGBA4,false);
    8.         tex.LoadRawTextureData(w.bytes);
    9.         renderer.material.mainTexture = tex;
    10.         Debug.Log(dir.ToString() + " /pvrtestimg.pvr; format : " + tex.format.ToString() ;
    11.     }
    12.  
    thanx in advance...
     
  2. AngryGenius

    AngryGenius

    Joined:
    Jan 21, 2014
    Posts:
    14
    I'm pretty certain that Texture2D is an undocumented method, but Texture2D.LoadImage(byte[] data) should work just fine on PNG and JPG images. Unity Documentation
     
  3. PrisedRabbit

    PrisedRabbit

    Joined:
    Aug 14, 2012
    Posts:
    63
    You have to Apply loaded texture. Just add tex.Apply() after tex.LoadRawTextureData(w.bytes);
     
    MGGDev, Dylan56 and yuen-don like this.
  4. mmwizard

    mmwizard

    Joined:
    Mar 27, 2009
    Posts:
    65
    Yes i know Texture2D.LoadImage should work. But that was not my question. I want to load pre formatted images in pvrtc format. If you load an image on ios it wil be loaded as argb32 and this will, in my case, consume 120Mb memory. If i load it as pvrtc it wil consume 8.5 Mb. So if you have an answer for that i would be very happy...
     
    Propagant likes this.
  5. mmwizard

    mmwizard

    Joined:
    Mar 27, 2009
    Posts:
    65
    Hey mrbroshkin,
    thanx, i am 1 step further now. but as you see in the image below my downloaded texture looks a bit different.
    any ideas?

    $Screen Shot 2014-01-23 at 09.37.06.png
     
  6. mmwizard

    mmwizard

    Joined:
    Mar 27, 2009
    Posts:
    65
    Well i have solved it with the help of someone at reddit. and it has to do with the header size. It seems that images have some header info about the image. Unity asumes that the byte[] containes only rgb data. So after looking at the pvr header info i have found that it is 52 bytes long. so you have to remove that from you're byte[] and parse the result in as raw texture data.
    And look the final code....

    Code (csharp):
    1.  
    2. WWW www = new WWW("file://" + dir.ToString() + "/pvrtestimg.pvr");
    3.         yield return www;
    4.  
    5.         int headerSize = 52;
    6.         byte[] buffer = new byte[www.size - headerSize];
    7.         System.Buffer.BlockCopy(www.bytes, headerSize, buffer, 0, www.size - headerSize);
    8.  
    9.         Texture2D tex = new Texture2D(512,256,TextureFormat.PVRTC_RGBA4,false,true);
    10.  
    11.         tex.LoadRawTextureData(buffer);
    12.  
    13.         tex.Apply();
     
  7. PrisedRabbit

    PrisedRabbit

    Joined:
    Aug 14, 2012
    Posts:
    63
    Good job! I'm also going to load pvr textures like this method , thank you to.
     
  8. Taphos

    Taphos

    Joined:
    Dec 5, 2012
    Posts:
    11
    Hey, how do you save the raw texture?
    Do you save from Unity or any external software?
     
    MGGDev likes this.
  9. PrisedRabbit

    PrisedRabbit

    Joined:
    Aug 14, 2012
    Posts:
    63
    here is my script to grab raw texture data from Unity assets (not tested in Unity 5):

     
    Pulas, RIZ3N, lexon4eg and 2 others like this.
  10. tswalk

    tswalk

    Joined:
    Jul 27, 2013
    Posts:
    1,109
    I know this is an old"er" thread but still very relevant.

    I just wanted to add that in the PVR File Format specification (atleast NOT for legacy PVR files)... their is a metadata boundary after the Header that also needs to be skipped.

    You can find this by examining the last 4 bytes of the header (unsigned 32bit integer) to get the size of the boundary and include that in the excluded byte range to push to your buffer.

    just incase anyone else comes across this thread and still can't get it to work and happen to "not" use the legacy switch (it's being deprecated).
     
  11. friuns3

    friuns3

    Joined:
    Oct 30, 2009
    Posts:
    307
    Code (CSharp):
    1. tex = new Texture2D(2048, 1024, TextureFormat.ETC2_RGBA1, false);
    2. var readAllBytes = File.ReadAllBytes(@"example.etc");
    3. var padding_size = readAllBytes.Length - (tex.width / 4) * (tex.height / 4) * 8;
    4. var na = new byte[readAllBytes.Length - padding_size];
    5. Buffer.BlockCopy(readAllBytes, padding_size, na, 0, na.Length);
    6. tex.LoadRawTextureData(na);
    7. tex.Apply();
    this worked for me, interesting is there any header specifications somewhere?
     
  12. PrisedRabbit

    PrisedRabbit

    Joined:
    Aug 14, 2012
    Posts:
    63
    http://cdn.imgtec.com/sdk-documentation/PVR+File+Format.Specification.Legacy.pdf