GreenScreen.shader 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //======= Copyright (c) Stereolabs Corporation, All rights reserved. ===============
  2. //Sets the transparency. Add the shadows and the lights
  3. Shader "Custom/Green Screen/Green Screen" {
  4. Properties
  5. {
  6. [MaterialToggle] directionalLightEffect("The directional light affects the real", Int) = 0
  7. }
  8. SubShader
  9. {
  10. Tags{
  11. "RenderType" = "Transparent"
  12. "Queue" = "Transparent-1"
  13. "LightMode" = "Always"
  14. }
  15. Pass
  16. {
  17. /*To use as a garbage matte*/
  18. Stencil{
  19. Ref 129
  20. Comp[_ZEDStencilComp]
  21. Pass keep
  22. }
  23. ZWrite On
  24. Blend SrcAlpha OneMinusSrcAlpha
  25. CGPROGRAM
  26. #pragma vertex vert
  27. #pragma fragment frag
  28. #pragma multi_compile FINAL FOREGROUND BACKGROUND ALPHA KEY
  29. #pragma multi_compile __ ZED_XYZ
  30. #pragma target 4.0
  31. #include "UnityCG.cginc"
  32. #include "../../../SDK/Helpers/Shaders/ZED_Utils.cginc"
  33. #define ZED_SPOT_LIGHT_DECLARATION
  34. #define ZED_POINT_LIGHT_DECLARATION
  35. #include "../../../SDK/Helpers/Shaders/Lighting/ZED_Lighting.cginc"
  36. struct appdata
  37. {
  38. float4 vertex : POSITION;
  39. float4 uv : TEXCOORD0;
  40. };
  41. struct v2f
  42. {
  43. float4 uv : TEXCOORD0;
  44. float4 pos : SV_POSITION;
  45. ZED_WORLD_DIR(1)
  46. };
  47. struct fragOut {
  48. float depth : SV_Depth;
  49. fixed4 color : SV_Target;
  50. };
  51. sampler2D _DepthXYZTex;
  52. sampler2D _CameraTex;
  53. float4 _DepthXYZTex_ST;
  54. float4 _CameraTex_ST;
  55. sampler2D _MaskTex;
  56. uniform float4x4 _ProjectionMatrix;
  57. float4 ZED_directionalLight[2];
  58. int directionalLightEffect;
  59. int _HasShadows;
  60. v2f vert(appdata_full v)
  61. {
  62. v2f o;
  63. o.pos = UnityObjectToClipPos(v.vertex);
  64. ZED_TRANSFER_WORLD_DIR(o)
  65. o.uv.xy = TRANSFORM_TEX(v.texcoord, _CameraTex);
  66. o.uv.zw = TRANSFORM_TEX(v.texcoord, _DepthXYZTex);
  67. o.uv.y = 1 - o.uv.y;
  68. o.uv.w = 1 - o.uv.w;
  69. return o;
  70. }
  71. uint _numberColors;
  72. float4 _CameraTex_TexelSize;
  73. int _erosion;
  74. uniform float4 _keyColor;
  75. uniform float _smoothness;
  76. uniform float _range;
  77. uniform float _spill;
  78. float _whiteClip;
  79. float _blackClip;
  80. sampler2D _DirectionalShadowMap;
  81. float4 _AmnbientLight;
  82. fragOut frag(v2f i)
  83. {
  84. //Get the depth in XYZ format
  85. float4 uv = i.uv;
  86. float3 colorXYZ = tex2D(_DepthXYZTex, uv.zw).xxx;
  87. //Compute the depth to work with Unity (1m real = 1m to Unity)
  88. float depthReal = computeDepthXYZ(colorXYZ.r);
  89. //Color from the camera
  90. float3 colorCamera = tex2D(_CameraTex, uv.xy).bgr;
  91. float3 normals = tex2D(_NormalsTex, uv.zw).rgb;
  92. float alpha = tex2D(_MaskTex, float2(uv.x, 1 - uv.y)).a;
  93. fragOut o;
  94. o.color.rgb = colorCamera.rgb;
  95. o.color.a = 1;
  96. float a = alpha <= 0.0 ? 0 : 1;
  97. o.depth = depthReal*a;
  98. #ifndef FOREGROUND
  99. float4 color = tex2D(_MaskTex, float2(uv.x, 1 - uv.y)).rgba;
  100. half4 c = color;
  101. //Apply directional light
  102. if (directionalLightEffect == 1) {
  103. color *= ZED_directionalLight[1];
  104. }
  105. //Apply Shadows
  106. if (_HasShadows == 1) {
  107. float3 shadows = tex2D(_DirectionalShadowMap, fixed2(uv.z, 1 - uv.w)).rgb;
  108. c = color*(half4(saturate(shadows),1));
  109. }
  110. else {
  111. c = half4(color);
  112. }
  113. //Compute world space
  114. float3 worldspace;
  115. GET_XYZ(i, colorXYZ.x, worldspace)
  116. // Apply lighting
  117. c += saturate(computeLighting(color.rgb, normals, worldspace, 1));
  118. o.color.a = alpha;
  119. o.color.rgb = c.rgb;
  120. #else
  121. o.depth = MAX_DEPTH;
  122. o.color.a = 1;
  123. #endif
  124. #ifdef ALPHA
  125. o.color.r = o.color.a;
  126. o.color.g = o.color.a;
  127. o.color.b = o.color.a;
  128. o.color.a = 1;
  129. o.depth = MAX_DEPTH;
  130. #endif
  131. #ifdef BACKGROUND
  132. o.color.a = 0;
  133. #endif
  134. #ifdef KEY
  135. o.depth = MAX_DEPTH;
  136. o.color.rgb = tex2D (_MaskTex, float2(uv.x, 1 - uv.y)).rgb;
  137. o.color.rgb *= alpha;
  138. o.color.rgb = clamp(o.color.rgb, float3(0., 0., 0.), float3(1, 1, 1));
  139. o.color.a = 1;
  140. #endif
  141. return o;
  142. }
  143. ENDCG
  144. }
  145. }
  146. }