Jump to content

Interesting SSAO shader


franck22000
 Share

Recommended Posts

I like the SSDO shader too but as Mickael Betke said:

 

In the comments of Joshs Blog we came to the conclusion that a slight blur filter over the SSDO effect would soften the effect in a good performance way. I adjusted the Quality settings and fps get down noticeable.

 

So doing a blur would be better but I dont know how to do this.

 

So Josh is it possible for you to update the source code of SSDO to to this ? :blink:

It would be very cool for a lot of user :lol:

You guys are going to be the death of me. Josh
Link to comment
Share on other sites

  • 4 weeks later...

For anyone interested, here's a cleaned up version of that shader that works with LE:

// from http://www.gamedev.net/community/forums/topic.asp?topic_id=556187&whichpage=9
// / http://www.pasteall.org/12282 (newer)
uniform sampler2D texture0; //RenderedTexture;
uniform sampler2D texture1; //DepthTexture;
//uniform sampler2D LuminanceTexture;

uniform float apptime;
uniform vec2  buffersize;
uniform vec2  camerarange;

// Math constants
const float _2PI = 2.0*3.14159265;

// Effect settings
const int   samples         = 6; // samples on each ring (3-7) ! This gets multiplied by the current ring index ! -> very expensive
const int   rings           = 4; // ring count (2-8)
const float aoCap           = 1.0;
const float aoMultiplier    = 100.0;
const float aoMultiplier2   = 1.0;
const float depthTolerance  = 0.0000;
const float aorange         = 60.0;// units in space the AO effect extends to (this gets divided by the camera camerarange.y range
const float radius          = 0.5;
const float aoexp           = 1; 
const float noiseMultiplier = 1.0;

const vec3 treshold = vec3(0.3);
const bool allowEarlyOut = true;

// Globals
vec2 texCoord;

// Functions
inline vec2 rand(in vec2 coord) //generating random noise
{
   float noiseX = (fract(sin(apptime*0.0001+dot(coord, vec2(12.9898,78.233)))     * 43758.5453));
   float noiseY = (fract(sin(apptime*0.0001+dot(coord, vec2(12.9898,78.233)*2.0)) * 43758.5453));
   return vec2(noiseX,noiseY) * 0.004;
}

inline float readDepth(in vec2 coord)
{
   float depth = texture2D(texture1, coord ).x;
   return (2.0 * camerarange.x) / (camerarange.y + camerarange.x - depth * (camerarange.y-camerarange.x));
}

inline float compareDepths( in float depth1, in float depth2 )
{
   float  diff = sqrt(clamp(1.0-(depth1-depth2) / (aorange/(camerarange.y-camerarange.x)),0.0,1.0));
   float  ao   = min(aoCap, max(0.0,depth1-depth2-depthTolerance) * aoMultiplier) * diff;
   return ao;
}

void main(void)
{
   // Calculate texCoord of current pixel
   texCoord = gl_FragCoord.xy/buffersize;

   // Get color and calculate luminance
   vec3 color = texture2D(texture0,texCoord).rgb;
   vec3 luminance = color;
   luminance = clamp(max(vec3(0.0),luminance-treshold) * vec3(3.0), vec3(0.0), vec3(1.0)); // high pass, original code

   // Early out if the luminance is nearly white
   if (allowEarlyOut && any(greaterThanEqual(luminance, vec3(0.7)))){gl_FragColor = vec4(color,1.0); return;}

   // Calculate ao
   float depth     = readDepth(texCoord);
   if (allowEarlyOut && depth >= 1.0){gl_FragColor = vec4(color,1.0); return;}

   float aspect    = buffersize.x/buffersize.y;
   vec2  noise     = rand(texCoord);
   vec2  pixelSize = (vec2(1.0) / buffersize)/ vec2(clamp(depth,0.05,1.0))+(noise*(vec2(1.0)-noise)) * vec2(noiseMultiplier);

   float ao = 0.0, // Total ao
         s  = 0.0;  // Steps

   float fade = 1.0;

   for (int i = 0 ; i < rings; ++i)
   {
       float _step = _2PI / (samples*i);
       fade *= 0.5;

       for (int j = 0 ; j < samples*i; ++j)
       {
           float pw = (cos(j * _step) * i * radius);
           float ph = (sin(j * _step) * i * radius) * aspect;
           float d = readDepth( 
               vec2(texCoord.s + pw * pixelSize.x, 
                    texCoord.t + ph * pixelSize.y));

           ao += compareDepths(depth, d)*fade;     
           s  += 1.0*fade;
       }
   }

   ao /= s;
   ao *= aoMultiplier2;
   ao = 1.0-ao;
   ao = clamp(ao, 0.0, 1.0);
   ao = pow(ao,aoexp);

   gl_FragColor = vec4(color * mix(vec3(ao), vec3(1.0), vec3(luminance)), 1.0);
   return;
}

 

Doesn't look too bad:

post-150-12737548795374_thumb.jpg

Link to comment
Share on other sites

This doesn't work with Framework, but I think Josh should make it work since the current SSAO shader is horrible, it's too slow and makes things look worse than without SSAO.

Ryzen 9 RX 6800M ■ 16GB XF8 Windows 11 ■
Ultra ■ LE 2.53DWS 5.6  Reaper ■ C/C++ C# ■ Fortran 2008 ■ Story ■
■ Homepage: https://canardia.com ■

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...