Advertisement
Guest User

fCraft's Spatial Filter

a guest
May 21st, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #version 330
  2. in vec2 TexCoord;
  3. out vec3 Color;
  4.  
  5. uniform sampler2D Input;
  6. uniform sampler2D BlockTexCoord;
  7. uniform sampler2D Normals;
  8. uniform sampler2D WorldPos;
  9. uniform sampler2D TemporalSampling;
  10.  
  11. uniform mat4 ProjectionMatrix;
  12. uniform mat4 ViewMatrix;
  13.  
  14. uniform bool Vertical;
  15.  
  16. const int Pixels = 16;
  17.  
  18.  
  19. void main() {
  20.  
  21.     vec2 TextureSize = vec2(textureSize(Input, 0));
  22.     vec2 TexelSize = 1.0 / TextureSize;
  23.     float TotalPixelsFound = 0.0;
  24.  
  25.     vec3 CurrentInput = texture(Input, TexCoord).xyz;
  26.  
  27.     float CurrentTemporalSampling = texture(TemporalSampling, TexCoord).x;
  28.  
  29.  
  30.  
  31.     Color = texture(Normals, TexCoord).xyz == vec3(0.) ? CurrentInput : vec3(0.);
  32.     if (Color.r > 0.001 || Color.g > 0.001 || Color.b > 0.001)
  33.         return;
  34.  
  35.     vec4 CurrentWorldPosition = texture(WorldPos, TexCoord);
  36.  
  37.     vec3 WorldPosPositive = texture(WorldPos, TexCoord + vec2(!Vertical ? TexelSize.x : 0.0, Vertical ? TexelSize.y : 0.0)).xyz;
  38.     vec3 WorldPosNegative = texture(WorldPos, TexCoord - vec2(!Vertical ? TexelSize.x : 0.0, Vertical ? TexelSize.y : 0.0)).xyz;
  39.  
  40.     vec3 RayDirectionPositive = normalize(WorldPosPositive - CurrentWorldPosition.xyz);
  41.     vec3 RayDirectionNegative = normalize(WorldPosPositive - CurrentWorldPosition.xyz);
  42.  
  43.     vec4 NewWorldPositionPositive = vec4(CurrentWorldPosition.xyz + RayDirectionPositive*0.5, 1.);
  44.     vec4 NewWorldPositionNegative = vec4(CurrentWorldPosition.xyz + RayDirectionNegative*0.5, 1.);
  45.  
  46.     vec4 NDCPositive = ProjectionMatrix * ViewMatrix * NewWorldPositionPositive;
  47.     vec2 TCPositive = (NDCPositive.xy / NDCPositive.w) * 0.5 + 0.5;
  48.  
  49.     vec4 NDCNegative = ProjectionMatrix * ViewMatrix * NewWorldPositionPositive;
  50.     vec2 TCNegative = (NDCNegative.xy / NDCNegative.w) * 0.5 + 0.5;
  51.  
  52.     int RangeNegative = min(max(Vertical ? int(floor(abs(TCNegative.y - TexCoord.y) * TextureSize.y)) : int(floor(abs(TCNegative.x - TexCoord.x) * TextureSize.x)), int(ceil(mix(10.0, 7.0, CurrentTemporalSampling)))), int(ceil(mix(32.0, 24.0, CurrentTemporalSampling))));
  53.     int RangePositive = min(max(Vertical ? int(floor(abs(TCPositive.y - TexCoord.y) * TextureSize.y)) : int(floor(abs(TCPositive.x - TexCoord.x) * TextureSize.x)), int(ceil(mix(10.0, 7.0, CurrentTemporalSampling)))), int(ceil(mix(32.0, 24.0, CurrentTemporalSampling))));
  54.  
  55.     float PowerCoef = mix(0.025, 0.05, CurrentTemporalSampling);
  56.  
  57.     vec4 PrevWorldPos = CurrentWorldPosition;
  58.  
  59.     float PixelAddon = 0.0;
  60.  
  61.     float Power = 1.0;
  62.  
  63.     for (int Pixel = -1; Pixel > RangeNegative; Pixel--) {
  64.  
  65.         Power -= PowerCoef;
  66.  
  67.         vec2 Addon = vec2(!Vertical ? TexelSize.x * Pixel : 0., Vertical ? TexelSize.y * Pixel : 0.);
  68.         vec2 NC = TexCoord + Addon;
  69.  
  70.         vec4 ThisWorldPos = texture(WorldPos, NC);
  71.  
  72.  
  73.  
  74.         if (abs(ThisWorldPos.x - PrevWorldPos.x) > 0.6 || abs(ThisWorldPos.y - PrevWorldPos.y) > 0.6 || abs(ThisWorldPos.z - PrevWorldPos.z) > 0.6 || abs(ThisWorldPos.a - PrevWorldPos.a) > 0.1)
  75.             break;
  76.  
  77.         PrevWorldPos = ThisWorldPos;
  78.  
  79.         vec3 c = texture(Input, NC).rgb;
  80.  
  81.         Color += texture(Input, NC).rgb * Power;
  82.  
  83.  
  84.  
  85.         PixelAddon += 1.0 * Power;
  86.  
  87.     }
  88.  
  89.     PrevWorldPos = CurrentWorldPosition;
  90.  
  91.     Power = 1.0;
  92.  
  93.     for (int Pixel = 1; Pixel < RangePositive; Pixel++) {
  94.  
  95.         Power -= PowerCoef;
  96.  
  97.         vec2 Addon = vec2(!Vertical ? TexelSize.x * Pixel : 0., Vertical ? TexelSize.y * Pixel : 0.);
  98.         vec2 NC = TexCoord + Addon;
  99.  
  100.         vec4 ThisWorldPos = texture(WorldPos, NC);
  101.  
  102.  
  103.  
  104.         if (abs(ThisWorldPos.x - PrevWorldPos.x) > 0.6 || abs(ThisWorldPos.y - PrevWorldPos.y) > 0.6 || abs(ThisWorldPos.z - PrevWorldPos.z) > 0.6 || abs(ThisWorldPos.a - PrevWorldPos.a) > 0.1)
  105.             break;
  106.  
  107.         PrevWorldPos = ThisWorldPos;
  108.  
  109.         //float Power = pow(PowerCoef, Pixel);
  110.  
  111.         vec3 c = texture(Input, NC).rgb;
  112.  
  113.         Color += texture(Input, NC).rgb * Power;
  114.  
  115.         PixelAddon += 1.0 * Power;
  116.  
  117.     }
  118.  
  119.  
  120.  
  121.     Color = PixelAddon < 0.1 ? CurrentInput : Color / PixelAddon;
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement