【问题标题】:How to hide part of the game object when viewed through another invisible game object?通过另一个不可见的游戏对象查看时如何隐藏部分游戏对象?
【发布时间】:2018-03-28 17:32:02
【问题描述】:

我遇到了以下问题。我需要在通过另一个不可见的游戏对象查看时隐藏部分游戏对象。理论上,这应该如下工作(如下图所示)。

part of the AR-hat hides behind the head

but when viewed through the AR-hat, this part should not be transparent

但在实践中,当为 AR-head 使用以下模板着色器时:

Shader "Custom/Stencil/Mask OneZLess"
{
SubShader
{
    Tags { "RenderType"="Opaque" "Queue"="Geometry-1" }
    ColorMask 0
    ZWrite off

    Stencil
    {
        Ref 1
        Comp always
        Pass replace
    }

    Pass
    {
        Cull Back
        ZTest Less

        CGPROGRAM
        #pragma vertex vert
        #pragma fragment frag

        struct appdata
        {
            float4 vertex : POSITION;
        };
        struct v2f
        {
            float4 pos : SV_POSITION;
        };
        v2f vert(appdata v)
        {
            v2f o;
            o.pos = UnityObjectToClipPos(v.vertex);
            return o;
        }
        half4 frag(v2f i) : COLOR
        {
            return half4(1,1,0,1);
        }

        ENDCG
    }
} 
}

AR-hat 的下一个着色器:

Shader "Custom/Stencil/Diffuse NotEqualOne"
{

    Properties
    {
        _Color ("Main Color", Color) = (1,1,1,1)
        _MainTex ("Base (RGB)", 2D) = "white" {}
    }

    SubShader
    {
        Tags { "RenderType"="Opaque" "Queue"="Geometry" }
        LOD 200

        Stencil
        {
            Ref 1
            Comp notequal
            Pass keep
        }

        CGPROGRAM
        #pragma surface surf Lambert

        sampler2D _MainTex;
        fixed4 _Color;

        struct Input
        {
            float2 uv_MainTex;
        };

        void surf (Input IN, inout SurfaceOutput o)
        {
            fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
            o.Albedo = c.rgb;
            o.Alpha = c.a;
        }

        ENDCG
    }

Fallback "VertexLit"
}

场景如下(如图):

The hat is always transparent when it crosses with the head

也许有人会帮助我(谁有使用着色器的经验),如何使 AR-hat 只有在通过 AR-head 观看时才变得透明。如果我们透过 AR 帽子看 AR 头,那么什么都不会发生。

对于问题的令人困惑的陈述,我深表歉意,提前感谢您的帮助。

【问题讨论】:

  • 对不起,如果我在这里遗漏了一些东西,但你为什么认为你需要在这里需要模板缓冲区?似乎没有什么比普通 Z 排序无法处理的棘手问题了。
  • @PhilMcLaren 感谢您的回答!你说得对。我使用深度蒙版着色器和渲染队列解决了这个问题。
  • 您介意发布您的解决方案作为答案吗?我想看看你做了什么。

标签: unity3d shader augmented-reality mask


【解决方案1】:

http://wiki.unity3d.com/index.php/DepthMask

我认为这是您提到的查找方式。把它放在这里,供像我这样未来搜索的人使用。

请确保不要使用产生 2000 的几何值并使用文章中提供的值 (3000)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多