2
\$\begingroup\$

I have a problem. I want a Grayscale effect to be applied ONLY to the chair on the stage. The chair has a layer CurrentItem. And I have two cameras:

  1. Camera_All renders everything except layer CurrentItem (I set this exception in CullingMask)

  2. Camera_CurrentItem

enter image description here

Finally:enter image description here

I need to apply not just one effect, but a few.

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Everything is becoming gray because post-processing always draws the whole screen, so the effect gets applied to every pixel. Since the order of the cameras is Camera_All and then Camera_CurrentItem, the post-processing of Camera_CurrentItem gets applied to all the pixels that were drawn earlier by Camera_All.

If you exchange the drawing order (by switching the Depth properties of the two cameras), and set the Clear Flags of Camera_All to "Don't Clear", the chair will be drawn first, then the grayscale effect on just that, then all the non-chair stuff.

If this works, then great!


I think it will not quite work. You will probably see only a gray chair, with whatever background Camera_CurrentItem's clear flags dictate. To fix this, you'll need to modify the grayscale effect shader.

Make a copy the shader file, and add ZWrite Off in the SubShader block. I don't have your particular shader, but the code near your addition will be something like

SubShader
{
    Tags { "RenderType"="Opaque" }
    LOD 100

    // You'll add the following line
    ZWrite Off

There may be other lines in there, and that's fine. Look for the Tags or LOD line and put your new ZWrite Off line near them.

Now you can drag the new shader into the Shader property of the Grayscale Effect component, and you should see the whole scene assembled as expected.

\$\endgroup\$
2
  • \$\begingroup\$ I did a quick test, and flipping the order of rendering the two cameras gives the desired result, in both forward and deferred rendering. The one caveat is that it doesn't play well with the built-in skybox, since Unity wants to clear depth when we set clear flags to "Skybox". Since the example image is an interior it's probably not a big issue. Worst-case, you can render a skybox separately. \$\endgroup\$
    – DMGregory
    Commented May 9, 2017 at 1:49
  • \$\begingroup\$ Good point. It looks like Unity draws its skybox with no z-test and z-write on, always outputting max. z. A more robust solution would completely separate the rendering of the grayscale objects from the rest of the scene, but that may not be necessary here. \$\endgroup\$
    – Victor T.
    Commented May 11, 2017 at 14:09

You must log in to answer this question.

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