AVProVideo-VR-InsideSphere-AndroidOES.shader 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. Shader "AVProVideo/VR/InsideSphere Unlit (stereo+color) - 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, Left, Right)] ForceEye("Force Eye Mode", Float) = 0
  10. [KeywordEnum(None, EquiRect180)] Layout("Layout", Float) = 0
  11. [Toggle(STEREO_DEBUG)] _StereoDebug("Stereo Debug Tinting", Float) = 0
  12. [Toggle(HIGH_QUALITY)] _HighQuality("High Quality", Float) = 0
  13. [Toggle(APPLY_GAMMA)] _ApplyGamma("Apply Gamma", Float) = 0
  14. }
  15. SubShader
  16. {
  17. Tags{ "Queue" = "Geometry" }
  18. Pass
  19. {
  20. Cull Front
  21. //ZTest Always
  22. ZWrite On
  23. Lighting Off
  24. GLSLPROGRAM
  25. #pragma only_renderers gles gles3
  26. // TODO: replace use multi_compile_local instead (Unity 2019.1 feature)
  27. #pragma multi_compile MONOSCOPIC STEREO_TOP_BOTTOM STEREO_LEFT_RIGHT STEREO_CUSTOM_UV
  28. #pragma multi_compile FORCEEYE_NONE FORCEEYE_LEFT FORCEEYE_RIGHT
  29. #pragma multi_compile __ LAYOUT_EQUIRECT180
  30. #pragma multi_compile __ STEREO_DEBUG
  31. #pragma multi_compile __ HIGH_QUALITY
  32. #pragma multi_compile __ APPLY_GAMMA
  33. #pragma multi_compile __ USING_DEFAULT_TEXTURE
  34. #pragma multi_compile __ USING_URP
  35. #extension GL_OES_EGL_image_external : require
  36. #extension GL_OES_EGL_image_external_essl3 : enable
  37. precision mediump float;
  38. #ifdef VERTEX
  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. INLINE bool Android_IsStereoEyeLeft()
  46. {
  47. #if defined(STEREO_MULTIVIEW_ON)
  48. int eyeIndex = SetupStereoEyeIndex();
  49. return (eyeIndex == 0);
  50. #else
  51. return IsStereoEyeLeft();
  52. #endif
  53. }
  54. #if defined(HIGH_QUALITY)
  55. varying vec3 texNormal;
  56. #if defined(STEREO_TOP_BOTTOM) || defined(STEREO_LEFT_RIGHT)
  57. varying vec4 texScaleOffset;
  58. #endif
  59. #else
  60. varying vec3 texVal;
  61. uniform vec4 _MainTex_ST;
  62. uniform mat4 _MainTex_Xfrm;
  63. #endif
  64. #if defined(STEREO_DEBUG)
  65. varying vec4 tint;
  66. #endif
  67. /// @fix: explicit TRANSFORM_TEX(); Unity's preprocessor chokes when attempting to use the TRANSFORM_TEX() macro in UnityCG.glslinc
  68. /// (as of Unity 4.5.0f6; issue dates back to 2011 or earlier: http://forum.unity3d.com/threads/glsl-transform_tex-and-tiling.93756/)
  69. vec2 transformTex(vec2 texCoord, vec4 texST)
  70. {
  71. return (texCoord * texST.xy + texST.zw);
  72. }
  73. void main()
  74. {
  75. #if defined(STEREO_MULTIVIEW_ON)
  76. int eyeIndex = SetupStereoEyeIndex();
  77. mat4 vpMatrix = GetStereoMatrixVP(eyeIndex);
  78. gl_Position = vpMatrix * unity_ObjectToWorld * gl_Vertex;
  79. #else
  80. gl_Position = XFormObjectToClip(gl_Vertex);
  81. #endif
  82. #if defined(HIGH_QUALITY)
  83. texNormal = normalize(gl_Normal.xyz);
  84. #if defined(STEREO_TOP_BOTTOM) || defined(STEREO_LEFT_RIGHT)
  85. texScaleOffset = GetStereoScaleOffset(Android_IsStereoEyeLeft(), false);
  86. #endif
  87. #else
  88. texVal.xy = gl_MultiTexCoord0.xy;
  89. texVal.xy = transformTex(gl_MultiTexCoord0.xy, _MainTex_ST);
  90. texVal.x = 1.0 - texVal.x;
  91. #if defined(LAYOUT_EQUIRECT180)
  92. texVal.x = ((texVal.x - 0.5) * 2.0) + 0.5;
  93. // Set value for clipping if UV area is behind viewer
  94. texVal.z = (gl_MultiTexCoord0.x > 0.25 && gl_MultiTexCoord0.x < 0.75) ? 1.0 : -1.0;
  95. #endif
  96. // Apply texture transformation matrix - adjusts for offset/cropping (when the decoder decodes in blocks that overrun the video frame size, it pads)
  97. texVal.xy = (_MainTex_Xfrm * vec4(texVal.x, texVal.y, 0.0, 1.0)).xy;
  98. #if defined(STEREO_TOP_BOTTOM) || defined(STEREO_LEFT_RIGHT)
  99. vec4 scaleOffset = GetStereoScaleOffset(Android_IsStereoEyeLeft(), false);
  100. texVal.xy *= scaleOffset.xy;
  101. texVal.xy += scaleOffset.zw;
  102. #elif defined(STEREO_CUSTOM_UV)
  103. if(!Android_IsStereoEyeLeft())
  104. {
  105. texVal.xy= transformTex(gl_MultiTexCoord1.xy, _MainTex_ST);
  106. texVal.xy = vec2(1.0, 1.0) - texVal.xy;
  107. }
  108. #endif
  109. #endif
  110. #if defined(STEREO_DEBUG)
  111. tint = GetStereoDebugTint(Android_IsStereoEyeLeft());
  112. #endif
  113. }
  114. #endif
  115. #ifdef FRAGMENT
  116. #if defined(HIGH_QUALITY)
  117. #if defined (GL_FRAGMENT_PRECISION_HIGH)
  118. precision highp float;
  119. #endif
  120. varying vec3 texNormal;
  121. #if defined(STEREO_TOP_BOTTOM) || defined(STEREO_LEFT_RIGHT)
  122. varying vec4 texScaleOffset;
  123. #endif
  124. uniform mat4 _MainTex_Xfrm;
  125. #else
  126. varying vec3 texVal;
  127. #endif
  128. #if defined(STEREO_DEBUG)
  129. varying vec4 tint;
  130. #endif
  131. #if defined(APPLY_GAMMA)
  132. vec3 GammaToLinear(vec3 col)
  133. {
  134. return col * (col * (col * 0.305306011 + 0.682171111) + 0.012522878);
  135. }
  136. #endif
  137. #if defined(HIGH_QUALITY)
  138. vec2 NormalToEquiRect(vec3 n)
  139. {
  140. const float M_1_PI = 0.31830988618379067153776752674503; // 1.0/PI
  141. const float M_1_2PI = 0.15915494309189533576888376337251; // 2.0/PI
  142. vec2 uv;
  143. uv.x = 0.5 - atan(n.z, n.x) * M_1_2PI;
  144. uv.y = 0.5 - asin(-n.y) * M_1_PI;
  145. return uv;
  146. }
  147. /// @fix: explicit TRANSFORM_TEX(); Unity's preprocessor chokes when attempting to use the TRANSFORM_TEX() macro in UnityCG.glslinc
  148. /// (as of Unity 4.5.0f6; issue dates back to 2011 or earlier: http://forum.unity3d.com/threads/glsl-transform_tex-and-tiling.93756/)
  149. vec2 transformTex(vec2 texCoord, vec4 texST)
  150. {
  151. return (texCoord * texST.xy + texST.zw);
  152. }
  153. uniform vec4 _MainTex_ST;
  154. #endif
  155. uniform vec4 _Color;
  156. #if defined(USING_DEFAULT_TEXTURE)
  157. uniform sampler2D _MainTex;
  158. #else
  159. uniform samplerExternalOES _MainTex;
  160. #endif
  161. void main()
  162. {
  163. vec2 uv;
  164. #if defined(HIGH_QUALITY)
  165. vec3 n = normalize(texNormal);
  166. #if defined(LAYOUT_EQUIRECT180)
  167. if( n.z > 0.0001 )
  168. {
  169. // Clip pixels on the back of the sphere
  170. discard;
  171. }
  172. #endif
  173. uv = NormalToEquiRect(n);
  174. uv.x += 0.75;
  175. uv.x = mod(uv.x, 1.0);
  176. uv = transformTex(uv, _MainTex_ST);
  177. #if defined(LAYOUT_EQUIRECT180)
  178. uv.x = ((uv.x - 0.5) * 2.0) + 0.5;
  179. #endif
  180. // Apply texture transformation matrix - adjusts for offset/cropping (when the decoder decodes in blocks that overrun the video frame size, it pads)
  181. uv.xy = (_MainTex_Xfrm * vec4(uv.x, uv.y, 0.0, 1.0)).xy;
  182. #if defined(STEREO_TOP_BOTTOM) || defined(STEREO_LEFT_RIGHT)
  183. uv.xy *= texScaleOffset.xy;
  184. uv.xy += texScaleOffset.zw;
  185. #endif
  186. #else
  187. uv = texVal.xy;
  188. #if defined(LAYOUT_EQUIRECT180)
  189. if( texVal.z < -0.0001 )
  190. {
  191. // Clip pixels on the back of the sphere
  192. discard;
  193. }
  194. #endif
  195. #endif
  196. vec4 col = vec4(1.0, 1.0, 0.0, 1.0);
  197. #if defined(SHADER_API_GLES) || defined(SHADER_API_GLES3)
  198. #if __VERSION__ < 300
  199. col = texture2D(_MainTex, uv);
  200. #else
  201. col = texture(_MainTex, uv);
  202. #endif
  203. #endif
  204. col *= _Color;
  205. #if defined(APPLY_GAMMA)
  206. col.rgb = GammaToLinear(col.rgb);
  207. #endif
  208. #if defined(STEREO_DEBUG)
  209. col *= tint;
  210. #endif
  211. gl_FragColor = col;
  212. }
  213. #endif
  214. ENDGLSL
  215. }
  216. }
  217. Fallback "AVProVideo/VR/InsideSphere Unlit (stereo+fog)"
  218. }