Download

[HELP] Portal StencilBuffer and Shader Effect

Hello everyone.
I’m having some real trouble mimicking a Unity effect in UE4.
Specifically, it is a shader that uses the stencilbuffer to imitate an ARPortal. ( example AR Portal Tutorial with Unity - Portal Mask Implementation - Part 4 - YouTube )

https://gyazo.com/5f2bfbfd50fb42ee6aad8918bb5df265
[video]https://gyazo.com/5f2bfbfd50fb42ee6aad8918bb5df265[/video]

I can’t find a way to implement it in UE4 in the same way, because my knowledge of shader code is little. The mesh materials have this shader applied, but I don’t know which is the equivalent in UE4.
[SPOILER]


Shader "Unlit/SponzaShader"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        [Enum(CompareFunction)] _StencilComp("Stencil Comp", Int) = 3
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100
        Pass
        {
            Stencil {
                Ref 1
                Comp [_StencilComp]
            }
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            
            #include "UnityCG.cginc"
            struct appdata
            {
                float4 vertex : POSITION;
                float4 normal: NORMAL;
                float2 uv : TEXCOORD0;
            };
            struct v2f
            {
                float2 uv : TEXCOORD0;
                float3 normal: TEXCOORD1;
                float4 vertex : SV_POSITION;
            };
            sampler2D _MainTex;
            float4 _MainTex_ST;
            
            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                o.normal = normalize(mul(v.normal, unity_WorldToObject));
                return o;
            }
            
            fixed4 frag (v2f i) : SV_Target
            {
                float3 normal = normalize(i.normal);
                float3 lightDir = normalize(float3(0.0, 10.0, 10.0));
                float l = max(dot(lightDir, normal), 0.0);
                float4 texCol = tex2D(_MainTex, i.uv);
                fixed4 col = texCol * l + texCol * 0.4f;
                return col;
            }
            ENDCG
        }
    }
}

[/SPOILER]

And the portal plane shader:
[SPOILER]


Shader "Unlit/PortalPlane"
{
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100
        Zwrite Off
        ColorMask 0
        Pass
        {
            Stencil{
                Ref 1
                Comp always
                Pass replace
            }
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            
            #include "UnityCG.cginc"
            struct appdata
            {
                float4 vertex : POSITION;
            };
            struct v2f
            {
                float4 vertex : SV_POSITION;
            };
            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                return o;
            }
            
            fixed4 frag (v2f i) : SV_Target
            {
                return fixed4(0.0, 0.0, 0.0, 0.0);
            }
            ENDCG
        }
    }
}

[/SPOILER]

First step is to enable PostProcessing CustomDepth-StencilPass:
753294508dc2e165289104f76f4bb6704790d901.png
And then change every mesh material domain to PostProcess and Unlit and mayby enable stencilTest:
08cdcf01dde92f52946f1f3ec680cf0a4f306283.png

Although from the UE4 editor I see that I can change the Stencil comparison mode but I can’t find a way to change it in runtime (from always to equals values and vice versa).
a4992888e3bea08c42bb8d3ec14673364defca45.png
https://forums.unrealengine.com/core/image/gif;base64

Unity C# code to change StencilTest from Always to Equals every time the player goes through the portal plane.
92550e4269ad97087eb378f20e8effcb92a430ae.png

I hope someone can help me or guide me a bit in getting the equivalency.
Thanks and greetings.

Ok I found another post with a similar problem. I am applying the info at the moment it works to see through the portal with stencilbuffer.

https://forums.unrealengine.com/development-discussion/rendering/1806405-using-stencil-buffer

I will continue to report on the progress, before closing this post.

Greetings

I have made progress.
However, something happens to some faces of the mesh as they are not visible when deactivating the customstencil and setting the opacity to 1. Gaps appear.