Jump to content

SSAO with Distance Cutoff!


Haydenmango
 Share

Recommended Posts

Hey everyone I had an idea this morning and it seems like it worked!

What I did to the ssao shader was very simple and it seems to work as intended. I just thought I would share this because I found it useful. If there are any issues let me know.

Credit for the original ssao.shader goes to Igor as well as Josh and Shadmar. The original ssao.shader can be found here - http://www.leadwerks.com/werkspace/page/viewitem?fileid=393925036

Also, before doing this I recommend saving a back up of your ssao.shader just in case.

 

In the Fragment section of the ssao.shader

 

replace the main loop with this -

 

void main(void)
{
float depth;
float lineardepth;
vec2 pixelsize = 1.0 / buffersize;
vec2 texcoord = GetTexCoord();

depth = texelFetch(texture1, ivec2(texcoord * buffersize), 0).x;

if(depth < 1.0) {
lineardepth = DepthToZPosition(depth);
if(lineardepth < 62.5) {
vec2 rotationTC=(texcoord*buffersize)/4.0;
vec3 vRotation=normalize((texture(texture10, rotationTC).xyz * 2.0) - 1.0);
mat3 rotMat=vec3tomat3(vRotation);

float fSceneDepthP = DepthToZPosition(depth);

float offsetScale = aoscale;
float offsetScaleStep = 1.0 + 1.5 / samples;


float ao = 0.0;

for(int i = 0; i < (samples / 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)
{
 vec3 vOffset = normalize (vec3(x, y, z)) * (offsetScale *= offsetScaleStep);
 vec3 vRotatedOffset = rotMat * vOffset;
 vec3 vSamplePos = vec3(texcoord, fSceneDepthP);
 vSamplePos += vec3(vRotatedOffset.xy, vRotatedOffset.z * fSceneDepthP*raycasts);

 float fSceneDepthS = DepthToZPosition(texelFetch(texture1, ivec2(vSamplePos.xy * buffersize), 0).x);
 float fRangeIsInvalid = clamp(((fSceneDepthP - fSceneDepthS) / fSceneDepthS), 0.0, 1.0);

 ao += mix(clamp(ceil(fSceneDepthS - vSamplePos.z), 0.0, 1.0), 0.5, fRangeIsInvalid);
}

ao /= samples * 1.5;
fragData0 = vec4(clamp(ao + ao + ao * 1.22, 0.0, 1.0));
}
else {
fragData0 = vec4(1.0);
}
}
else {
fragData0 = vec4(1.0);
}
}

 

To edit the distance cutoff range just change 62.5 in lineardepth < 62.5 to a different float value.

  • Upvote 4

Check out my game Rogue Snowboarding- 

https://haydenmango.itch.io/roguesnowboarding

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...