AndroidViewportShader.shader 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. Shader "Vuplex/Android Viewport Shader" {
  20. Properties {
  21. _MainTex ("Base (RGB)", 2D) = "white" {}
  22. [Toggle(FLIP_X)] _FlipX ("Flip X", Float) = 0
  23. [Toggle(FLIP_Y)] _FlipY ("Flip Y", Float) = 0
  24. [Header(Properties set programmatically)]
  25. _VideoCutoutRect("Video Cutout Rect", Vector) = (0, 0, 0, 0)
  26. _CropRect("Crop Rect", Vector) = (0, 0, 0, 0)
  27. // Include these UI properties from UI-Default.shader
  28. // in order to support UI Scroll Views.
  29. _StencilComp ("Stencil Comparison", Float) = 8
  30. _Stencil ("Stencil ID", Float) = 0
  31. _StencilOp ("Stencil Operation", Float) = 0
  32. _StencilWriteMask ("Stencil Write Mask", Float) = 255
  33. _StencilReadMask ("Stencil Read Mask", Float) = 255
  34. _ColorMask ("Color Mask", Float) = 15
  35. }
  36. SubShader {
  37. Pass {
  38. Tags { "Queue" = "Transparent" "RenderType" = "Transparent" }
  39. // Include these UI properties from UI-Default.shader
  40. // in order to support UI Scroll Views.
  41. Stencil {
  42. Ref [_Stencil]
  43. Comp [_StencilComp]
  44. Pass [_StencilOp]
  45. ReadMask [_StencilReadMask]
  46. WriteMask [_StencilWriteMask]
  47. }
  48. Lighting Off
  49. ZWrite Off
  50. Blend SrcAlpha OneMinusSrcAlpha
  51. ColorMask [_ColorMask]
  52. GLSLPROGRAM
  53. #pragma only_renderers gles gles3
  54. #extension GL_OES_EGL_image_external : require
  55. #extension GL_OES_EGL_image_external_essl3 : enable
  56. #pragma multi_compile ___ FLIP_X
  57. #pragma multi_compile ___ FLIP_Y
  58. #pragma multi_compile _ UNITY_COLORSPACE_GAMMA
  59. precision mediump int;
  60. precision mediump float;
  61. #ifdef VERTEX
  62. uniform mat4 video_matrix;
  63. uniform int unity_StereoEyeIndex;
  64. varying vec2 uv;
  65. // Pass the vertex color to the fragment shader
  66. // so that it can be used for calculating alpha.
  67. // This is needed, for example, to allow CanvasGroup.alpha
  68. // to control the alpha.
  69. varying vec4 vertexColor;
  70. void main() {
  71. gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
  72. vec4 untransformedUV = gl_MultiTexCoord0;
  73. vertexColor = gl_Color;
  74. #ifdef FLIP_X
  75. untransformedUV.x = 1.0 - untransformedUV.x;
  76. #endif
  77. #ifdef FLIP_Y
  78. untransformedUV.y = 1.0 - untransformedUV.y;
  79. #endif
  80. uv = (video_matrix * untransformedUV).xy;
  81. }
  82. #endif
  83. #ifdef FRAGMENT
  84. // A port of GammaToLinearSpace from UnityCG.cginc
  85. vec3 GammaToLinearSpace (vec3 sRGB) {
  86. return sRGB * (sRGB * (sRGB * 0.305306011 + 0.682171111) + 0.012522878);
  87. }
  88. uniform samplerExternalOES _MainTex;
  89. uniform vec4 _VideoCutoutRect;
  90. uniform vec4 _CropRect;
  91. varying vec2 uv;
  92. varying vec4 vertexColor;
  93. void main() {
  94. vec4 col = texture2D(_MainTex, uv);
  95. float cutoutWidth = _VideoCutoutRect.z;
  96. float cutoutHeight = _VideoCutoutRect.w;
  97. #ifdef FLIP_X
  98. float nonflippedX = 1.0 - uv.x;
  99. #else
  100. float nonflippedX = uv.x;
  101. #endif
  102. #ifdef FLIP_Y
  103. float nonflippedY = uv.y;
  104. #else
  105. float nonflippedY = 1.0 - uv.y;
  106. #endif
  107. // Make the pixels transparent if they fall within the video rect cutout and the they're black.
  108. // Keeping non-black pixels allows the video controls to still show up on top of the video.
  109. bool pointIsInCutout = cutoutWidth != 0.0 &&
  110. cutoutHeight != 0.0 &&
  111. nonflippedX >= _VideoCutoutRect.x &&
  112. nonflippedX <= _VideoCutoutRect.x + cutoutWidth &&
  113. nonflippedY >= _VideoCutoutRect.y &&
  114. nonflippedY <= _VideoCutoutRect.y + cutoutHeight;
  115. if (pointIsInCutout) {
  116. // Use a threshold of 0.15 to consider a pixel as black.
  117. bool pixelIsBlack = all(lessThan(col.xyz, vec3(0.15, 0.15, 0.15)));
  118. if (pixelIsBlack) {
  119. col = vec4(0.0, 0.0, 0.0, 0.0);
  120. }
  121. }
  122. float cropWidth = _CropRect.z;
  123. float cropHeight = _CropRect.w;
  124. bool pointIsOutsideOfCrop = cropWidth != 0.0 &&
  125. cropHeight != 0.0 &&
  126. (nonflippedX < _CropRect.x || nonflippedX > _CropRect.x + cropWidth || nonflippedY < _CropRect.y || nonflippedY > _CropRect.y + cropHeight);
  127. if (pointIsOutsideOfCrop) {
  128. col = vec4(0.0, 0.0, 0.0, 0.0);
  129. }
  130. // Place color correction last so it doesn't effect cutout rect functionality.
  131. #ifndef UNITY_COLORSPACE_GAMMA
  132. col = vec4(GammaToLinearSpace(col.xyz), col.w);
  133. #endif
  134. // Multiply the alpha by the vertex color's alpha to support CanvasGroup.alpha.
  135. gl_FragColor = vec4(col.xyz, col.w * vertexColor.w);
  136. }
  137. #endif
  138. ENDGLSL
  139. }
  140. }
  141. Fallback "Unlit/Texture"
  142. }