Can't get mesh mask filters to work
Closed this issue · 5 comments
AssertionException: Shader named 'Meta/EnvironmentDepth/DepthMask' wasn't found in Resources folder.
Assertion failure. Value was Null
Expected: Value was not Null
UnityEngine.Assertions.Assert.Fail (System.String message, System.String userMessage) (at <0652bf0e14024522b5c92574ad0ef550>:0)
UnityEngine.Assertions.Assert.IsNotNull (UnityEngine.Object value, System.String message) (at <0652bf0e14024522b5c92574ad0ef550>:0)
UnityEngine.Assertions.Assert.IsNotNull[T] (T value, System.String message) (at <0652bf0e14024522b5c92574ad0ef550>:0)
Meta.XR.EnvironmentDepth.EnvironmentDepthManager+Mask..ctor (System.Int32 width, System.Int32 height, System.Single bias) (at ./Library/PackageCache/com.meta.xr.sdk.core@71.0.0/Scripts/EnvironmentDepth/EnvironmentDepthManager.cs:410)
Meta.XR.EnvironmentDepth.EnvironmentDepthManager.TryFetchDepthTexture (UnityEngine.Matrix4x4 trackingSpaceWorldToLocal) (at ./Library/PackageCache/com.meta.xr.sdk.core@71.0.0/Scripts/EnvironmentDepth/EnvironmentDepthManager.cs:369)
Meta.XR.EnvironmentDepth.EnvironmentDepthManager.Update () (at ./Library/PackageCache/com.meta.xr.sdk.core@71.0.0/Scripts/EnvironmentDepth/EnvironmentDepthManager.cs:296)
I'm getting this error when I try to send a mesh filter to the EnvironmentDepthManager. Im using everything from meta's all in one sdk. Ive included URP shaders from the git url mentioned in this repo. Nowhere on the internet I could find this issue or this DepthMask shader. Please help!
Hello,
This feature was just delivered in v71 but unfortunately we mistakenly didn't push the preprocessing shader to production. We caught this quite late, we'll update the docs with known issues and v72 will have the fix once it lands sometime next month. In the meantime, I'll share the solution there for v71. The feature is looking for a specific shader in the Resources folder, so simply create a shader file and place this code inside it :
Shader "Meta/EnvironmentDepth/DepthMask"
{
SubShader
{
Pass
{
Name "Pass 0" // write mask's depth to the texture
ColorMask R
ZWrite True
LOD 200
CGPROGRAM
#pragma target 4.5
#pragma vertex vert
#pragma fragment frag
float4x4 _DepthMask_MVP_Matrices[2];
float4 _EnvironmentDepthZBufferParams;
float _MaskBias;
struct appdata
{
float4 vertex : POSITION;
uint instanceId : SV_InstanceID;
};
struct v2f
{
float4 positionCS : SV_POSITION;
uint depthSlice : SV_RenderTargetArrayIndex;
};
v2f vert(const appdata i)
{
v2f o;
o.positionCS = mul(_DepthMask_MVP_Matrices[i.instanceId], float4(i.vertex.xyz, 1.0f));
o.depthSlice = i.instanceId;
return o;
}
float ToLinearDepth(const float nonLinearDepth)
{
const float ndcDepth = nonLinearDepth * 2.0f - 1.0f;
return 1.0f / (ndcDepth + _EnvironmentDepthZBufferParams.y) * _EnvironmentDepthZBufferParams.x;
}
float ToNonLinearDepth(const float linearDepth)
{
const float ndcDepth = _EnvironmentDepthZBufferParams.x / linearDepth - _EnvironmentDepthZBufferParams.y;
return ndcDepth * 0.5f + 0.5f;
}
float4 frag(const v2f i) : SV_Target
{
float depth = i.positionCS.z;
#if defined(UNITY_REVERSED_Z)
depth = 1.0f - depth;
#endif
const float linearDepthWithBias = ToLinearDepth(depth) * (1.0f - _MaskBias);
return float4(ToNonLinearDepth(linearDepthWithBias), 0, 0, 0);
}
ENDCG
}
Pass
{
Name "Pass 1" // combine the mask's depth from previous pass with the envDepthTexture
ColorMask R
ZWrite Off
CGPROGRAM
#pragma target 4.5
#pragma vertex vert
#pragma fragment frag
Texture2DArray_float _EnvironmentDepthTexture;
SamplerState sampler_EnvironmentDepthTexture;
Texture2DArray_float _MaskTexture;
SamplerState sampler_MaskTexture;
struct Attributes
{
uint vertexId : SV_VertexID;
uint instanceId : SV_InstanceID;
};
struct Varyings
{
float4 positionCS : SV_POSITION;
float2 uv : TEXCOORD0;
uint depthSlice : SV_RenderTargetArrayIndex;
};
Varyings vert(const Attributes input)
{
Varyings output;
// see GetFullScreenTriangleTexCoord() and GetFullScreenTriangleVertexPosition() in Common.hlsl
const uint vertexID = input.vertexId;
const float2 uv = float2((vertexID << 1) & 2, vertexID & 2);
output.uv = float2(uv.x, 1.0 - uv.y);
output.positionCS = float4(uv * 2.0 - 1.0, 1.0, 1.0);
output.depthSlice = input.instanceId;
return output;
}
float4 frag(const Varyings input) : SV_Target
{
float3 uv = float3(input.uv, input.depthSlice);
const float maskDepth = _MaskTexture.Sample(sampler_MaskTexture, uv);
const float envDepth = _EnvironmentDepthTexture.Sample(sampler_EnvironmentDepthTexture, uv);
const float depth = maskDepth < envDepth ? 1.0f : envDepth;
return float4(depth, 0, 0, 0);
}
ENDCG
}
}
}
We're sorry for the mix-up, once this feature is fixed in v72, we'll deliver a sample to show a common use-case as well.
Thank you so much! I spent hours looking through docs again and again and trying to find that shader. I just tried it and it works great. Can't wait for v72. Also one more thing while I have you here, is there any utility prefab available using which I can see the grayscale processed depth map like its shown in this video? https://developers.meta.com/horizon/documentation/unity/unity-depthapi-occlusions-advanced-usage#using-the-depth-mask-feature
It'll be part of the sample update in v72. Until then, here's the shader. Just create a material with it and place it on a quad:
Shader "Unlit/ShowDepthMap"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
Texture2DArray_half _PreprocessedEnvironmentDepthTexture;
SamplerState sampler_PreprocessedEnvironmentDepthTexture;
Texture2DArray_half _EnvironmentDepthTexture;
SamplerState sampler_EnvironmentDepthTexture;
struct Attribures
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct Interpolators
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
UNITY_VERTEX_OUTPUT_STEREO
};
sampler2D _MainTex;
float4 _MainTex_ST;
Interpolators vert (Attribures v)
{
Interpolators o;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_OUTPUT(Interpolators, o);
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
return o;
}
//UNITY_DECLARE_SCREENSPACE_TEXTURE(_MainTex);
fixed4 frag (Interpolators i) : SV_Target
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
float3 uv = float3(i.uv, 0);//just show one texture so we don't get eye strain
fixed4 col = _PreprocessedEnvironmentDepthTexture.Sample(sampler_PreprocessedEnvironmentDepthTexture, uv);
//Uncomment the line below and comment the line above to change between the preprocessed depth texture and the raw depth texture
//fixed4 col = _EnvironmentDepthTexture.Sample(sampler_EnvironmentDepthTexture, uv);
col.a = 1;
return col;
}
ENDCG
}
}
}
I don't suppose you could post a shader that works in URP as well? Unless that one should and I simply screwed up implementation.
This shader works for both URP and BiRP. v72 is finally out and it has the fix, so simply updating your core package will now just have the shader in there. This repo will receive an update soon to v72 and will contain an example scene on how to use the feature.
Edit: v72 is out now and contains the shader in core sdk. Updating to this version no longer requires the use of the workaround highlighted in this thread. Samples and documentation have been updated with an example scene utilizing Depth Mask. If you still have questions, I recommend starting a new issue rather than posting here, visibility is better that way.