Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Creating a tunnel through a mesh

Discussion in 'General Graphics' started by GlenBourgonjon, Jan 25, 2020.

  1. GlenBourgonjon

    GlenBourgonjon

    Joined:
    May 25, 2015
    Posts:
    42
    I don't know how I should handle this problem I'm having. If anyone can help me find a good solution, please let me know! It would be super appreciated. And sorry for the wall of text!
    So I'm making a game where the player can terraform (on a grid-based landscape). The player can also have roads that can go through landscape elements (mountains and stuff). I already detect when a road goes through a piece of landscape and it automatically creates a tunnel mesh around the path when it notices that it intersects with terrain. That works fine. Keep in mind that both the tunnel mesh and terrain mesh are procedurally generated, and that the terrain is just a mesh and not a standard Unity terrain type of thing.

    My problem is now that I want to hide the parts of the terrain that intersect with the tunnel. I have solved this before, but not in the most optimal way, by placing a plane at the entrance of the tunnel that shows a render texture, generated by a camera that's in the exact same position as the main camera but that doesn't render the terrain layer. This way no terrain is rendered when you look through the tunnel. This looks great, but has 2 major problems.
    1. If you look through a tunnel where you can see the outside of the tunnel again, no terrain is rendered there either, because that part is still rendered by the camera that ignores the terrain layer.
    2. If you'd fly your camera through the tunnel, the intersecting landscape would become visible to the camera once you fly through the plane with the render texture at the start of the tunnel.

    I'm really stuck here. I have looked into trying to use boolean operations to actually cut holes in the terrain mesh, but it looks like that would be hard to do if you'd want to write all of the code for that yourself, and kinda expensive for real-time fast manipulation of the terrain. Also seems like a difficult thing in itself, as major 3d programs still have problems with boolean operations at times.
    I also looked at doing stuff with stencils but those don't seem to work properly for me since I am using the deferred rendering pipeline. Any tips aboyt what direction I could try to go would be really appreciated!

    I am wondering if it is possible to write a shader that tells the renderer it should render the current pixel again, but with a new range for the ray from the camera to the pixel. Instead of rendering the first visible thing in the range [near plane, far plane], it should try to now render the first visible thing in the range [distance to the current hitpoint, far plane], but only look for certain layer types.
    This way I could have a plane at the entrance of a tunnel, with a shader telling the camera to only render stuff behind that plane that isn't terrain. And then another plane at the end of the tunnel, that has a shader telling the camera to render anything that's behind the plane, terrain included. But ignore anything that could maybe have been visible from the camera POV that would be in front of the plane.
     
  2. knobblez

    knobblez

    Joined:
    Nov 26, 2017
    Posts:
    223
    Man I wish they'd expand on the terrain system. From my understanding, you could create your own cave entrance and have it change scenes when you enter it. Maybe add a sprite of a picture of the outside placed just outside the cave so it gives the appearance of being open.

    You seem to have a better idea understanding of Unity than I do but ^ is what I've done before and it seemed to work okay.