Depth Map texture for prerendered background

First of all, I still have a lot to learn, but I’m currently developing a survival horror game inspired by Resident Evil HD Remaster, with pre-rendered backgrounds.
I created the background in Blender and also generated the depth map texture there.

My goal is to have the character be occluded by objects (using the depth map) when walking behind them, so they become hidden from view.
I’ve set up a post-process material to handle occlusion, but right now everything gets occluded (characters, enemies, etc.) regardless of their position.

What I’d really like to achieve is that the player character and enemies are only occluded when they are actually behind an obstacle (according to the depth map), and not at all times.

Does anyone have any suggestions or best practices for selective occlusion/masking using a depth map?
Any advice, guides, or references would be greatly appreciated!

Thank you so much in advance!

P.S. Is there a better solution for pre-rendered backgrounds on Unreal Engine? Thanks

Hiding something due to being occluded by a depth map sounds like something where you’d just like to mask it out in the stencil buffer?

Is the essential idea to compare the Z-value of a background pixel vs. the Z-value of a character pixel, and if the former is lower than the latter, to cull the character pixel?

I solved the problem, but now I have to try to solve the aliasing problem.
If you have in mind a better and cleaner solution at the material blueprints code level let me know, I managed to make it work like this :slight_smile: