SHARE
TWEET
Untitled
a guest
Sep 17th, 2014
194
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- /** Reconstruct camera-space P.xyz from screen-space S = (x, y) in
- pixels and camera-space z < 0. Assumes that the upper-left pixel center
- is at (0.5, 0.5) [but that need not be the location at which the sample tap
- was placed!]
- Costs 3 MADD. Error is on the order of 10^3 at the far plane, partly due to z precision.
- */
- float3 reconstructCSPosition(float2 S, float z) {
- return float3((S * u_projInfo.xy + u_projInfo.zw) * z, z);
- }
- float3 getNormal(float4 n)
- {
- float3 normal = n.xyw * 2.0 - 1.0;
- normal.z *= sqrt(saturate((1.0 - normal.x * normal.x) - normal.y * normal.y));
- return normal;
- }
- ///////////////////////////////////////////////////////////////////////////////////
- // Pixel Shader
- float3 getAmbientTint(float3 cookieUV, float3 dir, float wrap, float mip, float shininess)
- {
- cookieUV.xy -= dir.xz * (0.02 * cookieUV.z / max(dir.y, 0.05)); // what is this scale?
- float cookie = lerp(1.0, u_texture3.SampleLevel(samClouds, cookieUV.xy, mip).r, u_cookieIntensity);
- float alpha = 0.5 * dir.y + 0.5;
- float3 fogColor = (0.6 * wrap + 0.4) * lerp(u_fogBottomColor, u_fogTopColor, alpha);
- float3 lightDir = u_lightPosition;
- float lDotV = dot(lightDir, dir.xzy);
- // henyey greenstein phase function
- float g = lerp(u_aerosolSizes.x, u_aerosolSizes.y, shininess);
- float de = g * (-2.0 * lDotV + g) + 1.0;
- float d = rsqrt(de * de * de);
- float mie = 0.0795774715 * (-(g * g) * d + d);// 0.25/pi
- // molecul constant normalize and 3/16pi and scaled to match with mie coef
- float3 ray = (float3(0.107279693, 0.25862069, 0.634099617) * 0.0954929658) * (lDotV*lDotV + 1.0);
- fogColor += 1.6 * wrap * cookie * u_lightIntensity * u_lightColor * (ray + mie);
- return fogColor;
- }
- float3 main (PS_INPUT input) : SV_Target
- {
- int3 screenUV = int3(input.position.xy, 0);
- float4 tex0 = u_color.Load(screenUV);
- float4 tex1 = u_normal.Load(screenUV);
- float vertexAO = u_texture4.Load(screenUV).a;
- float3 normal = getNormal(tex1);
- float shininess = tex1.z;
- float roughness = 1.0 - shininess;
- float viewZ = u_depth.Load(screenUV).r;
- float3 viewPos = reconstructCSPosition(screenUV.xy, viewZ);
- float3 viewDir = normalize(-viewPos);
- float nDotV = saturate(dot( normal, viewDir));
- // AMBIENT
- float3 worldNormal = mul(u_viewToWorld, float4(normal, 0.0)).xzy;
- float3 ambientCubeDiffuse = u_textureCube0.Sample( samLinear, worldNormal ).rgb;
- float3 reflectVector = reflect(-viewDir, normal);
- float3 worldReflect = mul(u_viewToWorld, float4(reflectVector, 0.0)).xzy;
- float3 ambientCubeSpecular = u_textureCube1.SampleLevel( samLinear, worldReflect, (roughness) * 4.5 /*NUM_MIP*/ ).rgb;
- float3 cookieUV = mul(u_viewToCookieAmbient, float4(viewPos, 1.0)).xyz;
- float ao = vertexAO;
- #if defined(SCREENSPACESHADOWS)
- float4 gi = u_texture0.Load(screenUV );
- ao = (0.3 + ao * 0.7) * gi.a;
- if(ao < 0.2)
- ao = sqrt(ao)*0.447213595;
- #endif
- float skyWrap = smoothstep(-roughness, roughness, worldReflect.y);
- ambientCubeSpecular *= ao * getAmbientTint(cookieUV, worldReflect, skyWrap, roughness * 5.5 + 1.0, shininess);
- skyWrap = saturate(worldNormal.y * 0.5 + 0.5);
- float3 ambientDiffuse = ao * ambientCubeDiffuse * getAmbientTint(cookieUV, worldNormal, skyWrap * skyWrap, 6.5, shininess);
- #if defined(SCREENSPACESHADOWS)
- ambientDiffuse += gi.rgb;
- ambientCubeSpecular += gi.rgb * roughness;
- #endif
- float fresnelTerm = pow(1.0 - nDotV, 5.0);
- float specIntensity = tex0.a;
- float fresnel = specIntensity + saturate(max(shininess, 0.05) - specIntensity) * fresnelTerm; //ambient fresnell need to be tuned down with rough materials
- ambientDiffuse *= (1.0 - fresnel);
- bool isMetal = specIntensity >= 0.5;
- float3 albedo = tex0.rgb;
- ambientDiffuse *= isMetal ? 0.0 : albedo;
- float3 spec = isMetal ? albedo * fresnel : fresnel;
- #if defined(SSR)
- [branch]
- if (fresnel > 0.04)
- {
- float4 projPos = mul(u_proj, float4(viewPos, 1.0));
- float4 projPos2 = mul(u_proj, float4(viewPos + reflectVector, 1.0));
- float3 samplePos = projPos.xyz / projPos.w;
- float3 sampleDir = (projPos2.xyz / projPos2.w) - samplePos;
- #if defined(LOW_QUALITY)
- float treshold = 0.003;
- float scale = 0.05;
- int samples = 16;
- #elif defined(ULTRA_QUALITY)
- float treshold = 0.001;
- float scale = 0.0275;
- int samples = 64;
- #else
- float treshold = 0.0015;
- float scale = 0.03;
- int samples = 32;
- #endif
- sampleDir *= rsqrt(dot(sampleDir.xy, sampleDir.xy)) * scale; // fix scale
- //clip to screenspace
- sampleDir.xy *= float2(0.5, -0.5);
- samplePos.xy = samplePos.xy * float2(0.5, -0.5) + 0.5;
- // checker pattern jitter starting position by half step
- float jitter = 0.5 * ((screenUV.x+screenUV.y) & 1);
- samplePos += sampleDir * jitter;
- for(int i = 0; i < samples; i++)
- {
- samplePos += sampleDir;
- #if defined(XT_FEATURE_LEVEL_10_0)
- float d = 1.0 - rcp(samplePos.z) * u_texture2.SampleLevel(samData, samplePos.xy, 0).r;
- #else
- float4 d = 1.0 - rcp(samplePos.z) * u_texture2.Gather(samData, samplePos.xy);
- #endif
- float occluded = dot(step(0.0, d), step(d, treshold));
- if (occluded > 0)
- {
- float vDotR = saturate(dot(reflectVector, viewDir)); // smooth out rays that point towards viewer
- float2 toEdgeDist = 0.5 - abs( samplePos.xy - 0.5);
- float fade = (1.0-vDotR*vDotR) * saturate(10.0 * min(toEdgeDist.x, toEdgeDist.y));
- fade *= 1.0 - saturate(smoothstep(samples * 0.75, samples, i));
- float roughnessFactor = 0.5 * shininess + 0.5;
- float mipLevel = (0.75 * REPROJECTION_MIP_COUNT + 0.5 * REPROJECTION_MIP_COUNT * (i / 32.0) ) * roughness;
- float3 reflectionColor = u_texture1.SampleLevel(samLinear, samplePos.xy, mipLevel).rgb;
- #if defined(SCREENSPACESHADOWS)
- reflectionColor = lerp((ao * roughnessFactor) * reflectionColor, reflectionColor, shininess);
- #else
- reflectionColor *= roughnessFactor;
- #endif
- ambientCubeSpecular = lerp(ambientCubeSpecular, reflectionColor, fade * roughnessFactor);
- break;
- }
- }
- }
- #endif
- return spec * ambientCubeSpecular + ambientDiffuse;
- }
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.
