123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- // Custom lighting effect on deferred
- Shader "ZED/ZED Deferred Lighting"
- {
- Properties {
- _LightTexture0 ("", any) = "" {}
- _LightTextureB0 ("", 2D) = "" {}
- _ShadowMapTexture ("", any) = "" {}
- _SrcBlend ("", Float) = 1
- _DstBlend ("", Float) = 1
- }
- SubShader {
- // Pass 1: Lighting pass
- // LDR case - Lighting encoded into a subtractive ARGB8 buffer
- // HDR case - Lighting additively blended into floating point buffer
- Pass {
- ZWrite Off
- Blend [_SrcBlend] [_DstBlend]
- CGPROGRAM
- #pragma target 3.0
- #pragma vertex vert_deferred
- #pragma fragment frag
- #pragma multi_compile_lightpass
- #pragma multi_compile ___ UNITY_HDR_ON
- #pragma exclude_renderers nomrt
- #include "UnityCG.cginc"
- #include "UnityDeferredLibrary.cginc"
- #include "UnityPBSLighting.cginc"
- #include "UnityStandardUtils.cginc"
- #include "UnityGBuffer.cginc"
- #include "UnityStandardBRDF.cginc"
- sampler2D _CameraGBufferTexture0;
- sampler2D _CameraGBufferTexture1;
- sampler2D _CameraGBufferTexture2;
- half Pow(half base, half exponent) {
- return pow(max(0.0, base), exponent);
- }
- half3 Pow3(half3 base, half exponent) {
- return half3(Pow(base.x, exponent), Pow(base.y, exponent), Pow(base.z, exponent));
- }
- float4x4 _LightMatrixR;
- float4x4 _LightMatrixG;
- float4x4 _LightMatrixB;
- float _ZEDExposure;
- sampler2D _ZEDMap;
- half3 EnvironmentalLightingDiffuse(half3 normal) {
- half4 h_normal;
- h_normal.xyz = normal;
- h_normal.w = 1;
- half4 Mn_r = mul(_LightMatrixR, h_normal);
- half4 Mn_g = mul(_LightMatrixG, h_normal);
- half4 Mn_b = mul(_LightMatrixB, h_normal);
- half3 color;
- color.r = dot(Mn_r, h_normal);
- color.g = dot(Mn_g, h_normal);
- color.b = dot(Mn_b, h_normal);
- return Pow3(_ZEDExposure * color, 0.454545);
- }
- float _ZEDFactorAffectReal;
- half4 CalculateLight (unity_v2f_deferred i)
- {
- float3 wpos;
- float2 uv;
- float atten, fadeDist;
- UnityLight light;
- UNITY_INITIALIZE_OUTPUT(UnityLight, light);
- UnityDeferredCalculateLightParams (i, wpos, uv, light.dir, atten, fadeDist);
- light.color = _LightColor.rgb * atten;
- half4 gbuffer0 = tex2D (_CameraGBufferTexture0, uv);
- half4 gbuffer1 = tex2D (_CameraGBufferTexture1, uv);
- half4 gbuffer2 = tex2D (_CameraGBufferTexture2, uv);
- UnityStandardData data = UnityStandardDataFromGbuffer(gbuffer0, gbuffer1, gbuffer2);
- int mask = gbuffer2.w * 3; // if mask equals 1, it's the ZED texture
- #if defined(DIRECTIONAL)
- if(mask == 1 ) return half4(data.diffuseColor*saturate(atten + log(1 + 1.72*length(UNITY_LIGHTMODEL_AMBIENT.rgb) / 4.0)), 1);
- #endif
- float3 eyeVec = normalize(wpos-_WorldSpaceCameraPos);
- half oneMinusReflectivity = 1 - SpecularStrength(data.specularColor.rgb);
- UnityIndirect ind;
- UNITY_INITIALIZE_OUTPUT(UnityIndirect, ind);
- //ind.diffuse = 0;
- ind.specular = 0;
- half4 res = UNITY_BRDF_PBS(data.diffuseColor, data.specularColor, oneMinusReflectivity, data.smoothness, data.normalWorld, -eyeVec, light, ind);
- #if defined(DIRECTIONAL)
- if (mask != 1 && _ZEDExposure != -1) {
- half3 d = dot(light.dir, data.normalWorld)*EnvironmentalLightingDiffuse(data.normalWorld);
- res *= half4(d,1);
- }
- #endif
- return res;
- }
- #ifdef UNITY_HDR_ON
- half4
- #else
- fixed4
- #endif
- frag (unity_v2f_deferred i) : SV_Target
- {
- half4 c = CalculateLight(i);
- #ifdef UNITY_HDR_ON
- return c;
- #else
- return exp2(-c);
- #endif
- }
- ENDCG
- }
- // Pass 2: Final decode pass.
- // Used only with HDR off, to decode the logarithmic buffer into the main RT
- Pass {
- ZTest Always Cull Off ZWrite Off
- Stencil {
- ref [_StencilNonBackground]
- readmask [_StencilNonBackground]
- // Normally just comp would be sufficient, but there's a bug and only front face stencil state is set (case 583207)
- compback equal
- compfront equal
- }
- CGPROGRAM
- #pragma target 3.0
- #pragma vertex vert
- #pragma fragment frag
- #pragma exclude_renderers nomrt
- #include "UnityCG.cginc"
- sampler2D _LightBuffer;
- struct v2f {
- float4 vertex : SV_POSITION;
- float2 texcoord : TEXCOORD0;
- };
- v2f vert (float4 vertex : POSITION, float2 texcoord : TEXCOORD0)
- {
- v2f o;
- o.vertex = UnityObjectToClipPos(vertex);
- o.texcoord = texcoord.xy;
- #ifdef UNITY_SINGLE_PASS_STEREO
- o.texcoord = TransformStereoScreenSpaceTex(o.texcoord, 1.0f);
- #endif
- return o;
- }
- fixed4 frag (v2f i) : SV_Target
- {
- return -log2(tex2D(_LightBuffer, i.texcoord));
- }
- ENDCG
- }
- }
- Fallback Off
- }
|