Baking 3D to 2D

Hi,
I have the necessity to display 3D object like 2D bitmaps: the textures of the 3D models change dinamically, so I can’t prerender everything outside my application, I have to do it at runtime. Is it possible to “take a picture” of a 3D model with transparent background as if I was drawing on a transparent BitmapData?
I am on Away3D, don’t think the target makes a difference…

Hi,

Yes it’s possible but for some unknown reason at the moment (needs looking into) transparency isn’t working :frowning:

Basically, you setup up the scene of what you need to take a snapshot of and call the following function which will add it as a Bitmap to the main stage. As I said, transparency should work by setting the backgroundAlpha to 0 and then back to 1. The method queueSnapshot will copy the next view.render() call to the bitmap and then you can carry with your normal 3D rendering (assuming the scene is setup back up correctly). It may be easier to have a separate view3D scene for the snapshotting - to save trying to restore the original scene - if that makes sense.

Anyway, here’s the function.

private function takeSnapshot():Void
{
    var bmd:BitmapData = new BitmapData(Std.int(_view.width),  Std.int(_view.height), true, 0x0);
    _view.backgroundAlpha = 0;
    _view.renderer.queueSnapshot( bmd );
    _view.render();
    _view.backgroundAlpha = 1;

    var b = new Bitmap( bmd );
    addChild( b );
}

Hope that gives you what you need (apart from transparency).

@Greg209 thanks!

Uh, the lack of transparency is a big problem for what I need to do: I need to render small objects that I have to group later.

If I have a recurrent shape I could create a separate transparent image (i.e. in Photoshop) and use the alpha channel to cut out what I need, but it would be very annoying.

context3D.drawToBitmapData uses gl.readPixels on the back buffer, currently, which may explain the lack of transparency. I have been thinking we should consider using a separate framebuffer for Stage3D content, which would allow us to present() less often than each frame, and would likely solve this issue as well

2 Likes

I like the idea of a framebuffer for Stage3D :wink: