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 DepthOnly shader does not render into my secondary render texture

Discussion in 'Shaders' started by Kaamoo, Mar 2, 2024.

  1. Kaamoo

    Kaamoo

    Joined:
    Dec 13, 2022
    Posts:
    3
    This is the situtation:
    In the scene I have two objects, one is with default shader, the other one is using my own shader.
    This shader is quite simple: I set its LightMode to DepthOnly to just render its depth.
    Code (CSharp):
    1. SubShader
    2.     {
    3.         ZWrite On
    4.         ZTest LEqual
    5.         Cull Front
    6.      
    7.         Tags
    8.         {
    9.             "RenderType"="Opaque"
    10.             "Queue"="Geometry"
    11.         }
    12.         LOD 100
    13.  
    14.         Pass
    15.         {
    16.             Tags
    17.             {
    18.                [B] "LightMode" = "DepthOnly"[/B]
    19.             }
    20.             HLSLPROGRAM
    21.             #pragma vertex vert
    22.             #pragma fragment frag
    23.             #pragma multi_compile_shadowcaster
    24.  
    25.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    26.  
    27.             struct appdata
    28.             {
    29.                 float4 vertex : POSITION;
    30.                 float2 uv : TEXCOORD0;
    31.             };
    32.  
    33.             struct v2f
    34.             {
    35.                 float2 uv : TEXCOORD0;
    36.                 float4 vertex : SV_POSITION;
    37.             };
    38.  
    39.             sampler2D _MainTex;
    40.             float4 _MainTex_ST;
    41.  
    42.             v2f vert(appdata v)
    43.             {
    44.                 v2f o;
    45.                 o.vertex = TransformObjectToHClip(v.vertex);
    46.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    47.                 return o;
    48.             }
    49.  
    50.             float4 frag(v2f i) : SV_Target
    51.             {
    52.                 return float4(1, 0, 0, 0);
    53.             }
    54.             ENDHLSL
    55.         }
    56.     }
    In my own render feature, I display the result as a kind of post process.
    upload_2024-3-2_17-39-58.png
    The result is quite normal in the _CameraDepthTexture: all of them are rendered
    upload_2024-3-2_17-24-19.png

    But I used another camera to render a secondary depth texture, the left object just disappeared.
    Code (CSharp):
    1. private void Awake()
    2.         {
    3.             _camera = GetComponent<Camera>();
    4.             _depthTexture = new RenderTexture(Width, Height, 24, [B]RenderTextureFormat.Depth[/B]);
    5.             _depthTexture.Create();
    6.             _camera.depthTextureMode = DepthTextureMode.Depth;
    7.             _camera.targetTexture = _depthTexture;
    8.         }
    9.     }
    (From the view of my secondary camera)
    upload_2024-3-2_17-24-57.png
    These two objects are in the same layer so the culling mask should have no influence on them.

    If I set the LightMode to UniversalForward, there is no problem with my secondary depth texture:
    upload_2024-3-2_17-26-7.png

    But what is very strange is that in the original _CameraDepthTexture the object disappeared again:
    upload_2024-3-2_17-28-4.png

    I figure the problem is related to the LightMode tag, but I have no idea about why these two depth texture does not behave the same. My assumption is that all of these two objects can be rendered normally with either DepthOnly or UniversalForward tag because I just sampled these two depth texture.