Monday, January 4, 2021

The Sacrifice Devlog 005: The Portal Shader (04/01/21)

Over the past couple days I've been putting together my portal shader. The idea behind this was to create a tunnel which opens up and seems to pass through anything behind it, which also allows stuff to pass into it and then not be visible when the portal closes.

Here what I ended up with for the main portal:

Obviously, it's got some work left to pretty it up a bit, but the main part of the shader is working.

For this post, i'm splitting the effect into 2 main parts, the portal shader, and the object function.

Portal Shader:

To achieve this effect I'm using 2 meshes, the tunnel, and a disc which is fitted to the opening of the tunnel.


In engine I put the 2 meshes into a blueprint with the disc aligned to the tunnel. I then set the disc to render to the custom depth buffer, while not being rendered in the main pass. 


What this allows me to do is, in the tunnel shader, compare scene depth to the custom depth buffer, and use the output to drive an opacity value. In essence if scene depth is more than custom depth, then the tunnel will be visible.


With this set up the next problem was that if the portal goes through any geometry, that geometry is visible. I fixed this with a bit of a cheat, but it does the job. I just make a duplicate of any materials I need, and set them to transparent, with a lower sort order priority than the portal.

And boom, portal shader halfway there!

Now, onto allowing stuff to get in it.

Object Function:

For objects going into the portal, I put together a material function that can be plugged into the opacity of any material, to allow it to interact with the portal.






The main part of the function is just running the same check as the portal tunnel, using custom depth to check if it should be visible or not. The key with this however is making an exception to this if the object isn't in the portal yet, otherwise the object would be invisible when it's not even close to the portal.

This turned out to be a relatively easy fix, all I had to do was set up a vector material parameter storing the location of the portal, and then check if the object is in front or behind the portal.


Now, currently i'm only checking the object position on one axis, meaning should the portal rotate it wouldn't work. If I was going to fix this I could probably do a bit of math and figure it out, but for my project I just don't need to.

No comments:

Post a Comment