Rendering only part a mesh when going through a circular portal. Is this even possible?

I want to have a creature spawn in through a one way portal, basically a flat sphere/oval. The cube represents the monsters mesh coming out and the circle represents the portal. How complex would this have to be? What if colliders were included?

At the moment, I’m just spawning them in from behind walls so collisions/visbility behind is not an issue but I would like to know if this is even possible without the use of an external 3D modeling program.

image

1 Like

I think these sort of effects are usually done with stencil buffer shaders but I can’t really help you much with that since I haven’t dealt with these myself.

Basically, the stencil buffer is sort of like the depth buffer except you can write whatever arbitrary values you want to it. That gives you some control over which pixels can be rendered over other pixels and what-not. Then you can use it to mask-out certain parts of the screen.

An alternative might be creative use of render textures. Maybe you could render the entire view to a render texture and apply that to a quad that has a hole punched in it. Something like that.

2 Likes

Hey there!

I recently found incredibly helpful this amazing tutorial about writing a stencil buffering shader in Unity.
Here is the official Unity docs to keep track of all stencil operation values (more about them in the video).

This would probably allow the enemies to be visible from the opposite side of the portal too.
You could either render another object as the back of the portal

OR

change the enemies’ render queue depending on what the rotation of the camera in relation with the rotation of the portal is. In case you don’t know, the render queue determines the order in which the objects are rendered. Objects with a higher value usually appear on top because rendered after anything with a smaller value.

I understand your worries about colliders.
The way I personally would approach this is very simple:

  1. Spawn enemies with the collider component disabled.
    As they spawn, you could instead use the portal’s collider and even use it as a hitbox/hurtbox.

  2. Once the transition from the portal to the level is complete, enable the enemies’ colliders and deactivate the portal’s.

Hope this was helpful! Let me know how it goes :slight_smile:

2 Likes

This looks promising, I bookmarked the video to look at later. Thank you!

1 Like