Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #version 120
- #include "constants.glsl"
- #include "lightmap.glsl"
- #include "shadow.glsl"
- varying vec3 vertexPos;
- varying vec3 Normal;
- float rand(vec2 co){
- return fract(sin(dot(co, vec2(12.9898, 78.233))) * 43758.5453);
- }
- float lerp(float a, float b, float f)
- {
- return a + f * (b - a);
- }
- float AmbientOcclusion(){
- const int kernel_size = 64; // change to 16 to compare
- const float radius = 0.65;
- const float bias = 0.025;
- vec3 kernel[kernel_size];
- for(int i = 0; i<kernel_size ; i++){
- kernel[i] = vec3(
- noise1(1.0),
- noise1(1.0),
- noise1(1.0)*0.5+0.5
- );
- //kernel[i] = normalize(kernel[i]);
- //kernel[i] *= rand(vec2(0.0, 1.0));
- float scale = float(i) / float(kernel_size);
- scale = lerp(0.1f, 1.0f, scale * scale);
- kernel[i] *= scale;
- }
- vec3 normal = normalize( texture2D( colortex1, tex_coords ).rgb * 2.0 - 1.0 );
- vec2 NoiseScale = vec2(viewWidth/noiseTextureResolution, viewHeight/noiseTextureResolution);
- float depth = texture2D(depthtex0, tex_coords).x;
- vec3 origin = vec3(tex_coords,texture2D(depthtex0, tex_coords)) * 2.0 - 1.0;
- vec4 homogenous = gbufferProjectionInverse * vec4(origin, 1.0);
- origin = homogenous.xyz/homogenous.w;
- float ao = 0.0;
- for(int i = 0; i<kernel_size; i++){
- vec3 RandomVector = texture2D(noisetex, tex_coords*NoiseScale).rgb;
- vec3 tangent = normalize(RandomVector - normal * dot(RandomVector, normal));
- vec3 bitangent = cross(normal, tangent);
- mat3 tbn = mat3(tangent, bitangent, normal);
- vec3 samplePos = tbn*kernel[i];
- samplePos = radius * samplePos + origin;
- //vec3 samplePos = viewPos + RandomVector * kernel[i];
- vec4 intermediaryPos = gbufferProjection * vec4(samplePos, 1.0);
- vec3 offset = intermediaryPos.xyz/intermediaryPos.w;
- offset = offset * 0.5 + 0.5;
- float thisDepth = texture2D(depthtex0, offset.xy).r;
- float rangeCheck = smoothstep(0.0, 1.0, radius / abs(origin.z - thisDepth));
- ao += (thisDepth >= samplePos.z + bias? 1.0 : 0.0) * rangeCheck;
- }
- ao = 1.0 - (ao / kernel_size);
- return ao;
- }
- void
- main()
- {
- /* Account for gamma correction */
- vec3 albedo = pow( texture2D( colortex0, tex_coords ).rgb, vec3( gamma_correction ) );
- /* Depth check for sky */
- float depth = texture2D( depthtex0, tex_coords ).r;
- if( depth == 1.0 ) {
- gl_FragData[0] = vec4( albedo, 1.0 );
- return;
- }
- /* Get the normal */
- vec3 normal = normalize( texture2D( colortex1, tex_coords ).rgb * 2.0 - 1.0 );
- /* Get lightmap and it's color */
- vec2 lightmap = texture2D( colortex2, tex_coords ).rg;
- vec3 lightmap_color = get_lightmap_color( lightmap );
- /* Compute cos theta between the normal and sun directions */
- float NdotL = max( dot( normal, normalize( sunPosition ) ), 0.0 );
- float ao = AmbientOcclusion();
- vec3 ambient = vec3(ambient_gamma * albedo * ao);
- // if( worldTime > 13050 || rainStrength>0.1){
- // vec3 normal = normalize( texture2D( colortex1, tex_coords ).rgb * 2.0 - 1.0 );
- // /* Get lightmap and it's color */
- // vec2 lightmap = texture2D( colortex2, tex_coords ).rg;
- // sky_color = vec3(0.05, 0.03, 0.25);
- // vec3 lightmap_color = get_lightmap_color( lightmap );
- // /* Compute cos theta between the normal and sun directions */
- // float NdotL = max( dot( normal, normalize( sunPosition ) ), 0.0 );
- // vec3 diffuse;
- // if(rainStrength > 0.1){
- // diffuse = albedo *(ambient + NdotL + lightmap_color*2.0);
- // }else{
- // diffuse = albedo * (ambient + lightmap_color*2);
- // }
- // /* DRAWBUFFERS:0 */
- // gl_FragData[0] = vec4( diffuse, 1.0);
- // return;
- // }
- /* Final diffuse color */
- vec3 diffuse = albedo * ( lightmap_color + NdotL * get_shadow( depth ) + ambient_gamma) ;
- /* DRAWBUFFERS:0 */
- gl_FragData[0] = vec4( diffuse, 1.0 );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement