Ooops
September 18, 2021, 23:31:55

Author Topic: ssao & pale outline code  (Read 245 times)

Offline iWasAdam

  • Hero Member
  • *****
  • Posts: 2444
ssao & pale outline code
« on: April 17, 2021, 14:31:22 »
Basic shader code for faux ssao and pale outlines


Code: [Select]
//@renderpasses 0

uniform sampler2D r_DepthBuffer;
uniform sampler2D r_SourceBuffer;
uniform sampler2D r_NormalBuffer;
uniform sampler2D r_AccumBuffer;

uniform vec2 r_SourceBufferScale;

varying vec2 v_SourceBufferCoords;

//@vertex

attribute vec2 a_Position;
void main(){
v_SourceBufferCoords=a_Position * r_SourceBufferScale;

gl_Position=vec4( a_Position * 2.0 - 1.0,-1.0,1.0 );
}

//@fragment


uniform float m_Level;


float SSAO( float dColor, float dist, float mul, vec2 uv ){
dist = dColor * 0.02 * dist;
float dColor1 = texture2D( r_DepthBuffer, vec2(v_SourceBufferCoords.x + dist, v_SourceBufferCoords.y)).r;
float dColor2 = texture2D( r_DepthBuffer, vec2(v_SourceBufferCoords.x - dist, v_SourceBufferCoords.y)).r;
float dColor3 = texture2D( r_DepthBuffer, vec2(v_SourceBufferCoords.x, v_SourceBufferCoords.y + dist)).r;
float dColor4 = texture2D( r_DepthBuffer, vec2(v_SourceBufferCoords.x, v_SourceBufferCoords.y - dist)).r;
float dColor0 = (dColor1 + dColor2 + dColor3 + dColor4) * 0.25;

float diff = (dColor) - (dColor0);

if( diff > 0.1 ) diff = 0.0;

return diff * mul;
}

float Outline( float dColor, float dist, vec2 uv ){
float dColor1 = texture2D( r_NormalBuffer, vec2(v_SourceBufferCoords.x + dist, v_SourceBufferCoords.y)).r;
float dColor2 = texture2D( r_NormalBuffer, vec2(v_SourceBufferCoords.x - dist, v_SourceBufferCoords.y)).r;
float dColor3 = texture2D( r_NormalBuffer, vec2(v_SourceBufferCoords.x, v_SourceBufferCoords.y + dist)).r;
float dColor4 = texture2D( r_NormalBuffer, vec2(v_SourceBufferCoords.x, v_SourceBufferCoords.y - dist)).r;
float dColor0 = (dColor1 + dColor2 + dColor3 + dColor4) * 0.25;

float diff = abs(dColor - dColor0);

return diff;
}

void main(){
vec4 iColor = texture2D( r_SourceBuffer, v_SourceBufferCoords );
vec3 outColor = iColor.rgb;

// float mono = (iColor.r + iColor.g + iColor.b) / 3.0;
// vec3 oColor = vec3( mono );
// gl_FragColor = vec4( oColor, iColor.a );

float dColor = texture2D( r_DepthBuffer, v_SourceBufferCoords ).r;

//ssao
if( dColor < 1.0 ){
float diff = SSAO( dColor, 1.0, 1.0, v_SourceBufferCoords );
float diff2 = SSAO( dColor, 0.7, 2.0, v_SourceBufferCoords );
float diff3 = SSAO( dColor, 0.5, 3.0, v_SourceBufferCoords );
float diff4 = SSAO( dColor, 0.25, 4.0, v_SourceBufferCoords );

diff += diff2 + diff3 + diff4;
if(diff > 0.0){
outColor = mix( iColor.rgb, iColor.rgb * 0.1, clamp(diff * 6.0, 0.0, 1.0));
}
}

//hi-light
dColor = texture2D( r_NormalBuffer, v_SourceBufferCoords ).r;
float lColor = Outline( dColor, 0.0005, v_SourceBufferCoords );
float add = 0.0;
if( lColor > 0.0005 ){
add = 0.01;
}
lColor = Outline( dColor, 0.0002, v_SourceBufferCoords );
if( lColor > 0.0005 ){
add += 0.03;
}

gl_FragColor = vec4( mix(outColor, vec3(1.0, 1.0, 1.0), add), iColor.a );

}

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal