ZED_Deferred_Lighting.shader 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Custom lighting effect on deferred
  2. Shader "ZED/ZED Deferred Lighting"
  3. {
  4. Properties {
  5. _LightTexture0 ("", any) = "" {}
  6. _LightTextureB0 ("", 2D) = "" {}
  7. _ShadowMapTexture ("", any) = "" {}
  8. _SrcBlend ("", Float) = 1
  9. _DstBlend ("", Float) = 1
  10. }
  11. SubShader {
  12. // Pass 1: Lighting pass
  13. // LDR case - Lighting encoded into a subtractive ARGB8 buffer
  14. // HDR case - Lighting additively blended into floating point buffer
  15. Pass {
  16. ZWrite Off
  17. Blend [_SrcBlend] [_DstBlend]
  18. CGPROGRAM
  19. #pragma target 3.0
  20. #pragma vertex vert_deferred
  21. #pragma fragment frag
  22. #pragma multi_compile_lightpass
  23. #pragma multi_compile ___ UNITY_HDR_ON
  24. #pragma exclude_renderers nomrt
  25. #include "UnityCG.cginc"
  26. #include "UnityDeferredLibrary.cginc"
  27. #include "UnityPBSLighting.cginc"
  28. #include "UnityStandardUtils.cginc"
  29. #include "UnityGBuffer.cginc"
  30. #include "UnityStandardBRDF.cginc"
  31. sampler2D _CameraGBufferTexture0;
  32. sampler2D _CameraGBufferTexture1;
  33. sampler2D _CameraGBufferTexture2;
  34. half Pow(half base, half exponent) {
  35. return pow(max(0.0, base), exponent);
  36. }
  37. half3 Pow3(half3 base, half exponent) {
  38. return half3(Pow(base.x, exponent), Pow(base.y, exponent), Pow(base.z, exponent));
  39. }
  40. float4x4 _LightMatrixR;
  41. float4x4 _LightMatrixG;
  42. float4x4 _LightMatrixB;
  43. float _ZEDExposure;
  44. sampler2D _ZEDMap;
  45. half3 EnvironmentalLightingDiffuse(half3 normal) {
  46. half4 h_normal;
  47. h_normal.xyz = normal;
  48. h_normal.w = 1;
  49. half4 Mn_r = mul(_LightMatrixR, h_normal);
  50. half4 Mn_g = mul(_LightMatrixG, h_normal);
  51. half4 Mn_b = mul(_LightMatrixB, h_normal);
  52. half3 color;
  53. color.r = dot(Mn_r, h_normal);
  54. color.g = dot(Mn_g, h_normal);
  55. color.b = dot(Mn_b, h_normal);
  56. return Pow3(_ZEDExposure * color, 0.454545);
  57. }
  58. float _ZEDFactorAffectReal;
  59. half4 CalculateLight (unity_v2f_deferred i)
  60. {
  61. float3 wpos;
  62. float2 uv;
  63. float atten, fadeDist;
  64. UnityLight light;
  65. UNITY_INITIALIZE_OUTPUT(UnityLight, light);
  66. UnityDeferredCalculateLightParams (i, wpos, uv, light.dir, atten, fadeDist);
  67. light.color = _LightColor.rgb * atten;
  68. half4 gbuffer0 = tex2D (_CameraGBufferTexture0, uv);
  69. half4 gbuffer1 = tex2D (_CameraGBufferTexture1, uv);
  70. half4 gbuffer2 = tex2D (_CameraGBufferTexture2, uv);
  71. UnityStandardData data = UnityStandardDataFromGbuffer(gbuffer0, gbuffer1, gbuffer2);
  72. int mask = gbuffer2.w * 3; // if mask equals 1, it's the ZED texture
  73. #if defined(DIRECTIONAL)
  74. if(mask == 1 ) return half4(data.diffuseColor*saturate(atten + log(1 + 1.72*length(UNITY_LIGHTMODEL_AMBIENT.rgb) / 4.0)), 1);
  75. #endif
  76. float3 eyeVec = normalize(wpos-_WorldSpaceCameraPos);
  77. half oneMinusReflectivity = 1 - SpecularStrength(data.specularColor.rgb);
  78. UnityIndirect ind;
  79. UNITY_INITIALIZE_OUTPUT(UnityIndirect, ind);
  80. //ind.diffuse = 0;
  81. ind.specular = 0;
  82. half4 res = UNITY_BRDF_PBS(data.diffuseColor, data.specularColor, oneMinusReflectivity, data.smoothness, data.normalWorld, -eyeVec, light, ind);
  83. #if defined(DIRECTIONAL)
  84. if (mask != 1 && _ZEDExposure != -1) {
  85. half3 d = dot(light.dir, data.normalWorld)*EnvironmentalLightingDiffuse(data.normalWorld);
  86. res *= half4(d,1);
  87. }
  88. #endif
  89. return res;
  90. }
  91. #ifdef UNITY_HDR_ON
  92. half4
  93. #else
  94. fixed4
  95. #endif
  96. frag (unity_v2f_deferred i) : SV_Target
  97. {
  98. half4 c = CalculateLight(i);
  99. #ifdef UNITY_HDR_ON
  100. return c;
  101. #else
  102. return exp2(-c);
  103. #endif
  104. }
  105. ENDCG
  106. }
  107. // Pass 2: Final decode pass.
  108. // Used only with HDR off, to decode the logarithmic buffer into the main RT
  109. Pass {
  110. ZTest Always Cull Off ZWrite Off
  111. Stencil {
  112. ref [_StencilNonBackground]
  113. readmask [_StencilNonBackground]
  114. // Normally just comp would be sufficient, but there's a bug and only front face stencil state is set (case 583207)
  115. compback equal
  116. compfront equal
  117. }
  118. CGPROGRAM
  119. #pragma target 3.0
  120. #pragma vertex vert
  121. #pragma fragment frag
  122. #pragma exclude_renderers nomrt
  123. #include "UnityCG.cginc"
  124. sampler2D _LightBuffer;
  125. struct v2f {
  126. float4 vertex : SV_POSITION;
  127. float2 texcoord : TEXCOORD0;
  128. };
  129. v2f vert (float4 vertex : POSITION, float2 texcoord : TEXCOORD0)
  130. {
  131. v2f o;
  132. o.vertex = UnityObjectToClipPos(vertex);
  133. o.texcoord = texcoord.xy;
  134. #ifdef UNITY_SINGLE_PASS_STEREO
  135. o.texcoord = TransformStereoScreenSpaceTex(o.texcoord, 1.0f);
  136. #endif
  137. return o;
  138. }
  139. fixed4 frag (v2f i) : SV_Target
  140. {
  141. return -log2(tex2D(_LightBuffer, i.texcoord));
  142. }
  143. ENDCG
  144. }
  145. }
  146. Fallback Off
  147. }