Search Unity

Question Shader for user-facing camera human occlusion

Discussion in 'AR' started by ARrrrgonaut, Feb 10, 2022.

  1. ARrrrgonaut

    ARrrrgonaut

    Joined:
    Jan 5, 2021
    Posts:
    5
    Hi all, I'm tantalizingly close to getting human occlusion working with the user-facing camera, but I've hit a wall with what I suspect is probably a relatively simple shader issue.

    AR Foundation human occlusion stencil censored.png

    Like the author of this excellent thread, I'm blitting the human stencil texture to a material and setting the camera image to a second texture named "_CameraTex" on the same material. The key line in the shader below is line 101 (lightly adapted from ar-foundation-samples HumanStencil.shader). The frag function returns RGB from the camera texture, but returns the stencil's red channel for the alpha channel.

    My intuition is that this should return alpha 0 when the human stencil (second image) is black and alpha 1 when it's red. However, I'm getting that third image, which is successfully returning alpha 0 for all black areas on the stencil texture, but seems to be doing that with the darker areas of _CameraTex, too.

    If someone could point me in the right direction, I'd be very grateful!

    Code (CSharp):
    1. Shader "Unlit/HumanStencil"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Main Texture", 2D) = "black" {} //human stencil, blitted
    6.         _CameraTex ("Camera Texture", 2D) = "black" {}
    7.     }
    8.     SubShader
    9.     {
    10.         Tags
    11.         {
    12.             "Queue" = "Transparent"
    13.             "RenderType" = "Transparent"
    14.             "ForceNoShadowCasting" = "True"
    15.         }
    16.  
    17.         Pass
    18.         {
    19.             Blend SrcAlpha OneMinusSrcAlpha
    20.             Cull Off
    21.             ZTest Always
    22.             ZWrite Off
    23.             Lighting Off
    24.             LOD 100
    25.             Tags
    26.             {
    27.                 "LightMode" = "Always"
    28.             }
    29.  
    30.  
    31.             HLSLPROGRAM
    32.  
    33.             #pragma vertex vert
    34.             #pragma fragment frag
    35.  
    36.             #include "UnityCG.cginc"
    37.  
    38.             #define real half
    39.             #define real3 half3
    40.             #define real4 half4
    41.             #define TransformObjectToHClip UnityObjectToClipPos
    42.  
    43.             #define DECLARE_TEXTURE2D_FLOAT(texture) UNITY_DECLARE_TEX2D_FLOAT(texture)
    44.             #define DECLARE_SAMPLER_FLOAT(sampler)
    45.             #define SAMPLE_TEXTURE2D(texture,sampler,texcoord) UNITY_SAMPLE_TEX2D(texture,texcoord)
    46.  
    47.  
    48.             struct appdata
    49.             {
    50.                 float3 position : POSITION;
    51.                 float2 texcoord : TEXCOORD0;
    52.  
    53.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    54.             };
    55.  
    56.             struct v2f
    57.             {
    58.                 float4 position : SV_POSITION;
    59.                 float2 texcoord : TEXCOORD0;
    60.  
    61.                 UNITY_VERTEX_OUTPUT_STEREO
    62.             };
    63.  
    64.             struct fragment_output
    65.             {
    66.                 real4 color : SV_Target;
    67.             };
    68.  
    69.  
    70.             CBUFFER_START(DisplayRotationPerFrame)
    71.             float4x4 _DisplayRotationPerFrame;
    72.             CBUFFER_END
    73.  
    74.  
    75.             v2f vert (appdata v)
    76.             {
    77.                 v2f o;
    78.  
    79.                 UNITY_SETUP_INSTANCE_ID(v);
    80.                 UNITY_INITIALIZE_OUTPUT(v2f, o);
    81.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
    82.  
    83.                 o.position = TransformObjectToHClip(v.position);
    84.                 o.texcoord = mul(float3(v.texcoord, 1.0f), _DisplayRotationPerFrame).xy;
    85.                 return o;
    86.             }
    87.  
    88.  
    89.             DECLARE_TEXTURE2D_FLOAT(_MainTex);
    90.             DECLARE_SAMPLER_FLOAT(sampler_MainTex);
    91.             DECLARE_TEXTURE2D_FLOAT(_CameraTex);
    92.  
    93.             fragment_output frag (v2f i)
    94.             {
    95.                 UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
    96.  
    97.                 half4 mask = UNITY_SAMPLE_TEX2D(_MainTex, i.texcoord);
    98.                 half4 cam = UNITY_SAMPLE_TEX2D(_CameraTex, i.texcoord);
    99.  
    100.                 fragment_output o;
    101.                 o.color = half4(cam.r, cam.g, cam.b, mask.r);
    102.  
    103.                 return o;
    104.             }
    105.  
    106.             ENDHLSL
    107.         }
    108.     }
    109. }
    110.  
     
    Last edited: Feb 10, 2022
  2. xamidium

    xamidium

    Joined:
    Feb 3, 2021
    Posts:
    2
    Did you ever figure out how to do this? I'm trying to do something similar and would love to see the script you. used to blit the human stencil texture