Orihaus

Main Shader - July 2013 v2.0 - UE4 Light Model

Jul 26th, 2013
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.58 KB | None | 0 0
  1. Shader "Base_Texture"
  2. {
  3.     Properties  
  4.     {
  5.         _DiffuseAmount ( "Diffuse Amount", Range( 0.0, 1.0 ) ) = 0.25
  6.         _TexAmount ( "Texture Amount", Range( 0.0, 1.0 ) ) = 0.25
  7.         _SpecAmount ( "Specular Amount", Range( 0.0, 16.0 ) ) = 1.0
  8.         _SpecTexAmount ( "Spec Texture Amount", Range( 0.0, 1.0 ) ) = 0.25
  9.        
  10.         _Normalmap ( "Normalmap", 2D ) = "normal" {}
  11.         _Specmap ( "Specmap", 2D ) = "spec" {}
  12.        
  13.         _Gloss ( "Gloss", Range( 0.001, 1.0 ) ) = 1.0
  14.         _SRNI ( "Specular Reflectance at Normal Incidence", Range( 0.0, 1.0 ) ) = 0.075
  15.     }
  16.        
  17.     SubShader
  18.     {
  19.         Tags
  20.         {
  21.           "Queue"="Geometry+0"
  22.           "IgnoreProjector"="False"
  23.           "RenderType"="Opaque"
  24.         }
  25.  
  26.         Cull Back
  27.         ZWrite On
  28.         ZTest LEqual
  29.  
  30.         CGPROGRAM
  31.         #pragma target 3.0
  32.         #pragma surface surf SimpleSpecular novertexlights fullforwardshadows
  33.        
  34.         float _Gloss;
  35.         fixed _SpecAmount;
  36.         float _SRNI;
  37.        
  38.         float CalculateGGX( float alpha, float cosThetaM )
  39.         {
  40.             float roughness = alpha * alpha;
  41.             roughness *= roughness;
  42.            
  43.             float CosSquared = cosThetaM * cosThetaM;
  44.             float TanSquared = ( 1.0 - CosSquared ) + 1.0;
  45.             float GGX = ( CosSquared * ( roughness - 1.0 ) ) + 1.0;
  46.             GGX = 0.31831 * GGX * GGX;
  47.             return roughness / GGX;
  48.         }
  49.        
  50.         float CalculateGeometric( float3 norm, float3 vec, float3 vDir, float roughness )
  51.         {
  52.             float n_dot_v = saturate( dot( norm, vec ) );
  53.             return n_dot_v / ( dot( norm, vDir ) * ( 1.0 - roughness ) + roughness );
  54.         }
  55.        
  56.         float CalculateGeometricAtten( float3 norm, float3 lDir, float3 vDir, float3 hDir, float roughness )
  57.         {
  58.             float roughnessRemap = roughness + 1.0;
  59.             roughnessRemap *= roughnessRemap;
  60.             roughnessRemap *= 0.125;
  61.            
  62.             float atten = CalculateGeometric( norm, vDir, vDir, roughnessRemap ) * CalculateGeometric( norm, lDir, vDir, roughnessRemap );
  63.             return atten;
  64.         }
  65.        
  66.         float CalculateSpecular( fixed3 lDir, fixed3 vDir, fixed3 norm, float lightSize, float gloss )
  67.         {    
  68.             fixed n_dot_l = dot( norm, lDir );
  69.             fixed n_dot_v = dot( norm, vDir );
  70.            
  71.             float3 halfVector = normalize( lDir + vDir );
  72.             float specDot = dot( halfVector, norm );
  73.            
  74.             float fresnel = pow( 1.0 - saturate( dot( halfVector, vDir ) ), 5.0 );
  75.             fresnel = _SRNI + ( 1.0 - _SRNI ) * fresnel;
  76.  
  77.             float geoAtten = CalculateGeometricAtten( norm, lDir, vDir, halfVector, gloss );
  78.             float distribution = CalculateGGX( gloss, saturate( specDot ) );
  79.            
  80.             float BRDF = ( distribution * geoAtten * fresnel ) / ( 4.0 * n_dot_l * n_dot_v );
  81.             return BRDF;
  82.         }
  83.        
  84.         fixed4 LightingSimpleSpecular( SurfaceOutput s, fixed3 lightDir, fixed3 viewDir, fixed atten )
  85.         {
  86.             fixed spec = CalculateSpecular( lightDir, viewDir, s.Normal, 1.0, s.Gloss ) * 0.01 * _SpecAmount;
  87.             fixed diff = dot( s.Normal, lightDir ) * s.Albedo;
  88.            
  89.             diff = lerp( diff, 0.0, spec );
  90.            
  91.             fixed4 c;
  92.             c.rgb = diff * _LightColor0.rgb;
  93.             c.rgb += _LightColor0.rgb * spec;
  94.             c.rgb *= atten;
  95.             c.a = s.Alpha;
  96.            
  97.             return c;
  98.         }
  99.        
  100.         struct Input
  101.         {
  102.             float2 uv_Normalmap;
  103.             float2 uv_Specmap;
  104.             float3 viewDir;
  105.         };
  106.    
  107.         sampler2D _Normalmap;
  108.         sampler2D _Specmap;
  109.  
  110.         fixed _SpecTexAmount;
  111.         fixed _TexAmount;
  112.         fixed _DiffuseAmount;
  113.  
  114.         void surf( Input IN, inout SurfaceOutput o )
  115.         {
  116.             o.Normal = UnpackNormal( tex2D( _Normalmap, IN.uv_Normalmap ) );
  117.            
  118.             float spec = tex2D( _Specmap, IN.uv_Specmap ).r;
  119.             o.Gloss = lerp( spec * 2.0, 0.5, _SpecTexAmount ) * _Gloss;
  120.             o.Albedo = _DiffuseAmount * lerp( spec, 1.0, _TexAmount );
  121.         }
  122.    
  123.         ENDCG
  124.     }
  125.        
  126.     Fallback "Diffuse"
  127. }
Advertisement
Add Comment
Please, Sign In to add comment