Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.59 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.         gl_FragColor = getBlurredColor();
  78.     } else {
  79.         if ((depth - cursorDepth) > (cursorDepth * HYPERFOCAL) / (HYPERFOCAL - cursorDepth)) {
  80.             gl_FragColor = getBlurredColor();
  81.         } else if (depth > cursorDepth) {
  82.             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));
  83.         } else if (depth <= 0.5 * cursorDepth) {
  84.             gl_FragColor = getBlurredColor();
  85.         } else {
  86.             gl_FragColor = mix(baseColor, getBlurredColor(), clamp(sin(2.0 * (depth - 0.25 * cursorDepth) / cursorDepth * PICONSTANT) / 2.0 + 0.5, 0.0, 1.0));
  87.         }
  88.     }
  89. }
  90.  
  91. float getDepth(vec2 coord) {
  92.     float depth = texture2D(sampler1, coord).x;
  93.     float depth2 = texture2D(sampler2, coord).x;
  94.     if (depth2 < 1.0) {
  95.         depth = depth2;
  96.     }
  97.    
  98.     depth = 2.0 * near * far / (far + near - (2.0 * depth - 1.0) * (far - near));
  99.    
  100.     return depth;
  101. }
  102.  
  103. float getCursorDepth(vec2 coord) {
  104.     return 2.0 * near * far / (far + near - (2.0 * texture2D(sampler1, coord).x - 1.0) * (far - near));
  105. }
  106.  
  107. vec4 getBlurredColor() {
  108.     vec4 blurredColor = vec4(0.0);
  109.     float depth = getDepth(gl_TexCoord[0].xy);
  110.     vec2 aspectCorrection = vec2(1.0, aspectRatio) * 0.005;
  111.  
  112.     vec2 ac0_4 = 0.4 * aspectCorrection;    // 0.4
  113.     //vec2 ac0_4x0_4 = 0.4 * ac0_4;         // 0.16
  114.     //vec2 ac0_4x0_7 = 0.7 * ac0_4;         // 0.28
  115.    
  116.     vec2 ac0_29 = 0.29 * aspectCorrection;  // 0.29
  117.     //vec2 ac0_29x0_7 = 0.7 * ac0_29;           // 0.203
  118.     //vec2 ac0_29x0_4 = 0.4 * ac0_29;           // 0.116
  119.    
  120.     vec2 ac0_15 = 0.15 * aspectCorrection;  // 0.15
  121.     vec2 ac0_37 = 0.37 * aspectCorrection;  // 0.37
  122.     //vec2 ac0_15x0_9 = 0.9 * ac0_15;           // 0.135
  123.     //vec2 ac0_37x0_9 = 0.37 * ac0_37;      // 0.1369
  124.    
  125.     vec2 lowSpace = gl_TexCoord[0].st;
  126.     vec2 highSpace = 1.0 - lowSpace;
  127.     space = vec2(min(lowSpace.s, highSpace.s), min(lowSpace.t, highSpace.t));
  128.        
  129.     if (space.s >= ac0_4.s && space.t >= ac0_4.t) {
  130.  
  131.         blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(0.0, ac0_4.t));
  132.         blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_4.s, 0.0));  
  133.         blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(0.0, -ac0_4.t));
  134.         blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_4.s, 0.0));
  135.        
  136.         //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_4x0_7.s, 0.0));      
  137.         //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(0.0, -ac0_4x0_7.t));    
  138.         //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_4x0_7.s, 0.0));    
  139.         //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(0.0, ac0_4x0_7.t));
  140.    
  141.         //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_4x0_4.s, 0.0));
  142.         //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(0.0, -ac0_4x0_4.t));
  143.         //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_4x0_4.s, 0.0));
  144.         //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(0.0, ac0_4x0_4.t));
  145.  
  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.         blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_29.s, -ac0_29.t));
  150.    
  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.         //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_29x0_7.s, -ac0_29x0_7.t));
  155.        
  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.         //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_29x0_4.s, -ac0_29x0_4.t));
  160.        
  161.        
  162.         blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_15.s, ac0_37.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_37.s, -ac0_15.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_15.s, ac0_37.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_37.s, -ac0_15.t));
  169.         blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_15.s, -ac0_37.t));
  170.        
  171.         //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_15x0_9.s, ac0_37x0_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_37x0_9.s, -ac0_15x0_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_15x0_9.s, ac0_37x0_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_37x0_9.s, -ac0_15x0_9.t));
  178.         //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_15x0_9.s, -ac0_37x0_9.t));
  179.  
  180.         //blurredColor /= 41.0;
  181.         blurredColor /= 16.0;
  182.        
  183.     } else {
  184.        
  185.         blurredColor += getSampleWithBoundsCheck(vec2(0.0, ac0_4.t));
  186.         blurredColor += getSampleWithBoundsCheck(vec2(ac0_4.s, 0.0));  
  187.         blurredColor += getSampleWithBoundsCheck(vec2(0.0, -ac0_4.t));
  188.         blurredColor += getSampleWithBoundsCheck(vec2(-ac0_4.s, 0.0));
  189.        
  190.         //blurredColor += getSampleWithBoundsCheck(vec2(ac0_4x0_7.s, 0.0));      
  191.         //blurredColor += getSampleWithBoundsCheck(vec2(0.0, -ac0_4x0_7.t));    
  192.         //blurredColor += getSampleWithBoundsCheck(vec2(-ac0_4x0_7.s, 0.0));    
  193.         //blurredColor += getSampleWithBoundsCheck(vec2(0.0, ac0_4x0_7.t));
  194.    
  195.         //blurredColor += getSampleWithBoundsCheck(vec2(ac0_4x0_4.s, 0.0));
  196.         //blurredColor += getSampleWithBoundsCheck(vec2(0.0, -ac0_4x0_4.t));
  197.         //blurredColor += getSampleWithBoundsCheck(vec2(-ac0_4x0_4.s, 0.0));
  198.         //blurredColor += getSampleWithBoundsCheck(vec2(0.0, ac0_4x0_4.t));
  199.  
  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.         blurredColor += getSampleWithBoundsCheck(vec2(-ac0_29.s, -ac0_29.t));
  204.    
  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.         //blurredColor += getSampleWithBoundsCheck(vec2(-ac0_29x0_7.s, -ac0_29x0_7.t));
  209.        
  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.         //blurredColor += getSampleWithBoundsCheck(vec2(-ac0_29x0_4.s, -ac0_29x0_4.t));
  214.        
  215.        
  216.         blurredColor += getSampleWithBoundsCheck(vec2(ac0_15.s, ac0_37.t));
  217.         blurredColor += getSampleWithBoundsCheck(vec2(-ac0_37.s, ac0_15.t));
  218.         blurredColor += getSampleWithBoundsCheck(vec2(ac0_37.s, -ac0_15.t));
  219.         blurredColor += getSampleWithBoundsCheck(vec2(-ac0_15.s, -ac0_37.t));
  220.         blurredColor += getSampleWithBoundsCheck(vec2(-ac0_15.s, ac0_37.t));
  221.         blurredColor += getSampleWithBoundsCheck(vec2(ac0_37.s, ac0_15.t));
  222.         blurredColor += getSampleWithBoundsCheck(vec2(-ac0_37.s, -ac0_15.t));
  223.         blurredColor += getSampleWithBoundsCheck(vec2(ac0_15.s, -ac0_37.t));
  224.        
  225.         //blurredColor += getSampleWithBoundsCheck(vec2(ac0_15x0_9.s, ac0_37x0_9.t));
  226.         //blurredColor += getSampleWithBoundsCheck(vec2(-ac0_37x0_9.s, ac0_15x0_9.t));
  227.         //blurredColor += getSampleWithBoundsCheck(vec2(ac0_37x0_9.s, -ac0_15x0_9.t));
  228.         //blurredColor += getSampleWithBoundsCheck(vec2(-ac0_15x0_9.s, -ac0_37x0_9.t));
  229.         //blurredColor += getSampleWithBoundsCheck(vec2(-ac0_15x0_9.s, ac0_37x0_9.t));
  230.         //blurredColor += getSampleWithBoundsCheck(vec2(ac0_37x0_9.s, ac0_15x0_9.t));
  231.         //blurredColor += getSampleWithBoundsCheck(vec2(-ac0_37x0_9.s, -ac0_15x0_9.t));
  232.         //blurredColor += getSampleWithBoundsCheck(vec2(ac0_15x0_9.s, -ac0_37x0_9.t));
  233.    
  234.         blurredColor /= samples;
  235.        
  236.     }
  237.  
  238.     return blurredColor;
  239. }
  240.  
  241. vec4 getSampleWithBoundsCheck(vec2 offset) {
  242.     vec2 coord = gl_TexCoord[0].st + offset;
  243.     if (coord.s <= 1.0 && coord.s >= 0.0 && coord.t <= 1.0 && coord.t >= 0.0) {
  244.         samples += 1.0;
  245.         return texture2D(sampler0, coord);
  246.     } else {
  247.         return vec4(0.0);
  248.     }
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement