Make a seethrough window without making hole in the Wall

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 :confused:

Try creating a material with this shader and assigning it to your “window” mesh: Glass Shader | Alastair Aitchison

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 :slight_smile:

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.

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 :confused:

Here is my code

Window shader:

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 //always
                 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"
}

Walls shader:

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"
}

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

Below you can see that i can not look through two windows
1895072--122037--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.

3 Likes

Thanks for share your code! If i find a solution for the second window i will share it in return.:):smile:

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?

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