Update: 17.03.17
So its been a while, but finally got round to updating this project and putting the source on github for easier access and modification. The Project is available in versions for Unity 5.2.2.f1 and 5.4.2.f2.
View the Unity-StencilPortalRoomCube Project on Github.
Improved Shader Workflow.
Switched shaders to use Material Property Drawers which both make the stencil states modifyable per material and provides a nice ui in the inspector. This removes the need for many duplicate shaders and the old method of hard-coding values as well as streamlining the project.
Improved Project Layout
Cleaned up the entire project, folder structure, shaders, scripts, materials etc.
Support for Unity DepthNormalsTexture Generation
This means that image effects such as Edge Detection now work correctly with the stencil materials, though in Unity 5.2.2f1 it only works in dx9, the other api’s just fail - most likely a bug. Works fine in dx9, dx11 and openglCore in Unity 5.4.2f2.
Improved Example Scenes
The example scenes now have a UI that allow for enabling/disabling various options to observe their effects.
Additional Features
- Stencil Viewer
- Portal Culling
- Revamped Guide Document
Original Post
One of the more interesting features of Unity was the inclusion of access to the stencil buffer, which has a wide range of uses and enables a number of different effects. Two of the most popular being portals and stencil shadows.
In order to explore the new stencil feature I created a little pet project based around portal rendering, with a scene of 9 ‘room’ cubes, with each side of each cube showing a unique view into a different space.
If anyone has played Anti-chamber you’ll be familiar with the effect, but its a little hard to explain in words, so I made a quick video of my project to demonstrate the effect.
Its a cool little effect and it can have a variety of uses. I wouldn’t be surprised if Anti-chamber made extensive use of stencil portals for many of its mind-bending spatail effects (although its possible to achieve the same thing via Render To Texture portals too).
I’m intending to post the project source here as soon as i’ve cleaned it up and added some documentation. I’d hoped to provide a compressive guide to the effect and documentation, but time is against me, so i’ll likely just have to upload what I already have written.
It should be noted that use of the stencil buffer is only really practical in forward rendering as Unity uses most of it itself in deferred rendering. Its still possible to use it in deferred, but its probably not practical to do so.
Update 22.07.13 WebPlayer Available
Uploaded a webplayer demo which can be found here.
Update 23.07.13: Demo with full source for Unity 4.2
You can download the demo, full source unity project here.
Important!
To run the demo on mobile, you must go into the ‘Player Settings → Resolution Presentation’ tab and enable ‘Use 24 - bit depth buffer’. This will enable stencils on the device, otherwise the effect doesn’t work. However please note this project was designed for standalone (Win Mac) and the demo scene is not optimised for mobile, so performance is quite low.