You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity URP中透明表面遮挡不透明网格的Shader问题求助

URP中实现透明表面遮挡后方不透明网格的问题

我正在为一款AR应用寻找实现透明表面遮挡后方不透明网格的方法,这类效果常用于制作“悬浮门”动画、地面孔洞等场景——通过带孔洞的透明网格遮挡无法透过孔洞看到的物体,这是Unity中常用的技巧,但在URP中实现时遇到了问题。

遮挡立方体上方视图
遮挡立方体下方视图
高亮胶囊体的遮挡立方体下方视图

我找到的最接近的Shader如下,它似乎能正确捕获深度信息,但无法让立方体呈现透明状态。该Shader解决了其他URP透明遮挡Shader存在的“拖影”问题,但我怀疑自己在渲染层设置上有所遗漏,却因经验不足无法明确。理想效果应为:顶部视图仅显示胶囊体上半部分,底部视图无任何显示。

{
    Properties
    {
        [HDR] _BaseColor("Base Color", Color) = (1, 1, 1, 1)
        _MainTex("Texture", 2D) = "white" {}
    }
        SubShader
    {
        Tags {"Queue" = "Transparent" "RenderType" = "Transparent"}
        Pass
        {
            Stencil
            {
                Ref 1
                Comp always
                Pass replace
            }

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_fog

            #include "UnityCG.cginc"

            struct appdata_t
            {
                float4 vertex : POSITION;
                float2 texcoord : TEXCOORD0;
            };

            struct v2f
            {
                float2 texcoord : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;

            v2f vert(appdata_t v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
                return o;
            }

            half4 frag(v2f i) : SV_Target
            {
                // Make the object invisible, but still write to the depth buffer.
                return half4(0, 0, 0, 0);
            }
            ENDCG
        }
    }
}

内容的提问来源于stack exchange,提问作者Dalton Sessumes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 03:43:24