HardSurfaceProScreenSpaceRefraction.cginc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  2. // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
  3. //Hard Surface Shader Package, Written for the Unity engine by Bruno Rime: http://www.behance.net/brunorime brunorime@gmail.com
  4. #ifndef HARD_SURFACE_PRO_SCREEN_SPACE_REFRACTION_INCLUDED
  5. #define HARD_SURFACE_PRO_SCREEN_SPACE_REFRACTION_INCLUDED
  6. struct v2f
  7. {
  8. float4 pos : SV_POSITION;
  9. float3 viewDir : TEXCOORD0;
  10. float2 uv_BumpMap : TEXCOORD1;
  11. float3 TtoV0 : TEXCOORD2;
  12. float3 TtoV1 : TEXCOORD3;
  13. float4 GPscreenPos : TEXCOORD4;
  14. float3 vertworldpos : TEXCOORD5;
  15. };
  16. // define variables
  17. sampler2D _BumpMap;
  18. sampler2D _GrabTexture;
  19. float4 _BumpMap_ST;
  20. fixed _FrezPow;
  21. fixed _FrezFalloff;
  22. fixed _Metalics;
  23. fixed4 _Color;
  24. fixed _Reflection;
  25. fixed _RefScale;
  26. half _Density;
  27. v2f vert (appdata_tan v)
  28. {
  29. v2f o;
  30. o.pos = UnityObjectToClipPos (v.vertex);
  31. o.vertworldpos = mul(unity_ObjectToWorld, v.vertex).xyz;
  32. o.uv_BumpMap = TRANSFORM_TEX( v.texcoord, _BumpMap );
  33. o.GPscreenPos.xy = (float2(o.pos.x, o.pos.y) + o.pos.w) * 0.5;
  34. o.GPscreenPos.zw = o.pos.zw;
  35. TANGENT_SPACE_ROTATION;
  36. o.viewDir.xyz = mul( rotation, ObjSpaceViewDir( v.vertex ) );
  37. o.TtoV0 = mul(rotation, UNITY_MATRIX_IT_MV[0].xyz);
  38. o.TtoV1 = mul(rotation, UNITY_MATRIX_IT_MV[1].xyz);
  39. return o;
  40. }
  41. half4 frag( v2f IN ) : COLOR
  42. {
  43. #ifdef HardsurfaceNormal
  44. fixed3 Bumpnormal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
  45. #else
  46. fixed3 Bumpnormal = fixed3(0,0,1);
  47. #endif
  48. //screenspaced normal direction
  49. half2 vn;
  50. vn.x = dot(IN.TtoV0, Bumpnormal);
  51. vn.y = dot(IN.TtoV1, Bumpnormal);
  52. #ifdef SHADER_API_D3D9
  53. vn.y *= -_ProjectionParams.x;
  54. #endif
  55. // calculates VeiwAngle falloff, distance to vert, and base blend for realtime relection
  56. float3 viewVect = _WorldSpaceCameraPos - IN.vertworldpos;
  57. half dist = length (viewVect);
  58. half refscale = min (1/dist,dist/1);
  59. IN.viewDir = normalize(IN.viewDir);
  60. half SurfAngle= dot(IN.viewDir,Bumpnormal);
  61. SurfAngle *= (1 - _Density);
  62. _Density *= 20;
  63. // screen space coords for capture
  64. fixed2 grabTexcoord = IN.GPscreenPos.xy / IN.GPscreenPos.w;
  65. #ifdef SHADER_API_D3D9
  66. grabTexcoord.y = 1-grabTexcoord.y;
  67. #endif
  68. vn = vn * (1-SurfAngle) * -_Density * refscale;
  69. half2 screenedges = abs((saturate(grabTexcoord + vn)) * 2 - 1);
  70. screenedges = 1 - (screenedges * screenedges ) ;
  71. vn = vn * screenedges;
  72. fixed3 refAberationColor;
  73. #ifdef ShaderModel3
  74. half2 vnr = vn * .96;
  75. half2 vng = vn * .98;
  76. refAberationColor.r = tex2D(_GrabTexture,grabTexcoord+vnr).r;
  77. refAberationColor.g = tex2D(_GrabTexture,grabTexcoord+vng).g;
  78. refAberationColor.b = tex2D(_GrabTexture,grabTexcoord+vn).b;
  79. #else
  80. refAberationColor = tex2D(_GrabTexture,grabTexcoord+vn).rgb;
  81. #endif
  82. return float4 (refAberationColor,1);
  83. }
  84. #endif