我怎么能“掩盖”使用Unity掩码组件的粒子效应

发布于 2025-02-03 01:50:40 字数 1379 浏览 1 评论 0原文

图像组件可以按预期掩盖,但在粒子方面行不通。
问题
我尝试更改模板ID与粒子着色器中蒙版组件中显示的ID相同,但仍然不起作用。阅读了Unity面具的源代码后,我了解到有两种材料可以正确绘制面膜。一个写下模板,另一个恢复。只有在这两种材料之间绘制掩模的子对象才能正确掩盖。

    public override Material GetModifiedMaterial(Material baseMaterial)
    {
        ...

        if (desiredStencilBit == 1)
        {
            var maskMaterial = StencilMaterial.Add(baseMaterial, 1, StencilOp.Replace, CompareFunction.Always, showMaskGraphic ? ColorWriteMask.All : 0);
            StencilMaterial.Remove(c_MaskMaterial);
            c_MaskMaterial = maskMaterial;
            
            // this code generate material that recover the stencil
            var unmaskMaterial = StencilMaterial.Add(baseMaterial, 1, StencilOp.Zero, CompareFunction.Always, 0);**
            StencilMaterial.Remove(c_UnmaskMaterial);
            c_UnmaskMaterial = unmaskMaterial;
            graphic.canvasRenderer.popMaterialCount = 1;
            graphic.canvasRenderer.SetPopMaterial(c_UnmaskMaterial, 0);

            return c_MaskMaterial;
        }
        ...
    }

框架调试器

所以更具体的问题是,我可以调整粒子的渲染顺序以使其提出。在两种模板材料之间,以正确掩盖它们?如果有人能弄清楚模板材料的工作原理,那会更好。

Image component can be masked as expected, but it doesn't work when it comes to particles.
Problem
I have tried changing stencil id to be the same as the id shown in mask component in my particle shader, still not work. After reading the source code of unity mask, I learned that there were two material generated to draw mask correctly. One writes stencil and the other recover it. Only if child object of the mask was drawn between these two material can be correctly masked.

    public override Material GetModifiedMaterial(Material baseMaterial)
    {
        ...

        if (desiredStencilBit == 1)
        {
            var maskMaterial = StencilMaterial.Add(baseMaterial, 1, StencilOp.Replace, CompareFunction.Always, showMaskGraphic ? ColorWriteMask.All : 0);
            StencilMaterial.Remove(c_MaskMaterial);
            c_MaskMaterial = maskMaterial;
            
            // this code generate material that recover the stencil
            var unmaskMaterial = StencilMaterial.Add(baseMaterial, 1, StencilOp.Zero, CompareFunction.Always, 0);**
            StencilMaterial.Remove(c_UnmaskMaterial);
            c_UnmaskMaterial = unmaskMaterial;
            graphic.canvasRenderer.popMaterialCount = 1;
            graphic.canvasRenderer.SetPopMaterial(c_UnmaskMaterial, 0);

            return c_MaskMaterial;
        }
        ...
    }

frame debugger

So the more specific question is, can I adjust the render order of particles to put it between the two stencil material, in order to make them correctly masked? It would be better if someone can figure out how the stencil material works.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文