AVProVideo-VR-InsideSphere-Transparent-AndroidOES.shader 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. Shader "AVProVideo/VR/InsideSphere Unlit Transparent (stereo+color+alpha) - Android OES ONLY"
  2. {
  3. Properties
  4. {
  5. _MainTex ("Base (RGB)", 2D) = "black" {}
  6. _ChromaTex("Chroma", 2D) = "white" {} // For fallback shader
  7. _Color("Color", Color) = (0.0, 1.0, 0.0, 1.0)
  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. [KeywordEnum(None, Left, Right)] ForceEye("Force Eye Mode", Float) = 0
  11. [KeywordEnum(None, EquiRect180)] Layout("Layout", Float) = 0
  12. [Toggle(STEREO_DEBUG)] _StereoDebug("Stereo Debug Tinting", Float) = 0
  13. [Toggle(HIGH_QUALITY)] _HighQuality("High Quality", Float) = 0
  14. [Toggle(APPLY_GAMMA)] _ApplyGamma("Apply Gamma", Float) = 0
  15. _EdgeFeather("Edge Feather", Range (0, 1)) = 0.02
  16. }
  17. SubShader
  18. {
  19. Tags{ "Queue" = "Transparent" "RenderType" = "Transparent" }
  20. Pass
  21. {
  22. ZWrite On
  23. Blend SrcAlpha OneMinusSrcAlpha
  24. Cull Front
  25. Lighting Off
  26. GLSLPROGRAM
  27. #pragma only_renderers gles gles3
  28. #pragma multi_compile MONOSCOPIC STEREO_TOP_BOTTOM STEREO_LEFT_RIGHT STEREO_CUSTOM_UV
  29. #pragma multi_compile ALPHAPACK_NONE ALPHAPACK_TOP_BOTTOM ALPHAPACK_LEFT_RIGHT
  30. #pragma multi_compile FORCEEYE_NONE FORCEEYE_LEFT FORCEEYE_RIGHT
  31. #pragma multi_compile __ LAYOUT_EQUIRECT180
  32. #pragma multi_compile __ STEREO_DEBUG
  33. #pragma multi_compile __ HIGH_QUALITY
  34. #pragma multi_compile __ APPLY_GAMMA
  35. #pragma multi_compile __ USING_DEFAULT_TEXTURE
  36. #pragma multi_compile __ USING_URP
  37. #extension GL_OES_EGL_image_external : require
  38. #extension GL_OES_EGL_image_external_essl3 : enable
  39. precision mediump float;
  40. #include "UnityCG.glslinc"
  41. #if defined(STEREO_MULTIVIEW_ON)
  42. UNITY_SETUP_STEREO_RENDERING
  43. #endif
  44. #define SHADERLAB_GLSL
  45. #include "AVProVideo.cginc"
  46. #ifdef VERTEX
  47. INLINE bool Android_IsStereoEyeLeft()
  48. {
  49. #if defined(STEREO_MULTIVIEW_ON)
  50. int eyeIndex = SetupStereoEyeIndex();
  51. return (eyeIndex == 0);
  52. #else
  53. return IsStereoEyeLeft();
  54. #endif
  55. }
  56. #if defined(HIGH_QUALITY)
  57. varying vec3 texNormal;
  58. #if defined(STEREO_TOP_BOTTOM) || defined(STEREO_LEFT_RIGHT)
  59. varying vec4 texScaleOffset;
  60. #endif
  61. #else
  62. varying vec3 texVal;
  63. #if defined(ALPHAPACK_TOP_BOTTOM) || defined(ALPHAPACK_LEFT_RIGHT)
  64. varying vec2 alphaPackOffset;
  65. #endif
  66. uniform vec4 _MainTex_ST;
  67. uniform vec4 _MainTex_TexelSize;
  68. uniform mat4 _MainTex_Xfrm;
  69. #endif
  70. #if defined(STEREO_DEBUG)
  71. varying vec4 tint;
  72. #endif
  73. /// @fix: explicit TRANSFORM_TEX(); Unity's preprocessor chokes when attempting to use the TRANSFORM_TEX() macro in UnityCG.glslinc
  74. /// (as of Unity 4.5.0f6; issue dates back to 2011 or earlier: http://forum.unity3d.com/threads/glsl-transform_tex-and-tiling.93756/)
  75. vec2 transformTex(vec2 texCoord, vec4 texST)
  76. {
  77. return (texCoord * texST.xy + texST.zw);
  78. }
  79. void main()
  80. {
  81. #if defined(STEREO_MULTIVIEW_ON)
  82. int eyeIndex = SetupStereoEyeIndex();
  83. mat4 vpMatrix = GetStereoMatrixVP(eyeIndex);
  84. gl_Position = vpMatrix * unity_ObjectToWorld * gl_Vertex;
  85. #else
  86. gl_Position = XFormObjectToClip(gl_Vertex);
  87. #endif
  88. #if defined(HIGH_QUALITY)
  89. texNormal = normalize(gl_Normal.xyz);
  90. #if defined(STEREO_TOP_BOTTOM) || defined(STEREO_LEFT_RIGHT)
  91. texScaleOffset = GetStereoScaleOffset(Android_IsStereoEyeLeft(), false);
  92. #endif
  93. #else
  94. texVal.xy = gl_MultiTexCoord0.xy;
  95. texVal.xy = transformTex(gl_MultiTexCoord0.xy, _MainTex_ST);
  96. texVal.x = 1.0 - texVal.x;
  97. #if defined(LAYOUT_EQUIRECT180)
  98. texVal.x = ((texVal.x - 0.5) * 2.0) + 0.5;
  99. // Set value for clipping if UV area is behind viewer
  100. texVal.z = (gl_MultiTexCoord0.x > 0.25 && gl_MultiTexCoord0.x < 0.75) ? 1.0 : -1.0;
  101. #else
  102. texVal.z = 0.0;
  103. #endif
  104. // Apply texture transformation matrix - adjusts for offset/cropping (when the decoder decodes in blocks that overrun the video frame size, it pads)
  105. texVal.xy = (_MainTex_Xfrm * vec4(texVal.x, texVal.y, 0.0, 1.0)).xy;
  106. #if defined(STEREO_TOP_BOTTOM) || defined(STEREO_LEFT_RIGHT)
  107. vec4 scaleOffset = GetStereoScaleOffset(Android_IsStereoEyeLeft(), false);
  108. texVal.xy *= scaleOffset.xy;
  109. texVal.xy += scaleOffset.zw;
  110. #elif defined(STEREO_CUSTOM_UV)
  111. if(!Android_IsStereoEyeLeft())
  112. {
  113. texVal.xy= transformTex(gl_MultiTexCoord1.xy, _MainTex_ST);
  114. texVal.xy = vec2(1.0, 1.0) - texVal.xy;
  115. }
  116. #endif
  117. #if defined(ALPHAPACK_TOP_BOTTOM) || defined(ALPHAPACK_LEFT_RIGHT)
  118. vec4 alphaOffset = OffsetAlphaPackingUV(_MainTex_TexelSize.xy, texVal.xy, _MainTex_ST.y < 0.0);
  119. #if defined(ALPHAPACK_TOP_BOTTOM)
  120. alphaOffset.yw = alphaOffset.wy;
  121. #endif
  122. texVal.xy = alphaOffset.xy;
  123. alphaPackOffset = alphaOffset.zw;
  124. #endif
  125. #endif
  126. #if defined(STEREO_DEBUG)
  127. tint = GetStereoDebugTint(Android_IsStereoEyeLeft());
  128. #endif
  129. }
  130. #endif
  131. #ifdef FRAGMENT
  132. #if defined(HIGH_QUALITY)
  133. #if defined (GL_FRAGMENT_PRECISION_HIGH)
  134. precision highp float;
  135. #endif
  136. varying vec3 texNormal;
  137. #if defined(STEREO_TOP_BOTTOM) || defined(STEREO_LEFT_RIGHT)
  138. varying vec4 texScaleOffset;
  139. #endif
  140. uniform mat4 _MainTex_Xfrm;
  141. #else
  142. varying vec3 texVal;
  143. #if defined(ALPHAPACK_TOP_BOTTOM) || defined(ALPHAPACK_LEFT_RIGHT)
  144. varying vec2 alphaPackOffset;
  145. #endif
  146. #endif
  147. uniform float _EdgeFeather;
  148. #if defined(STEREO_DEBUG)
  149. varying vec4 tint;
  150. #endif
  151. #if defined(HIGH_QUALITY)
  152. vec2 NormalToEquiRect(vec3 n)
  153. {
  154. const float M_1_PI = 0.31830988618379067153776752674503; // 1.0/PI
  155. const float M_1_2PI = 0.15915494309189533576888376337251; // 2.0/PI
  156. vec2 uv;
  157. uv.x = 0.5 - atan(n.z, n.x) * M_1_2PI;
  158. uv.y = 0.5 - asin(-n.y) * M_1_PI;
  159. return uv;
  160. }
  161. /// @fix: explicit TRANSFORM_TEX(); Unity's preprocessor chokes when attempting to use the TRANSFORM_TEX() macro in UnityCG.glslinc
  162. /// (as of Unity 4.5.0f6; issue dates back to 2011 or earlier: http://forum.unity3d.com/threads/glsl-transform_tex-and-tiling.93756/)
  163. vec2 transformTex(vec2 texCoord, vec4 texST)
  164. {
  165. return (texCoord * texST.xy + texST.zw);
  166. }
  167. uniform vec4 _MainTex_ST;
  168. uniform vec4 _MainTex_TexelSize;
  169. #endif
  170. uniform vec4 _Color;
  171. #if defined(USING_DEFAULT_TEXTURE)
  172. uniform sampler2D _MainTex;
  173. #else
  174. uniform samplerExternalOES _MainTex;
  175. #endif
  176. void main()
  177. {
  178. vec4 uv = vec4(0.0, 0.0, 0.0, 0.0);
  179. #if defined(HIGH_QUALITY)
  180. vec3 n = normalize(texNormal);
  181. #if defined(LAYOUT_EQUIRECT180)
  182. if( n.z > 0.0001 )
  183. {
  184. // Clip pixels on the back of the sphere
  185. discard;
  186. }
  187. #endif
  188. uv.xy = NormalToEquiRect(n);
  189. uv.x += 0.75;
  190. uv.x = mod(uv.x, 1.0);
  191. uv.xy = transformTex(uv.xy, _MainTex_ST);
  192. #if defined(LAYOUT_EQUIRECT180)
  193. uv.x = ((uv.x - 0.5) * 2.0) + 0.5;
  194. #endif
  195. // Apply texture transformation matrix - adjusts for offset/cropping (when the decoder decodes in blocks that overrun the video frame size, it pads)
  196. uv.xy = (_MainTex_Xfrm * vec4(uv.x, uv.y, 0.0, 1.0)).xy;
  197. #if defined(STEREO_TOP_BOTTOM) || defined(STEREO_LEFT_RIGHT)
  198. uv.xy *= texScaleOffset.xy;
  199. uv.xy += texScaleOffset.zw;
  200. #endif
  201. #if defined(ALPHAPACK_TOP_BOTTOM) || defined(ALPHAPACK_LEFT_RIGHT)
  202. uv = OffsetAlphaPackingUV(_MainTex_TexelSize.xy, uv.xy, _MainTex_ST.y < 0.0);
  203. #if defined(ALPHAPACK_TOP_BOTTOM)
  204. uv.yw = uv.wy;
  205. #endif
  206. #endif
  207. #else
  208. uv.xy = texVal.xy;
  209. #if defined(ALPHAPACK_TOP_BOTTOM) || defined(ALPHAPACK_LEFT_RIGHT)
  210. uv.zw = alphaPackOffset;
  211. #endif
  212. #if defined(LAYOUT_EQUIRECT180)
  213. if( texVal.z < -0.0001 )
  214. {
  215. // Clip pixels on the back of the sphere
  216. discard;
  217. }
  218. #endif
  219. #endif
  220. vec4 col = vec4(1.0, 1.0, 0.0, 1.0);
  221. #if defined(SHADER_API_GLES) || defined(SHADER_API_GLES3)
  222. #if __VERSION__ < 300
  223. col = texture2D(_MainTex, uv.xy);
  224. #else
  225. col = texture(_MainTex, uv.xy);
  226. #endif
  227. #endif
  228. col *= _Color;
  229. #if defined(APPLY_GAMMA)
  230. col.rgb = GammaToLinear(col.rgb);
  231. #endif
  232. #if defined(ALPHAPACK_TOP_BOTTOM) || defined(ALPHAPACK_LEFT_RIGHT)
  233. #if defined(SHADER_API_GLES) || defined(SHADER_API_GLES3)
  234. #if __VERSION__ < 300
  235. vec3 rgb = texture2D(_MainTex, uv.zw).rgb;
  236. #else
  237. vec3 rgb = texture(_MainTex, uv.zw).rgb;
  238. #endif
  239. col.a = (rgb.r + rgb.g + rgb.b) / 3.0;
  240. #else
  241. col.a = 1.0;
  242. #endif
  243. #endif
  244. #if defined(STEREO_DEBUG)
  245. col *= tint;
  246. #endif
  247. #if defined(LAYOUT_EQUIRECT180)
  248. // 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
  249. if (_EdgeFeather > 0.0)
  250. {
  251. vec4 featherDirection = vec4(0.0, 0.0, 1.0, 1.0);
  252. #if defined(STEREO_TOP_BOTTOM)
  253. if (uv.y > 0.5)
  254. {
  255. featherDirection.y = 0.5;
  256. }
  257. else
  258. {
  259. featherDirection.w = 0.5;
  260. }
  261. #endif
  262. #if defined(STEREO_LEFT_RIGHT)
  263. if (uv.x > 0.5)
  264. {
  265. featherDirection.x = 0.5;
  266. }
  267. else
  268. {
  269. featherDirection.z = 0.5;
  270. }
  271. #endif
  272. #if defined(ALPHAPACK_TOP_BOTTOM)
  273. featherDirection.w *= 0.5;
  274. #endif
  275. #if defined(ALPHAPACK_LEFT_RIGHT)
  276. featherDirection.z *= 0.5;
  277. #endif
  278. float d = min(uv.x - featherDirection.x, min((uv.y - featherDirection.y), min(featherDirection.z - uv.x, featherDirection.w - uv.y)));
  279. float a = smoothstep(0.0, _EdgeFeather, d);
  280. col.a *= a;
  281. }
  282. #endif
  283. gl_FragColor = col;
  284. }
  285. #endif
  286. ENDGLSL
  287. }
  288. }
  289. Fallback "AVProVideo/VR/InsideSphere Unlit Transparent (stereo+color+fog+alpha)"
  290. }