Advertisement
czaffik

opengl framebuffer shaders

Mar 24th, 2019
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // framebuffer.glvs
  2. #version 330
  3.  
  4. in vec4 position;
  5. in vec4 texcoord;
  6.  
  7. out vec2 vTexcoord;
  8.  
  9. void main()
  10. {
  11.     vTexcoord = texcoord.xy;
  12.     gl_Position = position;
  13. }
  14.  
  15. // voronoi.glfs
  16. #version 330
  17.  
  18. in vec2 vTexcoord;
  19.  
  20. out vec4 outColor;
  21.  
  22. uniform sampler2D tex0;
  23. uniform float width;
  24. uniform float height;
  25. uniform float elapsedTime;
  26.  
  27. void main()
  28. {
  29.     vec2 st = gl_FragCoord.xy/vec2(width, height);
  30.     st.x *= width/height;
  31.  
  32.     vec3 color = vec3(0.0);
  33.  
  34.     // Cell positions
  35.     vec2 point[1];
  36.     point[0] = vec2(width*0.5/height, 0.5);
  37.  
  38.     float m_dist = 1.0;  // minimun distance
  39.     vec2 m_point;        // minimum position
  40.  
  41.     // Iterate through the points positions
  42.     for (int i = 0; i < 1; i++)
  43.     {
  44.         float dist = distance(st, point[i]);
  45.         if (dist < m_dist)
  46.         {
  47.             // Keep the closer distance
  48.             m_dist = dist;
  49.  
  50.             // Kepp the position of the closer point
  51.             m_point = point[i];
  52.         }
  53.     }
  54.  
  55.     // Add distance field to closest point center
  56.     color += m_dist*2.;
  57.  
  58.     // tint acording the closest point position
  59.     //color = vec3(m_point, m_point, m_point);
  60.  
  61.     // Show isolines
  62.     color -= abs(sin(80.0*m_dist))*0.07;
  63.  
  64.     // Draw point center
  65.     //color += 1.-step(.02, m_dist);
  66.  
  67.     gl_FragColor = (vec4(1.0, 1.0, 1.0, 1.0) - vec4(color, 1.0))*texture(tex0, vTexcoord);
  68. }
  69.  
  70. // frost.glfs
  71. #version 330
  72.  
  73. in vec2 vTexcoord;
  74. out vec4 outColor;
  75.  
  76. uniform sampler2D tex0;
  77. uniform sampler2D tex1;
  78.  
  79. uniform float PixelX = 2.0;
  80. uniform float PixelY = 2.0;
  81. uniform float Freq = 0.115;
  82. uniform float width;
  83. uniform float height;
  84.  
  85. vec4 spline(float x, vec4 c1, vec4 c2, vec4 c3, vec4 c4, vec4 c5, vec4 c6, vec4 c7, vec4 c8, vec4 c9)
  86. {
  87.     float w1 = 0.0, w2 = 0.0, w3 = 0.0, w4 = 0.0, w5 = 0.0, w6 = 0.0, w7 = 0.0, w8 = 0.0, w9 = 0.0;
  88.     float tmp = x * 8.0;
  89.     if (tmp <= 1.0)
  90.     {
  91.         w1 = 1.0 - tmp;
  92.         w2 = tmp;
  93.     }
  94.     else if (tmp <= 2.0)
  95.     {
  96.         tmp = tmp - 1.0;
  97.         w2 = 1.0 - tmp;
  98.         w3 = tmp;
  99.     }
  100.     else if (tmp <= 3.0)
  101.     {
  102.         tmp = tmp - 2.0;
  103.         w3 = 1.0-tmp;
  104.         w4 = tmp;
  105.     }
  106.     else if (tmp <= 4.0)
  107.     {
  108.         tmp = tmp - 3.0;
  109.         w4 = 1.0-tmp;
  110.         w5 = tmp;
  111.     }
  112.     else if (tmp <= 5.0)
  113.     {
  114.         tmp = tmp - 4.0;
  115.         w5 = 1.0-tmp;
  116.         w6 = tmp;
  117.     }
  118.     else if (tmp <= 6.0)
  119.     {
  120.         tmp = tmp - 5.0;
  121.         w6 = 1.0-tmp;
  122.         w7 = tmp;
  123.     }
  124.     else if (tmp <= 7.0)
  125.     {
  126.         tmp = tmp - 6.0;
  127.         w7 = 1.0 - tmp;
  128.         w8 = tmp;
  129.     }
  130.     else
  131.     {
  132.         tmp = clamp(tmp - 7.0, 0.0, 1.0);
  133.         w8 = 1.0-tmp;
  134.         w9 = tmp;
  135.     }
  136.  
  137.     return w1*c1 + w2*c2 + w3*c3 + w4*c4 + w5*c5 + w6*c6 + w7*c7 + w8*c8 + w9*c9;
  138. }
  139.  
  140. vec3 noise(vec2 p)
  141. {
  142.     return texture2D(tex1, p).xyz;
  143. }
  144.  
  145. void main()
  146. {
  147.     vec3 tc = vec3(1.0, 0.0, 0.0);
  148.  
  149.     float DeltaX = PixelX / width;
  150.     float DeltaY = PixelY / height;
  151.  
  152.     vec2 ox = vec2(DeltaX, 0.0);
  153.     vec2 oy = vec2(0.0, DeltaY);
  154.  
  155.     vec2 PP = vTexcoord - oy;
  156.     vec4 C00 = texture2D(tex0, PP - ox);
  157.     vec4 C01 = texture2D(tex0, PP);
  158.     vec4 C02 = texture2D(tex0, PP + ox);
  159.  
  160.     PP = vTexcoord;
  161.     vec4 C10 = texture2D(tex0, PP - ox);
  162.     vec4 C11 = texture2D(tex0, PP);
  163.     vec4 C12 = texture2D(tex0, PP + ox);
  164.  
  165.     PP = vTexcoord + oy;
  166.     vec4 C20 = texture2D(tex0, PP - ox);
  167.     vec4 C21 = texture2D(tex0, PP);
  168.     vec4 C22 = texture2D(tex0, PP + ox);
  169.  
  170.     float n = noise(Freq*vTexcoord).x;
  171.     n = mod(n, 0.111111)/0.111111;
  172.     vec4 result = spline(n, C00, C01, C02, C10, C11, C12, C20, C21, C22);
  173.     tc = result.rgb;
  174.  
  175.     outColor = vec4(tc, 1.0);
  176. }
  177.  
  178. // hdr - glfs
  179. #version 330
  180.  
  181. in vec2 vTexcoord;
  182.  
  183. out vec4 outColor;
  184.  
  185. uniform sampler2D tex0;
  186. uniform float gamma;
  187. uniform float width;
  188. uniform float height;
  189.  
  190. void main()
  191. {
  192.     vec2 texOffset = vec2(1.0/600.0, 1.0/600.0);
  193.     float kernel[9] = float[]
  194.     (
  195.         0.00481007202,
  196.         0.0286864862,
  197.         0.102712765,
  198.         0.220796734,
  199.         0.284958780,
  200.         0.220796734,
  201.         0.102712765,
  202.         0.0286864862,
  203.         0.00481007202
  204.     );
  205.  
  206.     float exposure = 1.0;
  207.     vec3 gammaCorrection = vec3(1.0/gamma, 1.0/gamma, 1.0/gamma);
  208.  
  209.     vec3 hdrColor = texture(tex0, vTexcoord).rgb;
  210.     vec3 mapped = vec3(1.0) - exp(-hdrColor*exposure);
  211.     //mapped = pow(mapped, gammaCorrection);
  212.  
  213.     vec4 sum = vec4(0.0);
  214.     for (int i = -4; i <= 4; i++)
  215.         sum += texture(tex0, vec2(vTexcoord.x + i*texOffset.x, vTexcoord.y + i*texOffset.y))*kernel[i+4];
  216.  
  217.     outColor = pow(vec4(mapped, 1.0)*sum, vec4(gammaCorrection, 1.0));
  218. }
  219.  
  220. // nightvision.glfs
  221. #version 330
  222.  
  223. in vec2 vTexcoord;
  224.  
  225. out vec4 outColor;
  226.  
  227. uniform sampler2D tex0;
  228. uniform sampler2D tex1;
  229.  
  230. uniform float elapsedTime;
  231. uniform float luminanceThreshold; // 0.2
  232. uniform float colorAmplification; // 4.0
  233. uniform float width;
  234. uniform float height;
  235.  
  236. void main()
  237. {
  238.     vec3 finalColor;
  239.  
  240.     vec2 uv;
  241.     uv.x = 0.4*sin(elapsedTime*40.0);
  242.     uv.y = 0.4*cos(elapsedTime*40.0);
  243.  
  244.     vec3 n  = texture(tex1, (vTexcoord*3.5) + uv).rgb;
  245.     vec3 c  = texture(tex0, vTexcoord + (n.xy*0.005)).rgb;
  246.  
  247.     float lum = dot(vec3(0.30, 0.59, 0.11), c);
  248.     if (lum < luminanceThreshold) c *= colorAmplification;
  249.  
  250.     vec3 visionColor = vec3(0.1, 0.95, 0.2);
  251.     finalColor = (c + (n*0.2)) * visionColor * 0.3;
  252.  
  253.     float w = width/2.0;
  254.     float h = height/2.0;
  255.     float w2 = w*w;
  256.     float h2 = h*h;
  257.     float x2 = (gl_FragCoord.x - w)*(gl_FragCoord.x - w);
  258.     float y2 = (gl_FragCoord.y - h)*(gl_FragCoord.y - h);
  259.  
  260.     if (x2/w2 + y2/h2 <= 1.0)
  261.         outColor = vec4(finalColor, 1.0);
  262.     else
  263.         outColor = vec4(0.0, 0.0, 0.0, 1.0);
  264. }
  265.  
  266. // oldtv.glfs
  267. #version 330
  268.  
  269. in vec2 vTexcoord;
  270.  
  271. out vec4 outColor;
  272.  
  273. uniform sampler2D tex0;
  274. uniform sampler2D tex1;
  275. uniform float gamma;
  276. uniform float width;
  277. uniform float height;
  278. uniform float level;
  279. uniform float angle;
  280.  
  281. vec2 translatedTexcoord(in vec2 _texcoord, in float _angle)
  282. {
  283.     return _texcoord + vec2(0.0, -angle);
  284. }
  285.  
  286. vec2 rotatedTexcoord(in vec2 _texcoord, in float _angle, in float _width, in float _height)
  287. {
  288.     float sinf = sin(_angle);
  289.     float cosf = cos(_angle);
  290.     return mat2(cosf, sinf, -sinf, cosf)*vec2((_texcoord.x - 0.5)*(_width/_height), _texcoord.y - 0.5);
  291. }
  292.  
  293. vec2 modifiedTexcoord(in vec2 _texcoord, in float _angle)
  294. {
  295.     vec2 uv;
  296.     uv.x = 0.4*sin(_angle*40.0);
  297.     uv.y = 0.4*cos(_angle*40.0);
  298.  
  299.     return _texcoord*0.5 + uv;
  300. }
  301.  
  302. void main()
  303. {
  304.     vec2 texOffset = vec2(1.0/200.0, 1.0/200.0);
  305.     vec2 texOffset1 = vec2(1.0/100.0, 1.0/100.0);
  306.     float kernel[9] = float[]
  307.     (
  308.         0.00481007202,
  309.         0.0286864862,
  310.         0.102712765,
  311.         0.220796734,
  312.         0.284958780,
  313.         0.220796734,
  314.         0.102712765,
  315.         0.0286864862,
  316.         0.00481007202
  317.     );
  318.  
  319.     float exposure = 1.0;
  320.     vec3 gammaCorrection = vec3(1.0/gamma, 1.0/gamma, 1.0/gamma);
  321.  
  322.     vec3 hdrColor = texture(tex0, vTexcoord).rgb;
  323.     vec3 mapped = vec3(1.0) - exp(-hdrColor*exposure);
  324.     mapped = pow(mapped, gammaCorrection);
  325.  
  326.     vec4 sum = vec4(0.0);
  327.     for (int i = -4; i <= 4; i++)
  328.         sum += texture(tex0, vec2(vTexcoord.x + i*texOffset.x, vTexcoord.y + i*texOffset.y))*kernel[i+4];
  329.  
  330.     vec4 sum1 = vec4(0.0);
  331.     for (int i = -4; i <= 4; i++)
  332.         sum1 += texture(tex0, vec2(vTexcoord.x + i*texOffset1.x, vTexcoord.y + i*texOffset1.y))*kernel[i+4];
  333.  
  334.     vec4 c = pow(vec4(mapped, 1.0)*sum, vec4(gammaCorrection, 1.0));
  335.     float a = (c.r + c.g + c.b + c.a)/4.0;
  336.  
  337.     vec4 d = pow(vec4(mapped, 1.0)*sum1, vec4(gammaCorrection, 1.0));
  338.     float b = (d.r + d.g + d.b + d.a)/4.0;
  339.  
  340.     vec2 texcoord = modifiedTexcoord(vTexcoord, angle);
  341.     vec4 cr = mix(vec4(a, a, a, 1.0), texture(tex1, texcoord), 0.1);
  342.     vec4 cb = mix(vec4(b, b, b, 1.0), texture(tex1, texcoord), 0.1)*1.03;
  343.  
  344.     if (gl_FragCoord.y > level - 40.0 && gl_FragCoord.y < level + 40.0)
  345.         outColor = cb;
  346.     else
  347.         outColor = cr;
  348. }
  349.  
  350. // sobel.glfs
  351. #version 330
  352.  
  353. in vec2 vTexcoord;
  354.  
  355. out vec4 outColor;
  356.  
  357. uniform sampler2D tex0;
  358.  
  359. void main()
  360. {
  361.     vec4 top         = texture(tex0, vec2(vTexcoord.x, vTexcoord.y + 1.0/100.0));
  362.     vec4 bottom      = texture(tex0, vec2(vTexcoord.x, vTexcoord.y - 1.0/100.0));
  363.     vec4 left        = texture(tex0, vec2(vTexcoord.x - 1.0/100.0, vTexcoord.y));
  364.     vec4 right       = texture(tex0, vec2(vTexcoord.x + 1.0/100.0, vTexcoord.y));
  365.     vec4 topLeft     = texture(tex0, vec2(vTexcoord.x - 1.0/100.0, vTexcoord.y + 1.0/100.0));
  366.     vec4 topRight    = texture(tex0, vec2(vTexcoord.x + 1.0/100.0, vTexcoord.y + 1.0/100.0));
  367.     vec4 bottomLeft  = texture(tex0, vec2(vTexcoord.x - 1.0/100.0, vTexcoord.y - 1.0/100.0));
  368.     vec4 bottomRight = texture(tex0, vec2(vTexcoord.x + 1.0/100.0, vTexcoord.y - 1.0/100.0));
  369.  
  370.     vec4 sx = -topLeft - 2*left - bottomLeft + topRight + 2*right + bottomRight;
  371.     vec4 sy = -topLeft - 2*top - topRight + bottomLeft + 2*bottom + bottomRight;
  372.  
  373.     vec4 sobel = sqrt(sx*sx + sy*sy);
  374.     vec4 diffuse = texture(tex0, vTexcoord);
  375.  
  376.     outColor = sobel;
  377. }
  378.  
  379. // sobel_v2.glfs
  380. #version 330
  381.  
  382. in vec2 vTexcoord;
  383.  
  384. out vec4 outColor;
  385.  
  386. uniform sampler2D tex0;
  387.  
  388. void main()
  389. {
  390.     vec3 diffuse = texture(tex0, vTexcoord).rgb;
  391.  
  392.     mat3 I;
  393.     for (int i = -1; i < 2; i++)
  394.     {
  395.         for (int j = -1; j < 2; j++)
  396.         {
  397.             vec3 s  = texture(tex0, vec2(vTexcoord.x + i/100.0, vTexcoord.y + j/100.0)).rgb;
  398.             I[i+1][j+1] = length(s);
  399.         }
  400.     }
  401.  
  402.     mat3 sx = mat3(
  403.                    1.0,  2.0,  1.0,
  404.                    0.0,  0.0,  0.0,
  405.                   -1.0, -2.0, -1.0
  406.                    );
  407.     mat3 sy = mat3(
  408.                    1.0, 0.0, -1.0,
  409.                    2.0, 0.0, -2.0,
  410.                    1.0, 0.0, -1.0
  411.                    );
  412.  
  413.     float gx = dot(sx[0], I[0]) + dot(sx[1], I[1]) + dot(sx[2], I[2]);
  414.     float gy = dot(sy[0], I[0]) + dot(sy[1], I[1]) + dot(sy[2], I[2]);
  415.     float g = sqrt(pow(gx, 2.0) + pow(gy, 2.0));
  416.  
  417.     outColor = vec4(diffuse - vec3(g), 1.0);
  418. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement