Search Unity

Question AR Occlusion Environment + ARPlane Flickering. How to fix it ?

Discussion in 'AR' started by Makio64, Nov 15, 2020.

  1. Makio64

    Makio64

    Joined:
    Nov 9, 2014
    Posts:
    27
    Hi,

    im currently using the AR Plane Manager to show the shadow of my object and would love to use the AR Occlusion environment but because of the depth the ARPlane keep flicker as its logically at an equal depth..

    Any clues on how to fix this ?
    I was thinking to 2 ways :
    - force the ARPlane to be y+0.01 to fix some problem
    - not apply the Environment Occlusion to the ARPlane, but not sure how we can exclude it.

    Thanks in advance.
     
  2. eliamarca

    eliamarca

    Joined:
    Nov 1, 2020
    Posts:
    2
    I have the same problem and I can't find any solution. Hope for an answer. Thanks
     
    Makio64 likes this.
  3. KyryloKuzyk

    KyryloKuzyk

    Joined:
    Nov 4, 2013
    Posts:
    1,142
    This problem is called Z-Fighting and the common solution is to write a custom shader with the Offset command.
    Here are two great videos about Z-Fighting.
    http://answers.unity.com/answers/51144/view.html

    I took the FeatheredPlaneShader from AR Foundation Samples and added an Offset command to it. While typically Z-Fighting can be fixed by adding Offset -1, -1, I only managed to get it working with ARKit by setting factor and scale to -50.

    Here is the resulted shader.
    Code (CSharp):
    1. Shader "Unlit/FeatheredPlaneShader"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.         _TexTintColor("Texture Tint Color", Color) = (1,1,1,1)
    7.         _PlaneColor("Plane Color", Color) = (1,1,1,1)
    8.     }
    9.     SubShader
    10.     {
    11.         Tags { "RenderType"="Transparent" "Queue"="Transparent" }
    12.         LOD 100
    13.         Blend SrcAlpha OneMinusSrcAlpha
    14.         ZWrite Off
    15.         Offset -50, -50
    16.  
    17.         Pass
    18.         {
    19.             CGPROGRAM
    20.             #pragma vertex vert
    21.             #pragma fragment frag
    22.          
    23.             #include "UnityCG.cginc"
    24.  
    25.             struct appdata
    26.             {
    27.                 float4 vertex : POSITION;
    28.                 float2 uv : TEXCOORD0;
    29.                 float3 uv2 : TEXCOORD1;
    30.             };
    31.  
    32.             struct v2f
    33.             {
    34.                 float4 vertex : SV_POSITION;
    35.                 float2 uv : TEXCOORD0;
    36.                 float3 uv2 : TEXCOORD1;
    37.             };
    38.  
    39.             sampler2D _MainTex;
    40.             float4 _MainTex_ST;
    41.             fixed4 _TexTintColor;
    42.             fixed4 _PlaneColor;
    43.             float _ShortestUVMapping;
    44.  
    45.             v2f vert (appdata v)
    46.             {
    47.                 v2f o;
    48.                 o.vertex = UnityObjectToClipPos(v.vertex);
    49.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    50.                 o.uv2 = v.uv2;
    51.                 return o;
    52.             }
    53.          
    54.             fixed4 frag (v2f i) : SV_Target
    55.             {
    56.                 //return 1;
    57.                 fixed4 col = tex2D(_MainTex, i.uv) * _TexTintColor;
    58.                 col = lerp( _PlaneColor, col, col.a);
    59.                 // Fade out from as we pass the edge.
    60.                 // uv2.x stores a mapped UV that will be "1" at the beginning of the feathering.
    61.                 // We fade until we reach at the edge of the shortest UV mapping.
    62.                 // This is the remmaped UV value at the vertex.
    63.                 // We choose the shorted one so that ll edges will fade out completely.
    64.                 // See ARFeatheredPlaneMeshVisualizer.cs for more details.
    65.                 col.a *=  1-smoothstep(1, _ShortestUVMapping, i.uv2.x);
    66.                 return col;
    67.             }
    68.             ENDCG
    69.         }
    70.     }
    71. }
    72.  
     
    Last edited: Nov 18, 2020
    Makio64 and eliamarca like this.
  4. eliamarca

    eliamarca

    Joined:
    Nov 1, 2020
    Posts:
    2
    I tried to force the ARPlane to be at an higher y. In the ARPlaneManager script, add two lines of code.
    Code (CSharp):
    1.         protected override void OnAfterSetSessionRelativeData(
    2.             ARPlane plane,
    3.             BoundedPlane sessionRelativeData)
    4.         {
    5.             ARPlane subsumedByPlane;
    6.             if (m_Trackables.TryGetValue(sessionRelativeData.subsumedById, out subsumedByPlane))
    7.             {
    8.                 plane.subsumedBy = subsumedByPlane;
    9.             }
    10.             else
    11.             {
    12.                 plane.subsumedBy = null;
    13.             }
    14.             // Here is the workaround
    15.             Vector3 temp = new Vector3(0.0f, 0.01f, 0.0f);
    16.             plane.transform.position += temp;
    17.             // End workaroud
    18.             plane.UpdateBoundary(subsystem);
    19.         }
    You can do the same using translation.
    Now the floor no more flickers, but i think this could not be the solution... it's only a workaround.
     
    Makio64 likes this.
  5. Makio64

    Makio64

    Joined:
    Nov 9, 2014
    Posts:
    27
    Thanks the Offset in my differents ground shaders made the tricks ! <3
     
    KyryloKuzyk likes this.
  6. areavisuale

    areavisuale

    Joined:
    Jul 23, 2015
    Posts:
    60
    Hi, I just tested the environment occlusion with iPhone 12 Pro, and I noticed this problem. The texture I gave to the tracked planes flickers, but also the shadows of my instantiated objects.
    I tried to modify the FeatheredPlaneShader but I noticed no difference. Could you explain me your solution? Thanks!
     
  7. Makio64

    Makio64

    Joined:
    Nov 9, 2014
    Posts:
    27
    Hi, I just added the Offset in my shader :)
    Check the video above for more details explanation!

    Best,
    D
     
  8. areavisuale

    areavisuale

    Joined:
    Jul 23, 2015
    Posts:
    60
    I added an offset to my shaders, but if the offset is small it continues to flicker, if is high the occlusion doesn't work.
     
  9. kidsfab

    kidsfab

    Joined:
    Oct 16, 2018
    Posts:
    19
    For anyone using Shader Graph and willing to use the Offset method told by @KirillKuzyk to fix the z fight problem with AR occlusion, such as on the BlurredShadowPlane shader available here, you can add to the Position Input of the PBR Master, the following fix from @bgolus instead of "Object Position".
    I personally set the offset property to 0.06 but you might want to experiment on that one.
     
  10. baibhavbista

    baibhavbista

    Joined:
    Jul 4, 2021
    Posts:
    1
    Hey can you provide the shader after making the adjustments?
     
    masai2k likes this.
  11. TheKakarin

    TheKakarin

    Joined:
    Jul 28, 2017
    Posts:
    3
    Moving offset to -50,-50 works fine but this comes with a problem. I'm having a plane at (0,0) then I'm adding a model with it's base point at (0,0) and it seems that the plane cuts into the model proportional with the offset value and this is ugly. Any idea how we can fix this?

    Screenshot 2022-02-03 at 10.40.57.png
     
  12. DigitalBeach

    DigitalBeach

    Joined:
    Jan 17, 2015
    Posts:
    37
    Agreed. I have the same problem. Offset just makes the problem move up. We really kind of want the detected ground plane not to be considered part of the occlusion. If we could set the ground level in the AR Occlusion control based on where user places object or something it would work. Or the occlusion planes that are horizontal should be able to be pushed back a bit. 0.01 or such.
     
    EthanFischer likes this.
  13. DigitalBeach

    DigitalBeach

    Joined:
    Jan 17, 2015
    Posts:
    37
    What would be helpful is to have access to the material/shader used to draw the Z buffer for the occlusion. We could make that a shadow catcher so that at least we get stable shadows on it.
     
    EthanFischer likes this.
  14. EthanFischer

    EthanFischer

    Joined:
    Feb 10, 2016
    Posts:
    46
    Has anyone found a solution for this? In my case the shadows we are rendering on the floor get occluded. I tried moving things up a bit, but the amount I have to move up for it to stop occluding the shadows is too high that they don't look like they're on the floor anymore
     
    Greken likes this.
  15. Greken

    Greken

    Joined:
    Nov 16, 2012
    Posts:
    9
    I would also like to know if anyone has a solution for this issue.