Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - zorroMask

Pages: [1]
1
Other Packages / Re: Tasharen Fog of War
« on: June 26, 2015, 02:34:52 AM »
     I render my scene with a shadowmap, so I can't use the camera depthTextureMode = DepthTextureMode.Depth , cause there gonna be a depth texture conflict . So I replace the fragment shader _CameraDepthTexture , with a RenderTexture , format depth. That RenderTexture i create as an asset . The fragement shader runs well in Editor, but failed in android/ios gles rendering. I know that gles depth texture use ARB format , but how can i sample the depth texture?I try UnityCG.cginc micro ,like SAMPLE_DEPTH_TEXTURE, but that is included in HLSLSupport.cginc , cause some problem in my IOS rendering . I used unity3d 5.1.1 .


  1. Shader "Image Effects/Fog of War"
  2. {
  3.         Properties
  4.         {
  5.                 _MainTex ("Base (RGB)", 2D) = "white" {}
  6.                 _FogTex0 ("Fog 0", 2D) = "white" {}
  7.                 _FogTex1 ("Fog 1", 2D) = "white" {}
  8.                 _Unexplored ("Unexplored Color", Color) = (0.05, 0.05, 0.05, 0.05)
  9.                 _Explored ("Explored Color", Color) = (0.35, 0.35, 0.35, 0.35)
  10.         }
  11.         SubShader
  12.         {
  13.                 Pass
  14.                 {
  15.                         ZTest Always
  16.                         Cull Off
  17.                         ZWrite Off
  18.                         Fog { Mode off }
  19.                                        
  20. CGPROGRAM
  21. #pragma vertex vert
  22. #pragma fragment frag
  23. #pragma fragmentoption ARB_precision_hint_fastest
  24. #include "UnityCG.cginc"
  25.  
  26. sampler2D _MainTex;
  27. sampler2D _FogTex0;
  28. sampler2D _FogTex1;
  29. sampler2D _DepthTexture;
  30.  
  31. uniform float4x4 _InverseMVP;
  32. uniform float4 _Params;
  33. uniform float4 _CamPos;
  34. uniform half4 _Unexplored;
  35. uniform half4 _Explored;
  36.  
  37. struct Input
  38. {
  39.         float4 position : POSITION;
  40.         float2 uv : TEXCOORD0;
  41. };
  42.  
  43. Input vert (appdata_full v)
  44. {
  45.         Input o;
  46.         o.position = mul(UNITY_MATRIX_MVP, v.vertex);
  47.         o.uv = v.texcoord.xy;
  48.         return o;
  49. }
  50.  
  51. float3 CamToWorld (in float2 uv, in float depth)
  52. {
  53.         float4 pos = float4(uv.x, uv.y, depth, 1.0);
  54.         pos.xyz = pos.xyz * 2.0 - 1.0;
  55.         pos = mul(_InverseMVP, pos);
  56.         return pos.xyz / pos.w;
  57. }
  58.  
  59. fixed4 frag (Input i) : COLOR
  60. {
  61.         half4 original = tex2D(_MainTex, i.uv);  
  62. //      return original;
  63. #if SHADER_API_D3D9||SHADER_API_D3D10||SHADER_API_D3D11
  64.         float2 depthUV = i.uv;  
  65.         //depthUV.y = lerp(depthUV.y, 1.0 - depthUV.y, _CamPos.w);
  66.         //depthUV.y = 1.0 - depthUV.y;
  67.         float depth = tex2D(_DepthTexture, depthUV).r;
  68.         float3 pos = CamToWorld(depthUV, depth);
  69. #else
  70.         float depth = tex2D(_DepthTexture, i.uv).r;
  71.         float3 pos = CamToWorld(i.uv, depth);
  72. #endif
  73.        
  74.         // Limit the fog of war to sea level
  75.         if (pos.y < 0.0)
  76.         {
  77.                 // This is a simplified version of the ray-plane intersection formula: t = -( N.O + d ) / ( N.D )
  78.                 float3 dir = normalize(pos - _CamPos.xyz);
  79.                 pos = _CamPos.xyz - dir * (_CamPos.y / dir.y);
  80.         }
  81.        
  82.         float2 uv = pos.xz * _Params.z + _Params.xy;
  83.         half4 fog = lerp(tex2D(_FogTex0, uv), tex2D(_FogTex1, uv), _Params.w);
  84.        
  85.         return lerp(lerp(original * _Unexplored, original * _Explored, fog.g), original, fog.r);
  86. }
  87.  

Pages: [1]