0
\$\begingroup\$

I am trying to make a transparent clipping volume using shaders. I found this shader code that was supposed to hide everything within whatever volume it's applied to while drawing what's behind somewhere and applied it to a cube.

Shader "Custom/BufferStencil" {
  SubShader {
    Tags { "Queue"="Geometry+1" }
    Pass {
      Blend Zero One // keep the image behind it
    }
  } 
  FallBack "Diffuse"
}

For some objects in the scene, this works. The part of them within the clipping volume disappears. However, I have a thin cube with a textured plane on top of it that I want to hide. The cube just has the default material applied to it. When I move the cube with this shader applied however, nothing changes. Why could this be? Keep in mind I'm not too experienced with Unity shaders. I'm at the very beginning of learning how to use them.

Thanks in advance.

\$\endgroup\$
3
  • \$\begingroup\$ Are the objects it works with using a transparent or alpha tested material, perchance? The way this is set up, it won't affect opaque objects rendered in the default geometry queue. \$\endgroup\$
    – DMGregory
    Commented Oct 15, 2022 at 23:27
  • \$\begingroup\$ Thanks, that worked very well. \$\endgroup\$
    – zenzicubic
    Commented Oct 16, 2022 at 2:03
  • 1
    \$\begingroup\$ If you solved your problem, post your solution as an Answer below. Since I just asked you a question, I don't know what you changed to fix it. \$\endgroup\$
    – DMGregory
    Commented Oct 16, 2022 at 2:29

1 Answer 1

0
\$\begingroup\$

The materials to be hidden have to have transparent materials applied to them. If the materials to be hidden have opaque materials, this shader will not work. After editing the material attributes for what I wanted to hide, it worked very well.

Edit: If you are worried about rendering cost, you can change the render queue of your opaque material in the inspector menu.

\$\endgroup\$
1
  • \$\begingroup\$ Note that transparent materials have an increased blending cost. If you'd like to do this more efficiently, you can change the render queue of the opaque material you want to hide so it comes after the "Geometry+1" queue slot used by your masking material. \$\endgroup\$
    – DMGregory
    Commented Oct 16, 2022 at 13:59

You must log in to answer this question.

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