将ATF纹理加载到ByteArray中

时间:2014-01-13 10:03:30

标签: actionscript-3 textures bytearray stage3d

我正在尝试优化纹理用于Stage3D项目的GPU内存量,所以我想我最终会切换到ATF纹理。我已经使用png2atf工具将它们从png转换为atf但是一旦我将它们加载到Flash中(使用URLLoader)我将生成的.data传递给ByteArrays时遇到了麻烦。这是我在ImageLoader类中的内容:

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;
}

然后,从游戏初始化开始,我这样做:

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);
}

但是我在'uploadCompressedTextureFromByteArray'行上得到一个null错误,或者,如果我在ImageLoader类中使用'ByteArray(loader.data)'选项,Flash告诉我加载的数据不能被强制转换为ByteArray。有关如何将加载的ATF数据作为ByteArray传递以上传到GPU的任何提示? 感谢。

1 个答案:

答案 0 :(得分:0)

我相信你错过了:

loader.dataFormat = URLLoaderDataFormat.BINARY;

这是导致.data属性返回ByteArray

的原因

针对您的具体案例的更多评论:

  • 小心从加载器中删除事件侦听器,否则会出现内存泄漏。您应该将loader作为类变量,并在imageLoaded处理程序和loadingError处理程序中删除这些侦听器。
  • 为了获得最高的质量/效率,有一种名为PVRTexTool的工具可以创建比png2atf更好的质量/更小的压缩纹理。我wrote about it some in the starling forums
  • 您可能已经意识到,但您没有提及目标平台,并且ATF introduction on adobe.com中列出了每种压缩GPU纹理格式。您需要为每个目标平台使用适当的编码。我在elaborated the png2atf commands per platform找到了另一个答案。