AVProVideo-VR-InsideSphere-Transparent.shader 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. Shader "AVProVideo/VR/InsideSphere Unlit Transparent (stereo+color+fog+alpha)"
  2. {
  3. Properties
  4. {
  5. _MainTex ("Texture", 2D) = "black" {}
  6. _ChromaTex("Chroma", 2D) = "white" {}
  7. _Color("Main Color", Color) = (1,1,1,1)
  8. [KeywordEnum(None, Top_Bottom, Left_Right, Custom_UV)] Stereo ("Stereo Mode", Float) = 0
  9. [KeywordEnum(None, Top_Bottom, Left_Right)] AlphaPack("Alpha Pack", Float) = 0
  10. [Toggle(STEREO_DEBUG)] _StereoDebug ("Stereo Debug Tinting", Float) = 0
  11. [KeywordEnum(None, EquiRect180)] Layout("Layout", Float) = 0
  12. [Toggle(HIGH_QUALITY)] _HighQuality ("High Quality", Float) = 0
  13. [Toggle(APPLY_GAMMA)] _ApplyGamma("Apply Gamma", Float) = 0
  14. [Toggle(USE_YPCBCR)] _UseYpCbCr("Use YpCbCr", Float) = 0
  15. _EdgeFeather("Edge Feather", Range (0, 1)) = 0.02
  16. }
  17. SubShader
  18. {
  19. Tags { "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
  20. ZWrite On
  21. //ZTest Always
  22. Blend SrcAlpha OneMinusSrcAlpha
  23. Cull Front
  24. Lighting Off
  25. Pass
  26. {
  27. CGPROGRAM
  28. #include "UnityCG.cginc"
  29. #include "AVProVideo.cginc"
  30. #if HIGH_QUALITY || APPLY_GAMMA
  31. #pragma target 3.0
  32. #endif
  33. #pragma vertex vert
  34. #pragma fragment frag
  35. #pragma multi_compile_fog
  36. // TODO: replace use multi_compile_local instead (Unity 2019.1 feature)
  37. #pragma multi_compile MONOSCOPIC STEREO_TOP_BOTTOM STEREO_LEFT_RIGHT STEREO_CUSTOM_UV
  38. #pragma multi_compile ALPHAPACK_NONE ALPHAPACK_TOP_BOTTOM ALPHAPACK_LEFT_RIGHT
  39. #pragma multi_compile __ STEREO_DEBUG
  40. #pragma multi_compile __ HIGH_QUALITY
  41. #pragma multi_compile __ APPLY_GAMMA
  42. #pragma multi_compile __ USE_YPCBCR
  43. #pragma multi_compile __ LAYOUT_EQUIRECT180
  44. struct appdata
  45. {
  46. float4 vertex : POSITION; // vertex position
  47. #if HIGH_QUALITY
  48. float3 normal : NORMAL;
  49. #else
  50. float2 uv : TEXCOORD0; // texture coordinate
  51. #if STEREO_CUSTOM_UV
  52. float2 uv2 : TEXCOORD1; // Custom uv set for right eye (left eye is in TEXCOORD0)
  53. #endif
  54. #endif
  55. #ifdef UNITY_STEREO_INSTANCING_ENABLED
  56. UNITY_VERTEX_INPUT_INSTANCE_ID
  57. #endif
  58. };
  59. struct v2f
  60. {
  61. float4 vertex : SV_POSITION; // clip space position
  62. #if HIGH_QUALITY
  63. float3 normal : TEXCOORD0;
  64. #if STEREO_TOP_BOTTOM || STEREO_LEFT_RIGHT
  65. float4 scaleOffset : TEXCOORD1; // texture coordinate
  66. UNITY_FOG_COORDS(2)
  67. #else
  68. UNITY_FOG_COORDS(1)
  69. #endif
  70. #else
  71. float4 uv : TEXCOORD0; // texture coordinate
  72. UNITY_FOG_COORDS(1)
  73. #endif
  74. #if STEREO_DEBUG
  75. float4 tint : COLOR;
  76. #endif
  77. #ifdef UNITY_STEREO_INSTANCING_ENABLED
  78. UNITY_VERTEX_OUTPUT_STEREO
  79. #endif
  80. };
  81. uniform sampler2D _MainTex;
  82. #if USE_YPCBCR
  83. uniform sampler2D _ChromaTex;
  84. uniform float4x4 _YpCbCrTransform;
  85. #endif
  86. uniform float4 _MainTex_ST;
  87. uniform float4 _MainTex_TexelSize;
  88. uniform fixed4 _Color;
  89. uniform float _EdgeFeather;
  90. v2f vert (appdata v)
  91. {
  92. v2f o;
  93. #ifdef UNITY_STEREO_INSTANCING_ENABLED
  94. UNITY_SETUP_INSTANCE_ID(v); // calculates and sets the built-n unity_StereoEyeIndex and unity_InstanceID Unity shader variables to the correct values based on which eye the GPU is currently rendering
  95. UNITY_INITIALIZE_OUTPUT(v2f, o); // initializes all v2f values to 0
  96. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o); // tells the GPU which eye in the texture array it should render to
  97. #endif
  98. o.vertex = XFormObjectToClip(v.vertex);
  99. #if !HIGH_QUALITY
  100. o.uv.zw = 0.0;
  101. o.uv.xy = TRANSFORM_TEX(v.uv, _MainTex);
  102. #if LAYOUT_EQUIRECT180
  103. o.uv.x = ((o.uv.x - 0.5) * 2.0) + 0.5;
  104. // Set value for clipping if UV area is behind viewer
  105. o.uv.z = -1.0;
  106. if (v.uv.x > 0.25 && v.uv.x < 0.75)
  107. {
  108. o.uv.z = 1.0;
  109. }
  110. #endif
  111. o.uv.xy = float2(1.0-o.uv.x, o.uv.y);
  112. #endif
  113. #if STEREO_TOP_BOTTOM || STEREO_LEFT_RIGHT
  114. float4 scaleOffset = GetStereoScaleOffset(IsStereoEyeLeft(), _MainTex_ST.y < 0.0);
  115. #if !HIGH_QUALITY
  116. o.uv.xy *= scaleOffset.xy;
  117. o.uv.xy += scaleOffset.zw;
  118. #else
  119. o.scaleOffset = scaleOffset;
  120. #endif
  121. #elif STEREO_CUSTOM_UV && !HIGH_QUALITY
  122. if (!IsStereoEyeLeft())
  123. {
  124. o.uv.xy = TRANSFORM_TEX(v.uv2, _MainTex);
  125. o.uv.xy = float2(1.0 - o.uv.x, o.uv.y);
  126. }
  127. #endif
  128. #if !HIGH_QUALITY
  129. #if ALPHAPACK_TOP_BOTTOM || ALPHAPACK_LEFT_RIGHT
  130. o.uv = OffsetAlphaPackingUV(_MainTex_TexelSize.xy, o.uv.xy, _MainTex_ST.y > 0.0);
  131. #endif
  132. #endif
  133. #if HIGH_QUALITY
  134. o.normal = v.normal;
  135. #endif
  136. #if STEREO_DEBUG
  137. o.tint = GetStereoDebugTint(IsStereoEyeLeft());
  138. #endif
  139. UNITY_TRANSFER_FOG(o, o.vertex);
  140. return o;
  141. }
  142. fixed4 frag (v2f i) : SV_Target
  143. {
  144. float4 uv = 0;
  145. #if HIGH_QUALITY
  146. float3 n = normalize(i.normal);
  147. #if LAYOUT_EQUIRECT180
  148. clip(-n.z); // Clip pixels on the back of the sphere
  149. #endif
  150. float M_1_PI = 1.0 / 3.1415926535897932384626433832795;
  151. float M_1_2PI = 1.0 / 6.283185307179586476925286766559;
  152. uv.x = 0.5 - atan2(n.z, n.x) * M_1_2PI;
  153. uv.y = 0.5 - asin(-n.y) * M_1_PI;
  154. uv.x += 0.75;
  155. uv.x = fmod(uv.x, 1.0);
  156. //uv.x = uv.x % 1.0;
  157. uv.xy = TRANSFORM_TEX(uv, _MainTex);
  158. #if LAYOUT_EQUIRECT180
  159. uv.x = ((uv.x - 0.5) * 2.0) + 0.5;
  160. #endif
  161. #if STEREO_TOP_BOTTOM | STEREO_LEFT_RIGHT
  162. uv.xy *= i.scaleOffset.xy;
  163. uv.xy += i.scaleOffset.zw;
  164. #endif
  165. #if ALPHAPACK_TOP_BOTTOM | ALPHAPACK_LEFT_RIGHT
  166. uv = OffsetAlphaPackingUV(_MainTex_TexelSize.xy, uv.xy, _MainTex_ST.y < 0.0);
  167. #endif
  168. #else
  169. uv = i.uv;
  170. #if LAYOUT_EQUIRECT180
  171. clip(i.uv.z); // Clip pixels on the back of the sphere
  172. #endif
  173. #endif
  174. fixed4 col;
  175. #if USE_YPCBCR
  176. col = SampleYpCbCr(_MainTex, _ChromaTex, uv.xy, _YpCbCrTransform);
  177. #else
  178. col = SampleRGBA(_MainTex, uv.xy);
  179. #endif
  180. #if ALPHAPACK_TOP_BOTTOM | ALPHAPACK_LEFT_RIGHT
  181. col.a = SamplePackedAlpha(_MainTex, uv.zw);
  182. #endif
  183. #if STEREO_DEBUG
  184. col *= i.tint;
  185. #endif
  186. col *= _Color;
  187. UNITY_APPLY_FOG(i.fogCoord, col);
  188. #if LAYOUT_EQUIRECT180
  189. // Apply edge feathering based on UV mapping - this is useful if you're using a hemisphere mesh for 180 degree video and want to have soft edges
  190. if (_EdgeFeather > 0.0)
  191. {
  192. float4 featherDirection = float4(0.0, 0.0, 1.0, 1.0);
  193. #if STEREO_TOP_BOTTOM
  194. if (uv.y > 0.5)
  195. {
  196. featherDirection.y = 0.5;
  197. }
  198. else
  199. {
  200. featherDirection.w = 0.5;
  201. }
  202. #endif
  203. #if STEREO_LEFT_RIGHT
  204. if (uv.x > 0.5)
  205. {
  206. featherDirection.x = 0.5;
  207. }
  208. else
  209. {
  210. featherDirection.z = 0.5;
  211. }
  212. #endif
  213. #if ALPHAPACK_TOP_BOTTOM
  214. featherDirection.w *= 0.5;
  215. #endif
  216. #if ALPHAPACK_LEFT_RIGHT
  217. featherDirection.z *= 0.5;
  218. #endif
  219. float d = min(uv.x - featherDirection.x, min((uv.y - featherDirection.y), min(featherDirection.z - uv.x, featherDirection.w - uv.y)));
  220. float a = smoothstep(0.0, _EdgeFeather, d);
  221. col.a *= a;
  222. }
  223. #endif
  224. return col;
  225. }
  226. ENDCG
  227. }
  228. }
  229. }