How to create a shader for a transparent wall that cover objects behind it and have portal to see trough it?

75 Views Asked by At

enter image description here

I'm building a mixed reality application that create some portal trough walls. In the first picture attached I have 2 wall with 2 different shader but I need to use both on 1 wall:

The first wall with the hole at the center have a shader that allow me to create at runtime a hole and move it to see what is behind the wall The second wall that looks like a black wall actually it's a shader that allow in mixed reality application to have invisible wall that cover all objects behind it, is very useful in mixed reality because allow me to cover object behind a invisible wall.

In my application I need a invisible wall that cover all the objects behind it and also can create a portal on it, so it's a fusion between the 2 shaders already have. I'm not really great with shader and I think I have to use Stencil (maybe?), someone can tell me some tips to merge togheter this 2 shader or some workaround?

I need something like this enter image description here

1

There are 1 best solutions below

0
Mohit Kumar On

You can use the cracked mesh and using the script you can disable that crack of the mesh. Set you camera to skybox, Enable the passthrough and you have to write custom shader(Opaques and render queue is 2000) which is use to set you passthrough over wall and ceilings and floor. create material for that shader and apply that material to all the crack meshes and all set. Now you have to write logic to disable the mesh of the crack. And for the lights you have to fake it by creating a mesh and on that mesh you have to use your custom shader which draw lines on the mesh. Here is Sample for the shader.

Shader "Unlit/Transparent"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        
    }
    SubShader
    { 
        
    Tags{ "RenderType" = "Opaque"  "Queue" = "Geometry+0" }
    LOD 100
    CGINCLUDE
    #pragma target 3.0
    ENDCG
    AlphaToMask Off
    Cull Back 
    ColorMask RGBA
    ZWrite On
    ZTest Always
    Offset 0 , 0

    Pass
    {
        Name "Base"
        Tags { "LightMode" = "ForwardBase" }
        CGPROGRAM

        #ifndef UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX
            // only defining to not throw compilation error over Unity 5.5
            #define UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input)
        #endif
        #pragma vertex vert
        #pragma fragment frag
        #pragma multi_compile_instancing

        #include "UnityCG.cginc"
        #pragma multi_compile_fwdbase


        struct appdata
        {
            float4 positionOS : POSITION;
            float2 uv : TEXCOORD0;
            float4 color : COLOR;
            UNITY_VERTEX_INPUT_INSTANCE_ID
        };

        struct v2f
        {
            float2 uv : TEXCOORD0;
            float4 positionOS : TEXCOORD1;
            float4 positionCS : SV_POSITION;
            float4 color : COLOR;
            UNITY_VERTEX_OUTPUT_STEREO
        };

        //sampler2D _MainTex;
        float4 _MainTex_ST;

      
      

        v2f vert (appdata v)
        {
            v2f o;
            UNITY_SETUP_INSTANCE_ID(v);
            UNITY_INITIALIZE_OUTPUT(v2f, o);
            UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
            o.positionCS = UnityObjectToClipPos(v.positionOS.xyz);
            o.uv = TRANSFORM_TEX(v.uv, _MainTex);
            o.color = v.color;
            o.positionOS = v.positionOS;
            return o;
        }

        fixed4 frag (v2f i) : SV_Target
        {
            UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);

            float4 col = UNITY_SAMPLE_SCREENSPACE_TEXTURE(_MainTex, i.uv);
            return col * i.color;

        }
        ENDCG
    }
}
}