Is it possible to create a DepthMask effect in Unity's HDRP?

假装没事ソ 提交于 2021-01-28 08:47:05

问题




I've been banging my head against this for a while but I cannot figure out if it is possible to create a DepthMask shader for HDRP (as descibed here).

For my exact use, I'm trying to create a "hole" in the shape of whatever I have the material applied to, that shows the contents of a different camera rendered behind everything.

I tried messing around with the render queue within the shader, different ZTest and ZWrite combinations, as well as some variations of the shader I found. On top of that, I tried messing around with every configuration of the base HDRP shaders I could think of that might do something.

The closest I could get was making transparent materials behind the object vanish. This does mean that if I set all my Opaque materials to Transparent I get the effect I desire, however this isn't ideal, as it breaks more than it solves. Ideally, I'd be able to somehow use the HDRP transparent priority system on Opaque materials, but am not sure it's possible.

I'm not quite sure what to try next, any assistance would be greatly appreciated!


回答1:


So I managed to find a way to achieve this, it is using Custom Pass here it is :

HDRP Unity Depth Mask Overview

There are three objects on this scene :

  1. A plane that will be masked, and have a part of it disappearing (It's the yellow one). It do uses HDRP Lit shader as transparent, here is his shader :

Masked object with HDRP Lit shader

  1. A plane that wont be masked. It do uses HDRP Lit shader as opaque (It's the red one). It's just basic.

  2. A plane that will be the mask. It has a custom layer, I named this layer "Mask". The plane has the HDRP Lit shader as transparent and using depth test : never. Here it is :

The Mask object with HDRP Lit shader

Ultimately you'll need an object with a custom pass volume component. This custom pass volume will target the custom layer you created : The layer "Mask", and will replace it's material by a custom material before the transparent pass is rendered. Here it is :

Custom Pass settings

Finally here is the code of the shader to uses on the material inserted on the custom pass (The one named StencilMask in the custom pass) :

Shader "Custom/Stencil/MaskStencil"
{
    SubShader {
        // Render the mask after regular geometry, but before masked geometry and
        // transparent things.
 
        Tags {"Queue" = "Geometry+10" }
 
        // Don't draw in the RGBA channels; just the depth buffer
 
        ColorMask 0
        ZWrite On
 
        // Do nothing specific in the pass:
 
        Pass {}
    }
}


来源:https://stackoverflow.com/questions/57044672/is-it-possible-to-create-a-depthmask-effect-in-unitys-hdrp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!