oculus-samples / Unity-DepthAPI

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

Depth texture similar to Scene Depth? #54

Closed GTD-Carthage closed 1 month ago

GTD-Carthage commented 1 month ago

Hello all! I am hoping to reference the environment depth texture directly via Depth API to achieve certain shaders (namely in Shader Graph) - is there a way to access this and not just the occlusion result? Unfortunately, I am not exactly overly HLSL code inclined, thus trying to read all the includes at the moment is not working well for me.

We are hoping to get something similar to the result of Scene Depth node to control certain effects.

TudorJude commented 1 month ago

Hello,

Try this:

ifndef SHADERGRAPH_PREVIEW

include "Packages/com.meta.xr.sdk.core/Shaders/EnvironmentDepth/URP/EnvironmentOcclusionURP.hlsl"

endif

void GetEnvironmentSceneDepth_float(float3 posWorld, out float environmentDepth) {

ifndef SHADERGRAPH_PREVIEW

const float4 depthSpace = mul(_EnvironmentDepthReprojectionMatrices[unity_StereoEyeIndex], float4(posWorld, 1.0)); const float2 uvCoords = (depthSpace.xy / depthSpace.w + 1.0f) * 0.5f; environmentDepth = SampleEnvironmentDepthLinear(uvCoords);

else

environmentDepth = 0;

endif

}

void GetEnvironmentSceneDepth_half(float3 posWorld, out float environmentDepth) {

ifndef SHADERGRAPH_PREVIEW

const float4 depthSpace = mul(_EnvironmentDepthReprojectionMatrices[unity_StereoEyeIndex], float4(posWorld, 1.0)); const float2 uvCoords = (depthSpace.xy / depthSpace.w + 1.0f) * 0.5f; environmentDepth = SampleEnvironmentDepthLinear(uvCoords);

else

environmentDepth = 0;

endif

}



This will give you the linear depth value at a certain world position.
GTD-Carthage commented 1 month ago

Thank you and for the quick response too! This works perfectly and is exactly what we want!