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

Question How to hide objects not under a certain mesh using stencil shaders?

Discussion in 'Shaders' started by dottonecioni, Jun 1, 2021.

  1. dottonecioni

    dottonecioni

    Joined:
    Jul 21, 2017
    Posts:
    8
    Hello,
    a few days ago I posted this in the 2D threads, hoping to get some help with a situation I'm stuck in. It's become clear to me that if I want to achieve a 2D FOV effect using a mesh and also keep the lights from the 2D URP renderer I'll need to use some custom shaders. So I'm trying to learn how to create shaders, but I'd like to ask, is it possible at all to create a shader/multiple shaders that will enable me to only see certain sprites when they are under a mesh? What I want to achieve is something like this:
    • There's a semi-obscured overlay over all of the scene.
    • I am dynamically creating a Field of View mesh that collides with objects that shouldn't be "see-through".
    And I need to find a way to make it so that the "obscured overlay" isn't drawn over the FOV mesh, so what the character sees is clear. I also need to make it so that certain sprites (enemies) aren't drawn/rendered/seen when not under the FOV mesh. However, I can't change the material (and therefore the shader) on the enemy sprites, since sprites that don't use the URP default sprite shader aren't seen or have weird colouration when using the 2D renderer. So is it possible to achieve this effect using just a shader for the FOV mesh and maybe a shader for the dark overlay?