I unwrapping the mesh vertices to paint on it, and sample the depth texture to determine if the brush is occluded by other objects. But for some reason I get the wrong uv coordinates when sampling the depth texture:

How do I get the correct UV for Depth Texture sampling for an unwrapped mesh?
Shader
Shader "Custom/Renderer World 1"
{
Properties { }
SubShader
{
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "PreviewType"="Plane"}
Cull Off Lighting Off ZWrite Off ZTest Off//Always
Pass
{
BlendOp Add, Add
Blend SrcAlpha OneMinusSrcAlpha, SrcAlpha One
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
float4x4 _Matrix_IVP;
UNITY_DECLARE_DEPTH_TEXTURE(_CameraDepthTexture);
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
float4 worldPos : TEXCOORD1;
float4 screenPos : TEXCOORD2;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = float4(float2(1, _ProjectionParams.x) * (v.uv.xy * float2(2, 2) - float2(1, 1)), 0, 1);
o.uv = v.uv;
o.worldPos = mul(unity_ObjectToWorld, v.vertex);
o.screenPos = ComputeScreenPos(UnityObjectToClipPos(v.vertex));
return o;
}
fixed4 frag (v2f i) : SV_Target
{
float2 screenUV = i.screenPos / i.screenPos.w;
float depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, screenUV);
#if UNITY_REVERSED_Z
if (depth < 0.0001)
return 0;
#else
if (depth > 0.9999)
return 0;
#endif
float4 positionCS = float4(screenUV * 2.0 - 1.0, depth, 1.0);
#if UNITY_UV_STARTS_AT_TOP
// positionCS.y = -positionCS.y;
#endif
float4 hpositionWS = mul(_Matrix_IVP, positionCS);
float3 worldPos = hpositionWS.xyz / hpositionWS.w;
float sceneZ = LinearEyeDepth(depth);
return float4(worldPos, sceneZ);
}
ENDCG
}
}
}
C# Code
var mainCamera = Camera.main;
var projectionMatrix = GL.GetGPUProjectionMatrix(mainCamera.projectionMatrix, false);
var inverseViewProjectionMatrix = (projectionMatrix * mainCamera.worldToCameraMatrix).inverse;
rendererMaterial.SetMatrix("_Matrix_IVP", inverseViewProjectionMatrix);

