Hi, I’m encountering issues with the depth texture of my second Camera. I’m currently trying to get the Z-map of my second camera and use it in a custom shader, however I’m not able to find a way in order to realize this.
I tried to use the “_LastCameraDepthTexture” like this :
Shader "RenderDepth" {
SubShader
{
Pass
{
cull off
ZWrite On
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _LastCameraDepthTexture;
v2f_img vert(appdata_img v)
{
return vert_img(v);
}
float4 frag(v2f_img i) : COLOR
{
float depth = tex2D(_LastCameraDepthTexture, i.uv.xy);
depth = Linear01Depth(depth);
return float4(depth, depth, depth,1);
}
ENDCG
}
}
FallBack "Diffuse"
}
I tried to make a render texture with the depth buffer only :
using UnityEngine;
using System.Collections;
using UnityEngine.Rendering;
[RequireComponent(typeof(Camera))]
public class DepthCameraManager : MonoBehaviour
{
[SerializeField] private RenderTexture target = null;
[SerializeField] private RenderTexture targetDepth = null;
[SerializeField] private Vector2Int _rangeWithHeight = new Vector2Int(256, 256);
[SerializeField] private Material _mat;
void Start()
{
Camera camera = GetComponent<Camera>();
camera.depthTextureMode = camera.depthTextureMode | DepthTextureMode.Depth;
target = new RenderTexture(_rangeWithHeight.x, _rangeWithHeight.y, 0, RenderTextureFormat.Default);
targetDepth = new RenderTexture(_rangeWithHeight.x, _rangeWithHeight.y, 24, RenderTextureFormat.Depth);
camera.SetTargetBuffers(target.colorBuffer, targetDepth.depthBuffer);
_mat.SetTexture("_DepthTexture", targetDepth);
}
}
I’m pretty sure I’m missing something, maybe some camera parameters or render texture parameters. Perhaps I need to make a blit in a custom render pass. But I don’t find any clues. I’m currently using Unity URP 2022.3. Thanks for your help, I can provide you more details if needed.