Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // By Christian Jakobsen
- // Ported by Ethatron
- // ---------------------------------------
- // TWEAKABLE VARIABLES.
- #undef TEST_MODE
- // testMode. set to 1, you can see the raw ssao
- float distance = 0.0025;
- // END OF TWEAKABLE VARIABLES.
- // ---------------------------------------
- // shader codes begin here
- texture2D s24;
- sampler2D PassSamplerL3 = sampler_state
- {
- texture = <obge_LastRendertarget0_EFFECTPASS>;
- AddressU = CLAMP;
- AddressV = CLAMP;
- MINFILTER = LINEAR;
- MAGFILTER = LINEAR;
- MIPFILTER = LINEAR;
- };
- texture2D cust_NoiseTexture < string filename = "Noise.dds"; >;
- sampler2D NoiseSampler = sampler_state {
- texture = <cust_NoiseTexture>;
- AddressU = WRAP;
- AddressV = WRAP;
- MINFILTER = LINEAR;
- MAGFILTER = LINEAR;
- MIPFILTER = LINEAR;
- };
- struct VSINv
- {
- float4 vertPos : POSITION0;
- float2 UVCoord : TEXCOORD0;
- };
- struct VSOUTv
- {
- float4 vertPos : POSITION;
- float2 UVCoord : TEXCOORD0;
- };
- VSOUT FrameVSb(VSIN IN) {
- VSOUT OUT = (VSOUT)0.0f; // initialize to zero, avoid complaints.
- OUT.vertPos = IN.vertPos;
- OUT.UVCoord = IN.UVCoord;
- return OUT;
- }
- float4 Occlusion(VSOUT IN) : COLOR0 {
- float3 sample = tex2D(PassSamplerL, IN.UVCoord).rgb;
- #define screenTC IN.UVCoord
- #define screenSize (IN.UVCoord)
- #define sSceneDepthSampler DpthSampler
- #define sRandVectSampler NoiseSampler
- //Tile the texture coordinates
- float2 rotationTC = screenTC * screenSize / 4.0f;
- //Sample a random vector and transform it into [-1, 1] range
- float3 vRotation = 2.0f * tex2Dlod(sRandVectSampler, float4(rotationTC, 0, 0)).rgb - 1.0f;
- //Create rotation matrix
- float3x3 matRotate;
- float offsetScale = distance;
- float h = 1.0f / (1.0f + vRotation.z);
- matRotate._m00 = h * vRotation.y * vRotation.y + vRotation.z;
- matRotate._m01 = -h * vRotation.y * vRotation.x;
- matRotate._m02 = -vRotation.x;
- matRotate._m10 = -h * vRotation.y * vRotation.x;
- matRotate._m11 = h * vRotation.x * vRotation.x + vRotation.z;
- matRotate._m12 = -vRotation.y;
- matRotate._m20 = vRotation.x;
- matRotate._m21 = vRotation.y;
- matRotate._m22 = vRotation.z;
- //Specify number of samples
- const int nSamplesNum = 24;
- //Sample the depth at the current pixel
- float fSceneDepthP = (screenTC);
- //Set the offset scale step
- float fOffsetScaleStep = 1.0f + 2.4f / nSamplesNum;
- //Initialize the accessibility factor to zero
- float fAccessibility = 0;
- //Sample area around current pixel and accumulate the accessibility factor
- [loop]
- for (int i = 0 ; i < (nSamplesNum / 8) ; i++)
- for (int x = -1 ; x <= 1 ; x += 2)
- for (int y = -1 ; y <= 1 ; y += 2)
- for (int z = -1 ; z <= 1 ; z += 2) {
- //Create offset vector
- float3 vOffset = normalize(float3(x, y, z)) * (offsetScale *= fOffsetScaleStep);
- //Rotate the offset vector
- float3 vRotatedOffset = mul(vOffset, matRotate);
- //Center pixel's coordinates in screen space
- float3 vSamplePos = float3(screenTC, fSceneDepthP);
- //Offset sample point
- vSamplePos += float3(vRotatedOffset.xy, vRotatedOffset.z * fSceneDepthP);
- //Read sample point depth
- float fSceneDepthS = (vSamplePos.xy);
- //Discard if depth equals max
- if (fSceneDepthS >= 1.0f)
- fAccessibility += 1.0f;
- else {
- //Compute accessibility factor
- float fRangeIsInvalid = saturate(fSceneDepthP - fSceneDepthS);
- fAccessibility += lerp(fSceneDepthS > vSamplePos.z, 0.5f, fRangeIsInvalid);
- }
- }
- //Compute average accessibility
- fAccessibility = fAccessibility / nSamplesNum;
- //Amplify and return the ambient occlusion coefficient
- return float4(sample, saturate(fAccessibility * fAccessibility + fAccessibility) / (fSceneDepthP));
- }
- float4 Blur2(VSOUT IN) : COLOR0 {
- float4 sample = tex2D(PassSamplerL, IN.UVCoord).rgba;
- #if 0
- float ao = sample.a * 4;
- ao += tex2D(PassSamplerL, IN.UVCoord + float2( rcpres.x, 0)).a * 2;
- ao += tex2D(PassSamplerL, IN.UVCoord + float2(-rcpres.x, 0)).a * 2;
- ao += tex2D(PassSamplerL, IN.UVCoord + float2(0, rcpres.y)).a * 2;
- ao += tex2D(PassSamplerL, IN.UVCoord + float2(0, -rcpres.y)).a * 2;
- ao += tex2D(PassSamplerL, IN.UVCoord + rcpres ).a;
- ao += tex2D(PassSamplerL, IN.UVCoord - rcpres ).a;
- ao += tex2D(PassSamplerL, IN.UVCoord + rcpres * float2(1, -1)).a;
- ao += tex2D(PassSamplerL, IN.UVCoord - rcpres * float2(1, -1)).a;
- ao /= 16;
- #else
- float ao = sample.a;
- ao += 0.67 * tex2Dlod(PassSamplerL, float4(IN.UVCoord + float2( rcpres.x, rcpres.y), 0, 1)).a;
- ao += 0.57 * tex2Dlod(PassSamplerL, float4(IN.UVCoord + float2(-rcpres.x, -rcpres.y), 0, 1)).a;
- ao += 0.43 * tex2Dlod(PassSamplerL, float4(IN.UVCoord + float2( rcpres.x, -rcpres.y), 0, 2)).a;
- ao += 0.33 * tex2Dlod(PassSamplerL, float4(IN.UVCoord + float2(-rcpres.x, rcpres.y), 0, 2)).a;
- ao *= 0.333f;
- #endif
- ao = saturate(pow(saturate(ao * 1.25 - 0.25), 0.25));
- #ifdef TEST_MODE
- return ao;
- #endif
- return float4(sample.rgb * ao, 1);
- }
- technique t00
- <
- int group = 1;
- int fxclass = 2;
- int conditions = 3;
- >
- {
- pass p0 {
- VertexShader = compile vs_3_0 FrameVS();
- PixelShader = compile ps_3_0 Occlusion();
- }
- pass p1 {
- VertexShader = compile vs_3_0 FrameVS();
- PixelShader = compile ps_3_0 Blur();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement