It’s definitely possible, if you were taking the screenshot from the whole canvas and not only from the active layer what you did should be enough.
There is a small change you need to do in code to extend the render target to support alpha, in screenshot.js, line 129 change the colorbuffer format:
var colorBuffer = new pc.Texture(device, {
width: device.width,
height: device.height,
format: pc.PIXELFORMAT_R8_G8_B8_A8,
autoMipmap: true
});
Together with setting the clear color of Screenshot Camera to have alpha 0.0.
That should be enough though now that I am trying it myself I see that the layer isn’t fully cleared before taking the screenshot (which makes sense since we set it to alpha 0.0).
Not sure what the problem is, what you suggest should be enough.
Try setting app.scene.layers.logRenderActions to true, and use the debug engine … it should log out to debug console what it renders and if it clears - that might help to debug it.
yeah that seems all good, the screenshot camera renders first and clears color and depth - so as long as color is cleared to alpha 0, all should work?
Hopefully alpha write is not disabled when the clear takes place, but should not be.
Not directly helpful, but at the moment I’m working on a code that renders some data to texture, and I read it back from texture (not framebuffer), and that is working without a problem - I get alpha back as well… Code for reference:
There’s a bit of code that rotates the image to make it the right way up:
this.canvas.width = cb.width;
this.canvas.height = cb.height;
// The render is upside down and back to front so we need to correct it
this.context.setTransform(1, 0, 0, 1, 0, 0);
this.context.scale(1,-1);
this.context.translate(0,-this.canvas.height);
this.pixels = new Uint8Array(colorBuffer.width * colorBuffer.height * 4);
Commenting that out
This gives the correct alpha-ed background image but upside down
What’s happening is this script copies the pixels from the backbuffer into the 2d canvas, then invokes a canvas ‘drawImage’ call (which respects the transform @yaustar found) to flip the image.
If you place the following before the drawImage call: this.context.globalCompositeOperation = "copy";
then drawImage won’t blend and create a mirror copy.
However flipping the camera and removing the ‘drawImage’ call is a better option!