actionscript-3texturesarraysstage3d

Loading an ATF texture into a ByteArray


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.


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: