Search Unity

Android RenderTexture format depth, sampler failed on android/ios gles.

Discussion in 'Shaders' started by ZorroBlade, Jun 29, 2015.

  1. ZorroBlade

    ZorroBlade

    Joined:
    Jul 7, 2011
    Posts:
    25
    I wanna to use a RenderTexture ,format Depth, on Android/IOS . As I already use lightmap soft in scene., so I cann't use Camera.depthTextureMode = DepthTextureMode.Depth , for that gonna cause a depth texture conflict. So I have to user RenderTexture, format Depth instead .
    So I rendered a debug depth texture, that works well on PC , but faied on mobile device. :( I know that gles use ARB_Format for depth texture, did I sample it wrong?
    I just use "tex2D(_DepthTexture, i.uv).r" .
    my shader :
    Code (CSharp):
    1. Shader "Image Effects/Fog of War"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Base (RGB)", 2D) = "white" {}
    6.     }
    7.     SubShader
    8.     {
    9.         Pass
    10.         {
    11.             ZTest Always
    12.             Cull Off
    13.             ZWrite Off
    14.             Fog { Mode off }
    15.                  
    16. CGPROGRAM
    17. #pragma vertex vert
    18. #pragma fragment frag
    19. #pragma fragmentoption ARB_precision_hint_fastest
    20. #include "UnityCG.cginc"
    21. sampler2D _MainTex;
    22. sampler2D _DepthTexture;
    23.  
    24. uniform float4x4 _InverseMVP;
    25. uniform float _Ratio;
    26.  
    27. struct Input
    28. {
    29.     float4 position : POSITION;
    30.     float2 uv : TEXCOORD0;
    31. };
    32.  
    33. Input vert (appdata_full v)
    34. {
    35.     Input o;
    36.     o.position = mul(UNITY_MATRIX_MVP, v.vertex);
    37.     o.uv = v.texcoord.xy;
    38.     return o;
    39. }
    40.  
    41. float3 CamToWorld (in float2 uv, in float depth)
    42. {
    43.     float4 pos = float4(uv.x, uv.y, depth, 1.0);
    44.     pos.xyz = pos.xyz * 2.0 - 1.0;
    45.     pos = mul(_InverseMVP, pos);
    46.     return pos.xyz / pos.w;
    47. }
    48.  
    49. fixed4 frag (Input i) : COLOR
    50. {
    51.     half4 original = tex2D(_MainTex, i.uv);  
    52. //    return original;
    53. //#if SHADER_API_D3D9||SHADER_API_D3D10||SHADER_API_D3D11
    54. //    float2 depthUV = i.uv;
    55. //    //depthUV.y = lerp(depthUV.y, 1.0 - depthUV.y, _CamPos.w);
    56. //    //depthUV.y = 1.0 - depthUV.y;
    57. //    float depth = tex2D(_DepthTexture, depthUV).r;
    58. //    float3 pos = CamToWorld(depthUV, depth);
    59. //    float4 debug = float4(pos.z*0.05,pos.z*0.05,pos.z*0.05,1);
    60. //    return debug;
    61. //#else
    62.     float depth = tex2D(_DepthTexture, i.uv).r;
    63.     float3 pos = CamToWorld(i.uv, depth);
    64.     float4 debug = float4(pos.z*_Ratio,pos.z*_Ratio,pos.z*_Ratio,1);
    65.     return debug;
    66. //#endif
    67. }
    68.  
    69. ENDCG
    70.         }
    71.     }
    72.     Fallback off
    73. }
     
  2. ZorroBlade

    ZorroBlade

    Joined:
    Jul 7, 2011
    Posts:
    25
  3. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,624
    if attached image is what it should look like then it worked for me if i changed
    sampler2D _DepthTexture;
    to
    sampler2D_float _DepthTexture;
    if it doesnt help, we need more details