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:
And then change every mesh material domain to PostProcess and Unlit and mayby enable stencilTest:
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).
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.
I hope someone can help me or guide me a bit in getting the equivalency.
Thanks and greetings.