Search code examples
actionscript-3alphaalpha-transparencystage3d

AS3 Stage3D: how to render all triangles with an overall alpha?


I am working on a 2D AS3 project, where the different layers are rendered via Stage3D, in a single drawTriangles() call, as a single mesh. (if you are familiar with the BunnyMark GPUSprite mini render engine, that will give you an idea: http://www.bytearray.org/?p=4074)

What I would like is to draw one of these entire 'render layers', with an overall alpha transparency value, that would apply to ALL triangles drawn, adding to their own alpha values.

IE I am not looking to change the alpha by using textures with alpha transparency or via having to go through setting each triangle separately to have the same alpha: I want a master switch that would affect the alpha value of everything that is drawn? ( something computationally cheap)

Is this possible? Perhaps via a Shader, or a setProgramConstantsFromVector command?


Solution

  • Well you could render your layer to a texture, then you could put this texture on the stage with the alpha you want, this way your whole layer would have the same alpha!

    You can do this in two ways, the first way is to render it to a texture, and display that texture with Stage3D and create a shader that adjusts the alpha.

    The second way is rendering it to a bitmapData, and displaying it on the normal stage. I'll give an example for this way:

    var bmd:BitmapData = new BitmapData(renderWidth, renderHeight, true, 0);
    var bit:Bitmap = new Bitmap(bmd);
    bit.alpha = 0.5;
    addChild(bit);
    
    //In your render loop at the beginning
    context3D.renderToBitmapData(bmd);