oculus-samples / Unity-DepthAPI

Examples of using Depth API for real-time, dynamic occlusions
Other
179 stars 24 forks source link

Issue / Suggestion #47

Closed Squashi11 closed 1 month ago

Squashi11 commented 1 month ago

Is there any way to make occlusion work with UI elements such as sprites and images etc? It really break off my game. Also any more info on the occluded shadows solution? Thanks

TudorJude commented 1 month ago

Hello,

We plan on adding support for UI in an update. For now, I'll share the solution in this thread and leave it open until I add a sample to show how it works. Regarding the shadow solution, could you please elaborate?

TudorJude commented 1 month ago

One solution on occluding UI would be to add a Quad over your UI, offset by about 0.01 distance in front of it. You then apply a material on that quad that has this shader:

Shader "Meta/Depth/URP/OcclusionCutout"
{
    Properties
    {
        _EnvironmentDepthBias ("Environment Depth Bias", Float) = 0.0
    }

    SubShader
    {
        PackageRequirements
        {
            "com.unity.render-pipelines.universal": "14.0"
        }
        Tags { "RenderType"="Transparent" }
        LOD 100

        Blend Zero SrcAlpha
        ZTest LEqual

        Pass
        {
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "Packages/com.meta.xr.depthapi.urp/Shaders/EnvironmentOcclusionURP.hlsl"
            #pragma multi_compile _ HARD_OCCLUSION SOFT_OCCLUSION

            struct appdata
            {
                float4 vertex : POSITION;

                UNITY_VERTEX_INPUT_INSTANCE_ID
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;

                META_DEPTH_VERTEX_OUTPUT(0)

                UNITY_VERTEX_INPUT_INSTANCE_ID
                UNITY_VERTEX_OUTPUT_STEREO
            };

            float _EnvironmentDepthBias;

            v2f vert (appdata v) {
                v2f o;

                UNITY_SETUP_INSTANCE_ID(v);
                UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);

                o.vertex = TransformObjectToHClip(v.vertex.xyz);

                META_DEPTH_INITIALIZE_VERTEX_OUTPUT(o, v.vertex);

                return o;
            }

            half4 frag (v2f i) : SV_Target {
                UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i)

#if defined(HARD_OCCLUSION) || defined(SOFT_OCCLUSION)
                float occlusionValue = META_DEPTH_GET_OCCLUSION_VALUE(i, _EnvironmentDepthBias);
#else
                float occlusionValue = 1.0f;
#endif

                return half4(0, 0, 0, occlusionValue);
            }
            ENDHLSL
        }
    }
}

Here's how it might look in Unity: image

This solution works well, but you need to have the following in mind when using it:

Squashi11 commented 1 month ago

By the shadows solution I meant shadows from Meta mixed reality toolkit material to be also occluded. I remember they were being worked on

TudorJude commented 1 month ago

I just reached out to the MRUK team. It is being worked on.

Squashi11 commented 1 month ago

Hello, just checked V67 SDK and everything is working nice so far but the UI occlusion shader broke as it was probably suited for the older version

TudorJude commented 1 month ago

Hello,

Yes, you need to make one minor change. It's covered in the new documentation under section 11, when updating custom shaders for v67. You simply have to update this line: #include "Packages/com.meta.xr.depthapi.urp/Shaders/EnvironmentOcclusionURP.hlsl" to #include "Packages/com.meta.xr.sdk.core/Shaders/EnvironmentDepth/URP/EnvironmentOcclusionURP.hlsl"

Squashi11 commented 1 month ago

The solution you provided works as expected thanks