VignetteShaderV2.shader 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. Shader "Custom/VignetteShaderV2"
  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.179
  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. center = center + float2(_THRES_LR, 0);
  63. distFromCenter = distance(i.uv.xy, center);
  64. }
  65. else if (unity_StereoEyeIndex == 1) // right eye
  66. {
  67. center = center - float2(_THRES_LR, 0);
  68. distFromCenter = distance(i.uv.xy, center);
  69. }
  70. float vignette;
  71. if (distFromCenter < _IFOV)
  72. {
  73. vignette = 1;
  74. }
  75. else if (distFromCenter > _OFOV)
  76. {
  77. vignette = 0;
  78. }
  79. else
  80. {
  81. vignette = -(distFromCenter - _IFOV) / (_OFOV - _IFOV) + 1;
  82. }
  83. col = saturate(col * vignette);
  84. return col;
  85. }
  86. ENDCG
  87. }
  88. }
  89. }