Search Unity

Make a seethrough window without making hole in the Wall

Discussion in 'Shaders' started by Daviz, Dec 18, 2014.

  1. Daviz

    Daviz

    Joined:
    Jan 17, 2014
    Posts:
    5
    Hello,
    I would like to make window (probably with some special shader) which will hide that part of the wall where that window is placed but I will be able to see other walls behind that wall. So basically i want to create realistic window without having to cut hole into the wall.
    I have been searching on the internet but I havent found anything good :/
     
  2. tanoshimi

    tanoshimi

    Joined:
    May 21, 2013
    Posts:
    297
  3. Daviz

    Daviz

    Joined:
    Jan 17, 2014
    Posts:
    5
    Thanks, but what I need is to have object that not only acts like window but I need it to hide part of the wall where the window is as well :)
     
  4. BBeck

    BBeck

    Joined:
    Jan 12, 2014
    Posts:
    57
    That's kind of a tough one. I mean, the easy way would be to have two separate models where one has a window and the other doesn't.

    If you require it to be more dynamic, where the window can be placed anywhere on the wall, then I think you're going to have to do something a lot more complicated. Chances are pretty good that you're not going to find anyone who has done this before. I mean it has almost certainly been done before, but finding someone who did it is likely impossible.

    I would probably approach it with a dynamic mask. I'm not at all good with the stencil buffer, but that's probably where the answer lies.

    You could perhaps construct a texture at runtime to take the silhouette of the window and use that as an alpha channel map.
     
  5. Daviz

    Daviz

    Joined:
    Jan 17, 2014
    Posts:
    5
    I have found something. Now i can see through the window but when I look through the window on another window I can not see through second window and also when i look through one window and there is wall and second window is behind the wall I can still see that window as grey plane :/

    Here is my code

    Window shader:
    Code (CSharp):
    1. Shader "Custom/Window" {
    2.     Properties {
    3.         _MainTex ("Base (RGB)", 2D) = "white" {}
    4.         _StencilVal ("stencilVal", Int) = 1
    5.     }
    6.     SubShader {
    7.         Tags { "RenderType"="Opaque" }
    8.         LOD 200
    9.        
    10.         ZWrite Off
    11.          ColorMask 0
    12.    
    13.          Pass {
    14.              Stencil {
    15.                  Ref [_StencilVal]
    16.                  Comp NotEqual //always
    17.                  Pass replace
    18.              }
    19.          }
    20.  
    21.         CGPROGRAM
    22.         #pragma surface surf Lambert
    23.  
    24.         sampler2D _MainTex;
    25.  
    26.         struct Input {
    27.             float2 uv_MainTex;
    28.         };
    29.  
    30.         void surf (Input IN, inout SurfaceOutput o) {
    31.             half4 c = tex2D (_MainTex, IN.uv_MainTex);
    32.             o.Albedo = c.rgb;
    33.             o.Alpha = c.a;
    34.         }
    35.         ENDCG
    36.     }
    37.     FallBack "Diffuse"
    38. }
    Walls shader:
    Code (CSharp):
    1. Shader "Custom/Rest" {
    2.     Properties {
    3.         _MainTex ("Base (RGB)", 2D) = "white" {}
    4.         _Color ("Color", Color) = (1, 0, 0, 1)
    5.         _StencilVal ("stencilVal", Int) = 1
    6.     }
    7.     SubShader {
    8.         Tags { "RenderType"="Opaque" }
    9.         LOD 200
    10.  
    11.         Stencil {
    12.             Ref [_StencilVal]
    13.             Comp NotEqual
    14.         }
    15.        
    16.         CGPROGRAM
    17.         #pragma surface surf Lambert
    18.  
    19.         sampler2D _MainTex;
    20.         uniform fixed4 _Color;
    21.  
    22.         struct Input {
    23.             float2 uv_MainTex;
    24.         };
    25.  
    26.         void surf (Input IN, inout SurfaceOutput o) {
    27.             half4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    28.             o.Albedo = c.rgb;
    29.             o.Alpha = c.a;
    30.         }
    31.         ENDCG
    32.     }
    33.     FallBack "Diffuse"
    34. }
    I set the stencilVal through script so every wall without window has stencilVal equal to 1 and every other wall where is window has its own stencilVal (2 - 255)

    Here it is working fine
    windows1.jpg

    Below you can see that i can not look through two windows
    windows2.jpg

    I would like to be able so see through all the windows when I look at them as on the prevoius image but I can not find any solution.
     
  6. speedacidrain

    speedacidrain

    Joined:
    May 4, 2015
    Posts:
    6
    Thanks for share your code! If i find a solution for the second window i will share it in return.:):D
     
  7. stasgar

    stasgar

    Joined:
    Sep 6, 2016
    Posts:
    14
    Hello, thanks you very much for your shaders. I was searching a lot, and your code is the best one i found. I'll use it to hide part of the animated windshield texture to imitate windshield wipers rain cleaning. But there is one problem - no shadows behind this window, no solution for this?
     
  8. Gagan25

    Gagan25

    Joined:
    Jun 2, 2017
    Posts:
    4


    Hi, were you able to make it work properly and reolve the issue you were getting. I am also trying to make something like this. Please help