本文介绍了是否可以在 Unity 的 HDRP 中创建 DepthMask 效果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我一直在反对这个问题一段时间,但我不知道是否可以为 HDRP .



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.

我尝试在着色器中处理渲染队列、不同的 ZTest 和 ZWrite 组合,以及我发现的着色器的一些变体.最重要的是,我尝试弄乱我能想到的基本 HDRP 着色器的每个配置,这可能会有所作为.

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.

我能得到的最接近的是使物体后面的透明材料消失.这确实意味着,如果我将所有不透明材质设置为透明,我就会得到我想要的效果,但这并不理想,因为它破坏的比解决的要多.理想情况下,我能够以某种方式在不透明材料上使用 HDRP 透明优先系统,但我不确定这是否可行.

我不太确定接下来要尝试什么,任何帮助将不胜感激!

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!

推荐答案

所以我设法找到了一种方法来实现这一点,它在这里使用自定义传递:

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

HDRP Unity 深度蒙版概述

这个场景中有三个物体:

There are three objects on this scene :

  1. 一架将被遮罩并使其一部分消失的飞机(这是黄色的).它确实使用 HDRP Lit 着色器作为透明,这是他的着色器:

带 HDRP 光照着色器的遮罩对象

  1. 不会被蒙面的飞机.它确实使用 HDRP Lit 着色器作为不透明(它是红色的).这只是基本的.

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

将成为面具的平面.它有一个自定义层,我将此层命名为Mask".飞机将 HDRP 光照着色器设为透明并使用深度测试:从不.这是:

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 :

带有 HDRP 光照着色器的 Mask 对象

最终,您将需要一个具有自定义传递体积组件的对象.此自定义通道体积将针对您创建的自定义图层:图层蒙版",并将在渲染透明通道之前将其材质替换为自定义材质.这是:

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 :

自定义通行证设置

最后这里是着色器的代码,用于插入自定义通道的材质(自定义通道中名为 StencilMask 的那个):

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 {}
    }
}

这篇关于是否可以在 Unity 的 HDRP 中创建 DepthMask 效果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 15:01