使用模板缓冲区制作窗口

时间:2015-01-02 09:46:42

标签: unity3d stencil-buffer

我想在不同的墙壁上创建窗户,我希望它是动态的,所以我不想改变墙的几何形状,因为我希望能够移动窗户。所以我想使用模板着色器。我是着色器的新手,但我已经找到了一些东西,我已经想出了这个:

我用于Windows的着色器:

Shader "Custom/Window" {
Properties {
    _MainTex ("Base (RGB)", 2D) = "white" {}
    _StencilVal ("stencilVal", Int) = 1
}
SubShader {
    Tags { "RenderType"="Opaque" }
    LOD 200

    ZWrite Off
     ColorMask 0

     Pass {
         Stencil {
             Ref [_StencilVal]
             Comp NotEqual
             Pass replace
         }
     }

    CGPROGRAM
    #pragma surface surf Lambert

    sampler2D _MainTex;

    struct Input {
        float2 uv_MainTex;
    };

    void surf (Input IN, inout SurfaceOutput o) {
        half4 c = tex2D (_MainTex, IN.uv_MainTex);
        o.Albedo = c.rgb;
        o.Alpha = c.a;
    }
    ENDCG
}
FallBack "Diffuse"

}

我用于墙壁的着色器:

Shader "Custom/Rest" {
Properties {
    _MainTex ("Base (RGB)", 2D) = "white" {}
    _Color ("Color", Color) = (1, 0, 0, 1)
    _StencilVal ("stencilVal", Int) = 1
}
SubShader {
    Tags { "RenderType"="Opaque" }
    LOD 200

    Stencil {
        Ref [_StencilVal]
        Comp NotEqual
    }

    CGPROGRAM
    #pragma surface surf Lambert

    sampler2D _MainTex;
    uniform fixed4 _Color;

    struct Input {
        float2 uv_MainTex;
    };

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

}

我从脚本中设置_StencilVal,这样每个墙都有自己独特的值,我已经取得了不错的效果,但问题是我无法同时浏览两个(或更多)窗口。当它们在视图中重叠时,第二个窗口是灰色的(它不是透视的),但我仍然可以通过第一个窗口查看。

0 个答案:

没有答案