DistortShader.shader 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. Shader "DistortionShaderPack/DistortShader"
  2. {
  3. Properties
  4. {
  5. _MainColor("Main color", Color) = (0,0,0,1)
  6. _StrengthColor("Color strength", Float) = 1
  7. _DistortionStrength ("Distortion strength", Range(-2,2)) = 0.3
  8. _DistortionCircle ("Distortion circle", Range(0,1)) = 1
  9. _NormalTexture("Normal", 2D) = "blue" { }
  10. _NormalTexStrength("Normal strength", Range(0,1)) = 0
  11. _NormalTexFrameless("Normal circle", Range(0,1)) = 1.0
  12. _UVOffset("UVOffset XY, ignore ZW", Vector) = (0,0.01,0,0)
  13. }
  14. Category
  15. {
  16. Tags { "Queue" = "Transparent" "RenderType" = "Transparent" "IgnoreProjector" = "True" }
  17. Blend SrcAlpha OneMinusSrcAlpha
  18. ZWrite Off
  19. SubShader
  20. {
  21. GrabPass
  22. {
  23. "_GrabTexture"
  24. Name "BASE"
  25. Tags { "LightMode" = "Always" }
  26. }
  27. Pass
  28. {
  29. Name "BASE"
  30. Tags { "LightMode" = "Always" }
  31. CGPROGRAM
  32. #pragma vertex vert
  33. #pragma fragment frag
  34. #include "UnityCG.cginc"
  35. sampler2D _GrabTexture;
  36. float _DistortionStrength;
  37. float _DistortionCircle;
  38. float4 _MainColor;
  39. float _StrengthColor;
  40. sampler2D _NormalTexture;
  41. float4 _NormalTexture_ST;
  42. float _NormalTexStrength;
  43. float _NormalTexFrameless;
  44. float4 _UVOffset;
  45. struct VertexInput
  46. {
  47. float4 vertex : POSITION;
  48. float2 texcoord0 : TEXCOORD0;
  49. };
  50. struct Vert2Frag
  51. {
  52. float4 position : SV_POSITION;
  53. float4 uv_grab : TEXCOORD0;
  54. float2 uv : TEXCOORD1;
  55. float2 uv_normal : TEXCOORD2;
  56. float2 movement: TEXCOORD3;
  57. };
  58. Vert2Frag vert (VertexInput vertIn)
  59. {
  60. Vert2Frag output;
  61. output.position = UnityObjectToClipPos(vertIn.vertex);
  62. output.uv_grab = ComputeGrabScreenPos(output.position);
  63. output.uv = vertIn.texcoord0;
  64. output.uv_normal = vertIn.texcoord0.xy * _NormalTexture_ST.xy + _NormalTexture_ST.zw;
  65. output.movement = _UVOffset.xy*_Time.y;
  66. return output;
  67. }
  68. float2 getVectorFromCenter(float2 uv)
  69. {
  70. float factor = _ScreenParams.y / _ScreenParams.x;
  71. float2 direction = float2((uv.x-0.5)*.5, (uv.y-0.5)) * factor;
  72. return (direction);
  73. }
  74. float getDistortionStrength(float2 uv)
  75. {
  76. float2 diff = float2(distance(0.5, uv.x), distance(0.5, uv.y)) * 2.0;
  77. float dist = saturate(length(diff));
  78. return 1.0-dist;
  79. }
  80. float2 getNormal(sampler2D _NormalTexture, float2 normalUv, float2 uv, float2 uvOffset, float frameless, float strength)
  81. {
  82. float2 normal = tex2D( _NormalTexture, normalUv+uvOffset ).zy;
  83. float length = getDistortionStrength(uv);
  84. float normalTexStrength = ((1-frameless) + frameless*length) * strength;
  85. normal.x = ((normal.x-.5)*2) * normalTexStrength;
  86. normal.y = ((normal.y-.5)*2) * normalTexStrength;
  87. return normal;
  88. }
  89. half4 frag (Vert2Frag fragIn) : SV_Target
  90. {
  91. float4 uvScreen = UNITY_PROJ_COORD(fragIn.uv_grab);
  92. float2 direction = getVectorFromCenter(fragIn.uv);
  93. float strength = getDistortionStrength(fragIn.uv);
  94. strength = (_DistortionCircle*strength + (1-_DistortionCircle)) * _DistortionStrength;
  95. float2 newDirection = direction * strength;
  96. uvScreen += float4(newDirection.x, newDirection.y, 0, 0);
  97. float2 influence = normalize(direction) * strength;
  98. float2 offset = fragIn.movement;
  99. float2 normal = getNormal(_NormalTexture, fragIn.uv_normal, fragIn.uv, offset, _NormalTexFrameless, _NormalTexStrength);
  100. uvScreen += float4(normal.x, normal.y, 0, 0);
  101. influence += normal.xy;
  102. float4 final = tex2Dproj(_GrabTexture, uvScreen);
  103. float alpha = 1;
  104. final = float4(final.xyz, alpha);
  105. strength = saturate(sqrt(pow(abs(influence.x), 2.0) + pow(abs(influence.y), 2.0)) * _StrengthColor);
  106. final = final + (_MainColor*strength);
  107. final.w = saturate(final.w*_MainColor.w);
  108. return final;
  109. }
  110. ENDCG
  111. }
  112. }
  113. }
  114. }