I’m trying to create an effect where you can see a Sprite through specific geometry in the Universal Render Pipeline. To do this the most appropriate option seems to be to use a Depth Mask shader on the mask objects with ColorMask 0 and setting it to an earlier render queue than Geometry, and then just setting the sprite earlier in the render queue than the mask, so the sprite is only thing that appears in the mask area.
Which works well and achieves the desired effect, but I’d also like the mask to receive shadows (and possibly lighting), is anyone aware of a way to achieve this?
My temporary solution is to duplicate the mask objects and give these a “Shadow Matte” material that is invisible and receives shadows, but I’m still looking for a single shader solution ideally.
In case anyone is looking to do something similar, here’s a brief summary of how I achieved something close to the effect I was going for in URP 2021.2. It’s certainly not an ideal solution and I intend to replace it at some point, but it works well enough for now.
Essentially there are 4 elements to making an isometric 3D environment with 2D backgrounds that includes realistic 3D shadowing-
1 - 2D Background - A sprite that is placed behind and aligns with the 3D objects you want to have 2D images for, setting the rotation to that of the orthographic camera. Set the material to earlier in the render queue than geometry and the depth mask.
2 - 3D Depth Mask - On the original 3D object use a depth mask shader (ColorMask 0), set it to earlier in the render queue than geometry, but later than the background to allow you to see the image through it.
3 - 3D Invisible Shadow Receiver (Shadow Matte) - On a copy of the 3D object add a transparent shadow receiver, I used bgolus’s shader written for the URP.
4 - 3D Invisible Shadow Caster - On a second copy of the 3D object and a transparent shader that casts shadows. I did this with Shader Graph set to transparent and ensuring cast shadows is enabled in Graph Settings.
With that you should see a 2D image through a 3D object with shadows behaving as you would expect in 3D. This can allow for some really cool effects provided you’re able to create art in a specific way so it matches the 3D environment. In terms of optimizations there should be a way to combine 3 and 4 into a single shader so you don’t need 3 versions of the same mesh. But including the depth mask with that may be a bit of a challenge.