|
| 1 | +--- |
| 2 | +weight: 5 |
| 3 | +--- |
| 4 | + |
| 5 | +# Desenfoque Horizontal |
| 6 | + |
| 7 | +Los filtros de desenfoque son un efecto en el que |
| 8 | +se suaviza la textura sobre la que este se aplica, de |
| 9 | +modo que se atenúan las diferencias abruptas de color. |
| 10 | + |
| 11 | +Existen diferentes tipos de efectos de desenfoque, sin embargo en este |
| 12 | +apartado se tratará el desenfoque horizontal, que difiere de los otros al |
| 13 | +incrementar la pérdida de detalle sobre los elementos que se encuentren a mayor |
| 14 | +distancia vertical de un horizonte dado. |
| 15 | + |
| 16 | +La demostrasión de este efecto se hace sobre un conjunto de elementos aleatoriamente |
| 17 | +posicionados en el espacio, de modo que el observador pueda encontrar un objeto a |
| 18 | +diferentes alturas en la imagen y evidenciar de ese modo la forma en que el nivel de desenfoque |
| 19 | +varía. |
| 20 | + |
| 21 | +{{< p5-iframe sketch="/posteffects/sketches/horizontal/sketch.js" lib1="https://cdn.jsdelivr.net/gh/freshfork/p5.EasyCam/p5.easycam.js" lib2= |
| 22 | +"https://cdn.jsdelivr.net/gh/VisualComputing/p5.treegl/p5.treegl.js" width="625" height="325" >}} |
| 23 | + |
| 24 | +## Código del Fragment Shader del efecto. |
| 25 | + |
| 26 | +{{< details "horizontal.frag" open >}} |
| 27 | +``` |
| 28 | +precision mediump float; |
| 29 | +
|
| 30 | +uniform sampler2D tDiffuse; |
| 31 | +uniform float h; |
| 32 | +uniform float r; |
| 33 | +
|
| 34 | +varying vec2 texcoords2; |
| 35 | +
|
| 36 | +void main() { |
| 37 | + vec2 vUv = texcoords2.st; |
| 38 | + vec4 sum = vec4( 0.0 ); |
| 39 | +
|
| 40 | + float hh = h * abs( r - vUv.y ); |
| 41 | +
|
| 42 | + sum += texture2D( tDiffuse, vec2( vUv.x - 4.0 * hh, vUv.y ) ) * 0.051; |
| 43 | + sum += texture2D( tDiffuse, vec2( vUv.x - 3.0 * hh, vUv.y ) ) * 0.0918; |
| 44 | + sum += texture2D( tDiffuse, vec2( vUv.x - 2.0 * hh, vUv.y ) ) * 0.12245; |
| 45 | + sum += texture2D( tDiffuse, vec2( vUv.x - 1.0 * hh, vUv.y ) ) * 0.1531; |
| 46 | + sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y ) ) * 0.1633; |
| 47 | + sum += texture2D( tDiffuse, vec2( vUv.x + 1.0 * hh, vUv.y ) ) * 0.1531; |
| 48 | + sum += texture2D( tDiffuse, vec2( vUv.x + 2.0 * hh, vUv.y ) ) * 0.12245; |
| 49 | + sum += texture2D( tDiffuse, vec2( vUv.x + 3.0 * hh, vUv.y ) ) * 0.0918; |
| 50 | + sum += texture2D( tDiffuse, vec2( vUv.x + 4.0 * hh, vUv.y ) ) * 0.051; |
| 51 | +
|
| 52 | + gl_FragColor = sum; |
| 53 | +} |
| 54 | +``` |
| 55 | +{{< /details >}} |
| 56 | + |
| 57 | +Considerando que `r` representa la altura del horizonte imaginario en la textura de entrada (por lo |
| 58 | +que su valor está normalizado) y `h` el nivel de desenfoque, es más sencillo intuir el |
| 59 | +funcionamiento del shader a partir de esta linea: |
| 60 | +``` |
| 61 | + float hh = h * abs( r - vUv.y ); |
| 62 | +``` |
| 63 | + |
| 64 | +Es decir que `hh` es la distancia horizontal entre los pixeles cuya |
| 65 | +diferencia se atenuará posteriormente. esta cantidad es proporcional |
| 66 | +a la distancia entre la vertical de la coordenada actual de textura y el |
| 67 | +horizonte. |
| 68 | + |
| 69 | +Como es usual, este suavizado también depende de una convolución entre los |
| 70 | +valores de los pixeles y determinado kernel. En este caso, como los pixeles están siendo |
| 71 | +seleccionados unicamente en sentido horizontal, la máscara debe ser también un vector y el resultado |
| 72 | +de la operación (el nuevo color del fragmento) no es más que el producto punto entre estos elementos |
| 73 | + |
| 74 | +{{< katex display >}} |
| 75 | +sum = (u - 4hh, u - 3hh, u - 2hh, u - hh, u, u + hh, u +2hh , u + 3hh, u + 4hh) \cdot \left[ \begin{matrix} |
| 76 | + 0.051 \\ |
| 77 | + 0.0918 \\ |
| 78 | + 0.12245 \\ |
| 79 | + 0.1531 \\ |
| 80 | + 0.1633 \\ |
| 81 | + 0.1531 \\ |
| 82 | + 0.12245 \\ |
| 83 | + 0.0918 \\ |
| 84 | + 0.051 |
| 85 | +\end{matrix} \right] |
| 86 | +{{< /katex >}} |
| 87 | + |
| 88 | +Dónde {{< katex >}}u{{< /katex >}} es el componente horizontal de la coordenada |
| 89 | +de textura actual. |
| 90 | + |
| 91 | +``` |
| 92 | + sum += texture2D( tDiffuse, vec2( vUv.x - 4.0 * hh, vUv.y ) ) * 0.051; |
| 93 | + sum += texture2D( tDiffuse, vec2( vUv.x - 3.0 * hh, vUv.y ) ) * 0.0918; |
| 94 | + sum += texture2D( tDiffuse, vec2( vUv.x - 2.0 * hh, vUv.y ) ) * 0.12245; |
| 95 | + sum += texture2D( tDiffuse, vec2( vUv.x - 1.0 * hh, vUv.y ) ) * 0.1531; |
| 96 | + sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y ) ) * 0.1633; |
| 97 | + sum += texture2D( tDiffuse, vec2( vUv.x + 1.0 * hh, vUv.y ) ) * 0.1531; |
| 98 | + sum += texture2D( tDiffuse, vec2( vUv.x + 2.0 * hh, vUv.y ) ) * 0.12245; |
| 99 | + sum += texture2D( tDiffuse, vec2( vUv.x + 3.0 * hh, vUv.y ) ) * 0.0918; |
| 100 | + sum += texture2D( tDiffuse, vec2( vUv.x + 4.0 * hh, vUv.y ) ) * 0.051; |
| 101 | +
|
| 102 | + gl_FragColor = sum; |
| 103 | +``` |
| 104 | + |
| 105 | +## Referencias: |
| 106 | +* [OpenGL - Programming Guide, Chapter 9: Texture Mapping](http://www.glprogramming.com/red/chapter09.html#name3) |
| 107 | +* [Parte III: La Referencia de Funciones del GIMP, Capítulo 14, Filtros](https://docs.gimp.org/2.4/es/filters.html) |
0 commit comments