Advertisement
lylythechosenone

gbuffers_terrain.fsh

Feb 4th, 2021
2,965
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #version 120
  2.  
  3. #define COLORED_SHADOWS 1 //0: Stained glass will cast ordinary shadows. 1: Stained glass will cast colored shadows. 2: Stained glass will not cast any shadows. [0 1 2]
  4. #define SHADOW_BRIGHTNESS 0.75 //Light levels are multiplied by this number when the surface is in shadows [0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55 0.60 0.65 0.70 0.75 0.80 0.85 0.90 0.95 1.00]
  5.  
  6. uniform sampler2D lightmap;
  7. uniform sampler2D texture;
  8.  
  9. varying vec2 lmcoord;
  10. varying vec2 texcoord;
  11. varying vec4 glcolor;
  12. varying vec4 shadowPos;
  13.  
  14. varying flat int worldTimeVarying;
  15.  
  16. uniform int isEyeInWater;
  17.  
  18. uniform sampler2D shadowcolor0;
  19. uniform sampler2D shadowtex0;
  20. uniform sampler2D shadowtex1;
  21.  
  22. uniform vec3 cameraPosition;
  23.  
  24. #define Exposure 1.0 // The exposure [0.5 1.0 1.5 2.0 5.0]
  25.  
  26. const int shadowMapResolution = 1024; //Resolution of the shadow map. Higher numbers mean more accurate shadows. [128 256 512 1024 2048 4096 8192]
  27.  
  28. const bool shadowcolor0Nearest = true;
  29. const bool shadowtex0Nearest = true;
  30. const bool shadowtex1Nearest = true;
  31.  
  32. void main() {
  33.     vec4 color = texture2D(texture, texcoord) * glcolor * Exposure;
  34.     vec2 lm = lmcoord;
  35.     if (shadowPos.w > 0.0) {
  36.         //surface is facing towards shadowLightPosition
  37.         #if COLORED_SHADOWS == 0
  38.  
  39.         //for normal shadows, only consider the closest thing to the sun,
  40.         //regardless of whether or not it's opaque.
  41.         if (texture2D(shadowtex0, shadowPos.xy).r < shadowPos.z) {
  42.             #else
  43.             //for invisible and colored shadows, first check the closest OPAQUE thing to the sun.
  44.             if (texture2D(shadowtex1, shadowPos.xy).r < shadowPos.z) {
  45.                 #endif
  46.                 //surface is in shadows. reduce light level.
  47.                 lm.y *= SHADOW_BRIGHTNESS;
  48.             }
  49.             else {
  50.                 //surface is in direct sunlight. increase light level.
  51.                 lm.y = mix(31.0 / 32.0 * SHADOW_BRIGHTNESS, 31.0 / 32.0, sqrt(shadowPos.w));
  52.                 #if COLORED_SHADOWS == 1
  53.                 //when colored shadows are enabled and there's nothing OPAQUE between us and the sun,
  54.                 //perform a 2nd check to see if there's anything translucent between us and the sun.
  55.                 if (texture2D(shadowtex0, shadowPos.xy).r < shadowPos.z) {
  56.                     //surface has translucent object between it and the sun. modify its color.
  57.                     //if the block light is high, modify the color less.
  58.                     vec4 shadowLightColor = texture2D(shadowcolor0, shadowPos.xy);
  59.                     //make colors more intense when the shadow light color is more opaque.
  60.                     shadowLightColor.rgb = mix(vec3(1.0), shadowLightColor.rgb, shadowLightColor.a);
  61.                     //also make colors less intense when the block light level is high.
  62.                     shadowLightColor.rgb = mix(shadowLightColor.rgb, vec3(1.0), lm.x);
  63.                     //apply the color.
  64.                     lightmap.rgb *= shadowLightColor.rgb;
  65.                 }
  66.                     #endif
  67.             }
  68.         }
  69.     }
  70.    
  71.     vec4 lmVec4 = texture2D(lightmap, lmcoord);
  72.     // Normalized pixel coordinates (from 0 to 1)
  73.     vec2 uv = lmcoord/vec2(shadowMapResolution);
  74.  
  75.     // Define the bluring radius/strength (direction independant)
  76.     vec2 radius = vec2(20.0);
  77.     // Calculate the value at the corners so that the matrix can be easily inverted
  78.     float maximum = sqrt(radius.x * radius.x + radius.y * radius.y);
  79.     // Define the variable which will hold the blured values
  80.     vec3 blur = vec3(0.0);
  81.  
  82.     // Define the variable which will be used to normalize the image
  83.     float sum = 0.0;
  84.     // The kernel is dynamically created based on the bluring radius
  85.     for(float u = -radius.x; u<=radius.x; u++){
  86.         for(float v = -radius.y; v<=radius.y; v++){
  87.             // The pixel weight used by the kernel is defined as: the distance from the kernel origin (0,0)
  88.             // to the current kernel position, subtracted from the maximum possible distance. This leads
  89.             // to a gradient from 0% relative weight on the edges to 100% relative weight at the origin of the kernel
  90.             float weight = maximum - sqrt(u * u + v * v);
  91.             // The weight is then exponentialized which seams to sleightly maintain more of the origianl detail
  92.             //weight = pow(weight, 2.0);
  93.             // The weight is then multiplied by the texture being sampled and added to the overall blurred values
  94.             blur += weight * texture2D(lightmap, uv + (vec2(u, v)/vec2(shadowMapResolution))).xyz;
  95.             // The weight is then added for normalizing purposes
  96.             sum += weight;
  97.         }
  98.     }
  99.     // Finally the blurred image is normalized
  100.     blur /= sum;
  101.  
  102.     // Output to screen
  103.     lmVec4 = vec4(blur , 1.0);
  104.    
  105.     if (isEyeInWater == 1) {
  106.         // Water tint
  107.         color *= vec4(0.0, 0.3, 0.7, 1.0) * (((-(worldTimeVarying - 24000)) + 18000) / 18000);
  108.     } else {
  109.         // Normal rendering
  110.         color *= lmVec4;
  111.     }
  112.  
  113. /* DRAWBUFFERS:0 */
  114.     gl_FragData[0] = color; //gcolor
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement