Search Unity

Depth texture not working on some devices

Discussion in 'Shaders' started by Phantomx, Apr 17, 2015.

  1. Phantomx

    Phantomx

    Joined:
    Oct 30, 2012
    Posts:
    202
    I try to just show the Depth Texture in a post effect, on most devices it's working, but on some devices like HTC One I get only a black texture. Is there another reason than the shader that this is not working on some devices?

    Thanks!

    Shader code:
    Code (CSharp):
    1. Pass{
    2.    
    3.         Tags{
    4.             "Queue"="Geometry"
    5.             "IgnoreProjector"="True"
    6.             "RenderType"="Opaque"
    7.         }
    8.        
    9.         //cull Off
    10.        
    11.         CGPROGRAM
    12.         #pragma vertex vert
    13.         #pragma fragment frag
    14.         #pragma fragmentoption ARB_precision_hint_fastest
    15.         #include "unityCG.cginc"
    16.        
    17.         sampler2D _CameraDepthTexture;
    18.         half4 _MainTex_TexelSize;
    19.         float4 _CurveParams;
    20.  
    21.         struct vertexInput{
    22.             float4 texcoord : TEXCOORD0;
    23.             float4 vertex : POSITION;
    24.         };
    25.        
    26.         struct vertexOutput{
    27.             float4 pos : SV_POSITION;
    28.             float4 texcoord : TEXCOORD0;
    29.         };
    30.        
    31.         vertexOutput vert(vertexInput v) {
    32.             vertexOutput o;
    33.            
    34.             o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
    35.             o.texcoord.xy = v.texcoord.xy;
    36.  
    37.             #if UNITY_UV_STARTS_AT_TOP
    38.             if (_MainTex_TexelSize.y < 0)
    39.                 o.texcoord.xy = o.texcoord.xy * half2(1,-1)+half2(0,1);
    40.             #endif
    41.             return o;
    42.         }
    43.        
    44.        
    45.         fixed4 frag(vertexOutput i) : SV_Target
    46.         {          
    47.             float depth = UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture, i.texcoord));
    48.             depth         = Linear01Depth(depth);
    49.  
    50.                
    51.             return saturate(depth);  
    52.         }
    53.        
    54.         ENDCG
    55.    
    56.    
    57.     }
     
  2. Phantomx

    Phantomx

    Joined:
    Oct 30, 2012
    Posts:
    202
    So, I made my own Depth Texture with a renderWithShader camera. it's working if I set my renderTexture format to ARGB32 however I see a lot of texture banding. All other texture formats will render all white on the device but is fine in the editor.
     
  3. Phantomx

    Phantomx

    Joined:
    Oct 30, 2012
    Posts:
    202
    I think I found the solution. When I render my depth texture manually ( same way unity does it ) it doesn't work. However, if I set my RenderTexture Depth format to 16 bits instead of 24 bits it's working!

    Makes me think that's the issue with DepthTextureMode.Depth, probably rendering in 24bits.
     
  4. BrahimHadriche

    BrahimHadriche

    Joined:
    Nov 26, 2018
    Posts:
    1
    I'm gonna leave this here after days of struggle: If nothing worked, trying using RenderTextureFormat.Shadowmap.