Week 7 – 10.11 VR Portal Development Stencil Layer

VR portal

I implemented the VR portal using the Stencil Layer technique.

The stencil shader works by using a stencil buffer to mask certain parts of the rendered scene. This allows us to create effects like portals, where only objects within the portal’s boundaries are visible, and everything outside is hidden. Specifically, the stencil buffer marks the area of the portal, and only objects within this area are rendered in the virtual world.

VR portal
Stencil Shader

Code Implementation of Portal Effect

Portal Trigger Detection:
The OnTriggerEnter method detects when an object with a specific tag (e.g., the player) enters the portal area. It calculates the angle between the object’s direction and the portal’s axis to determine if the object is moving toward the portal. If the angle is less than 90 degrees (indicating entry), the object’s layer is switched to a new layer associated with the virtual world.

Layer Switching:
The SetLayerRecursively method recursively updates the layer of the portal and its child objects to the specified new layer. This ensures that the virtual world is properly rendered when the object passes through the portal.

Portal Exit Handling:
The OnTriggerExit method handles the object’s exit from the portal. It activates the corresponding portal on the other side and deactivates the current portal to simulate seamless teleportation. Additionally, it ensures that other connected portals are updated based on whether the object is entering the virtual world or returning to the real world.

Portal Orientation:
The GetDoorToVirtualWorldDirection method determines the portal’s facing direction along the chosen axis (x, y, or z). This ensures that the portal can be oriented dynamically in the scene.

Other Portal Visibility Management:
The SetOtherPortals method manages the visibility of other portals based on the current layer. When entering the virtual world, other portals are hidden (SetOtherPortals(false)), and when returning to the real world, they are made visible again (SetOtherPortals(true)).

Code Implementation of Portal Effect

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *