I get asked how we render the lens effect in our game from time to time, so I thought I would go into the process of how we did it. It’s honestly not too difficult, it just required understanding a few rendering concepts and a couple of tricks (ie hacks lol). Also, there are a couple ways to do this, and one of them can work in the free version of Unity, but I will not cover that here. If you want to know how to do it in Unity free, please feel free to contact me! (jmroth79 at gmail dot com).
First, it should be noted that our method seems and feel very wasteful. We are essentially rendering two scenes worth of assets at the same time, which means double the polys, so optimization became some what of an issue for us. If you’ve played and had trouble in some areas, know that we are continuing to optimize the game in any way we can.
Back to the topic… there are a few techniques that we used. One was the use of culling mask, and the other was rendering depth. The first thing we do it use different layers for each of the worlds. So for example, everything in the main world is on a main layer, and everything in the alternate world is on the alternate layer. This is a common thing to do in games if you want to organize things by layer, or for example, a lot of FPS games put the gun on it’s own layer so the gun never clips through a wall. The power of this comes from being able to set what layers a camera is rendering by setting it’s culling mask. So in Lens, the main camera ignores the alternate world layer, and the alternate world camera ignores the main world layer. Pretty easy and simple to set up in just a few clicks.
The trouble comes from the title of this post… “Is it in the right layer?” I don’t know how many times I said this to our level designers lol. Often it went something like, “Hey Jason, I’m trying to set this thing up but it won’t transfer to the other world…,” in which I would cut them off and reply, “Is it in the right layer?” In which Drew or Adam would quickly see the small mistake and continue on. Eventually it sounded more like, “Is it in the right LAIR,” a la Wheatly from the end of Portal 2, which became somewhat of a running joke for a few weeks. It’s something that’s super simple to forget, and often the last thing you check when debugging. Thinking back, I probably should have tried to automate the process with a tool, but we eventually over came the issue.
So now we have our layers and culling mask set up, how do we ensure that the main world is rendered last? Again, super simple. Unity allows you to set the depth of a camera, which changes the order of which it’s rendered. All we had to do was set the main camera to a higher depth level than the alternate camera, and all is good.
Before I go into how the Lens actually works, it’s worth noting that once this was all set up and figured out, transferring objects became fairly straight forward. While there’s quite a bit that goes in in transferring (raycasting, effects, timing, etc.), the actually world swap it just changing the layer the object is on. This is why we used effects and dissolved the objects because it hid the fact that the objects are popping between layers, and gave it a more organic feel, not to mention it looks cool! (Adam Burns did the cool effect).