Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.55 KB | None | 0 0
  1. // More realistic depth-of-field by Azraeil.
  2. // This is a modification of Daxnitro's depth-of-field shader.
  3.  
  4. uniform sampler2D sampler0;
  5. uniform sampler2D sampler1;
  6. uniform sampler2D sampler2;
  7.  
  8. uniform float aspectRatio;
  9. uniform float near;
  10. uniform float far;
  11.  
  12.  
  13. // HYPERFOCAL = (Focal Distance ^ 2)/(Circle of Confusion * F Stop) + Focal Distance
  14. const float HYPERFOCAL = 3.132;
  15. const float PICONSTANT = 3.14159;
  16.  
  17. float getDepth(vec2 coord);
  18. float getCursorDepth(vec2 coord);
  19. vec4 getBlurredColor();
  20. vec4 getSample(vec2 coord, vec2 aspectCorrection);
  21. vec4 getSampleWithBoundsCheck(vec2 offset);
  22.  
  23. float samples = 0.0;
  24. vec2 space;
  25.  
  26. void main() {
  27.     vec4 baseColor = texture2D(sampler0, gl_TexCoord[0].st);
  28.  
  29.     float depth = getDepth(gl_TexCoord[0].st);
  30.        
  31.     float cursorDepth = getCursorDepth(vec2(0.5, 0.5));
  32.    
  33.     // foreground blur = 1/2 background blur. Blur should follow exponential pattern until cursor = hyperfocal -- Cursor before hyperfocal
  34.     // Blur should go from 0 to 1/2 hyperfocal then clear to infinity -- Cursor @ hyperfocal.
  35.     // hyperfocal to inifity is clear though dof extends from 1/2 hyper to hyper -- Cursor beyond hyperfocal
  36.    
  37.     /*  Use for testing DoF
  38.     float brightness = 0.0;
  39.    
  40.     if (cursorDepth > HYPERFOCAL) {
  41.         if (depth < HYPERFOCAL) {
  42.             if (depth < 0.5 * HYPERFOCAL) {
  43.                 brightness = 0.0;
  44.             } else {
  45.                 brightness = 1.0 - (sin(2.0 * (depth - 0.25 * HYPERFOCAL) / HYPERFOCAL * PICONSTANT) / 2.0 + 0.5);
  46.             }
  47.         } else {
  48.             brightness = 1.0;
  49.         }
  50.     } else {
  51.         if (depth - cursorDepth > (cursorDepth * HYPERFOCAL) / (HYPERFOCAL - cursorDepth)) {
  52.             brightness = 0.0;
  53.         } else if (depth > cursorDepth) {
  54.             brightness = 1.0 - (sin(PICONSTANT * ((depth - cursorDepth) - 0.5 * ((cursorDepth * HYPERFOCAL) / (HYPERFOCAL - cursorDepth))) / ((cursorDepth * HYPERFOCAL) / (HYPERFOCAL - cursorDepth))) / 2.0 + 0.5);
  55.         } else if (depth <= 0.5 * cursorDepth) {
  56.             brightness = 0.0;
  57.         } else {
  58.             brightness = 1.0 - (sin(2.0 * (depth - 0.25 * cursorDepth) / cursorDepth * PICONSTANT) / 2.0 + 0.5);
  59.         }
  60.     }
  61.    
  62.     gl_FragColor = vec4(brightness, brightness, brightness, 1.0);
  63.     */
  64.  
  65.     // need to optimize in hopes to avoid more branching.
  66.  
  67.     if (cursorDepth > HYPERFOCAL) {
  68.         if (depth < HYPERFOCAL) {
  69.             if (depth < (0.5 * HYPERFOCAL)) {
  70.                 gl_FragColor = getBlurredColor();
  71.             } else {
  72.                 gl_FragColor = mix(baseColor, getBlurredColor(), clamp(sin(2.0 * (depth - 0.25 * HYPERFOCAL) / HYPERFOCAL * PICONSTANT) / 2.0 + 0.5, 0.0, 1.0));
  73.             }
  74.         } else {
  75.             gl_FragColor = baseColor;
  76.         }
  77.     } else {
  78.         if ((depth - cursorDepth) > ((cursorDepth * HYPERFOCAL) / (HYPERFOCAL - cursorDepth))) {
  79.             gl_FragColor = getBlurredColor();
  80.         } else if (depth > cursorDepth) {
  81.             gl_FragColor = mix(baseColor, getBlurredColor(), clamp(sin(PICONSTANT * ((depth - cursorDepth) - 0.5 * ((cursorDepth * HYPERFOCAL) / (HYPERFOCAL - cursorDepth))) / ((cursorDepth * HYPERFOCAL) / (HYPERFOCAL - cursorDepth))) / 2.0 + 0.5, 0.0, 1.0));
  82.         } else if (depth <= (0.5 * cursorDepth)) {
  83.             gl_FragColor = getBlurredColor();
  84.         } else {
  85.             gl_FragColor = mix(baseColor, getBlurredColor(), clamp(sin(2.0 * (depth - 0.25 * cursorDepth) / cursorDepth * PICONSTANT) / 2.0 + 0.5, 0.0, 1.0));
  86.         }
  87.     }
  88. }
  89.  
  90. float getDepth(vec2 coord) {
  91.     float depth = texture2D(sampler1, coord).x;
  92.     float depth2 = texture2D(sampler2, coord).x;
  93.     if (depth2 < 1.0) {
  94.         depth = depth2;
  95.     }
  96.    
  97.     depth = 2.0 * near * far / (far + near - (2.0 * depth - 1.0) * (far - near));
  98.    
  99.     return depth;
  100. }
  101.  
  102. float getCursorDepth(vec2 coord) {
  103.     return 2.0 * near * far / (far + near - (2.0 * texture2D(sampler1, coord).x - 1.0) * (far - near));
  104. }
  105.  
  106. vec4 getBlurredColor() {
  107.     vec4 blurredColor = vec4(0.0);
  108.     float depth = getDepth(gl_TexCoord[0].xy);
  109.     vec2 aspectCorrection = vec2(1.0, aspectRatio) * 0.005;
  110.  
  111.     vec2 ac0_4 = 0.4 * aspectCorrection;    // 0.4
  112.     //vec2 ac0_4x0_4 = 0.4 * ac0_4;         // 0.16
  113.     //vec2 ac0_4x0_7 = 0.7 * ac0_4;         // 0.28
  114.    
  115.     vec2 ac0_29 = 0.29 * aspectCorrection;  // 0.29
  116.     //vec2 ac0_29x0_7 = 0.7 * ac0_29;           // 0.203
  117.     //vec2 ac0_29x0_4 = 0.4 * ac0_29;           // 0.116
  118.    
  119.     vec2 ac0_15 = 0.15 * aspectCorrection;  // 0.15
  120.     vec2 ac0_37 = 0.37 * aspectCorrection;  // 0.37
  121.     //vec2 ac0_15x0_9 = 0.9 * ac0_15;           // 0.135
  122.     //vec2 ac0_37x0_9 = 0.37 * ac0_37;      // 0.1369
  123.    
  124.     vec2 lowSpace = gl_TexCoord[0].st;
  125.     vec2 highSpace = 1.0 - lowSpace;
  126.     space = vec2(min(lowSpace.s, highSpace.s), min(lowSpace.t, highSpace.t));
  127.        
  128.     if (space.s >= ac0_4.s && space.t >= ac0_4.t) {
  129.  
  130.         blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(0.0, ac0_4.t));
  131.         blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_4.s, 0.0));  
  132.         blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(0.0, -ac0_4.t));
  133.         blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_4.s, 0.0));
  134.        
  135.         //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_4x0_7.s, 0.0));      
  136.         //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(0.0, -ac0_4x0_7.t));    
  137.         //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_4x0_7.s, 0.0));    
  138.         //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(0.0, ac0_4x0_7.t));
  139.    
  140.         //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_4x0_4.s, 0.0));
  141.         //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(0.0, -ac0_4x0_4.t));
  142.         //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_4x0_4.s, 0.0));
  143.         //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(0.0, ac0_4x0_4.t));
  144.  
  145.         blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_29.s, -ac0_29.t));
  146.         blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_29.s, ac0_29.t));
  147.         blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_29.s, ac0_29.t));
  148.         blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_29.s, -ac0_29.t));
  149.    
  150.         //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_29x0_7.s, ac0_29x0_7.t));
  151.         //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_29x0_7.s, -ac0_29x0_7.t));
  152.         //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_29x0_7.s, ac0_29x0_7.t));
  153.         //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_29x0_7.s, -ac0_29x0_7.t));
  154.        
  155.         //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_29x0_4.s, ac0_29x0_4.t));
  156.         //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_29x0_4.s, -ac0_29x0_4.t));
  157.         //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_29x0_4.s, ac0_29x0_4.t));
  158.         //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_29x0_4.s, -ac0_29x0_4.t));
  159.        
  160.        
  161.         blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_15.s, ac0_37.t));
  162.         blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_37.s, ac0_15.t));
  163.         blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_37.s, -ac0_15.t));
  164.         blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_15.s, -ac0_37.t));
  165.         blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_15.s, ac0_37.t));
  166.         blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_37.s, ac0_15.t));
  167.         blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_37.s, -ac0_15.t));
  168.         blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_15.s, -ac0_37.t));
  169.        
  170.         //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_15x0_9.s, ac0_37x0_9.t));
  171.         //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_37x0_9.s, ac0_15x0_9.t));
  172.         //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_37x0_9.s, -ac0_15x0_9.t));
  173.         //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_15x0_9.s, -ac0_37x0_9.t));
  174.         //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_15x0_9.s, ac0_37x0_9.t));
  175.         //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_37x0_9.s, ac0_15x0_9.t));
  176.         //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_37x0_9.s, -ac0_15x0_9.t));
  177.         //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_15x0_9.s, -ac0_37x0_9.t));
  178.  
  179.         //blurredColor /= 41.0;
  180.         blurredColor /= 16.0;
  181.        
  182.     } else {
  183.        
  184.         blurredColor += getSampleWithBoundsCheck(vec2(0.0, ac0_4.t));
  185.         blurredColor += getSampleWithBoundsCheck(vec2(ac0_4.s, 0.0));  
  186.         blurredColor += getSampleWithBoundsCheck(vec2(0.0, -ac0_4.t));
  187.         blurredColor += getSampleWithBoundsCheck(vec2(-ac0_4.s, 0.0));
  188.        
  189.         //blurredColor += getSampleWithBoundsCheck(vec2(ac0_4x0_7.s, 0.0));      
  190.         //blurredColor += getSampleWithBoundsCheck(vec2(0.0, -ac0_4x0_7.t));    
  191.         //blurredColor += getSampleWithBoundsCheck(vec2(-ac0_4x0_7.s, 0.0));    
  192.         //blurredColor += getSampleWithBoundsCheck(vec2(0.0, ac0_4x0_7.t));
  193.    
  194.         //blurredColor += getSampleWithBoundsCheck(vec2(ac0_4x0_4.s, 0.0));
  195.         //blurredColor += getSampleWithBoundsCheck(vec2(0.0, -ac0_4x0_4.t));
  196.         //blurredColor += getSampleWithBoundsCheck(vec2(-ac0_4x0_4.s, 0.0));
  197.         //blurredColor += getSampleWithBoundsCheck(vec2(0.0, ac0_4x0_4.t));
  198.  
  199.         blurredColor += getSampleWithBoundsCheck(vec2(ac0_29.s, -ac0_29.t));
  200.         blurredColor += getSampleWithBoundsCheck(vec2(ac0_29.s, ac0_29.t));
  201.         blurredColor += getSampleWithBoundsCheck(vec2(-ac0_29.s, ac0_29.t));
  202.         blurredColor += getSampleWithBoundsCheck(vec2(-ac0_29.s, -ac0_29.t));
  203.    
  204.         //blurredColor += getSampleWithBoundsCheck(vec2(ac0_29x0_7.s, ac0_29x0_7.t));
  205.         //blurredColor += getSampleWithBoundsCheck(vec2(ac0_29x0_7.s, -ac0_29x0_7.t));
  206.         //blurredColor += getSampleWithBoundsCheck(vec2(-ac0_29x0_7.s, ac0_29x0_7.t));
  207.         //blurredColor += getSampleWithBoundsCheck(vec2(-ac0_29x0_7.s, -ac0_29x0_7.t));
  208.        
  209.         //blurredColor += getSampleWithBoundsCheck(vec2(ac0_29x0_4.s, ac0_29x0_4.t));
  210.         //blurredColor += getSampleWithBoundsCheck(vec2(ac0_29x0_4.s, -ac0_29x0_4.t));
  211.         //blurredColor += getSampleWithBoundsCheck(vec2(-ac0_29x0_4.s, ac0_29x0_4.t));
  212.         //blurredColor += getSampleWithBoundsCheck(vec2(-ac0_29x0_4.s, -ac0_29x0_4.t));
  213.        
  214.        
  215.         blurredColor += getSampleWithBoundsCheck(vec2(ac0_15.s, ac0_37.t));
  216.         blurredColor += getSampleWithBoundsCheck(vec2(-ac0_37.s, ac0_15.t));
  217.         blurredColor += getSampleWithBoundsCheck(vec2(ac0_37.s, -ac0_15.t));
  218.         blurredColor += getSampleWithBoundsCheck(vec2(-ac0_15.s, -ac0_37.t));
  219.         blurredColor += getSampleWithBoundsCheck(vec2(-ac0_15.s, ac0_37.t));
  220.         blurredColor += getSampleWithBoundsCheck(vec2(ac0_37.s, ac0_15.t));
  221.         blurredColor += getSampleWithBoundsCheck(vec2(-ac0_37.s, -ac0_15.t));
  222.         blurredColor += getSampleWithBoundsCheck(vec2(ac0_15.s, -ac0_37.t));
  223.        
  224.         //blurredColor += getSampleWithBoundsCheck(vec2(ac0_15x0_9.s, ac0_37x0_9.t));
  225.         //blurredColor += getSampleWithBoundsCheck(vec2(-ac0_37x0_9.s, ac0_15x0_9.t));
  226.         //blurredColor += getSampleWithBoundsCheck(vec2(ac0_37x0_9.s, -ac0_15x0_9.t));
  227.         //blurredColor += getSampleWithBoundsCheck(vec2(-ac0_15x0_9.s, -ac0_37x0_9.t));
  228.         //blurredColor += getSampleWithBoundsCheck(vec2(-ac0_15x0_9.s, ac0_37x0_9.t));
  229.         //blurredColor += getSampleWithBoundsCheck(vec2(ac0_37x0_9.s, ac0_15x0_9.t));
  230.         //blurredColor += getSampleWithBoundsCheck(vec2(-ac0_37x0_9.s, -ac0_15x0_9.t));
  231.         //blurredColor += getSampleWithBoundsCheck(vec2(ac0_15x0_9.s, -ac0_37x0_9.t));
  232.    
  233.         blurredColor /= samples;
  234.        
  235.     }
  236.  
  237.     return blurredColor;
  238. }
  239.  
  240. vec4 getSampleWithBoundsCheck(vec2 offset) {
  241.     vec2 coord = gl_TexCoord[0].st + offset;
  242.     if (coord.s <= 1.0 && coord.s >= 0.0 && coord.t <= 1.0 && coord.t >= 0.0) {
  243.         samples += 1.0;
  244.         return texture2D(sampler0, coord);
  245.     } else {
  246.         return vec4(0.0);
  247.     }
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement