Preprocess.shader 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //======= Copyright (c) Stereolabs Corporation, All rights reserved. ===============
  2. //Applies the despill factor, the white clip and black clip on the ZED texture
  3. Shader "Custom/Green Screen/Preprocess"
  4. {
  5. Properties
  6. {
  7. _MainTex ("Texture", 2D) = "white" {}
  8. _MaskTex("Texture from ZED", 2D) = "" {}
  9. }
  10. SubShader
  11. {
  12. Tags { "RenderType"="Opaque" }
  13. LOD 100
  14. Pass
  15. {
  16. CGPROGRAM
  17. #pragma vertex vert
  18. #pragma fragment frag
  19. #include "UnityCG.cginc"
  20. struct appdata
  21. {
  22. float4 vertex : POSITION;
  23. float2 uv : TEXCOORD0;
  24. };
  25. struct v2f
  26. {
  27. float2 uv : TEXCOORD0;
  28. float4 vertex : SV_POSITION;
  29. };
  30. sampler2D _MaskTex;
  31. sampler2D _MainTex;
  32. float4 _MainTex_ST;
  33. float4 _MainTex_TexelSize;
  34. float _erosion;
  35. uniform float4 _keyColor;
  36. uniform float _smoothness;
  37. uniform float _range;
  38. uniform float _spill;
  39. float _whiteClip;
  40. float _blackClip;
  41. v2f vert (appdata v)
  42. {
  43. v2f o;
  44. o.vertex = UnityObjectToClipPos(v.vertex);
  45. o.uv = TRANSFORM_TEX(v.uv, _MainTex);
  46. return o;
  47. }
  48. fixed4 frag (v2f i) : SV_Target
  49. {
  50. //Get the depth in XYZ format
  51. float2 uv = i.uv;
  52. //Color from the camera
  53. float3 colorCamera = tex2D(_MainTex, float2(uv.x, 1 - uv.y)).bgr;
  54. float alpha = tex2D(_MaskTex, uv).r;
  55. fixed4 o;
  56. o.rgb = colorCamera.rgb;
  57. o.a = 1;
  58. // sample the texture
  59. float fullMask = pow(saturate(alpha / _smoothness), 1.5);
  60. o.a = fullMask;
  61. //To use the despill
  62. float spillVal = pow(saturate(alpha / _spill), 1.5);
  63. float desat = (colorCamera.r * 0.2126 + colorCamera.g * 0.7152 + colorCamera.b * 0.0722);
  64. o.rgb = float3(desat, desat, desat) * (1. - spillVal) + colorCamera.rgb * (spillVal);
  65. float2 uv1 = clamp(uv + float2(-_MainTex_TexelSize.x*_erosion, 0), fixed2(_MainTex_TexelSize.x, _MainTex_TexelSize.y), fixed2(1 - _MainTex_TexelSize.x, 1 - _MainTex_TexelSize.y));
  66. float2 uv3 = clamp(uv + float2(0, -_MainTex_TexelSize.y*_erosion), fixed2(_MainTex_TexelSize.x, _MainTex_TexelSize.y), fixed2(1 - _MainTex_TexelSize.x, 1 - _MainTex_TexelSize.y));
  67. float2 uv5 = clamp(uv + float2(_MainTex_TexelSize.x*_erosion, 0), fixed2(_MainTex_TexelSize.x, _MainTex_TexelSize.y), fixed2(1 - _MainTex_TexelSize.x, 1 - _MainTex_TexelSize.y));
  68. float2 uv7 = clamp(uv + float2(0, _MainTex_TexelSize.y*_erosion), fixed2(_MainTex_TexelSize.x, _MainTex_TexelSize.y), fixed2(1 - _MainTex_TexelSize.x, 1 - _MainTex_TexelSize.y));
  69. if (_erosion >= 1) {
  70. //Erosion with one pass not optimized, prefer erosion with multi pass
  71. //0 | X | 0
  72. //X | 0 | X
  73. //0 | X | 0
  74. //X are the sampling done
  75. float a1 = pow(saturate(tex2D(_MaskTex, uv1).r / _smoothness), 1.5);
  76. float a2 = pow(saturate(tex2D(_MaskTex, uv3).r / _smoothness), 1.5);
  77. float a3 = pow(saturate(tex2D(_MaskTex, uv5).r / _smoothness), 1.5);
  78. float a4 = pow(saturate(tex2D(_MaskTex, uv7).r / _smoothness), 1.5);
  79. o.a = min(min(min(min(o.a, a1), a2), a3), a4);
  80. }
  81. else {
  82. o.a = fullMask;
  83. }
  84. if (o.a > _whiteClip) o.a = 1;
  85. else if (o.a < _blackClip) o.a = 0;
  86. return o;
  87. }
  88. ENDCG
  89. }
  90. }
  91. }