SHARE
TWEET

Untitled

a guest Sep 17th, 2014 194 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /** Reconstruct camera-space P.xyz from screen-space S = (x, y) in
  2.         pixels and camera-space z < 0.  Assumes that the upper-left pixel center
  3.         is at (0.5, 0.5) [but that need not be the location at which the sample tap
  4.         was placed!]
  5.  
  6.         Costs 3 MADD.  Error is on the order of 10^3 at the far plane, partly due to z precision.
  7. */
  8. float3 reconstructCSPosition(float2 S, float z) {
  9.         return float3((S * u_projInfo.xy + u_projInfo.zw) * z, z);
  10. }
  11.  
  12. float3 getNormal(float4 n)
  13. {
  14.         float3 normal = n.xyw * 2.0 - 1.0;
  15.         normal.z *= sqrt(saturate((1.0 - normal.x * normal.x) - normal.y * normal.y));
  16.         return normal;
  17. }
  18.  
  19. ///////////////////////////////////////////////////////////////////////////////////
  20. // Pixel Shader
  21. float3 getAmbientTint(float3 cookieUV, float3 dir, float wrap, float mip, float shininess)             
  22. {
  23.         cookieUV.xy -=  dir.xz * (0.02 * cookieUV.z / max(dir.y, 0.05)); // what is this scale?
  24.         float cookie = lerp(1.0, u_texture3.SampleLevel(samClouds, cookieUV.xy, mip).r, u_cookieIntensity);
  25.         float alpha = 0.5 * dir.y + 0.5;
  26.  
  27.         float3 fogColor =  (0.6 * wrap + 0.4) * lerp(u_fogBottomColor, u_fogTopColor, alpha);
  28.         float3 lightDir = u_lightPosition;     
  29.         float lDotV     = dot(lightDir, dir.xzy);                      
  30.        
  31.         // henyey greenstein phase function
  32.         float g = lerp(u_aerosolSizes.x, u_aerosolSizes.y, shininess);
  33.         float de = g * (-2.0 * lDotV + g) + 1.0;
  34.         float d = rsqrt(de * de * de);
  35.         float mie =  0.0795774715 * (-(g * g) * d + d);// 0.25/pi
  36.  
  37.         // molecul constant normalize and 3/16pi and scaled to match with mie coef
  38.         float3 ray = (float3(0.107279693, 0.25862069, 0.634099617) * 0.0954929658) * (lDotV*lDotV + 1.0);
  39.         fogColor        += 1.6 * wrap * cookie * u_lightIntensity * u_lightColor * (ray + mie);
  40.  
  41.         return fogColor;               
  42. }
  43.  
  44. float3 main (PS_INPUT input) : SV_Target
  45. {
  46.         int3 screenUV           = int3(input.position.xy, 0);
  47.        
  48.         float4 tex0                     = u_color.Load(screenUV);
  49.         float4 tex1                     = u_normal.Load(screenUV);                     
  50.         float vertexAO          = u_texture4.Load(screenUV).a;
  51.         float3 normal           = getNormal(tex1);
  52.         float  shininess        = tex1.z;
  53.         float  roughness        = 1.0 - shininess;
  54.        
  55.         float viewZ                     = u_depth.Load(screenUV).r;
  56.         float3 viewPos          = reconstructCSPosition(screenUV.xy, viewZ);                   
  57.         float3 viewDir          = normalize(-viewPos);
  58.                                        
  59.         float  nDotV            = saturate(dot( normal, viewDir));
  60.        
  61.         // AMBIENT
  62.         float3 worldNormal = mul(u_viewToWorld, float4(normal, 0.0)).xzy;
  63.  
  64.         float3 ambientCubeDiffuse = u_textureCube0.Sample( samLinear, worldNormal ).rgb;                    
  65.         float3 reflectVector = reflect(-viewDir, normal);
  66.         float3 worldReflect = mul(u_viewToWorld, float4(reflectVector, 0.0)).xzy;
  67.         float3 ambientCubeSpecular = u_textureCube1.SampleLevel( samLinear, worldReflect, (roughness) * 4.5 /*NUM_MIP*/ ).rgb;
  68.  
  69.  
  70.         float3 cookieUV = mul(u_viewToCookieAmbient, float4(viewPos, 1.0)).xyz;
  71.                
  72.         float ao = vertexAO;
  73.         #if defined(SCREENSPACESHADOWS)
  74.                 float4 gi = u_texture0.Load(screenUV );
  75.                 ao = (0.3 + ao * 0.7) * gi.a;
  76.                 if(ao < 0.2)
  77.                         ao = sqrt(ao)*0.447213595;
  78.         #endif
  79.  
  80.         float skyWrap = smoothstep(-roughness, roughness, worldReflect.y);
  81.         ambientCubeSpecular *= ao * getAmbientTint(cookieUV, worldReflect, skyWrap, roughness * 5.5 + 1.0, shininess);
  82.         skyWrap = saturate(worldNormal.y * 0.5 + 0.5);
  83.         float3 ambientDiffuse   = ao * ambientCubeDiffuse * getAmbientTint(cookieUV, worldNormal, skyWrap * skyWrap, 6.5, shininess);
  84.         #if defined(SCREENSPACESHADOWS)
  85.                 ambientDiffuse += gi.rgb;
  86.                 ambientCubeSpecular += gi.rgb * roughness;
  87.         #endif
  88.  
  89.         float  fresnelTerm              = pow(1.0 - nDotV, 5.0);
  90.         float  specIntensity    = tex0.a;
  91.         float  fresnel                  = specIntensity + saturate(max(shininess, 0.05) - specIntensity) * fresnelTerm; //ambient fresnell need to be tuned down with rough materials
  92.         ambientDiffuse                  *= (1.0 - fresnel);
  93.  
  94.         bool isMetal            = specIntensity >= 0.5;
  95.         float3 albedo           = tex0.rgb;
  96.         ambientDiffuse          *= isMetal ? 0.0 : albedo;             
  97.         float3 spec                     = isMetal ? albedo * fresnel : fresnel;
  98.        
  99.         #if defined(SSR)
  100.                 [branch]
  101.                 if (fresnel > 0.04)
  102.                 {
  103.                         float4 projPos  = mul(u_proj, float4(viewPos, 1.0));
  104.                         float4 projPos2 = mul(u_proj, float4(viewPos + reflectVector, 1.0));
  105.                        
  106.                         float3 samplePos = projPos.xyz / projPos.w;
  107.                         float3 sampleDir = (projPos2.xyz / projPos2.w) - samplePos;
  108.                        
  109.                         #if defined(LOW_QUALITY)
  110.                                 float treshold = 0.003;
  111.                                 float scale        = 0.05;
  112.                                 int samples = 16;
  113.                         #elif defined(ULTRA_QUALITY)
  114.                                 float treshold = 0.001;
  115.                                 float scale        = 0.0275;
  116.                                 int samples = 64;
  117.                         #else
  118.                                 float treshold = 0.0015;
  119.                                 float scale        = 0.03;
  120.                                 int samples = 32;                                              
  121.                         #endif
  122.  
  123.                         sampleDir               *= rsqrt(dot(sampleDir.xy, sampleDir.xy)) * scale; // fix scale
  124.                        
  125.                         //clip to screenspace
  126.                         sampleDir.xy *= float2(0.5, -0.5);                             
  127.                         samplePos.xy = samplePos.xy * float2(0.5, -0.5) + 0.5;
  128.                        
  129.  
  130.                         // checker pattern jitter starting position by half step
  131.                         float jitter = 0.5 * ((screenUV.x+screenUV.y) & 1);                                    
  132.                         samplePos += sampleDir * jitter;                                       
  133.                        
  134.                         for(int i = 0; i < samples; i++)
  135.                         {      
  136.                                 samplePos += sampleDir;
  137.                                
  138.                                 #if defined(XT_FEATURE_LEVEL_10_0)
  139.                                         float d = 1.0 - rcp(samplePos.z) * u_texture2.SampleLevel(samData, samplePos.xy, 0).r;
  140.                                 #else
  141.                                         float4 d = 1.0 - rcp(samplePos.z) * u_texture2.Gather(samData, samplePos.xy);
  142.                                 #endif
  143.                                
  144.                                 float occluded = dot(step(0.0, d), step(d, treshold));                                         
  145.                                 if (occluded > 0)                                                                                                                      
  146.                                 {      
  147.                                         float vDotR = saturate(dot(reflectVector, viewDir)); // smooth out rays that point towards viewer
  148.                                         float2 toEdgeDist = 0.5 - abs( samplePos.xy - 0.5);
  149.                                         float fade = (1.0-vDotR*vDotR) * saturate(10.0 * min(toEdgeDist.x, toEdgeDist.y));
  150.                                         fade *= 1.0 - saturate(smoothstep(samples * 0.75, samples, i));
  151.                                         float roughnessFactor = 0.5 * shininess + 0.5;
  152.                                         float mipLevel = (0.75 * REPROJECTION_MIP_COUNT + 0.5 * REPROJECTION_MIP_COUNT * (i / 32.0) ) * roughness;
  153.                                         float3 reflectionColor          = u_texture1.SampleLevel(samLinear, samplePos.xy, mipLevel).rgb;
  154.                                         #if defined(SCREENSPACESHADOWS)
  155.                                                 reflectionColor = lerp((ao * roughnessFactor) * reflectionColor, reflectionColor, shininess);
  156.                                         #else
  157.                                                 reflectionColor *= roughnessFactor;
  158.                                         #endif
  159.                                         ambientCubeSpecular = lerp(ambientCubeSpecular, reflectionColor, fade * roughnessFactor);
  160.                                         break;                                 
  161.                                 }
  162.                         }
  163.                 }                                                                                              
  164.         #endif
  165.         return spec * ambientCubeSpecular + ambientDiffuse;
  166. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top