Use depth buffer from another camera

Hi, i want to render bloom only for specific meshes and right now i’m trying this approach:
Render bloom meshes to separate layer(by using separate camera), then in main camera post processing blur this texture and render as quad.
Problem is that bloom is above main camera, i think i could solve it by using depth buffer from main camera but i’m not sure how exactly(main camera renders, then bloom camera renders using main camera depth, then main camera post processing happens)

Also i guess more flexible option for bloom is to use texture with high color range, and then apply bloom only to, let’s say, pixels with average ((r + g + b) / 3) color value above 2, and i can increase this average value by increasing emission material value, but again, i’m not sure how i can change main camera color buffer to a texture with HDR

Any help?

There are some HDR bloom options here: Create modern looking bloom post-effect · Issue #5187 · playcanvas/engine · GitHub

There is also some discussions on a bloom on specific objects only, for example: Apply Bloom only to specific Materials

1 Like

@mvaligursky thanks! HDR bloom looks great, i tested it, but it doesn’t work well on mobiles(as expected) because of huge screens

But object bloom works well and i want to fix this “overlay” issue, i’m trying this approach now:

  1. render main camera
  2. render bloom camera to a render target and then bloom it, but before that copy main camera depth to bloom render target. Can i somehow get render target of a screen?

You can only copy the depth buffer into a texture buffer, but not to another depth buffer, so not sure that would help? Something like this

                device.copyRenderTarget(device.renderTarget, this.depthRenderTarget, false, true);

note that for a framebuffer (when not rendering to a render target), device.renderTarget is null, which is ok, it copies depth from the framebuffer depth.

@mvaligursky
Thanks for the help, i was able to make it work, it was troubling a bit, here is my setup for any who will be interesting:

  1. create separate camera “Bloom” and layer “Bloom”
  2. First i render main camera, then onPreRender of layer “Bloom” i copy color of framebuffer to my custom render target
  3. Then camera “Bloom” is rendered with only layer “Bloom”(Clear Color is enabled, Clear Depth is disabled) and bloom post processing is applied and i use my custom render target as base color source for bloom

It works well on mobiles, only possible problem i see here is flickering because of depth if same meshes is rendered on main camera and on bloom(because i need just 1 part of that mesh to bloom, i hide the other part)… I think this is what “Polygon Offset” is created for? Can i use it in PlayCanvas?

It’s not exposed in any way on a meshInstance or material or anything like that, so using it would not be straightforward.

One other way to possibly make this work easier, would be to use the destination alpha (alpha channel on your render target). Single camera, that renders the scene as normally. You’d modify blend state on materials the way that material you don’t want to bloom would write 0 alpha, and those that you want to bloom you could write higher alpha (intensity, or just 1). And a simple modification of the bloom post effect that runs on it, so that it only picks up pixels with alpha > 0. That’d avoid buffer copies and rendering mesh multiple times.

I see. So if i change “blendState” of a material from code it will still be rendered on Opaque layer and not Transparent?

Those would move to transparent sub-layer, yeah. It’s tricky to use bloom this way as well, in case you need other transparent objects in the scene - as blending will be used to mark objects for blooming instead of alpha being used for blending. So I guess that is a limited solution.