DefaultWebShader.shader 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright (c) 2024 Vuplex Inc. All rights reserved.
  2. //
  3. // Licensed under the Vuplex Commercial Software Library License, you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // https://vuplex.com/commercial-library-license
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. Shader "Vuplex/Default Web Shader" {
  15. Properties {
  16. _MainTex ("Base (RGB)", 2D) = "white" {}
  17. [Toggle(FLIP_X)] _FlipX ("Flip X", Float) = 0
  18. [Toggle(FLIP_Y)] _FlipY ("Flip Y", Float) = 0
  19. [Header(Properties set programmatically)]
  20. _VideoCutoutRect("Video Cutout Rect", Vector) = (0, 0, 0, 0)
  21. _CropRect("Crop Rect", Vector) = (0, 0, 0, 0)
  22. // Include these UI properties from UI-Default.shader
  23. // in order to support UI Scroll Views.
  24. _StencilComp ("Stencil Comparison", Float) = 8
  25. _Stencil ("Stencil ID", Float) = 0
  26. _StencilOp ("Stencil Operation", Float) = 0
  27. _StencilWriteMask ("Stencil Write Mask", Float) = 255
  28. _StencilReadMask ("Stencil Read Mask", Float) = 255
  29. _ColorMask ("Color Mask", Float) = 15
  30. }
  31. SubShader {
  32. Pass {
  33. Tags { "Queue" = "Transparent" "RenderType" = "Transparent" }
  34. // Include these UI properties from UI-Default.shader
  35. // in order to support UI Scroll Views.
  36. Stencil {
  37. Ref [_Stencil]
  38. Comp [_StencilComp]
  39. Pass [_StencilOp]
  40. ReadMask [_StencilReadMask]
  41. WriteMask [_StencilWriteMask]
  42. }
  43. Lighting Off
  44. ZWrite Off
  45. Blend SrcAlpha OneMinusSrcAlpha
  46. ColorMask [_ColorMask]
  47. CGPROGRAM
  48. #pragma multi_compile ___ FLIP_X
  49. #pragma multi_compile ___ FLIP_Y
  50. #pragma vertex vert
  51. #pragma fragment frag
  52. #include "UnityCG.cginc"
  53. struct appdata {
  54. float4 vertex : POSITION;
  55. float2 uv : TEXCOORD0;
  56. // Pass the vertex color to the fragment shader
  57. // so that it can be used for calculating alpha.
  58. // This is needed, for example, to allow CanvasGroup.alpha
  59. // to control the alpha.
  60. float4 vertexColor : COLOR;
  61. // For Single Pass Instanced stereo rendering
  62. UNITY_VERTEX_INPUT_INSTANCE_ID
  63. };
  64. struct v2f {
  65. float2 uv : TEXCOORD0;
  66. float4 vertex : SV_POSITION;
  67. float4 vertexColor : COLOR0;
  68. UNITY_VERTEX_OUTPUT_STEREO
  69. };
  70. Texture2D _MainTex;
  71. // Specify linear filtering by using a SamplerState
  72. // and specifying "linear" in its name.
  73. // https://docs.unity3d.com/Manual/SL-SamplerStates.html
  74. SamplerState linear_clamp_sampler;
  75. float4 _MainTex_ST;
  76. v2f vert(appdata v) {
  77. v2f o;
  78. // For Single Pass Instanced stereo rendering
  79. UNITY_SETUP_INSTANCE_ID(v);
  80. UNITY_INITIALIZE_OUTPUT(v2f, o);
  81. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  82. o.vertex = UnityObjectToClipPos(v.vertex);
  83. o.vertexColor = v.vertexColor;
  84. float2 untransformedUV = v.uv;
  85. #ifdef FLIP_X
  86. untransformedUV.x = 1.0 - untransformedUV.x;
  87. #endif
  88. #ifdef FLIP_Y
  89. untransformedUV.y = 1.0 - untransformedUV.y;
  90. #endif
  91. o.uv = TRANSFORM_TEX(untransformedUV, _MainTex);
  92. return o;
  93. }
  94. float4 _VideoCutoutRect;
  95. float4 _CropRect;
  96. fixed4 frag(v2f i) : SV_Target {
  97. fixed4 col = _MainTex.Sample(linear_clamp_sampler, i.uv);
  98. float cutoutWidth = _VideoCutoutRect.z;
  99. float cutoutHeight = _VideoCutoutRect.w;
  100. #ifdef FLIP_X
  101. float nonflippedX = 1.0 - i.uv.x;
  102. #else
  103. float nonflippedX = i.uv.x;
  104. #endif
  105. #ifdef FLIP_Y
  106. float nonflippedY = i.uv.y;
  107. #else
  108. float nonflippedY = 1.0 - i.uv.y;
  109. #endif
  110. // Make the pixels transparent if they fall within the video rect cutout and the they're black.
  111. // Keeping non-black pixels allows the video controls to still show up on top of the video.
  112. bool pointIsInCutout = cutoutWidth != 0.0 &&
  113. cutoutHeight != 0.0 &&
  114. nonflippedX >= _VideoCutoutRect.x &&
  115. nonflippedX <= _VideoCutoutRect.x + cutoutWidth &&
  116. nonflippedY >= _VideoCutoutRect.y &&
  117. nonflippedY <= _VideoCutoutRect.y + cutoutHeight;
  118. if (pointIsInCutout) {
  119. // Use a threshold of 0.15 to consider a pixel as black.
  120. bool pixelIsBlack = all(col.xyz < float3(0.15, 0.15, 0.15));
  121. if (pixelIsBlack) {
  122. col = float4(0.0, 0.0, 0.0, 0.0);
  123. }
  124. }
  125. float cropWidth = _CropRect.z;
  126. float cropHeight = _CropRect.w;
  127. bool pointIsOutsideOfCrop = cropWidth != 0.0 &&
  128. cropHeight != 0.0 &&
  129. (nonflippedX < _CropRect.x || nonflippedX > _CropRect.x + cropWidth ||nonflippedY < _CropRect.y || nonflippedY > _CropRect.y + cropHeight);
  130. if (pointIsOutsideOfCrop) {
  131. col = float4(0.0, 0.0, 0.0, 0.0);
  132. }
  133. // Color correction to convert gamma to linear space.
  134. // This is performed last so it doesn't effect cutout rect functionality.
  135. // Unity 2023.1 and 2023.2 have a bug where color correction is applied automatically for Vulkan on Android,
  136. // so color correction is skipped in that scenario.
  137. #if !defined(UNITY_COLORSPACE_GAMMA) && !(defined(SHADER_API_VULKAN) && UNITY_VERSION >= 202300)
  138. col = float4(GammaToLinearSpace(col.xyz), col.w);
  139. #endif
  140. // Multiply the alpha by the vertex color's alpha to support CanvasGroup.alpha.
  141. col = float4(col.xyz, col.w * i.vertexColor.w);
  142. return col;
  143. }
  144. ENDCG
  145. }
  146. }
  147. Fallback "Unlit/Texture"
  148. }