ZED_Forward_Lighting.shader 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //======= Copyright (c) Stereolabs Corporation, All rights reserved. ===============
  2. // Computes lighting and shadows and apply them to the real
  3. Shader "ZED/ZED Forward Lighting"
  4. {
  5. Properties{
  6. [MaterialToggle] directionalLightEffect("Directional light affects image", Int) = 0
  7. _MaxDepth("Max Depth Range", Range(1,40)) = 40
  8. _DepthXYZTex("Depth texture", 2D) = "" {}
  9. _MainTex("Main texture", 2D) = "" {}
  10. }
  11. SubShader
  12. {
  13. ZWrite On
  14. Pass
  15. {
  16. Name "FORWARD"
  17. Tags{ "LightMode" = "Always" }
  18. Cull Off
  19. CGPROGRAM
  20. #define ZEDStandard
  21. // compile directives
  22. #pragma target 4.0
  23. #pragma vertex vert_surf
  24. #pragma fragment frag_surf
  25. #pragma multi_compile_fwdbase
  26. #pragma multi_compile_fwdadd_fullshadows
  27. #pragma multi_compile __ NO_DEPTH
  28. #include "HLSLSupport.cginc"
  29. #include "UnityShaderVariables.cginc"
  30. #define UNITY_PASS_FORWARDBASE
  31. #include "UnityCG.cginc"
  32. #include "AutoLight.cginc"
  33. #include "../ZED_Utils.cginc"
  34. #define ZED_SPOT_LIGHT_DECLARATION
  35. #define ZED_POINT_LIGHT_DECLARATION
  36. #include "ZED_Lighting.cginc"
  37. sampler2D _MainTex;
  38. struct Input {
  39. float2 uv_MainTex;
  40. };
  41. float4x4 _CameraMatrix;
  42. sampler2D _DirectionalShadowMap;
  43. struct v2f_surf {
  44. float4 pos : SV_POSITION;
  45. float4 pack0 : TEXCOORD0;
  46. SHADOW_COORDS(4)
  47. ZED_WORLD_DIR(1)
  48. };
  49. float4 _MainTex_ST;
  50. sampler2D _DepthXYZTex;
  51. float4 _DepthXYZTex_ST;
  52. sampler2D _MaskTex;
  53. int _HasShadows;
  54. float4 ZED_directionalLight[2];
  55. int directionalLightEffect;
  56. float _ZEDFactorAffectReal;
  57. float _MaxDepth;
  58. bool Unity_IsNan_float3(float3 In)
  59. {
  60. bool Out = (In < 0.0 || In > 0.0 || In == 0.0) ? 0 : 1;
  61. return Out;
  62. }
  63. // vertex shader
  64. v2f_surf vert_surf(appdata_full v)
  65. {
  66. v2f_surf o;
  67. UNITY_INITIALIZE_OUTPUT(v2f_surf,o);
  68. o.pos = UnityObjectToClipPos(v.vertex);
  69. ZED_TRANSFER_WORLD_DIR(o)
  70. o.pack0.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
  71. o.pack0.zw = TRANSFORM_TEX(v.texcoord, _DepthXYZTex);
  72. o.pack0.y = 1 - o.pack0.y;
  73. o.pack0.w = 1 - o.pack0.w;
  74. TRANSFER_SHADOW(o);
  75. return o;
  76. }
  77. // fragment shader
  78. void frag_surf(v2f_surf IN, out fixed4 outColor : SV_Target, out float outDepth : SV_Depth)
  79. {
  80. UNITY_INITIALIZE_OUTPUT(fixed4,outColor);
  81. float4 uv = IN.pack0;
  82. float3 zed_xyz = tex2D(_DepthXYZTex, uv.zw).xxx;
  83. //Filter out depth values beyond the max value.
  84. if (_MaxDepth < 40.0) //Avoid clipping out FAR values when not using feature.
  85. {
  86. if (zed_xyz.z > _MaxDepth || Unity_IsNan_float3(zed_xyz.z)) discard;
  87. }
  88. #ifdef NO_DEPTH
  89. #if SHADER_API_D3D11
  90. outDepth = 0;
  91. #elif SHADER_API_GLCORE
  92. outDepth = 1000;//fake infinite depth
  93. #endif
  94. #else
  95. outDepth = computeDepthXYZ(zed_xyz.z);
  96. #endif
  97. fixed4 c = 0;
  98. float4 color = tex2D(_MainTex, uv.xy).bgra;
  99. float3 normals = tex2D(_NormalsTex, uv.zw).rgb;
  100. //Apply directional light
  101. color *= _ZEDFactorAffectReal;
  102. //Compute world space
  103. float3 worldspace;
  104. GET_XYZ(IN, zed_xyz.x, worldspace)
  105. //Apply shadows
  106. // e(1) == 2.71828182846
  107. if (_HasShadows == 1) {
  108. //Depends on the ambient lighting
  109. float atten = saturate(tex2D(_DirectionalShadowMap, fixed2(uv.z, 1 - uv.w)) + log(1 + 1.72*length(UNITY_LIGHTMODEL_AMBIENT.rgb)/4.0));
  110. c = half4(color*atten);
  111. }
  112. else {
  113. c = half4(color);
  114. }
  115. //Add light
  116. c += saturate(computeLighting(color.rgb, normals, worldspace, 1));
  117. c.a = 0;
  118. outColor.rgb = c;
  119. }
  120. ENDCG
  121. }
  122. }
  123. Fallback Off
  124. }