Stage3D: how to setup ND2D to properly use ATF textures

0 Flares Twitter 0 Facebook 0 Google+ 0 Email -- Filament.io 0 Flares ×

ND2D doesn’t “entirely” support ATF textures (Adobe Texture Format). To solve this problem, just follow those steps:

  1. download the latest version of  “AGALMiniAssembler.as” actionscript file from Starling:
    https://github.com/PrimaryFeather/Starling-Framework/blob/master/starling/src/com/adobe/utils/AGALMiniAssembler.as
  2. In “Texture2D.as” of ND2D, add a public variable:
    [sourcecode language=’as3′]public var textureFormat:String = Context3DTextureFormat.BGRA;[/sourcecode]
  3. Always in “Texture2D.as“, add the following code  in the “textureFromATF” function :
    [sourcecode language=’as3′]
    switch (atf[6])
    {
    case 0:
    case 1: tex.textureFormat = Context3DTextureFormat.BGRA; break;
    case 2:
    default:
    case 3: tex.textureFormat = Context3DTextureFormat.COMPRESSED; break;
    case 4:
    case 5: tex.textureFormat = “compressedAlpha”; break;
    }
    [/sourcecode]
    This will set the actual texture format so we can properly setup our shader after.
  4. In “TextureHelper.as” class, the function “generateTextureFromByteArray” needs to look something like this:
    [sourcecode language=’as3′]
    public static function generateTextureFromByteArray(context:Context3D, atf:ByteArray):Texture {
    var w:int = Math.pow(2, atf[7]);
    var h:int = Math.pow(2, atf[8]);
    //var numTextures:int = atf[9];

    var textureFormat:String = “”;

    switch (atf[6])
    {
    case 0:
    case 1: textureFormat = Context3DTextureFormat.BGRA; break;
    case 2:
    default:
    case 3: textureFormat = Context3DTextureFormat.COMPRESSED; break;
    case 4:
    case 5: textureFormat = “compressedAlpha”; break;
    }

    var texture:Texture = context.createTexture(w, h, textureFormat, false);
    texture.uploadCompressedTextureFromByteArray(atf, 0, false);

    // TODO: add memory usage to Statistics

    return texture;
    }
    [/sourcecode]

  5. Now in the “ShaderCache.as“, replace this line:
    [sourcecode language=’as3′]
    shader = shaders[key] = new Shader2D(context, commonShaderString, vertexShaderString, fragmentShaderString, numFloatsPerVertex, texture ? texture.textureOptions : 0);
    [/sourcecode]
    by this one:
    [sourcecode language=’as3′]
    shader = shaders[key] = new Shader2D(context, commonShaderString, vertexShaderString, fragmentShaderString, numFloatsPerVertex, texture);
    [/sourcecode]
    We are now passing the texture object itself instead of just its textureOptions object.
  6. In the “Shader2D.as“, replace the constructor function line like so
    from this
    [sourcecode language=’as3′]
    public function Shader2D(context:Context3D, commonShaderString:String, vertexShaderString:String, fragmentShaderString:String, numFloatsPerVertex:uint, textureOptions:uint) {
    [/sourcecode]
    to that:
    [sourcecode language=’as3′]
    public function Shader2D(context:Context3D, commonShaderString:String, vertexShaderString:String, fragmentShaderString:String, numFloatsPerVertex:uint, texture:Texture2D) {
    [/sourcecode]
  7. In the beginning of that same construction, add this variable:
    [sourcecode language=’as3′]
    var textureOptions:uint = texture.textureOptions;
    [/sourcecode]
    so the rest of the function still works properly like it should
  8. And last but not least, always in that same constructor, add this code just after the “if(optionMissing &&…” check (not inside that “if”, just after):
    [sourcecode language=’as3′]
    if ( texture.textureFormat == Context3DTextureFormat.COMPRESSED )
    {
    texOptions.push(“dxt1″);
    }
    else if ( texture.textureFormat == “compressedAlpha” )
    {
    texOptions.push(“dxt5″);
    }
    [/sourcecode]

That’s it, you’re done with it !
And thanks to Dooprod Workshop for the little correction !

0 Flares Twitter 0 Facebook 0 Google+ 0 Email -- Filament.io 0 Flares ×

Leave a Comment