Sharpen.shader 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. 
  2. Shader "Hidden/Mirza Beig/Image Effects/Sharpen"
  3. {
  4. Properties
  5. {
  6. _MainTex ("Base (RGB)", 2D) = "white" {}
  7. //_strength ("Strength", Range(0.0, 1.0)) = 1.0
  8. }
  9. SubShader
  10. {
  11. Pass
  12. {
  13. CGPROGRAM
  14. #pragma vertex vert_img
  15. #pragma fragment frag
  16. #include "UnityCG.cginc"
  17. // ...
  18. uniform sampler2D _MainTex;
  19. uniform float _strength;
  20. uniform float _edgeMult;
  21. // ...
  22. float4 frag (v2f_img image) : SV_Target
  23. {
  24. float4 textureColour = tex2D(_MainTex, image.uv);
  25. // x9 because colour is subtracted 8 times as well as the base.
  26. float4 sharpenedTextureColour = textureColour * 9.0f;
  27. // Offset distance proportional to texture size (usually same as screen size).
  28. float4 offset = 1.0f / (_ScreenParams / _edgeMult);
  29. // Begin offsets counter-clockwise.
  30. sharpenedTextureColour -= tex2D(_MainTex, image.uv + float2(offset.x, offset.y)); // Top right.
  31. sharpenedTextureColour -= tex2D(_MainTex, image.uv + float2(0.0f, offset.y)); // Up.
  32. sharpenedTextureColour -= tex2D(_MainTex, image.uv + float2(-offset.x, offset.y)); // Top left.
  33. sharpenedTextureColour -= tex2D(_MainTex, image.uv + float2(-offset.x, 0.0f)); // Left.
  34. sharpenedTextureColour -= tex2D(_MainTex, image.uv + float2(-offset.x, -offset.y)); // Bottom left.
  35. sharpenedTextureColour -= tex2D(_MainTex, image.uv + float2(0.0f, -offset.y)); // Bottom.
  36. sharpenedTextureColour -= tex2D(_MainTex, image.uv + float2(offset.x, -offset.y)); // Bottom right.
  37. sharpenedTextureColour -= tex2D(_MainTex, image.uv + float2(offset.x, 0.0f)); // Right.
  38. // Return mix between original and sharpened.
  39. return lerp(textureColour, sharpenedTextureColour, _strength);
  40. }
  41. ENDCG
  42. }
  43. }
  44. }