可以做镜头景深效果的shard-2

来源:互联网 发布:mac mail qq企业邮箱 编辑:程序博客网 时间:2024/04/29 22:26
  1. Properties {  
  2.  _MainTex ("", RECT) = "white" {}  
  3.  _BlurTex1 ("", RECT) = "white" {}  
  4.  _BlurTex2 ("", RECT) = "white" {}  
  5.  _DepthTex ("", RECT) = "white" {}  
  6. }  
  7. SubShader {  
  8.  Pass {  
  9.   ZTest Always Cull Off ZWrite Off Fog { Mode off }  
  10. CGPROGRAM  
  11. #pragma fragment frag  
  12. #pragma fragmentoption ARB_precision_hint_fastest  
  13. #include "UnityCG.cginc"   
  14. uniform samplerRECT _MainTex : register(s0);  
  15. uniform samplerRECT _BlurTex1 : register(s1);  
  16. uniform samplerRECT _BlurTex2 : register(s2);  
  17. uniform samplerRECT _DepthTex : register(s3);  
  18. uniform float4 _FocalParams; // x = distance, y = 1/distance, z = range, w = 1/range  
  19. struct v2f {  
  20.  float2 uv[4] : TEXCOORD0;  
  21. };  
  22. float DOFFactor( float z ) {  
  23.  float focalDist = _FocalParams.x;  
  24.  float invRange = _FocalParams.w;  
  25.    
  26.  float fromFocal = z - focalDist;  
  27.  if( fromFocal < 0.0 )  
  28.   fromFocal *= 4.0;  
  29.  return saturate( abs( fromFocal ) * invRange );  
  30. }  
  31. half4 frag (v2f i) : COLOR  
  32. {  
  33.  half4 original = texRECT(_MainTex, i.uv[0]);  
  34.  half3 blur1 = texRECT(_BlurTex1, i.uv[1]).rgb;  
  35.  half3 blur2 = texRECT(_BlurTex2, i.uv[2]).rgb;  
  36.    
  37.  // tint blur levels just for fun!   
  38.  blur1 *= float3(1,1,0.7);  
  39.  blur2 *= float3(0.7,0.7,1);  
  40.    
  41.  float dof = texRECT(_DepthTex, i.uv[3]).r;  
  42.    
  43.  half dof2;  
  44.  if( dof > 0.5 )  
  45.   dof2 = saturate( dof * 0.25 + 0.75 );  
  46.  else  
  47.   dof2 = saturate( dof * 1.5 );  
  48.  half factor = saturate( dof * 1.5 - 0.75 );  
  49.  half3 blur = lerp( blur1, blur2, factor );  
  50.  half3 col = lerp( original.rgb, blur, dof );   
  51.  return half4(col, original.a);  
  52. }  
  53. ENDCG  
  54.  }  
  55. }  
  56. Fallback off