Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug Depth textures are flipped when in VR mode

Discussion in 'VR' started by RogueSun, Jul 31, 2023.

  1. RogueSun

    RogueSun

    Joined:
    Sep 10, 2020
    Posts:
    8
    We are using URP in unity 2021.3.12f1 in flat mode our shaders work fine but in VR mode our custom "ghost" shader is misbehaving. We have narrowed it down using the frame debugger to the left eye using the right eye's depth texture and the right eye is using the left eye's depth texture. meaning that our "ghosts" are being occluded by / showing thoguh objects that are not present on each eye. (pictured in the attachment)

    Is this a known issue or am I doing something oviously wrong?

    upload_2023-7-31_10-29-51.png
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,884
    Did you try 2021.3.29 to check if it was a bug?
    If that doesn't work maybe share the shader if no one knows
     
  3. RogueSun

    RogueSun

    Joined:
    Sep 10, 2020
    Posts:
    8
    wow apparently I didnt press send on my response Doh!

    Yeah we've tested it in 2021.3.29 and its still broken there.

    I've attached the Shader in question :
     

    Attached Files:

  4. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,884
    I'm not great with text based shaders. You could file a bug report, and/or wait for someone else to respond.
    Another solution might be using stencil rendering
     
  5. Shane_Michael

    Shane_Michael

    Joined:
    Jul 8, 2013
    Posts:
    158
    If the buffers are consistently swapped, then you can use the "shader_feature" keyword to create a workaround. Add this to the shader to create a varint:

    Code (CSharp):
    1. #pragma shader_feature _SWAP_DEPTH_BUFFERS
    Then if your main shader body you can do

    Code (CSharp):
    1.  
    2. #if (defined(_SWAP_DEPTH_BUFFERS)
    3.                 float sceneZ = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE_PROJ_SWAP_DEPTH_BUFFERS(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos)));
    4. #else
    5.                 float sceneZ = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos)));
    6. #endif
    7.  
    Then you can search through the built-in shaders to find the definition of SAMPLE_DEPTH_TEXTURE_PROJ to create your own which just swaps the eye buffers before sampling it:

    Code (CSharp):
    1.  
    2. #define SAMPLE_DEPTH_TEXTURE_PROJ_SWAP_DEPTH_BUFFERS(sampler, uv) UNITY_SAMPLE_TEX2DARRAY(sampler, float3((uv).x/(uv).w, (uv).y/(uv).w, (float)(unity_StereoEyeIndex == 1 ? 0 : 1))).r
    3.  
    For now you can activate/deactivate the keyword to turn it on and off, and hopefully remove it entirely once the bug is fixed. Beyond that a simple repro project in a bug report would help get it fixed sooner rather than later.
     
    RogueSun and DevDunk like this.
  6. RogueSun

    RogueSun

    Joined:
    Sep 10, 2020
    Posts:
    8
    Unfortunatly this doesn't work as we are using URP. I'm hoping I can copy the URP package and edit wherever it does the depth texture stuff in the way you have suggested :)
     
  7. Shane_Michael

    Shane_Michael

    Joined:
    Jul 8, 2013
    Posts:
    158
    Ah, I see. Yes, I responded based on the shader you posted which is written using the built-in includes. Using that shader with URP may explain the buffer mismatch. The docs have an example of getting the depth for URP. You will want to include:

    Code (CSharp):
    1. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl"
    And then use SampleSceneDepth() to get the depth, and there is a good chance that will fix your left/right swap problem and not require that workaround at all.

    If you are still having issues with the buffers being swapped, you will have to rewrite the workaround to modify the "UnityStereoTransformScreenSpaceTex" function. Do the same thing to swap the buffers in the "TransformStereoScreenSpaceTex" function where it calculates the offset.

    Code (CSharp):
    1. float4 scaleOffset = unity_StereoScaleOffset[unity_StereoEyeIndex];
    to

    Code (CSharp):
    1. float4 scaleOffset = unity_StereoScaleOffset[unity_StereoEyeIndex == 0 ? 1 : 0];
    You shouldn't need to make your own copy of the URP. Just redefine the function with an appended name in your own shader or include and add a shader_feature like before to toggle using it or the base URP version.
     
    RogueSun likes this.