Sunday, April 7, 2013

Task #5: Loading DDS, BMP, TGA and other textures via WWW class in Unity3d

Practical use of Unity3d engine.



Task #5: Loading DDS, BMP, TGA and other textures via WWW class in Unity3d

  
  This article describes the task: Load images via WWW class. Current version of Unity3d allows you to download images via WWW class only in PNG or JPG format (read more here: http://docs.unity3d.com/Documentation/ScriptReference/WWW-texture.html). But we should implement image loading in such formats as DDS, BMP, GIF, PSD, TGA etc. 


  Demo of application is shown on fig. 1.
Figure 1. – Demo of application
  
  All these image formats are supported by DevIL library (read more here: http://openil.sourceforge.net/features.php).
  There is no need for full application description, because DevIL library is well documented and has a lot of examples. I will show only the short description of implementation:
  • Load texture using WWW class;
  • Get bytes data using «bytes» field of WWW class;
  • Create DevIL texture from this data;
  • Get Color32[] array from DevIL texture;
  • Create Texture2D object based on this color array;

  To illustrate implementation I created a demo application. I want to notice that this application loads also MipMaps data from the texture, and creates proper Texture2D. 
  If you need more detailed description, or implement image loading not supported by unity3d, please write it in comments.


  Application demo you can download here:
  Source code you can download from here:

9 comments:

  1. Hey! Thanks a lot for the article. This blog is really handy :)

    It turns out that I'm trying to make a level editor (with unity) for a non-unity game and it would be really nice if you could release the source code of this example since I'm clueless about how you implemented the file browser window, or at least you could give some hints about it.

    Thanks again and keep it up with the blog!

    ReplyDelete
    Replies
    1. Hi, NevaK!

      It's not a problem :) I've added the source codes to the article.

      Delete
  2. Hi Denis!
    I finally found some time to dedicate to Unity and I just wanted you to know that your code works like a charm and now I can load my textures with no problem :D.
    However, I'm using Unity free at the moment, and obviusly it complains about the license when I try to run the part of the code which refereces to DevIL and it doesn't load the textures. The funny thing is that it lets me build the project and runs perfectly once I have the executable.

    So again, thanks a lot for sharing your work!

    ReplyDelete
    Replies
    1. Hi, NevaK!

      It's great! Really, as i remember free Unity3d does not support plugins, so this is very interesting workaround:) Thank you for you comments!

      Delete
  3. Thanks Denis, just what I was looking for.
    Cпacибо

    ReplyDelete
    Replies
    1. I'm glad, that this may help you! На здоровье :)

      Delete
  4. I love this integration you have done and its very useful. What i'd really like to do is use the CRN format because it compacts the texture data much more efficiently. Have you any information on how that would be possible to integrate?

    ReplyDelete
    Replies
    1. Hi Mark,

      I showed just a one sample of "how to load texture data that does not support Unity3d". Using described mechanism you are able to load any format. Just find the library that are able to load it, and integrate it to Unity3d,

      -Denis

      Delete
  5. Hi Denis,

    Are there any usage restrictions or licenses associated with the software tutorials you have written here?
    I would potentially like to use them for non-educational purposes.

    In particular the DevilLoader.CS wrapper you have written.

    Thanks,
    Tam

    ReplyDelete