AndroidWebShader.shader 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // <copyright file="VideoUnlitShader.cs" company="Google Inc.">
  2. // Copyright (C) 2017 Google Inc. All Rights Reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. // </copyright>
  16. // This shader is a modified version of VideoUnlitShader.cs from the Unity Google VR SDK.
  17. // For Android, it renders from OES_external_image textures, which require special
  18. // OpenGLES extensions and a special texture sampler.
  19. // Thank you to the following developers for improving this shader:
  20. // - Tom Neumann at Rendever (@Mandelboxed) for his solution to support Single Pass stereo rendering: https://forum.unity.com/threads/unity_stereoeyeindex-with-glsl-single-pass-implementation-details.592990/#post-3982708
  21. // - Nanome (https://nanome.ai) for updating this shader to also work with non-VR cameras.
  22. Shader "Vuplex/Android Web Shader" {
  23. Properties {
  24. _MainTex ("Base (RGB)", 2D) = "white" {}
  25. [Toggle(FLIP_X)] _FlipX ("Flip X", Float) = 0
  26. [Toggle(FLIP_Y)] _FlipY ("Flip Y", Float) = 0
  27. [Header(Properties set programmatically)]
  28. _VideoCutoutRect("Video Cutout Rect", Vector) = (0, 0, 0, 0)
  29. _CropRect("Crop Rect", Vector) = (0, 0, 0, 0)
  30. // Include these UI properties from UI-Default.shader
  31. // in order to support UI Scroll Views.
  32. _StencilComp ("Stencil Comparison", Float) = 8
  33. _Stencil ("Stencil ID", Float) = 0
  34. _StencilOp ("Stencil Operation", Float) = 0
  35. _StencilWriteMask ("Stencil Write Mask", Float) = 255
  36. _StencilReadMask ("Stencil Read Mask", Float) = 255
  37. _ColorMask ("Color Mask", Float) = 15
  38. }
  39. SubShader {
  40. Pass {
  41. Tags { "Queue" = "Transparent" "RenderType" = "Transparent" }
  42. // Include these UI properties from UI-Default.shader
  43. // in order to support UI Scroll Views.
  44. Stencil {
  45. Ref [_Stencil]
  46. Comp [_StencilComp]
  47. Pass [_StencilOp]
  48. ReadMask [_StencilReadMask]
  49. WriteMask [_StencilWriteMask]
  50. }
  51. Lighting Off
  52. ZWrite Off
  53. Blend SrcAlpha OneMinusSrcAlpha
  54. ColorMask [_ColorMask]
  55. GLSLPROGRAM
  56. #pragma multi_compile ___ FLIP_X
  57. #pragma multi_compile ___ FLIP_Y
  58. #if defined(STEREO_MULTIVIEW_ON) || defined(STEREO_INSTANCING_ON)
  59. layout(std140) uniform UnityStereoGlobals {
  60. mat4 unity_StereoMatrixP[2];
  61. mat4 unity_StereoMatrixV[2];
  62. mat4 unity_StereoMatrixInvV[2];
  63. mat4 unity_StereoMatrixVP[2];
  64. mat4 unity_StereoCameraProjection[2];
  65. mat4 unity_StereoCameraInvProjection[2];
  66. mat4 unity_StereoWorldToCamera[2];
  67. mat4 unity_StereoCameraToWorld[2];
  68. vec3 unity_StereoWorldSpaceCameraPos[2];
  69. vec4 unity_StereoScaleOffset[2];
  70. };
  71. #endif
  72. #ifdef VERTEX
  73. #version 300 es
  74. #ifdef STEREO_MULTIVIEW_ON
  75. #extension GL_OVR_multiview2 : require
  76. // Requiring GL_OVR_multiview2 is supposed to implicitly enable GL_OVR_multiview, but there are
  77. // some old Android devices (e.g. Galaxy Tab A7 Lite (SM-T220)) where shader compilation fails unless
  78. // GL_OVR_multiview is explicitly required.
  79. #extension GL_OVR_multiview : require
  80. #endif
  81. #extension GL_OES_EGL_image_external : require
  82. #extension GL_OES_EGL_image_external_essl3 : enable
  83. uniform mat4 video_matrix;
  84. uniform vec4 _MainTex_ST;
  85. #ifdef STEREO_MULTIVIEW_ON
  86. layout(num_views = 2) in;
  87. #endif
  88. uniform int unity_StereoEyeIndex;
  89. in highp vec2 in_TEXCOORD0;
  90. out highp vec2 uv;
  91. // Pass the vertex color to the fragment shader
  92. // so that it can be used for calculating alpha.
  93. // This is needed, for example, to allow CanvasGroup.alpha
  94. // to control the alpha.
  95. varying vec4 vertexColor;
  96. int setupStereoEyeIndex() {
  97. int eyeIndex = unity_StereoEyeIndex;
  98. #if defined(STEREO_MULTIVIEW_ON)
  99. eyeIndex = int(gl_ViewID_OVR);
  100. #elif defined(STEREO_INSTANCING_ON)
  101. eyeIndex = int(gl_InstanceID & 1);
  102. gl_Layer = eyeIndex;
  103. #endif
  104. return eyeIndex;
  105. }
  106. mat4 getStereoMatrixVP(int eyeIndex) {
  107. mat4 stereoVP = unity_MatrixVP;
  108. #if defined(STEREO_MULTIVIEW_ON) || defined(STEREO_INSTANCING_ON)
  109. stereoVP = unity_StereoMatrixVP[eyeIndex];
  110. #endif
  111. return stereoVP;
  112. }
  113. void main() {
  114. int eye = setupStereoEyeIndex();
  115. gl_Position = getStereoMatrixVP(eye) * unity_ObjectToWorld * gl_Vertex;
  116. vec2 untransformedUV = in_TEXCOORD0;
  117. vertexColor = gl_Color;
  118. #ifdef FLIP_X
  119. untransformedUV.x = 1.0 - untransformedUV.x;
  120. #endif
  121. #ifdef FLIP_Y
  122. untransformedUV.y = 1.0 - untransformedUV.y;
  123. #endif
  124. uv.xy = untransformedUV.xy * _MainTex_ST.xy + _MainTex_ST.zw;
  125. }
  126. #endif
  127. #ifdef FRAGMENT
  128. // A port of GammaToLinearSpace from UnityCG.cginc
  129. vec3 GammaToLinearSpace (vec3 sRGB) {
  130. return sRGB * (sRGB * (sRGB * 0.305306011 + 0.682171111) + 0.012522878);
  131. }
  132. uniform samplerExternalOES _MainTex;
  133. uniform vec4 _VideoCutoutRect;
  134. uniform vec4 _CropRect;
  135. varying vec2 uv;
  136. varying vec4 vertexColor;
  137. void main() {
  138. vec4 col = texture2D(_MainTex, uv);
  139. float cutoutWidth = _VideoCutoutRect.z;
  140. float cutoutHeight = _VideoCutoutRect.w;
  141. #ifdef FLIP_X
  142. float nonflippedX = 1.0 - uv.x;
  143. #else
  144. float nonflippedX = uv.x;
  145. #endif
  146. #ifdef FLIP_Y
  147. float nonflippedY = uv.y;
  148. #else
  149. float nonflippedY = 1.0 - uv.y;
  150. #endif
  151. // Make the pixels transparent if they fall within the video rect cutout and the they're black.
  152. // Keeping non-black pixels allows the video controls to still show up on top of the video.
  153. bool pointIsInCutout = cutoutWidth != 0.0 &&
  154. cutoutHeight != 0.0 &&
  155. nonflippedX >= _VideoCutoutRect.x &&
  156. nonflippedX <= _VideoCutoutRect.x + cutoutWidth &&
  157. nonflippedY >= _VideoCutoutRect.y &&
  158. nonflippedY <= _VideoCutoutRect.y + cutoutHeight;
  159. if (pointIsInCutout) {
  160. // Use a threshold of 0.15 to consider a pixel as black.
  161. bool pixelIsBlack = all(lessThan(col.xyz, vec3(0.15, 0.15, 0.15)));
  162. if (pixelIsBlack) {
  163. col = vec4(0.0, 0.0, 0.0, 0.0);
  164. }
  165. }
  166. float cropWidth = _CropRect.z;
  167. float cropHeight = _CropRect.w;
  168. bool pointIsOutsideOfCrop = cropWidth != 0.0 &&
  169. cropHeight != 0.0 &&
  170. (nonflippedX < _CropRect.x || nonflippedX > _CropRect.x + cropWidth || nonflippedY < _CropRect.y || nonflippedY > _CropRect.y + cropHeight);
  171. if (pointIsOutsideOfCrop) {
  172. col = vec4(0.0, 0.0, 0.0, 0.0);
  173. }
  174. // Place color correction last so it doesn't effect cutout rect functionality.
  175. #ifndef UNITY_COLORSPACE_GAMMA
  176. col = vec4(GammaToLinearSpace(col.xyz), col.w);
  177. #endif
  178. // Multiply the alpha by the vertex color's alpha to support CanvasGroup.alpha.
  179. gl_FragColor = vec4(col.xyz, col.w * vertexColor.w);
  180. }
  181. #endif
  182. ENDGLSL
  183. }
  184. }
  185. Fallback "Unlit/Texture"
  186. }