- Home /
Impossible non euclidean geometry help
I've found a way to make non euclidean geometry in Unity using stencil buffer portals.
The portals are not made with multiple cameras and there is no teleporting.
The portals only hide the overlapping geometry by separating the two using stencil buffer portals and layers.
I'm importing maps from an old game named Marathon.
Marathon maps need portals to function properly.
The Marathon map editor Weland can export maps as .obj model files, then normals and textures are added with blender.
All normals point inward and the map is separated into sectors.
The sectors and portals are separate game objects.
I found a way to use stencil buffer portals without having to edit or move sectors in the maps.
The game objects that occupy the same space are separated into two layers.
Layer 1 and layer 2 cannot collide with each other or interact.
Unity explains how to make layers.
https://docs.unity3d.com/Manual/Layers.html
https://docs.unity3d.com/Manual/LayerBasedCollision.html
The default layer connects and collides with both layer 1 and 2.
When the game object, player camera or enemy enters a portal, the portal checks if they cross the portal plane.
The portal changes the layer of the game object that crosses the portal plane to that layer the sector is.
Portal layer 1 changes player to layer 1.
Exiting the sector portal changes the game object player layer to default.
Unity explains how to change layers with script.
https://docs.unity3d.com/ScriptReference/GameObject-layer.html
There are four stencil buffer shaders, a read and write for layer 1 and a read and write for layer 2.
The read shader goes on the game object sector and the write shader goes on the portal game object.
Ronja explains how the stencil buffer works.
https://www.ronja-tutorials.com/2018/08/18/stencil-buffers.html
One read and write shader is set to ref 1 and the other read and write shader is ref 2.
Write shader ref 1 only renders read shader ref 1 from behind the portal.
This Youtube video explains how stencil buffer portals work.
https://www.youtube.com/watch?v=OBb4iUiXaRA
When the player camera crosses the portal plane and enters the sector, the read shader comparison is changed from equal to always.
When the player exits the sector portal the read shader on the sector is changed back to equal.
Unity explains the stencil buffer.
https://docs.unity3d.com/Manual/SL-Stencil.html
The portals have box colliders.
Ontriggerstay with crossing the portal does two things for the player, it changes the player game object layer and it changes the sectors read shader comp to always or equal.
To hide the enemy or player, when entering the portal they also have a read shader attached.
Anything that enters a portal is changed to that sectors layer and stencil reference value changed to that portals value.
The overlapping space stays separate and only what enters or exits the portals is changed.
Is there a better way to do this?
Your answer
Follow this Question
Related Questions
creating holes in a ship 0 Answers
Unity 5 Deferred Stencil Buffer Usage 1 Answer
What is sortingLayerID used for? 2 Answers
Change the layer of trees on terrain? 0 Answers
Reversing the sorting layer on a camera 0 Answers