Question

I'm trying to optimise the amount of GPU memory my textures use for a Stage3D project so I thought I'd finally switch to ATF textures. I've converted them from png to atf using the png2atf tool but once I load them into Flash (using URLLoader) I'm having trouble passing the resulting .data into ByteArrays. Here's what I have in my ImageLoader class:

private function loadImage():void {
    loader=new URLLoader();
    loader.addEventListener(IOErrorEvent.IO_ERROR, loadingError);
    loader.addEventListener(Event.COMPLETE, imageLoaded);
    loader.load(new URLRequest("assets/"+_imageStrings[_loadCounter])); // _imageStrings is an Array of string paths for each atf. _loadCounter increments with each successful atf load     
}

private function loadingError(e:IOErrorEvent):void {
    _imageError = new ImageEvent("imageError", -1, _imageStrings[_loadCounter]);
    dispatchEvent(_imageError);
}

private function imageLoaded(e:Event):void {
    _images.push(loader.data as ByteArray); // _images is an Array
    // I have also tried...
    // _images.push(ByteArray(loader.data));
}

public function get image():Array {
    return _images;
}

Then, from the game initialisation, I do this:

for (var loop:uint = 0; loop<_imageLoader.numImages; loop++) {
    planetTextures.push(context3D.createTexture(2048, 2048, Context3DTextureFormat.BGRA, false));
    planetTextures[planetTextures.length-1].uploadCompressedTextureFromByteArray(_imageLoader.image[loop], 0);
}

But I either get a null error on the 'uploadCompressedTextureFromByteArray' line or, if I use the 'ByteArray(loader.data)' option in my ImageLoader class Flash tells me the loaded data can't be coerced into a ByteArray. Any tips on how to pass loaded ATF data as a ByteArray for upload to the GPU?
Thanks.

Was it helpful?

Solution

I believe you are missing:

loader.dataFormat = URLLoaderDataFormat.BINARY;

This is what causes the .data property to return a ByteArray.

Some more comments for your specific case:

  • Be careful to remove the event listeners from loader or you'll get a memory leak. You should have loader be a class variable and remove those listeners in both the imageLoaded handler and the loadingError handler.
  • For maximum quality / efficiency, there is a tool called PVRTexTool that can create better quality / smaller compressed textures than png2atf. I wrote about it some in the starling forums.
  • You're probably already aware, but you didn't mention target platform and there are different compressed GPU texture formats for each as outlined in the ATF introduction on adobe.com. You'll need to use the appropriate encoding per target platform. I found another answer where I elaborated the png2atf commands per platform.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top