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

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