title: Using stencil buffer to make windows
body: <p>I would like to create windows in different walls and I want it to be dynamic so I do not want to change geometry of the wall because I want to be able to move the window. So I want to use stencil shader. I am new to shaders, but I have found some things already and I have come up with this:</p>
  
  <p>Shader I use for Windows:</p>
  
  <pre><code>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"
  </code></pre>
  
  <p>}</p>
  
  <p>Shader I use for walls:</p>
  
  <pre><code>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"
  </code></pre>
  
  <p>}</p>
  
  <p>I set _StencilVal from script so that every wall has its own unique value and I have achieved pretty good result but the problem is that I can not look through two (or more) windows at the same time. When they overlap in the view the second window is grey (it is not seethrough) but I can still look through the first window.</p>