1
\$\begingroup\$

I've followed the CodeMonkey tutorial on using a dynamic mesh as a stencil for a field of view or visibility polygon effect, and have got it working using the latest version of Unity and its Universal Render Pipeline (I believe this uses a Forward Renderer).

I'd like to use the 2D lighting and this requires a 2D Renderer, which I have also got working.

Unfortunately I can't figure out a way of combining these. I've got a Universal RenderPipeline and added my 2D Renderer and the Forward Renderer to the renderer list, but it only allows one of these to work (I'd assumed it would trigger in order).

How can I get both of these effects working together, or if it isn't possible, is there a recommended approach for combining 2D lighting and stencil masking?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Figured out a workaround.

For the FOV custom mesh I had to use a new shader that just wrote to the stencil buffer. I found https://gist.github.com/ewandennis/ff0f12da482a0aff55486290c00c5e9e that does this. I set it to write the value 1 to the buffer.

I then had to create a duplicate shader to the lit sprite one that Unity provides now (not sure whether it's OK to post this so I'll err on the side of caution and not do so).

I added the following at the top of each of the three Pass sections:

            Stencil
            {
                   Ref 1               // use 1 as the value to check against
                   Comp equal       // write this pixel if equal to 1.
            }

I switched the sprites' material to point to this shader and everything was now clipped to the FOV mesh.

\$\endgroup\$
1
  • \$\begingroup\$ I ran into the same problem, I can't use both Renderer for lights 2D and FOV from CodeMonkey tutorial, unfortunately I'm just starting with shaders in Unity. I have a big request if you could contact me on Discord or elsewhere. Or describe in detail step by step how you managed to solve this problem. I would be very grateful. Nowhere else could I find an answer to this problem. \$\endgroup\$
    – LastCall
    Jan 26, 2021 at 15:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .