AVProVideo-VR-InsideSphere-AndroidOES.shader 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. #extension GL_OES_EGL_image_external : require
  35. #extension GL_OES_EGL_image_external_essl3 : enable
  36. precision mediump float;
  37. #ifdef VERTEX
  38. #include "UnityCG.glslinc"
  39. #if defined(STEREO_MULTIVIEW_ON)
  40. UNITY_SETUP_STEREO_RENDERING
  41. #endif
  42. #define SHADERLAB_GLSL
  43. #include "AVProVideo.cginc"
  44. INLINE bool Android_IsStereoEyeLeft()
  45. {
  46. #if defined(STEREO_MULTIVIEW_ON)
  47. int eyeIndex = SetupStereoEyeIndex();
  48. return (eyeIndex == 0);
  49. #else
  50. return IsStereoEyeLeft();
  51. #endif
  52. }
  53. #if defined(HIGH_QUALITY)
  54. varying vec3 texNormal;
  55. #if defined(STEREO_TOP_BOTTOM) || defined(STEREO_LEFT_RIGHT)
  56. varying vec4 texScaleOffset;
  57. #endif
  58. #else
  59. varying vec3 texVal;
  60. uniform vec4 _MainTex_ST;
  61. uniform mat4 _TextureMatrix;
  62. #endif
  63. #if defined(STEREO_DEBUG)
  64. varying vec4 tint;
  65. #endif
  66. /// @fix: explicit TRANSFORM_TEX(); Unity's preprocessor chokes when attempting to use the TRANSFORM_TEX() macro in UnityCG.glslinc
  67. /// (as of Unity 4.5.0f6; issue dates back to 2011 or earlier: http://forum.unity3d.com/threads/glsl-transform_tex-and-tiling.93756/)
  68. vec2 transformTex(vec2 texCoord, vec4 texST)
  69. {
  70. return (texCoord * texST.xy + texST.zw);
  71. }
  72. void main()
  73. {
  74. #if defined(STEREO_MULTIVIEW_ON)
  75. int eyeIndex = SetupStereoEyeIndex();
  76. mat4 vpMatrix = GetStereoMatrixVP(eyeIndex);
  77. gl_Position = vpMatrix * unity_ObjectToWorld * gl_Vertex;
  78. #else
  79. gl_Position = XFormObjectToClip(gl_Vertex);
  80. #endif
  81. #if defined(HIGH_QUALITY)
  82. texNormal = normalize(gl_Normal.xyz);
  83. #if defined(STEREO_TOP_BOTTOM) || defined(STEREO_LEFT_RIGHT)
  84. texScaleOffset = GetStereoScaleOffset(Android_IsStereoEyeLeft(), false);
  85. #endif
  86. #else
  87. texVal.xy = gl_MultiTexCoord0.xy;
  88. texVal.xy = transformTex(gl_MultiTexCoord0.xy, _MainTex_ST);
  89. texVal.x = 1.0 - texVal.x;
  90. #if defined(LAYOUT_EQUIRECT180)
  91. texVal.x = ((texVal.x - 0.5) * 2.0) + 0.5;
  92. // Set value for clipping if UV area is behind viewer
  93. texVal.z = (gl_MultiTexCoord0.x > 0.25 && gl_MultiTexCoord0.x < 0.75) ? 1.0 : -1.0;
  94. #endif
  95. // Apply texture transformation matrix - adjusts for offset/cropping (when the decoder decodes in blocks that overrun the video frame size, it pads)
  96. texVal.xy = (_TextureMatrix * vec4(texVal.x, texVal.y, 0.0, 1.0)).xy;
  97. #if defined(STEREO_TOP_BOTTOM) || defined(STEREO_LEFT_RIGHT)
  98. vec4 scaleOffset = GetStereoScaleOffset(Android_IsStereoEyeLeft(), false);
  99. texVal.xy *= scaleOffset.xy;
  100. texVal.xy += scaleOffset.zw;
  101. #elif defined(STEREO_CUSTOM_UV)
  102. if(!Android_IsStereoEyeLeft())
  103. {
  104. texVal.xy= transformTex(gl_MultiTexCoord1.xy, _MainTex_ST);
  105. texVal.xy = vec2(1.0, 1.0) - texVal.xy;
  106. }
  107. #endif
  108. #endif
  109. #if defined(STEREO_DEBUG)
  110. tint = GetStereoDebugTint(Android_IsStereoEyeLeft());
  111. #endif
  112. }
  113. #endif
  114. #ifdef FRAGMENT
  115. #if defined(HIGH_QUALITY)
  116. #if defined (GL_FRAGMENT_PRECISION_HIGH)
  117. precision highp float;
  118. #endif
  119. varying vec3 texNormal;
  120. #if defined(STEREO_TOP_BOTTOM) || defined(STEREO_LEFT_RIGHT)
  121. varying vec4 texScaleOffset;
  122. #endif
  123. uniform mat4 _TextureMatrix;
  124. #else
  125. varying vec3 texVal;
  126. #endif
  127. #if defined(STEREO_DEBUG)
  128. varying vec4 tint;
  129. #endif
  130. #if defined(APPLY_GAMMA)
  131. vec3 GammaToLinear(vec3 col)
  132. {
  133. return col * (col * (col * 0.305306011 + 0.682171111) + 0.012522878);
  134. }
  135. #endif
  136. #if defined(HIGH_QUALITY)
  137. vec2 NormalToEquiRect(vec3 n)
  138. {
  139. const float M_1_PI = 0.31830988618379067153776752674503; // 1.0/PI
  140. const float M_1_2PI = 0.15915494309189533576888376337251; // 2.0/PI
  141. vec2 uv;
  142. uv.x = 0.5 - atan(n.z, n.x) * M_1_2PI;
  143. uv.y = 0.5 - asin(-n.y) * M_1_PI;
  144. return uv;
  145. }
  146. /// @fix: explicit TRANSFORM_TEX(); Unity's preprocessor chokes when attempting to use the TRANSFORM_TEX() macro in UnityCG.glslinc
  147. /// (as of Unity 4.5.0f6; issue dates back to 2011 or earlier: http://forum.unity3d.com/threads/glsl-transform_tex-and-tiling.93756/)
  148. vec2 transformTex(vec2 texCoord, vec4 texST)
  149. {
  150. return (texCoord * texST.xy + texST.zw);
  151. }
  152. uniform vec4 _MainTex_ST;
  153. #endif
  154. uniform vec4 _Color;
  155. #if defined(USING_DEFAULT_TEXTURE)
  156. uniform sampler2D _MainTex;
  157. #else
  158. uniform samplerExternalOES _MainTex;
  159. #endif
  160. void main()
  161. {
  162. vec2 uv;
  163. #if defined(HIGH_QUALITY)
  164. vec3 n = normalize(texNormal);
  165. #if defined(LAYOUT_EQUIRECT180)
  166. if( n.z > 0.0001 )
  167. {
  168. // Clip pixels on the back of the sphere
  169. discard;
  170. }
  171. #endif
  172. uv = NormalToEquiRect(n);
  173. uv.x += 0.75;
  174. uv.x = mod(uv.x, 1.0);
  175. uv = transformTex(uv, _MainTex_ST);
  176. #if defined(LAYOUT_EQUIRECT180)
  177. uv.x = ((uv.x - 0.5) * 2.0) + 0.5;
  178. #endif
  179. // Apply texture transformation matrix - adjusts for offset/cropping (when the decoder decodes in blocks that overrun the video frame size, it pads)
  180. uv.xy = (_TextureMatrix * vec4(uv.x, uv.y, 0.0, 1.0)).xy;
  181. #if defined(STEREO_TOP_BOTTOM) || defined(STEREO_LEFT_RIGHT)
  182. uv.xy *= texScaleOffset.xy;
  183. uv.xy += texScaleOffset.zw;
  184. #endif
  185. #else
  186. uv = texVal.xy;
  187. #if defined(LAYOUT_EQUIRECT180)
  188. if( texVal.z < -0.0001 )
  189. {
  190. // Clip pixels on the back of the sphere
  191. discard;
  192. }
  193. #endif
  194. #endif
  195. vec4 col = vec4(1.0, 1.0, 0.0, 1.0);
  196. #if defined(SHADER_API_GLES) || defined(SHADER_API_GLES3)
  197. #if __VERSION__ < 300
  198. col = texture2D(_MainTex, uv);
  199. #else
  200. col = texture(_MainTex, uv);
  201. #endif
  202. #endif
  203. col *= _Color;
  204. #if defined(APPLY_GAMMA)
  205. col.rgb = GammaToLinear(col.rgb);
  206. #endif
  207. #if defined(STEREO_DEBUG)
  208. col *= tint;
  209. #endif
  210. gl_FragColor = col;
  211. }
  212. #endif
  213. ENDGLSL
  214. }
  215. }
  216. Fallback "AVProVideo/VR/InsideSphere Unlit (stereo+fog)"
  217. }