Advertisement
Davram

composite.fsh

Sep 2nd, 2023
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | Source Code | 0 0
  1. #version 120
  2. #include "constants.glsl"
  3. #include "lightmap.glsl"
  4. #include "shadow.glsl"
  5.  
  6. varying vec3 vertexPos;
  7. varying vec3 Normal;
  8.  
  9. float rand(vec2 co){
  10. return fract(sin(dot(co, vec2(12.9898, 78.233))) * 43758.5453);
  11. }
  12.  
  13. float lerp(float a, float b, float f)
  14. {
  15. return a + f * (b - a);
  16. }
  17.  
  18. float AmbientOcclusion(){
  19. const int kernel_size = 64; // change to 16 to compare
  20. const float radius = 0.65;
  21. const float bias = 0.025;
  22.  
  23. vec3 kernel[kernel_size];
  24.  
  25. for(int i = 0; i<kernel_size ; i++){
  26. kernel[i] = vec3(
  27. noise1(1.0),
  28. noise1(1.0),
  29. noise1(1.0)*0.5+0.5
  30. );
  31.  
  32. //kernel[i] = normalize(kernel[i]);
  33. //kernel[i] *= rand(vec2(0.0, 1.0));
  34. float scale = float(i) / float(kernel_size);
  35. scale = lerp(0.1f, 1.0f, scale * scale);
  36. kernel[i] *= scale;
  37. }
  38.  
  39. vec3 normal = normalize( texture2D( colortex1, tex_coords ).rgb * 2.0 - 1.0 );
  40.  
  41. vec2 NoiseScale = vec2(viewWidth/noiseTextureResolution, viewHeight/noiseTextureResolution);
  42.  
  43. float depth = texture2D(depthtex0, tex_coords).x;
  44. vec3 origin = vec3(tex_coords,texture2D(depthtex0, tex_coords)) * 2.0 - 1.0;
  45. vec4 homogenous = gbufferProjectionInverse * vec4(origin, 1.0);
  46. origin = homogenous.xyz/homogenous.w;
  47.  
  48. float ao = 0.0;
  49.  
  50. for(int i = 0; i<kernel_size; i++){
  51. vec3 RandomVector = texture2D(noisetex, tex_coords*NoiseScale).rgb;
  52.  
  53. vec3 tangent = normalize(RandomVector - normal * dot(RandomVector, normal));
  54. vec3 bitangent = cross(normal, tangent);
  55. mat3 tbn = mat3(tangent, bitangent, normal);
  56. vec3 samplePos = tbn*kernel[i];
  57. samplePos = radius * samplePos + origin;
  58. //vec3 samplePos = viewPos + RandomVector * kernel[i];
  59.  
  60. vec4 intermediaryPos = gbufferProjection * vec4(samplePos, 1.0);
  61. vec3 offset = intermediaryPos.xyz/intermediaryPos.w;
  62. offset = offset * 0.5 + 0.5;
  63.  
  64. float thisDepth = texture2D(depthtex0, offset.xy).r;
  65.  
  66. float rangeCheck = smoothstep(0.0, 1.0, radius / abs(origin.z - thisDepth));
  67.  
  68. ao += (thisDepth >= samplePos.z + bias? 1.0 : 0.0) * rangeCheck;
  69.  
  70. }
  71.  
  72. ao = 1.0 - (ao / kernel_size);
  73.  
  74. return ao;
  75. }
  76.  
  77.  
  78.  
  79. void
  80. main()
  81. {
  82. /* Account for gamma correction */
  83. vec3 albedo = pow( texture2D( colortex0, tex_coords ).rgb, vec3( gamma_correction ) );
  84.  
  85. /* Depth check for sky */
  86. float depth = texture2D( depthtex0, tex_coords ).r;
  87. if( depth == 1.0 ) {
  88. gl_FragData[0] = vec4( albedo, 1.0 );
  89. return;
  90. }
  91.  
  92. /* Get the normal */
  93. vec3 normal = normalize( texture2D( colortex1, tex_coords ).rgb * 2.0 - 1.0 );
  94.  
  95. /* Get lightmap and it's color */
  96. vec2 lightmap = texture2D( colortex2, tex_coords ).rg;
  97. vec3 lightmap_color = get_lightmap_color( lightmap );
  98.  
  99. /* Compute cos theta between the normal and sun directions */
  100. float NdotL = max( dot( normal, normalize( sunPosition ) ), 0.0 );
  101.  
  102. float ao = AmbientOcclusion();
  103.  
  104. vec3 ambient = vec3(ambient_gamma * albedo * ao);
  105.  
  106. // if( worldTime > 13050 || rainStrength>0.1){
  107.  
  108. // vec3 normal = normalize( texture2D( colortex1, tex_coords ).rgb * 2.0 - 1.0 );
  109.  
  110.  
  111. // /* Get lightmap and it's color */
  112. // vec2 lightmap = texture2D( colortex2, tex_coords ).rg;
  113. // sky_color = vec3(0.05, 0.03, 0.25);
  114. // vec3 lightmap_color = get_lightmap_color( lightmap );
  115.  
  116. // /* Compute cos theta between the normal and sun directions */
  117. // float NdotL = max( dot( normal, normalize( sunPosition ) ), 0.0 );
  118.  
  119. // vec3 diffuse;
  120. // if(rainStrength > 0.1){
  121. // diffuse = albedo *(ambient + NdotL + lightmap_color*2.0);
  122.  
  123. // }else{
  124. // diffuse = albedo * (ambient + lightmap_color*2);
  125. // }
  126.  
  127.  
  128. // /* DRAWBUFFERS:0 */
  129. // gl_FragData[0] = vec4( diffuse, 1.0);
  130.  
  131. // return;
  132. // }
  133.  
  134. /* Final diffuse color */
  135.  
  136.  
  137. vec3 diffuse = albedo * ( lightmap_color + NdotL * get_shadow( depth ) + ambient_gamma) ;
  138.  
  139. /* DRAWBUFFERS:0 */
  140.  
  141. gl_FragData[0] = vec4( diffuse, 1.0 );
  142. }
Tags: shaders
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement