ZED_Forward.shader 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //======= Copyright (c) Stereolabs Corporation, All rights reserved. ===============
  2. // Sets the ZED depth after the depth texture of Unity is created
  3. Shader "ZED/ZED Forward"
  4. {
  5. Properties
  6. {
  7. [HideInInspector] _MainTex("Base (RGB) Trans (A)", 2D) = "" {}
  8. _DepthXYZTex("Depth texture", 2D) = "" {}
  9. _CameraTex("Texture from ZED", 2D) = "" {}
  10. }
  11. SubShader
  12. {
  13. Stencil{
  14. Ref 129
  15. Comp[_ZEDStencilComp]
  16. Pass keep
  17. }
  18. Pass
  19. {
  20. Tags { "RenderType"="Opaque" "Queue"="Geometry" "LightMode" = "Always" }
  21. ZWrite On
  22. ZTest Always
  23. Cull Off
  24. CGPROGRAM
  25. #pragma target 4.0
  26. #pragma vertex vert
  27. #pragma fragment frag
  28. #pragma exclude_renderers nomrt
  29. #pragma multi_compile __ ZED_XYZ
  30. #pragma multi_compile __ DEPTH_ALPHA
  31. #pragma multi_compile __ NO_DEPTH
  32. #include "UnityCG.cginc"
  33. #include "../ZED_Utils.cginc"
  34. #include "Lighting.cginc"
  35. #include "UnityCG.cginc"
  36. float4 _MaskTex_ST;
  37. struct v2f
  38. {
  39. float4 pos : SV_POSITION;
  40. float4 screenUV : TEXCOORD0;
  41. float4 depthUV : TEXCOORD1;
  42. };
  43. v2f vert (float3 v : POSITION)
  44. {
  45. v2f o;
  46. #if SHADER_API_D3D11
  47. o.pos = float4(v.x*2.0,v.y*2.0,0,1);
  48. #elif SHADER_API_GLCORE
  49. o.pos = float4(v.x*2.0, -v.y*2.0, 0, 1);
  50. #else
  51. o.pos = float4(v.x*2.0, v.y*2.0, 0, 1);
  52. #endif
  53. o.screenUV = float4(v.x-0.5,v.y-0.5,0,1);
  54. o.depthUV = float4(v.x + 0.5, v.y + 0.5, 0, 1);
  55. return o;
  56. }
  57. sampler2D _MainTex;
  58. sampler2D _DepthXYZTex;
  59. uniform float4 _DepthXYZTex_TexelSize;
  60. sampler2D ZEDMaskTexGreenScreen;
  61. int ZEDGreenScreenActivated;
  62. int ZED_GreenScreen_BG;
  63. void frag(v2f i,
  64. out half4 outColor : SV_Target0,
  65. out float outDepth:SV_Depth)
  66. {
  67. float2 uv = i.screenUV.xy / i.screenUV.w;
  68. float2 depthUV = i.depthUV.xy / i.depthUV.w;
  69. float2 uvTex = float2(uv.x + 1.0f, uv.y + 1.0f);
  70. uvTex.y = 1 - uvTex.y;
  71. outColor = tex2D (_MainTex, uv).bgra;
  72. #if defined(ZED_XYZ)
  73. float4 dxyz = tex2D(_DepthXYZTex, depthUV).xyzw;
  74. float realDepth = computeDepthXYZ(dxyz.rgb);
  75. #else
  76. float4 dxyz = tex2D(_DepthXYZTex, depthUV).xxxx;
  77. float realDepth = computeDepthXYZ(dxyz.r);
  78. #endif
  79. //For the green screen to apply a mask on depth
  80. #if DEPTH_ALPHA
  81. float a = (tex2D(ZEDMaskTexGreenScreen, uvTex).a);
  82. a = a <= 0.5 ? 0 : 1;
  83. outDepth = realDepth*a;
  84. #if NO_DEPTH
  85. outDepth = 0;
  86. #endif
  87. #else
  88. outDepth = realDepth;
  89. #endif
  90. outColor = 0;
  91. }
  92. ENDCG
  93. }
  94. }
  95. }