Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question AR Occlusion Soft Edges

Discussion in 'AR' started by kunstlabdev, Jun 13, 2021.

  1. kunstlabdev

    kunstlabdev

    Joined:
    Feb 25, 2020
    Posts:
    6
    I've seen this effect in a number of applications (i.e. 3d Scanner App, last image below), but haven't been able to find a clear modern solution.

    Currently all objects are cut out with an all or nothing edge (Image for reference). (Both compatible Android and iOS devices)

    Is there a way to soften this transition for all objects and materials?

    The primary use-case is that the occlusion 'mask' has quite a lot of frame to frame jitter and variation, which is incredibly jarring. Especially when the device is in motion.

    lure_of_slowness666.jpg

    3d Scanner App soft edge, the desired effect:

    IMG_A97574188350-3.jpeg

    ---

    I am using Unity 2021.2 Alpha 20, and current AR Foundation 4.2.0-pre.9. Building for both iOS and Android.
     
    mertbsn likes this.
  2. taavetmalkov

    taavetmalkov

    Joined:
    May 5, 2021
    Posts:
    7
    have you found any solutions yet?
     
  3. mertbsn

    mertbsn

    Joined:
    Jan 22, 2017
    Posts:
    6
    Looking for a solution to this as well.
     
  4. Yann_de_Couessin

    Yann_de_Couessin

    Joined:
    Jan 2, 2015
    Posts:
    39
    Hello, bump !

    I'm also looking for a solution to achieve this kind of effect : (from Meta's colocation VR samples)

    upload_2023-5-30_14-55-24.png

    The tracked user is represented in Mixed reality with a kind of soft rectangle around the head and the body, but unfortunately the stock shader from ARFoundation is AR/Occlusion and doesn't offer soft edges.

    Here's the interesting shader code if someone is willing to help mixing these two :


    Code (CSharp):
    1. Shader "MR/SoftPublicPassthrough"
    2. {
    3.     Properties
    4.     {
    5.         _Alpha ("Alpha", Range(0, 1)) = 1
    6.         _Darken ("Darken", Range(0, 1)) = 0
    7.         _Feather ("Feather", Range(0, 2)) = 1
    8.     }
    9.     SubShader
    10.     {
    11.         Tags { "RenderType"="Opaque" "Queue"="Overlay" }
    12.         LOD 100
    13.  
    14.         Blend SrcAlpha OneMinusSrcAlpha
    15.         BlendOp RevSub
    16.         ZTest Always
    17.         ZWrite On
    18.         Stencil {
    19.             Ref 2
    20.             Comp NotEqual
    21.             Pass Zero
    22.         }
    23.  
    24.         Pass
    25.         {
    26.             CGPROGRAM
    27.             #pragma vertex vert
    28.             #pragma fragment frag
    29.  
    30.             #include "UnityCG.cginc"
    31.  
    32.             struct appdata
    33.             {
    34.                 float4 vertex : POSITION;
    35.                 float2 uv : TEXCOORD0;
    36.             };
    37.  
    38.             struct v2f
    39.             {
    40.                 float2 uv : TEXCOORD0;
    41.                 float4 vertex : SV_POSITION;
    42.             };
    43.  
    44.             float4 _MainTex_ST;
    45.             float _Alpha;
    46.             float _Darken;
    47.             float _Feather;
    48.  
    49.             v2f vert (appdata v)
    50.             {
    51.                 v2f o;
    52.                 o.vertex = UnityObjectToClipPos(v.vertex);
    53.                 o.uv = v.uv;
    54.                 return o;
    55.             }
    56.  
    57.             fixed4 frag (v2f i) : SV_Target
    58.             {
    59.                 float2 feather = float2(_Feather, _Feather);
    60.                 float2 uvD = max(float2(0, 0), (abs(float2(1, 1) - i.uv * 2) - (1 - feather)) / feather);
    61.                 float finalAlpha = _Alpha * saturate(1 - (pow(uvD.x, 4) + pow(uvD.y, 6)));
    62.                 float finalDarken = _Darken * finalAlpha;
    63.                 return fixed4(finalDarken, finalDarken, finalDarken, finalAlpha);
    64.             }
    65.             ENDCG
    66.         }
    67.     }
    68. }
    Code (CSharp):
    1. Shader "AR/Occlusion"
    2. {
    3.     // URP SubShader
    4.     SubShader
    5.     {
    6.         PackageRequirements
    7.         {
    8.             "com.unity.render-pipelines.universal": "12.0"
    9.         }
    10.  
    11.         Tags
    12.         {
    13.             "RenderType"="Opaque"
    14.             "Queue" = "Geometry-1"
    15.             "RenderPipeline" = "UniversalPipeline"
    16.         }
    17.         ZWrite On
    18.         ZTest LEqual
    19.         ColorMask 0
    20.  
    21.         Pass
    22.         {
    23.             HLSLPROGRAM
    24.             #pragma vertex vert
    25.             #pragma fragment frag
    26.  
    27.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    28.  
    29.             struct appdata
    30.             {
    31.                 float4 vertex : POSITION;
    32.  
    33.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    34.             };
    35.  
    36.             struct v2f
    37.             {
    38.                 float4 vertex : SV_POSITION;
    39.  
    40.                 UNITY_VERTEX_OUTPUT_STEREO
    41.             };
    42.  
    43.             v2f vert (appdata v)
    44.             {
    45.                 v2f o;
    46.  
    47.                 UNITY_SETUP_INSTANCE_ID(v);
    48.                 ZERO_INITIALIZE(v2f, o);
    49.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
    50.  
    51.                 o.vertex = TransformObjectToHClip(v.vertex);
    52.                 return o;
    53.             }
    54.  
    55.             real4 frag (v2f i) : SV_Target
    56.             {
    57.                 UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
    58.  
    59.                 return real4(0.0, 0.0, 0.0, 0.0);
    60.             }
    61.             ENDHLSL
    62.         }
    63.     }
    64.     // Built-in Render Pipeline Subshader
    65.     SubShader
    66.     {
    67.         Tags { "RenderType"="Opaque" }
    68.         Tags { "Queue" = "Geometry-1" }
    69.         ZWrite On
    70.         ZTest LEqual
    71.         ColorMask 0
    72.  
    73.         Pass
    74.         {
    75.             CGPROGRAM
    76.             #pragma vertex vert
    77.             #pragma fragment frag
    78.  
    79.             #include "UnityCG.cginc"
    80.  
    81.             struct appdata
    82.             {
    83.                 float4 vertex : POSITION;
    84.  
    85.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    86.             };
    87.  
    88.             struct v2f
    89.             {
    90.                 float4 vertex : SV_POSITION;
    91.  
    92.                 UNITY_VERTEX_OUTPUT_STEREO
    93.             };
    94.  
    95.             v2f vert (appdata v)
    96.             {
    97.                 v2f o;
    98.  
    99.                 UNITY_SETUP_INSTANCE_ID(v);
    100.                 UNITY_INITIALIZE_OUTPUT(v2f, o);
    101.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
    102.  
    103.                 o.vertex = UnityObjectToClipPos(v.vertex);
    104.                 return o;
    105.             }
    106.  
    107.             fixed4 frag (v2f i) : SV_Target
    108.             {
    109.                 UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
    110.  
    111.                 return fixed4(0.0, 0.0, 0.0, 0.0);
    112.             }
    113.             ENDCG
    114.         }
    115.     }
    116. }