ZED_Deferred.shader 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //======= Copyright (c) Stereolabs Corporation, All rights reserved. ===============
  2. //Set the depth/normals just after the depth texture is created
  3. Shader "ZED/ZED Deferred"
  4. {
  5. Properties
  6. {
  7. [HideInInspector] _MainTex("Base (RGB) Trans (A)", 2D) = "" {}
  8. _DepthXYZTex("Depth texture", 2D) = "" {}
  9. _CameraTex("Texture from ZED", 2D) = "" {}
  10. _MaxDepth("Max Depth Range", Range(1,20)) = 20
  11. }
  12. SubShader
  13. {
  14. Pass
  15. {
  16. // Stencil is 2^8 bits, Unity uses 4 first bits
  17. // 1 0 0 0 0 0 0 0 enables all lights
  18. // 1 1 0 0 0 0 0 0 enables all except the light may be not rendered if too far way
  19. // 1 1 1 0 0 0 0 0 enables all lights
  20. Stencil {
  21. Ref 128
  22. Comp always
  23. Pass replace
  24. }
  25. ZWrite On
  26. Cull Front
  27. CGPROGRAM
  28. #pragma target 4.0
  29. #pragma vertex vert
  30. #pragma fragment frag
  31. #include "UnityCG.cginc"
  32. #include "../ZED_Utils.cginc"
  33. #include "Lighting.cginc"
  34. #include "UnityCG.cginc"
  35. #pragma multi_compile ___ UNITY_HDR_ON
  36. #pragma multi_compile __ ZED_XYZ
  37. #pragma multi_compile __ NO_DEPTH
  38. struct v2f
  39. {
  40. float4 pos : POSITION;
  41. float4 screenUV : TEXCOORD0;
  42. float4 depthUV : TEXCOORD1;
  43. };
  44. sampler2D _MainTex;
  45. float4 _MainTex_ST;
  46. float4x4 _Model;
  47. float4x4 _Projection;
  48. sampler2D _DepthXYZTex;
  49. float4 _DepthXYZTex_TexelSize;
  50. float4 _DepthXYZTex_ST;
  51. v2f vert (v2f v)
  52. {
  53. v2f o;
  54. #if SHADER_API_D3D11
  55. o.pos = float4(v.pos.x*2.0, v.pos.y*2.0, 0, 1);
  56. #elif SHADER_API_GLCORE || SHADER_API_VULKAN
  57. o.pos = float4(v.pos.x*2.0, -v.pos.y*2.0, 0, 1);
  58. #endif
  59. o.screenUV = float4(v.pos.x - 0.5, v.pos.y - 0.5, 0, 1);
  60. o.depthUV = float4(v.pos.x + 0.5f, v.pos.y + 0.5f, 0, 1);
  61. return o;
  62. }
  63. sampler2D _MaskTex;
  64. sampler2D _NormalsTex;
  65. float _Exposure;
  66. uniform int _ZEDReferenceMeasure;
  67. uniform int ZEDGreenScreenActivated;
  68. sampler2D ZEDMaskTexGreenScreen;
  69. float _ZEDFactorAffectReal;
  70. float _MaxDepth;
  71. void frag(v2f i,
  72. out half4 outColor : SV_Target0,
  73. out half4 outSpecRoughness : SV_Target1,
  74. out half4 outNormal : SV_Target2,
  75. out half4 outEmission : SV_Target3,
  76. out float outDepth:DEPTH)
  77. {
  78. float2 uv = i.screenUV.xy / i.screenUV.w;
  79. #if defined(ZED_XYZ)
  80. float4 dxyz = tex2D (_DepthXYZTex, uv).xyzw;
  81. float d = computeDepthXYZ(dxyz.xyz);
  82. #else
  83. float4 dxyz = tex2D(_DepthXYZTex, i.depthUV).xxxx;
  84. //Filter out depth values beyond the max value.
  85. if (_MaxDepth < 20.0) //Avoid clipping out FAR values when not using feature.
  86. {
  87. //if (dxyz.z > _MaxDepth) discard;
  88. if (dxyz.z > _MaxDepth) discard;
  89. }
  90. float d = computeDepthXYZ(dxyz.x);
  91. #endif
  92. outSpecRoughness = half4(0,0,0,0);
  93. float3 normals = tex2D(_NormalsTex, i.depthUV).rgb;
  94. outColor = saturate(tex2D (_MainTex, i.depthUV).bgra);
  95. outColor *= _ZEDFactorAffectReal;
  96. #ifdef NO_DEPTH
  97. #if SHADER_API_D3D11
  98. outDepth = 0;
  99. #elif SHADER_API_GLCORE
  100. outDepth = 1000;//fake infinite depth
  101. #endif
  102. #else
  103. outDepth = saturate(d);
  104. #endif
  105. if (ZEDGreenScreenActivated == 1) {
  106. float a = (tex2D(ZEDMaskTexGreenScreen, float2(i.depthUV.x, 1 - i.depthUV.y)).a);
  107. a = a <= 0.5 ? 0 : 1;
  108. outDepth *= a;
  109. }
  110. #if UNITY_HDR_ON
  111. outEmission = half4(0,0,0,0.5);
  112. #else
  113. outEmission = half4(1,1,1,0);
  114. #endif
  115. outColor.a = 0;
  116. //Normal to world pos
  117. normals.rgb = mul((float3x3)unity_CameraToWorld, float3(normals)).rgb;
  118. normals = normalize(normals);
  119. outNormal.rgb = normals*0.5 + 0.5;
  120. outNormal.w = 0.33; // Used as mask
  121. }
  122. ENDCG
  123. }
  124. }
  125. }