Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Get Z-Map of another Camera

Discussion in 'Shaders' started by iesnihS_reaL, May 10, 2024.

  1. iesnihS_reaL

    iesnihS_reaL

    Joined:
    Jan 13, 2021
    Posts:
    1
    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 :

    Code (CSharp):
    1. Shader "RenderDepth" {
    2.  
    3.     SubShader
    4.     {
    5.         Pass
    6.         {
    7.             cull off
    8.             ZWrite On
    9.             CGPROGRAM
    10.             #pragma vertex vert
    11.             #pragma fragment frag
    12.             #include "UnityCG.cginc"      
    13.              
    14.             sampler2D _LastCameraDepthTexture;
    15.             v2f_img vert(appdata_img v)
    16.             {
    17.                 return vert_img(v);
    18.             }
    19.             float4 frag(v2f_img i) : COLOR
    20.             {
    21.                 float depth = tex2D(_LastCameraDepthTexture, i.uv.xy);
    22.                 depth = Linear01Depth(depth);
    23.                 return float4(depth, depth, depth,1);
    24.             }
    25.             ENDCG
    26.         }
    27.     }
    28.         FallBack "Diffuse"
    29. }
    I tried to make a render texture with the depth buffer only :
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Rendering;
    4. [RequireComponent(typeof(Camera))]
    5. public class DepthCameraManager : MonoBehaviour
    6. {
    7.     [SerializeField] private RenderTexture target = null;
    8.     [SerializeField] private RenderTexture targetDepth = null;
    9.     [SerializeField] private Vector2Int _rangeWithHeight = new Vector2Int(256, 256);
    10.     [SerializeField] private Material _mat;
    11.  
    12.     void Start()
    13.     {
    14.         Camera camera = GetComponent<Camera>();
    15.         camera.depthTextureMode = camera.depthTextureMode | DepthTextureMode.Depth;
    16.  
    17.         target = new RenderTexture(_rangeWithHeight.x, _rangeWithHeight.y, 0, RenderTextureFormat.Default);
    18.         targetDepth = new RenderTexture(_rangeWithHeight.x, _rangeWithHeight.y, 24, RenderTextureFormat.Depth);
    19.        
    20.         camera.SetTargetBuffers(target.colorBuffer, targetDepth.depthBuffer);
    21.  
    22.         _mat.SetTexture("_DepthTexture", targetDepth);
    23.     }
    24. }
    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.