Post Outline.shader 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  2. Shader "Outlined/Post Outline"
  3. {
  4. Properties
  5. {
  6. _MainTex("Main Texture",2D) = "black"{}
  7. _SceneTex("Scene Texture",2D) = "black"{}
  8. }
  9. SubShader
  10. {
  11. Pass
  12. {
  13. CGPROGRAM
  14. sampler2D _MainTex;
  15. //_TexelSize is a float2 that says how much screen space a texel occupies.
  16. float2 _MainTex_TexelSize;
  17. #pragma vertex vert
  18. #pragma fragment frag
  19. #include "UnityCG.cginc"
  20. struct v2f
  21. {
  22. float4 pos : SV_POSITION;
  23. float2 uvs : TEXCOORD0;
  24. };
  25. v2f vert(appdata_base v)
  26. {
  27. v2f o;
  28. //Despite the fact that we are only drawing a quad to the screen, Unity requires us to multiply vertices by our MVP matrix, presumably to keep things working when inexperienced people try copying code from other shaders.
  29. o.pos = UnityObjectToClipPos(v.vertex);
  30. //Also, we need to fix the UVs to match our screen space coordinates. There is a Unity define for this that should normally be used.
  31. o.uvs = o.pos.xy / 2 + 0.5;
  32. return o;
  33. }
  34. half frag(v2f i) : COLOR
  35. {
  36. //arbitrary number of iterations for now
  37. int NumberOfIterations = 20;
  38. //split texel size into smaller words
  39. float TX_x = _MainTex_TexelSize.x;
  40. //and a final intensity that increments based on surrounding intensities.
  41. float ColorIntensityInRadius;
  42. //for every iteration we need to do horizontally
  43. for (int k = 0; k0)
  44. {
  45. return tex2D(_SceneTex,float2(i.uvs.x,1 - i.uvs.y));
  46. }
  47. //for every iteration we need to do vertically
  48. for (int j = 0; j < NumberOfIterations; j += 1)
  49. {
  50. //increase our output color by the pixels in the area
  51. ColorIntensityInRadius += tex2D(
  52. _GrabTexture,
  53. float2(i.uvs.x,1 - i.uvs.y) + float2
  54. (
  55. 0,
  56. (j - NumberOfIterations / 2) * TX_y
  57. )
  58. ).r / NumberOfIterations;
  59. }
  60. //this is alpha blending, but we can't use HW blending unless we make a third pass, so this is probably cheaper.
  61. half4 outcolor = ColorIntensityInRadius * half4(0,1,1,1) * 2 + (1 - ColorIntensityInRadius) * tex2D(_SceneTex,float2(i.uvs.x,1 - i.uvs.y));
  62. return outcolor;
  63. }
  64. ENDCG
  65. }
  66. //end pass
  67. }
  68. //end subshader
  69. }