Files
Terrarum/assets/4096_bayer.frag
2017-12-10 14:33:12 +09:00

86 lines
5.2 KiB
GLSL

#version 120
#ifdef GL_ES
precision mediump float;
#endif
varying vec4 v_color;
varying vec2 v_texCoords;
uniform sampler2D u_texture;
// "steps" of R, G and B. Must be integer && equal or greater than 2
uniform float rcount = 64.0;
uniform float gcount = 64.0;
uniform float bcount = 64.0;
uniform float acount = 1.0;
/*int bayer[7 * 7] = int[](
32,42,10,27,37,5,15,
1,18,28,45,13,23,40,
26,36,4,14,31,48,9,
44,12,22,39,0,17,34,
20,30,47,8,25,35,3,
38,6,16,33,43,11,21,
7,24,41,2,19,29,46
); // I kind of accidentally create it...
float bayerSize = 7.0;*/
/*int bayer[9 * 9] = int[](
50,71,2,23,44,56,77,17,29,
72,12,33,45,66,6,18,39,60,
22,43,55,76,16,28,49,70,1,
53,65,5,26,38,59,80,11,32,
75,15,27,48,69,0,21,42,54,
25,37,58,79,10,31,52,64,4,
47,68,8,20,41,62,74,14,35,
78,9,30,51,63,3,24,36,57,
19,40,61,73,13,34,46,67,7
); // I kind of accidentally create it...
float bayerSize = 9.0;*/
//int bayer[25 * 25] = int[](165,530,106,302,540,219,477,100,231,417,314,223,424,37,207,434,326,22,448,338,111,454,523,278,579,334,19,410,495,57,352,158,318,598,109,509,157,524,282,606,83,225,539,163,234,607,313,206,71,470,251,608,216,135,275,609,415,29,451,204,397,21,373,107,462,348,482,120,362,508,33,147,572,388,142,447,77,345,565,439,104,215,546,279,69,567,311,585,258,177,17,266,601,55,428,270,461,331,26,560,164,271,486,186,16,336,457,150,342,471,245,161,56,396,496,555,385,146,321,190,526,97,182,511,297,429,553,49,374,536,263,575,43,501,124,368,538,450,121,309,84,210,449,561,79,356,610,256,378,58,105,315,156,244,423,118,183,408,220,611,15,198,293,596,221,375,581,39,238,500,287,14,437,139,595,227,403,590,478,68,612,295,517,87,312,413,515,78,433,13,476,134,340,414,160,466,213,547,324,456,542,141,12,335,214,357,11,381,242,469,159,265,383,176,545,285,197,503,108,576,51,387,98,200,34,358,489,277,570,96,441,554,123,534,52,556,112,605,330,70,392,613,28,288,361,232,602,300,502,267,102,195,399,152,484,264,166,289,427,192,298,407,25,249,520,114,233,444,543,170,498,131,452,66,562,310,586,54,531,346,42,614,354,23,588,491,151,468,353,187,483,369,153,85,425,10,276,371,174,420,32,459,222,304,136,421,103,458,230,339,67,260,578,93,544,9,280,594,327,248,582,472,50,615,254,537,359,91,600,475,212,525,168,558,128,455,370,179,301,405,209,467,48,442,127,355,184,332,481,126,286,175,436,273,31,377,306,36,412,294,616,8,473,60,603,116,347,532,191,568,61,522,90,218,391,592,62,514,122,552,149,617,241,513,81,202,272,557,333,226,507,255,72,305,402,229,418,296,551,7,411,317,236,416,337,480,64,389,132,350,487,404,89,162,435,44,419,618,113,505,20,604,138,465,188,493,133,580,6,169,259,320,548,193,593,40,178,512,364,591,144,319,196,386,261,351,205,384,76,269,38,349,208,504,440,99,490,5,426,243,322,574,281,4,237,460,527,3,549,155,577,47,533,316,619,394,519,82,268,325,566,199,299,119,529,75,400,125,492,344,86,217,308,463,80,395,284,474,117,201,95,235,422,620,143,45,372,597,453,343,185,479,247,569,171,409,584,129,365,239,488,94,224,438,559,283,541,18,194,401,516,262,148,41,250,621,24,329,92,446,27,291,485,35,622,180,535,379,30,341,443,145,363,494,246,101,445,550,390,499,115,432,521,211,623,253,528,189,430,307,53,323,130,624,172,46,589,292,63,599,328,203,74,290,181,376,274,140,393,59,367,88,380,137,506,252,571,431,240,497,382,228,464,167,398,2,573,366,518,1,583,73,563,303,510,154,564,257,587,65,406,173,0,360,110);
int bayer[14 * 14] = int[](131,187,8,78,50,18,134,89,155,102,29,95,184,73,22,86,113,171,142,105,34,166,9,60,151,128,40,110,168,137,45,28,64,188,82,54,124,189,80,13,156,56,7,61,186,121,154,6,108,177,24,100,38,176,93,123,83,148,96,17,88,133,44,145,69,161,139,72,30,181,115,27,163,47,178,65,164,14,120,48,5,127,153,52,190,58,126,81,116,21,106,77,173,92,191,63,99,12,76,144,4,185,37,149,192,39,135,23,117,31,170,132,35,172,103,66,129,79,3,97,57,159,70,141,53,94,114,20,49,158,19,146,169,122,183,11,104,180,2,165,152,87,182,118,91,42,67,25,84,147,43,85,125,68,16,136,71,10,193,112,160,138,51,111,162,26,194,46,174,107,41,143,33,74,1,101,195,15,75,140,109,90,32,62,157,98,167,119,179,59,36,130,175,55,0,150);
float bayerSize = 14.0;
float bayerDivider = bayerSize * bayerSize;
vec4 gammaIn(vec4 col) {
return pow(col, vec4(2.2));
}
vec4 gammaOut(vec4 col) {
return pow(col, vec4(1.0 / 2.2));
}
vec4 nearestColour(vec4 incolor) {
vec4 rgbaCounts = vec4(rcount, gcount, bcount, acount);
vec4 color = incolor;
color.r = floor((rgbaCounts.r - 1.0) * color.r + 0.5) / (rgbaCounts.r - 1.0);
color.g = floor((rgbaCounts.g - 1.0) * color.g + 0.5) / (rgbaCounts.g - 1.0);
color.b = floor((rgbaCounts.b - 1.0) * color.b + 0.5) / (rgbaCounts.b - 1.0);
color.a = 1.0;//floor((rgbaCounts.a - 1.0) * color.a + 0.5) / (rgbaCounts.a - 1.0);
return color;
}
void main(void) {
float spread = 1.0 / (0.299 * (rcount - 1.0) + 0.587 * (gcount - 1.0) + 0.114 * (bcount - 1.0)); // this spread value is optimised one -- try your own values for various effects!
// create texture coordinates based on pixelSize //
vec4 inColor = (texture2D(u_texture, v_texCoords));
vec2 entry = mod(gl_FragCoord.xy, vec2(bayerSize, bayerSize));
gl_FragColor = nearestColour(inColor + spread * (bayer[int(entry.y) * int(bayerSize) + int(entry.x)] / bayerDivider - 0.5));
//gl_FragColor = nearestColour(inColor);
//gl_FragColor = inColor;
}