Converting downloaded texture into PVRTC on iPhone

I download a series of textures on the iPhone, using the WWW class.

When I download them they are RGBA format.

To conserve memory I want to change them into PVRTC.

How can I do this?

It's not going to happen, there is no functionality to compress to PVRTC at runtime, and it would take an extremely long time to do it anyway. (It's time-consuming enough on a desktop CPU, never mind mobile.)

I have managed to solve this for Android. I think the same method can be applied for iOS as “the beef of the code” is relying upon standard OpenGL API.

I’ve uploaded a functional demo of my solution to this problem:

There are two projects:

  1. DynamicCompressedTextureTest - Unity project
  2. CompressedTextureLoader - Eclipse/Android project which contains the sources for “Assets/Plugins/Android/fi.jar”

One thing I’d like to ask from you: When one (or some) of you guys port this to iOS (I haven’t yet taken the time), please send me a copy of the code (or post here)!

The idea:

  • Let Unity allocate and do the initial uploading of the compressed texture, the “TexturePlaceholder”
  • When new textures are needed, the placeholder is duplicated (GameObject.Instantiate()) and thus Unity allocates and manages the new copy of the compressed placeholder texture
  • Then, the Android code downloads a new compressed texture data in the background and once it’s done, it simply overwrites the Unity managed texture - by uploading the downloaded data using Unity assigned texture ID.

At the time of writing, I didn’t have a PowerVR equipped Android at my hands but I’ve made it working with PVRTC4 the same way some time ago. It’s just about making sure that …

  1. the TexturePlaceholder is compressed with the same method as the ones that are downloaded
  2. the Android code’s texture format identifier is set correct
  3. the expected data lengths are set correct (bytes per pixel)

All in all, I think the code is fairly straight-forward, easy to read (not that elegant, though) and shows the common pitfalls to avoid.

I hope this helped, please let me know!

I found the answer in the documentation (of course):

Texture2D lTexture = new Texture2D(lWidth, lHeight, TextureFormat. your format here, false);

// assign the downloaded image to the main texture of the object www.LoadImageIntoTexture(lTexture );