GreenScreen_URP.shader 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 URP" {
  4. Properties
  5. {
  6. [MaterialToggle] directionalLightEffect("The directional light affects the real", Int) = 0
  7. _CameraTex("CameraTex", 2D) = "defaulttexture" {}
  8. }
  9. SubShader
  10. {
  11. Tags{
  12. "RenderPipeline"="UniversalPipeline"
  13. "RenderType" = "Transparent"
  14. "Queue" = "Transparent-1"
  15. "LightMode" = "UniversalForward"
  16. }
  17. Pass
  18. {
  19. /*To use as a garbage matte*/
  20. Stencil{
  21. Ref 129
  22. Comp[_ZEDStencilComp]
  23. Pass keep
  24. }
  25. ZWrite On
  26. Blend SrcAlpha OneMinusSrcAlpha
  27. HLSLPROGRAM
  28. #pragma multi_compile FINAL FOREGROUND BACKGROUND ALPHA KEY
  29. #pragma multi_compile __ ZED_XYZ
  30. #pragma prefer_hlslcc gles
  31. #pragma exclude_renderers d3d11_9x
  32. #pragma target 2.0
  33. #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
  34. #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
  35. #pragma shader_feature _RECEIVE_SHADOWS_OFF
  36. #pragma multi_compile _ _SHADOWS_SOFT
  37. #pragma vertex vert
  38. #pragma fragment frag
  39. //#include "UnityCG.cginc"
  40. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  41. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  42. #include "../../../SDK/Helpers/Shaders/ZED_Utils.cginc"
  43. #define ZED_SPOT_LIGHT_DECLARATION
  44. #define ZED_POINT_LIGHT_DECLARATION
  45. #include "../../../SDK/Helpers/Shaders/Lighting/ZED_Lighting_URP.cginc"
  46. struct appdata
  47. {
  48. float4 vertex : POSITION;
  49. float4 uv : TEXCOORD0;
  50. };
  51. struct Attributes
  52. {
  53. float4 positionOS : POSITION;
  54. float3 normalOS : NORMAL;
  55. float4 tangentOS : TANGENT;
  56. float2 uv : TEXCOORD0;
  57. float2 uvLM : TEXCOORD1;
  58. UNITY_VERTEX_INPUT_INSTANCE_ID
  59. };
  60. struct Varyings
  61. {
  62. float4 uv : TEXCOORD0;
  63. float3 positionWS : TEXCOORD1;
  64. float4 positionCS : SV_POSITION;
  65. };
  66. sampler2D _DepthXYZTex;
  67. sampler2D _CameraTex;
  68. float4 _DepthXYZTex_ST;
  69. float4 _CameraTex_ST;
  70. sampler2D _MaskTex;
  71. uniform float4x4 _ProjectionMatrix;
  72. float4 ZED_directionalLight[2];
  73. int directionalLightEffect;
  74. int _HasShadows;
  75. //Horizontal and vertical fields of view, assigned from ZEDRenderingPlane.
  76. //Needs to be assigned, not derived from projection matrix, because otherwise goofy things happen because of the Scene view camera.
  77. float _ZEDHFoVRad;
  78. float _ZEDVFoVRad;
  79. float _ZEDFactorAffectReal;
  80. float _MaxDepth;
  81. void vert(Attributes input, out Varyings output)
  82. {
  83. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  84. VertexNormalInputs vertexNormalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  85. // TRANSFORM_TEX is the same as the old shader library.
  86. output.uv.xy = TRANSFORM_TEX(input.uv, _CameraTex);
  87. output.uv.y = 1 - output.uv.y;
  88. output.uv.zw = TRANSFORM_TEX(input.uv, _DepthXYZTex);
  89. output.uv.w = 1 - output.uv.w;
  90. output.positionWS = vertexInput.positionWS;
  91. output.positionCS = vertexInput.positionCS;
  92. }
  93. uint _numberColors;
  94. float4 _CameraTex_TexelSize;
  95. int _erosion;
  96. uniform float4 _keyColor;
  97. uniform float _smoothness;
  98. uniform float _range;
  99. uniform float _spill;
  100. float _whiteClip;
  101. float _blackClip;
  102. sampler2D _DirectionalShadowMap;
  103. float4 _AmnbientLight;
  104. void frag(Varyings input, out half4 outColor: SV_Target, out float outDepth : SV_Depth)
  105. {
  106. /*outColor = half4(1, 0, 0, 1);
  107. outDepth = 1;
  108. return;*/
  109. //Get the depth in XYZ format
  110. float4 uv = input.uv;
  111. float zed_z = tex2D(_DepthXYZTex, uv.zw).x;
  112. //Compute the depth to work with Unity (1m real = 1m to Unity)
  113. float depthReal = computeDepthXYZ(zed_z);
  114. //Color from the camera
  115. float3 colorCamera = tex2D(_CameraTex, uv.xy).bgr;
  116. float3 normals = tex2D(_NormalsTex, uv.zw).rgb;
  117. float alpha = tex2D(_MaskTex, float2(uv.x, 1 - uv.y)).a;
  118. //fragOut o;
  119. outColor.rgb = colorCamera.rgb;
  120. outColor.a = 1;
  121. float a = alpha <= 0.0 ? 0 : 1;
  122. outDepth = depthReal * a;
  123. #ifndef FOREGROUND
  124. float4 color = tex2D(_MaskTex, float2(uv.x, 1 - uv.y)).rgba;
  125. half4 c = color;
  126. /*//Apply directional light
  127. if (directionalLightEffect == 1) {
  128. color *= ZED_directionalLight[1];
  129. }
  130. //Apply Shadows
  131. if (_HasShadows == 1) {
  132. float3 shadows = tex2D(_DirectionalShadowMap, half2(uv.z, 1 - uv.w)).rgb;
  133. c = color * (half4(saturate(shadows),1));
  134. }
  135. else {
  136. c = half4(color);
  137. }*/
  138. //Compute world normals.
  139. normals = float3(normals.x, 0 - normals.y, normals.z);
  140. float3 worldnormals = mul((float3x3)unity_ObjectToWorld, normals); //TODO: This erroneously applies object scale to the normals. The canvas object is scaled to fill the frame. Fix.
  141. //Compute world position of the pixel.
  142. float xfovpartial = (input.uv.x - 0.5) * _ZEDHFoVRad;
  143. float yfovpartial = (1 - input.uv.y - 0.5) * _ZEDVFoVRad;
  144. float xpos = tan(xfovpartial) * zed_z;
  145. float ypos = tan(yfovpartial) * zed_z;
  146. float3 camrelpose = float3(xpos, ypos, -zed_z);// +_WorldSpaceCameraPos;
  147. float3 worldPos = mul(UNITY_MATRIX_V, float4(camrelpose.xyz, 0)).xyz + _WorldSpaceCameraPos;
  148. // Apply lighting
  149. //c += saturate(computeLighting(color.rgb, normals, worldspace, 1, 1));
  150. c.rgb = computeLightingLWRP(colorCamera.rgb, worldnormals, worldPos, 1, _ZEDFactorAffectReal).rgb;
  151. outColor.a = alpha;
  152. outColor.rgb = c.rgb;
  153. //outColor.rgb = half3(1, 0, 0);
  154. #else
  155. outDepth = MAX_DEPTH;
  156. outColor.a = 1;
  157. #endif
  158. #ifdef ALPHA
  159. outColor.r = alpha;
  160. outColor.g = alpha;
  161. outColor.b = alpha;
  162. outColor.a = 1;
  163. outDepth = MAX_DEPTH;
  164. #endif
  165. #ifdef BACKGROUND
  166. outColor.a = 0;
  167. #endif
  168. #ifdef KEY
  169. outDepth = MAX_DEPTH;
  170. outColor.rgb = tex2D(_MaskTex, float2(uv.x, 1 - uv.y)).rgb;
  171. outColor.rgb *= alpha;
  172. outColor.rgb = clamp(c.rgb, float3(0.0, 0.0, 0.0), float3(1, 1, 1));
  173. outColor.a = 1;
  174. #endif
  175. //return o;
  176. }
  177. ENDHLSL
  178. }
  179. }
  180. }