• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Tharzzat · May 19, 2020 at 07:12 PM · layersgeometrystencil

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

127 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity 5 Deferred Stencil Buffer Usage 1 Answer

What is the purpose of layers? 3 Answers

Check if Layer is in Layermask? 5 Answers

Scene layers as Enum 0 Answers

Excluding a child object from also being drag-able 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges