VignetteShader.shader 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. Shader "Custom/VignetteShader"
  2. {
  3. Properties
  4. {
  5. _MainTex ("Texture", 2D) = "white" {}
  6. _OFOV ("Outer Radius", Range(0.0, 1.0)) = 0.7
  7. _IFOV ("Inner Radius", Range(0.0, 1.0)) = 0.3
  8. _THRES_LR ("Threshold L/R overlap", Range(0.0, 1.0)) = 0.2
  9. //linearly decrease transparency from _IFOV to _OFOV; see Fernandes & Feiner (2016)
  10. [MaterialToggle] _Smooth ("Smooth", Float) = 0 //alternatively use hermite function
  11. }
  12. SubShader
  13. {
  14. // No culling or depth
  15. Cull Off ZWrite Off ZTest Always
  16. Pass
  17. {
  18. CGPROGRAM
  19. #pragma vertex vert
  20. #pragma fragment frag
  21. #pragma multi_compile_instancing
  22. #include "UnityCG.cginc"
  23. struct appdata
  24. {
  25. float4 vertex : POSITION;
  26. float2 uv : TEXCOORD0;
  27. UNITY_VERTEX_INPUT_INSTANCE_ID
  28. };
  29. struct v2f
  30. {
  31. float2 uv : TEXCOORD0;
  32. float4 vertex : SV_POSITION;
  33. UNITY_VERTEX_OUTPUT_STEREO
  34. };
  35. v2f vert(appdata v)
  36. {
  37. v2f o;
  38. UNITY_SETUP_INSTANCE_ID(v);
  39. UNITY_INITIALIZE_OUTPUT(v2f, o); //Insert
  40. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o); //Insert
  41. o.vertex = UnityObjectToClipPos(v.vertex);
  42. o.uv = v.uv;
  43. return o;
  44. }
  45. //sampler2D _MainTex;
  46. UNITY_DECLARE_SCREENSPACE_TEXTURE(_MainTex); //Insert
  47. float _IFOV;
  48. float _OFOV;
  49. float _THRES_LR;
  50. float _Smooth;
  51. fixed4 frag(v2f i) : SV_Target
  52. {
  53. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i); //Insert
  54. fixed4 col = UNITY_SAMPLE_SCREENSPACE_TEXTURE(_MainTex, i.uv); //Insert
  55. float2 center = float2(0.5, 0.5);
  56. float distFromCenter = distance(i.uv.xy, center);
  57. float2 distFromCenter2 = i.uv.xy - center;
  58. float rest = 1 - _IFOV;
  59. //float distFromInnerRadius = -1 * (distFromCenter - _IFOV) / rest + 1;
  60. if (unity_StereoEyeIndex == 0) // left eye
  61. {
  62. if (distFromCenter2[0] >= 0) //right of center
  63. {
  64. if(abs(distFromCenter2[0]) < _THRES_LR)
  65. {
  66. distFromCenter = abs(distFromCenter2[1]);
  67. }else
  68. {
  69. center = center + float2(_THRES_LR,0);
  70. distFromCenter = distance(i.uv.xy, center);
  71. }
  72. //return 1 - col;
  73. }
  74. }
  75. else if (unity_StereoEyeIndex == 1) // right eye
  76. {
  77. if (distFromCenter2[0] <= 0) //left of center
  78. {
  79. if( abs(distFromCenter2[0]) < _THRES_LR)
  80. {
  81. distFromCenter = abs(distFromCenter2[1]);
  82. }else
  83. {
  84. center = center - float2(_THRES_LR,0);
  85. distFromCenter = distance(i.uv.xy, center);
  86. }
  87. //return 1 - col;
  88. }
  89. }
  90. float vignette;
  91. if (distFromCenter < _IFOV)
  92. {
  93. vignette = 1;
  94. }
  95. else if (distFromCenter > _OFOV)
  96. {
  97. vignette = 0;
  98. }
  99. else
  100. {
  101. vignette = -(distFromCenter - _IFOV) / (_OFOV - _IFOV) + 1;
  102. }
  103. col = saturate(col * vignette);
  104. return col;
  105. }
  106. ENDCG
  107. }
  108. }
  109. }