ComputeGreenScreen.shader 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //======= Copyright (c) Stereolabs Corporation, All rights reserved. ===============
  2. // Compares the YUV texture (input) and a YUV color. Set the comparison into the red canal
  3. Shader "Custom/Green Screen/Compute GreenScreen" {
  4. Properties
  5. {
  6. _MainTex("Texture from ZED", 2D) = "" {}
  7. }
  8. SubShader
  9. {
  10. Pass
  11. {
  12. Cull Off
  13. ZWrite Off
  14. Lighting Off
  15. CGPROGRAM
  16. #pragma vertex vert
  17. #pragma fragment frag
  18. #pragma target 4.0
  19. #include "UnityCG.cginc"
  20. #include "../../../SDK/Helpers/Shaders/ZED_Utils.cginc"
  21. struct appdata
  22. {
  23. float4 vertex : POSITION;
  24. float2 uv : TEXCOORD0;
  25. };
  26. struct v2f
  27. {
  28. float2 uv : TEXCOORD0;
  29. float4 vertex : SV_POSITION;
  30. };
  31. sampler2D _MainTex;
  32. float4 _MainTex_ST;
  33. v2f vert(appdata v)
  34. {
  35. v2f o;
  36. o.vertex = UnityObjectToClipPos(v.vertex);
  37. o.uv = TRANSFORM_TEX(v.uv, _MainTex);
  38. return o;
  39. }
  40. uint _numberColors;
  41. float4 _MainTex_TexelSize;
  42. int _erosion;
  43. uniform float4 _keyColor;
  44. uniform float _range;
  45. float4 frag(v2f i) : SV_Target
  46. {
  47. //Get the depth in XYZ format
  48. float2 uv = i.uv;
  49. uv.y = 1.0 - uv.y;
  50. //Color from the camera
  51. float3 colorCamera = RGBtoYUV(tex2D(_MainTex, uv).bgr);
  52. float alpha = 1;
  53. uint index = 0;
  54. //Compute the 4 UVs they need to be clamped. If these values are changed lines may appear on the screen
  55. float2 uv2 = clamp(uv + float2(-_MainTex_TexelSize.x, -_MainTex_TexelSize.y), fixed2(_MainTex_TexelSize.x, _MainTex_TexelSize.y), fixed2(1 - _MainTex_TexelSize.x, 1 - _MainTex_TexelSize.y));
  56. float2 uv4 = clamp(uv + float2(_MainTex_TexelSize.x, -_MainTex_TexelSize.y), fixed2(_MainTex_TexelSize.x, _MainTex_TexelSize.y), fixed2(1 - _MainTex_TexelSize.x, 1 - _MainTex_TexelSize.y));
  57. float2 uv6 = clamp(uv + float2(-_MainTex_TexelSize.x, _MainTex_TexelSize.y), fixed2(_MainTex_TexelSize.x, _MainTex_TexelSize.y), fixed2(1 - _MainTex_TexelSize.x, 1 - _MainTex_TexelSize.y));
  58. float2 uv8 = clamp(uv + float2(_MainTex_TexelSize.x, _MainTex_TexelSize.y), fixed2(_MainTex_TexelSize.x, _MainTex_TexelSize.y), fixed2(1 - _MainTex_TexelSize.x, 1 - _MainTex_TexelSize.y));
  59. float2 uv1 = clamp(uv + float2(-_MainTex_TexelSize.x, 0), fixed2(_MainTex_TexelSize.x, _MainTex_TexelSize.y), fixed2(1 - _MainTex_TexelSize.x, 1 - _MainTex_TexelSize.y));
  60. float2 uv3 = clamp(uv + float2(_MainTex_TexelSize.x, 0), fixed2(_MainTex_TexelSize.x, _MainTex_TexelSize.y), fixed2(1 - _MainTex_TexelSize.x, 1 - _MainTex_TexelSize.y));
  61. float2 uv5 = clamp(uv + float2(0, _MainTex_TexelSize.y), fixed2(_MainTex_TexelSize.x, _MainTex_TexelSize.y), fixed2(1 - _MainTex_TexelSize.x, 1 - _MainTex_TexelSize.y));
  62. float2 uv7 = clamp(uv + float2(0, -_MainTex_TexelSize.y), fixed2(_MainTex_TexelSize.x, _MainTex_TexelSize.y), fixed2(1 - _MainTex_TexelSize.x, 1 - _MainTex_TexelSize.y));
  63. //X | 0 | X
  64. //0 | X | 0
  65. //X | 0 | X
  66. //X are the sampling done
  67. float a = computeAlphaYUVFromYUV(RGBtoYUV(tex2D(_MainTex, uv2).bgr), _keyColor.rgb)
  68. + computeAlphaYUVFromYUV(RGBtoYUV(tex2D(_MainTex, uv4).bgr), _keyColor.rgb)
  69. + computeAlphaYUVFromYUV(RGBtoYUV(tex2D(_MainTex, uv8).bgr), _keyColor.rgb)
  70. + computeAlphaYUVFromYUV(RGBtoYUV(tex2D(_MainTex, uv6).bgr), _keyColor.rgb)
  71. + computeAlphaYUVFromYUV(RGBtoYUV(tex2D(_MainTex, uv1).bgr), _keyColor.rgb)
  72. + computeAlphaYUVFromYUV(RGBtoYUV(tex2D(_MainTex, uv3).bgr), _keyColor.rgb)
  73. + computeAlphaYUVFromYUV(RGBtoYUV(tex2D(_MainTex, uv5).bgr), _keyColor.rgb)
  74. + computeAlphaYUVFromYUV(RGBtoYUV(tex2D(_MainTex, uv7).bgr), _keyColor.rgb)
  75. + computeAlphaYUVFromYUV(colorCamera.rgb, _keyColor);
  76. a /= 9.0;
  77. alpha = a;
  78. //return in the red canal, because this shader is used with a rendertexture RFloat
  79. return saturate(float4(alpha - _range, 1,1,1));
  80. }
  81. ENDCG
  82. }
  83. }
  84. }