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 :
Shader "Image Effects/Fog of War"
{
Properties
{
_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader
{
Pass
{
ZTest Always
Cull Off
ZWrite Off
Fog { Mode off }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
sampler2D _MainTex;
sampler2D _DepthTexture;
uniform float4x4 _InverseMVP;
uniform float _Ratio;
struct Input
{
float4 position : POSITION;
float2 uv : TEXCOORD0;
};
Input vert (appdata_full v)
{
Input o;
o.position = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = v.texcoord.xy;
return o;
}
float3 CamToWorld (in float2 uv, in float depth)
{
float4 pos = float4(uv.x, uv.y, depth, 1.0);
pos.xyz = pos.xyz * 2.0 - 1.0;
pos = mul(_InverseMVP, pos);
return pos.xyz / pos.w;
}
fixed4 frag (Input i) : COLOR
{
half4 original = tex2D(_MainTex, i.uv);
// return original;
//#if SHADER_API_D3D9||SHADER_API_D3D10||SHADER_API_D3D11
// float2 depthUV = i.uv;
// //depthUV.y = lerp(depthUV.y, 1.0 - depthUV.y, _CamPos.w);
// //depthUV.y = 1.0 - depthUV.y;
// float depth = tex2D(_DepthTexture, depthUV).r;
// float3 pos = CamToWorld(depthUV, depth);
// float4 debug = float4(pos.z*0.05,pos.z*0.05,pos.z*0.05,1);
// return debug;
//#else
float depth = tex2D(_DepthTexture, i.uv).r;
float3 pos = CamToWorld(i.uv, depth);
float4 debug = float4(pos.z*_Ratio,pos.z*_Ratio,pos.z*_Ratio,1);
return debug;
//#endif
}
ENDCG
}
}
Fallback off
}