How to make an outline of an object behind a wall?

I want to make my scene so that if a ball is behind a wall you see an outline of it. I have absolutely no idea how to go about doing this so I need a head start. What method do I use to go about doing this?


First of all you need to determine whether your ball is behind a wall.

I'm sure there should be some solution to this. As far as I know, Unity doesn't do occlusion culling out of the box. Looking at the forums, I'd suggest Physics.Raycast between your camera and the ball and check whether something is in the way. I believe some form of occlusion culling is available for iPhone development.

Outline behind the wall. Here I would create some kind of outline-object. Either create an open cylinder which is always facing your camera, so you have the look of a "ring" around your ball. In order to make the ring visible behind the wall, make it a child of your ball and assign it a different layer. Add another camera to your scene which is a child of your current camera at the exact same transform. Render the new layer with this additional camera with "Clear Flags = Depth Only" so your ring is always visible despite the fact that it's occluded by the wall.

That's how I would approach the problem.

The simplest way is by using a shader that has a pass with ZTest Greater and some kind of color blending in it, so the ball's shape is always drawn in front of the wall. You can probably manage to draw just the outline also somehow, using shader knowledge that's beyond mine. This will draw in front of all objects and not just one particular wall, though, if that's a problem.

Another method is to use two cameras and shader replacement, such as with this "thermal camera" test I made quite a while ago, although again in this case it doesn't draw the outline as such.

As Eric said, this can be done with a custom shader. Make a shader with two passes, one for ZTest greater, one for ZTest less or equal, so that you render it normally, when it is not occluded, and you just render the outline (or maybe just a transparent version of the ball) when it is.