How do I achieve this effect?

I originally thought I could do it with a depth shader and render queues but I couldn’t get the back room to occlude the front room (but it turns out render queues don’t work like that). The I tried rendering the active room on a different camera, this worked but then the player couldn’t enter the room (as it was rendered on top) or would appear in front of inactive walls (if I rendered the player with the active room). Also using a second camera resulted in a very hard and ugly cutoff of the front room and I’d prefer a semi transparent result (similar to project zomboid).

I’m not looking for anyone to write a complete solution but some guidance would be great. I’m fairly new to Unity and very new to shaders but wanting to learn. Any help would be appreciated.

Thank you for your time.

I really have no idea for the other situations, but for the first two perhaps you could alternatively use for the room a shader that do, and one that don’t, cull back.
When the little orange guy is in a particular room you would assign to that room a material that has the shader that culls back faces (in your 3d modeling application you would want to have all the faces targeting with their normals the inside of the room).

I hope this could be of some help.
sorry for bad english

Well, to het this exact effect you need a few things. First of all you need a well designed order in which the different objects are drawn. This could be done by using custom shaders with a custom renderqueue value.

The objects should be rendered in that order:

  • First you only draw the active room and only draw the front faces as usual.
  • Now you draw all items / players which should be visible through the transparent areas.
  • Now you draw the active room again but this time only the backfaces and you only write to the z buffer and keep the color buffer. The shader also should offset the geometry towards the camera position to make sure it covers the whole area of the active room.
  • Finally draw the remaining rooms.

This would carve the active room from the others. The overlapping areas will simply be invisible. You could draw the remaining rooms again with a transparent shader that doesn’t do a depth test, but the result might look strange and of course causes a lot of overdraw which is bad for performance.

There are certainly other ways to accomplish this, maybe by using the alpha channel and alpha tests in the shaders, but this is what i just had in mind :wink:

ps: If your rooms don’t use a double sided shader but have actual geometry for both sides it’s getting a bit tricky. In this case you need some other hint to know what is in front of what.