Advertisement
noradninja

Internal-PrePassLighting

Apr 8th, 2024
744
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.82 KB | None | 0 0
  1. Shader "Hidden/Internal-PrePassLighting" {
  2.     Properties {
  3.         _LightTexture0 ("", any) = "" {}
  4.         _LightTextureB0 ("", 2D) = "" {}
  5.         _ShadowMapTexture ("", any) = "" {}
  6.     }
  7.     SubShader {
  8.         CGINCLUDE
  9.         #include "UnityCG.cginc"
  10.         #include "UnityDeferredLibrary.cginc"
  11.  
  12.         sampler2D _CameraNormalsTexture;
  13.         float4 _CameraNormalsTexture_ST;
  14.  
  15.         // GGX term for specular calculation
  16.         half GGXTerm(half NdotH, half roughness) {
  17.             half alpha = roughness * roughness;
  18.             half alphaSqr = alpha * alpha;
  19.             half a = NdotH * alphaSqr - NdotH * NdotH * alphaSqr + 1.0;
  20.             return 1.0 / (PI * alphaSqr * a * a);
  21.         }
  22.  
  23.         // Schlick Fresnel approximation
  24.         half3 SchlickFresnel(half cosTheta, half3 F0) {
  25.             return F0 + (1.0 - F0) * pow(1.0 - cosTheta, 2.0);
  26.         }
  27.  
  28.         // Smith Joint GGX for specular calculation
  29.         half SmithJointGGXCorrelated(half NdotL, half roughness) {
  30.             half alpha = roughness * roughness;
  31.             half alphaSqr = alpha * alpha;
  32.             half lambdaL = NdotL * sqrt((-NdotL * alphaSqr) + NdotL);
  33.             return 0.5 / (lambdaL + sqrt(1.0 + alphaSqr));
  34.         }
  35.  
  36.         half4 CalculateLight (unity_v2f_deferred i)
  37.         {
  38.             float3 wpos;
  39.             float2 uv;
  40.             half3 lightDir;
  41.             float atten, fadeDist;
  42.             UnityDeferredCalculateLightParams (i, wpos, uv, lightDir, atten, fadeDist);
  43.  
  44.             half4 nspec = tex2D (_CameraNormalsTexture, TRANSFORM_TEX(uv, _CameraNormalsTexture));
  45.             half3 normal = nspec.rgb * 2 - 1;
  46.             normal = normalize(normal);
  47.  
  48.             // Disney Diffuse
  49.             half3 albedo = _LightColor.rgb / 3.14159;
  50.  
  51.             // Schlick Fresnel
  52.             half3 fresnel = SchlickFresnel(max(0, dot(lightDir, normal))), _LightColor.rgb);
  53.  
  54.             // GGX Specular
  55.             half roughness = nspec.a;
  56.             half3 halfway = normalize(lightDir + normalize(i.viewDir));
  57.             half NdotH = max(0, dot(normal, halfway));
  58.             half D = GGXTerm(NdotH, roughness);
  59.             half3 F = fresnel;
  60.             half G = SmithJointGGXCorrelated(dot(lightDir, normal), roughness) * SmithJointGGXCorrelated(dot(normal, halfway), roughness);
  61.             half3 specular = D * F * G / (4 * max(0.001, dot(normal, lightDir)) * max(0.001, dot(normal, i.viewDir)));
  62.  
  63.             // Final result
  64.             half3 diff = albedo * (1 - fresnel);
  65.             half3 spec = specular;
  66.  
  67.             half4 res;
  68.             res.xyz = (diff + spec) * (diff * atten);
  69.             res.w = 1.0;
  70.  
  71.             float fade = fadeDist * unity_LightmapFade.z + unity_LightmapFade.w;
  72.             res *= saturate(1.0 - fade);
  73.  
  74.             return res;
  75.         }
  76.         ENDCG
  77.  
  78.         /*Pass 1: LDR Pass - Lighting encoded into a subtractive ARGB8 buffer*/
  79.         Pass {
  80.             ZWrite Off
  81.             Blend DstColor Zero
  82.  
  83.             CGPROGRAM
  84.             #pragma target 3.0
  85.             #pragma vertex vert_deferred
  86.             #pragma fragment frag
  87.             #pragma multi_compile_lightpass
  88.  
  89.             fixed4 frag (unity_v2f_deferred i) : SV_Target
  90.             {
  91.                 return exp2(-CalculateLight(i));
  92.             }
  93.             ENDCG
  94.         }
  95.  
  96.         /*Pass 2: HDR Pass - Lighting additively blended into floating point buffer*/
  97.         Pass {
  98.             ZWrite Off
  99.             Blend One One
  100.  
  101.             CGPROGRAM
  102.             #pragma target 3.0
  103.             #pragma vertex vert_deferred
  104.             #pragma fragment frag
  105.             #pragma multi_compile_lightpass
  106.  
  107.             fixed4 frag (unity_v2f_deferred i) : SV_Target
  108.             {
  109.                 return CalculateLight(i);
  110.             }
  111.             ENDCG
  112.         }
  113.     }
  114.     Fallback Off
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement