Golden Lens – Lens tutorial
I made a simple project that includes a completed version of this tutorial.
You can download it here: https://drive.google.com/file/d/0B3YgFUHT-lJqY0I3OER5YktNWGM/view?usp=sharing
In “Golden Lens”. a game I created for a recent game jam I used a mechanic that allowed players too see objects in a different color while looking at them trough a special lens.
Since it was requested by a few people I’m making this tutorial to show you how I did it.
Please keep in mind that it might not be the best way of achieving this effect – It is just something that works and I was able to do it in a short ammount of time.
The version that I’m showing here is a slight improvement over the game jam version, but the difference is only on the technical side and it looks the same in game.
This effect was achieved without any tricks and modifications to players camera or any line traces.
The whole things works by using custom depth and stencil but in a normal, not a post process material.
1) Project Settings
In “Project Settings”, in the “Rendering” tab “Custom Depth-Stencil Pass” has to be set to “Enable with Stencil”. This is only required if you want more than one color to be visible trough the lens.
2) Block Blueprint
Blocks are actually two identical meshes, one slightly smaller, inside the other. This prevents Z-fighting. When we are using normal rendering and custom depth
The one inside is a normal mesh, visible outside of the lens.
The one outside has to have “Render in Main Pass” set to false and “Render CustomDepth Pass” set to true. It also should cast shadows.
3) Block Blueprint – Construction Script
First we control if the normal mesh should be visible.
Then we need to inform the lens material about a desired color of the block.
Since the only data we can pass is the “CustomDepth stencil value” we need to use it.
In my first iteration I just assigned numbers to colors and had 8 conditional statements in my material, and that was painfully slow, so I devised a better way.
Here we convert 3 color bools into a “binary”. This way we avoid any loops and conditional statements.
Again, probably not the best to go about it.
4) Lens Material
Before we start with settings, here is an explanation of how the material works. We are creating a material that is somewhat between a normal material and a post process and in both cases we don’t have access to all the nodes that we would have wanted so we need to come up with a compromise.
We detect if the pixel is displaying a normal mesh or a custom depth mesh. If the first then we turn material transparent and that’s it. If the latter we turn material opaque and
color it depending on a custom stencil value.
Starting with settings, Blend Mode should be “Transparent” and Shading should be “Unlit”.
In this top section we decide if any given pixel displays something with normal depth or with custom depth.
The if node connects the Opacity input.
The second stage converts the stencil value to a color and outputs it. Final result is connected to the Emissive Color input.
5) Lens Blueprint
This is very simple. Make a new actor, any mesh will do. Add a plane mesh where you want your lens to be and set its material to the one we just made.
6) That should be all