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\$

2 Answers 2

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
    Commented Jan 26, 2021 at 15:57
1
\$\begingroup\$

OP has already explained their solution, but let me reword it in more details for those who come across this post in the future:

Instead of CodeMonkey's approach for FOV (by tweaking renderer settings), I used stencil shaders - One for the mask, one for any object I wished to mask. Here's a helpful tutorial as a starting point. (These shaders are very easy to write!)

If you are using URP and the masking effect isn't happening, or your stencil shaders are giving a parse error claiming there is an unexpected "}", you can fix it by creating a new URP shader graph (if this is for the stencil object shader, set up sprite color & alpha outputs from _MainTex inside the shader graph editor, so that the resulting material can get the color of your main texture), click on "View Generated Shader" in the inspector, copy the generated code from the shader graph to your .shade file, then make the subshader edits here. As long as both of your shaders are URP, things will work properly.

to OP: We ran into the same problem because of the same CodeMonkey tutorial, and found the same GitHub repo then settled for the same rework and solution! Took me hours of research to figure all this out. Then I chanced to run into your post here hours afterwards when I've moved onto an entirely different problem XD

\$\endgroup\$

You must log in to answer this question.

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