I have always been fascinated by portals. Rick and Morty always had portals but after the release of Doctor Strange it gained more popularity. If you had wished to experience portals, then guess what? There is a way! With the help of a stencil buffer, we can create portals and walk into a different world with the help of AR/ VR technology. Stencil buffers are generally used to only render parts of objects while discarding others. You can learn more about it here
Let's begin by setting up a Unity project. The stencil shader works best with the Universal Render Pipeline. So, we can either start with a 3D project and then download the UPR package or create a new project with URP itself.
In this blog, we’ll create a new URP project itself. To create it, Open Unity Hub → choose the latest version of Editor ( I am using 2021.3.4f1) → select the 3D(URP) Core template → give the project a name and click on Create Project.
Let’s now create a custom shader by writing a piece of code.
Now that we have the shader ready, we can create a Material with that shader referenced already ! To do so,
With that, we have two Stencil Materials with different ids which can be used to create two different portal windows.
In this section, we’ll be creating portal windows and portal objects. Creating these portal windows and objects is simple:
As the portals are invisible, we’ll add a framer around them for better visuals.
With that, we have set up our scene. Next, we’ll make use of the Renderer Features tool of the URP to achieve the portal effect.
Here, we can either create a new URP asset or use the existing one and modify its setting. In this blog, we’ll create a new asset and use that as our default renderer.
Finally, the time has come to create the Renderer Features which will enable the portal effect
There are other ways of creating portals too, but I believe this is the easiest way to do it. So, now we know how portals can be created using the Shader and Stencil feature of the URP asset. You can create amazing experiences with portals when combined with AR and VR. With the Oculus Passthrough feature, we can also create cool Mixed Reality experiences with the portal.
If you've enjoyed the insights shared here, why not spread the word? Share the post with your friends and colleagues who might also find it valuable.
Your support means the world to us and helps us create more content you'll love.