Creating porthole around objects of a certain layer with camera?

So I am prototyping an isometric style game, and there is a problem that if an object is behind something else, I cannot see it.

Now there are a few solutions to this that I thought of:

  • Use another camera to only draw entities ignoring depth
  • Have an outline round entities when they are obscured
  • Cutout a small area around the entity when viewing (like a porthole around the entity)

Now I tried the first 2 and they were ok but would like to try the last one but not sure how to achieve it in unity.

Am using unity free so was hoping there would be a way to get the cameras with a shader of some kind to ignore a radius around objects with a given layer.

So is this sort of thing possible and if so how do you go about doing it?

Hmm. I haven’t tried it but:
This is a depth mask shader:
http://wiki.unity3d.com/index.php?title=DepthMask

I think you could use that shader on a porthole object to define the shape of your hole.

Okay, I’ve been playing with it a little, and I guess the queue depth is an important part. So if you want holes in walls you need to set the render queue for those materials. And if you have walls that could pass in front and behind the porthole, you’ve got to have two materials and swap them out when needed.

But another thing I tried was I changed the one line from
Tags {“Queue” = “Geometry+10” }
to
Tags {“Queue” = “Geometry-10” }

So now there’s a hole through everything, right? But then you

  1. put the shape on a Porthole layer.
  2. duplicate the camera
  3. with camera 1, set clear flags to Don’t Clear
  4. with camera 2, set depth to -2 (lower than camera 1 is the important part)
  5. with camera 2, remove the Porthole layer from the Culling Mask
  6. with camera 2, increase the Near Clipping Plane until you see through the depth you want

I’m curious now how did you outline the entities when they are obscured?

Well I was lazy and just had it so if a ray from the camera could not hit it it would just highlight the entire thing, as I am still in the prototyping phase so not too fussed about that, it looked ok but it would need more polish to work out what part is ACTUALLY obscured and then make that bit vanish and just show the outline.

I currently have 2 cameras doing similar things so when I get back tonight I will look into your plan and see if I can get it working, thanks!

Just to confirm, all I should be doing is:

  • Create an object and give it the masked shader (I have used a sphere surrounding the player) and give it a unique layer
  • Create a wall object and assign it the render queue component, set its queue to 3020 (or anything above 3000)
  • Set the shader to be -10 not +10 as per in tutorial
  • Have one camera rendering everything with dont clear
  • Have another camera rendering everything except unique layer with a depth lower than previous camera

As I do not see any of the cutout effect on anything not even the object with the render queue set.

== EDIT ==

Oh no if I revert to +10 and go in front of the wall I can see the outline but if I go behind it does not seem to work, so ideally need it the other way around.

Yeah, I think it has to be closer to the camera than the wall. Not around the player. But you can probably calculate a position between the camera and the player that lines up.

How do you do that lining up? Sorry to ask what may be simple questions but I know next to nothing about this stuff…

Let me think what would be the math of it. Position of player minus position of camera, then normalize the resulting vector and multiply it by however far you want it from the camera and add it to the position of the camera.

Ah so it is the camera which I should be positioning.

No, I mean positioning the sphere relative to the camera and player. Though maybe that is over-complicated since I forgot you’re doing an isometric game. Maybe you can just attach the sphere to the player, offset in the direction of the camera however far you need it to be.