diff --git a/assets/4096_bayer.frag b/assets/4096_bayer.frag index 1669791d2..de37e2d34 100644 --- a/assets/4096_bayer.frag +++ b/assets/4096_bayer.frag @@ -22,7 +22,7 @@ uniform float acount = 1.0; {63,31,55,23,61,29,53,21} }; // fun fact: you can calculate bayer value on-the-fly but LUT is faster float bayerSize = 8.0;*/ -int bayer[12][12] = { +/*int bayer[12][12] = { {0 ,96 ,64 ,8 ,104,72 ,2 ,98 ,66 ,10 ,106,74 }, // 12x12 bayer ordered dithering {112,80 ,16 ,120,88 ,24 ,114,82 ,18 ,122,90 ,26 }, // pattern. Each input pixel {48 ,32 ,128,56 ,40 ,136,50 ,34 ,130,58 ,42 ,138}, // is scaled to the 0..143 range @@ -35,11 +35,87 @@ int bayer[12][12] = { {15 ,111,79 ,7 ,103,71 ,13 ,109,77 ,5 ,101,69 }, {127,95 ,31 ,119,87 ,23 ,125,93 ,29 ,117,85 ,21 }, {63 ,47 ,143,55 ,39 ,135,61 ,45 ,141,53 ,37 ,133}}; -float bayerSize = 12.0; +float bayerSize = 12.0;*/ + +/*int bayer[7][7] = { +{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] = { +{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} +}; +float bayerSize = 9.0; + +/*int bayer[14][14] = { +{128,168,40,108,148,20,60,130,170,42,110,150,22,62}, +{4,72,112,180,52,92,160,6,74,114,182,54,94,162}, +{104,144,16,56,124,192,36,106,146,18,58,126,194,38}, +{176,48,88,156,0,68,136,178,50,90,158,2,70,138}, +{80,120,188,32,100,140,12,82,122,190,34,102,142,14}, +{152,24,64,132,172,44,84,154,26,66,134,174,46,86}, +{28,96,164,8,76,116,184,30,98,166,10,78,118,186}, +{129,169,41,109,149,21,61,131,171,43,111,151,23,63}, +{5,73,113,181,53,93,161,7,75,115,183,55,95,163}, +{105,145,17,57,125,193,37,107,147,19,59,127,195,39}, +{177,49,89,157,1,69,137,179,51,91,159,3,71,139}, +{81,121,189,33,101,141,13,83,123,191,35,103,143,15}, +{153,25,65,133,173,45,85,155,27,67,135,175,47,87}, +{29,97,165,9,77,117,185,31,99,167,11,79,119,187} +}; +float bayerSize = 14.0;*/ + +/*int bayer[21][21] = { +{288,378,90,243,333,45,135,295,385,97,250,340,52,142,291,381,93,246,336,48,138}, +{9,162,252,405,117,207,360,16,169,259,412,124,214,367,12,165,255,408,120,210,363}, +{234,324,36,126,279,432,81,241,331,43,133,286,439,88,237,327,39,129,282,435,84}, +{396,108,198,351,0,153,306,403,115,205,358,7,160,313,399,111,201,354,3,156,309}, +{180,270,423,72,225,315,27,187,277,430,79,232,322,34,183,273,426,75,228,318,30}, +{342,54,144,297,387,99,189,349,61,151,304,394,106,196,345,57,147,300,390,102,192}, +{63,216,369,18,171,261,414,70,223,376,25,178,268,421,66,219,372,21,174,264,417}, +{294,384,96,249,339,51,141,293,383,95,248,338,50,140,290,380,92,245,335,47,137}, +{15,168,258,411,123,213,366,14,167,257,410,122,212,365,11,164,254,407,119,209,362}, +{240,330,42,132,285,438,87,239,329,41,131,284,437,86,236,326,38,128,281,434,83}, +{402,114,204,357,6,159,312,401,113,203,356,5,158,311,398,110,200,353,2,155,308}, +{186,276,429,78,231,321,33,185,275,428,77,230,320,32,182,272,425,74,227,317,29}, +{348,60,150,303,393,105,195,347,59,149,302,392,104,194,344,56,146,299,389,101,191}, +{69,222,375,24,177,267,420,68,221,374,23,176,266,419,65,218,371,20,173,263,416}, +{292,382,94,247,337,49,139,289,379,91,244,334,46,136,296,386,98,251,341,53,143}, +{13,166,256,409,121,211,364,10,163,253,406,118,208,361,17,170,260,413,125,215,368}, +{238,328,40,130,283,436,85,235,325,37,127,280,433,82,242,332,44,134,287,440,89}, +{400,112,202,355,4,157,310,397,109,199,352,1,154,307,404,116,206,359,8,161,314}, +{184,274,427,76,229,319,31,181,271,424,73,226,316,28,188,278,431,80,233,323,35}, +{346,58,148,301,391,103,193,343,55,145,298,388,100,190,350,62,152,305,395,107,197}, +{67,220,373,22,175,265,418,64,217,370,19,172,262,415,71,224,377,26,179,269,422} +}; +float bayerSize = 21.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); @@ -57,7 +133,7 @@ vec4 nearestColour(vec4 incolor) { color.a = 1.0; } - return color; + return (color); } void main(void) { @@ -65,10 +141,10 @@ void main(void) { // create texture coordinates based on pixelSize // - vec4 inColor = texture2D(u_texture, v_texCoords); + 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(entry.x)] / bayerDivider - 0.5)); + gl_FragColor = (nearestColour(inColor + spread * (bayer[int(entry.y)][int(entry.x)] / bayerDivider - 0.5))); //gl_FragColor = nearestColour(inColor); } \ No newline at end of file diff --git a/assets/4096_bayer_skyboxfill.frag b/assets/4096_bayer_skyboxfill.frag index 8e9562dc2..bdd8dac67 100644 --- a/assets/4096_bayer_skyboxfill.frag +++ b/assets/4096_bayer_skyboxfill.frag @@ -14,23 +14,23 @@ uniform float bcount = 64.0; -int bayer[12][12] = { -{0 ,96 ,64 ,8 ,104,72 ,2 ,98 ,66 ,10 ,106,74 }, // 12x12 bayer ordered dithering -{112,80 ,16 ,120,88 ,24 ,114,82 ,18 ,122,90 ,26 }, // pattern. Each input pixel -{48 ,32 ,128,56 ,40 ,136,50 ,34 ,130,58 ,42 ,138}, // is scaled to the 0..143 range -{12 ,108,76 ,4 ,100,68 ,14 ,110,78 ,6 ,102,70 }, // before looking in this table -{124,92 ,28 ,116,84 ,20 ,126,94 ,30 ,118,86 ,22 }, // to determine the action -{60 ,44 ,140,52 ,36 ,132,62 ,46 ,142,54 ,38 ,134}, -{3 ,99 ,67 ,11 ,107,75 ,1 ,97 ,65 ,9 ,105,73 }, -{115,83 ,19 ,123,91 ,27 ,113,81 ,17 ,121,89 ,25 }, -{51 ,35 ,131,59 ,43 ,139,49 ,33 ,129,57 ,41 ,137}, -{15 ,111,79 ,7 ,103,71 ,13 ,109,77 ,5 ,101,69 }, -{127,95 ,31 ,119,87 ,23 ,125,93 ,29 ,117,85 ,21 }, -{63 ,47 ,143,55 ,39 ,135,61 ,45 ,141,53 ,37 ,133}}; // fun fact: you can calculate bayer value on-the-fly but LUT is faster -float bayerSize = 12.0; -float bayerDivider = bayerSize * bayerSize; +int bayer[9][9] = { +{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} +}; +float bayerSize = 9.0; + +float bayerDivider = bayerSize * bayerSize; + vec4 nearestColour(vec4 incolor) { vec4 rgbaCounts = vec4(rcount, gcount, bcount, 1.0); diff --git a/src/net/torvald/terrarum/ColorLimiterTest.kt b/src/net/torvald/terrarum/ColorLimiterTest.kt index 6a891ff46..6e450675f 100644 --- a/src/net/torvald/terrarum/ColorLimiterTest.kt +++ b/src/net/torvald/terrarum/ColorLimiterTest.kt @@ -9,6 +9,7 @@ import com.badlogic.gdx.graphics.GL20 import com.badlogic.gdx.graphics.Texture import com.badlogic.gdx.graphics.g2d.SpriteBatch import com.badlogic.gdx.graphics.glutils.ShaderProgram +import com.badlogic.gdx.graphics.glutils.ShapeRenderer import com.badlogic.gdx.math.Matrix4 import net.torvald.terrarum.gameactors.sqrt import net.torvald.terrarumsansbitmap.gdx.GameFontBase @@ -34,6 +35,7 @@ object ColorLimiterTest : ApplicationAdapter() { lateinit var shader4096: ShaderProgram lateinit var batch: SpriteBatch + lateinit var shapeRenderer: ShapeRenderer lateinit var font: GameFontBase @@ -42,14 +44,16 @@ object ColorLimiterTest : ApplicationAdapter() { shader4096 = ShaderProgram(Gdx.files.internal("assets/4096.vert"), Gdx.files.internal("assets/4096_bayer.frag")) shader4096.begin() - shader4096.setUniformf("rcount", 2f) - shader4096.setUniformf("gcount", 2f) - shader4096.setUniformf("bcount", 2f) + shader4096.setUniformf("rcount", 4f) + shader4096.setUniformf("gcount", 4f) + shader4096.setUniformf("bcount", 4f) shader4096.end() + //img = Texture("assets/test_gradient.tga") img = Texture("assets/test_texture.tga") batch = SpriteBatch() + shapeRenderer = ShapeRenderer() font = GameFontBase("assets/graphics/fonts/terrarum-sans-bitmap", flipY = false) @@ -84,19 +88,24 @@ object ColorLimiterTest : ApplicationAdapter() { batch.inUse { batch.shader = shader4096 - shader4096.setUniformf("rcount", dither) - shader4096.setUniformf("gcount", dither) - shader4096.setUniformf("bcount", dither) + shader4096.setUniformf("rcount", 6f)//dither) + shader4096.setUniformf("gcount", 6f)//dither) + shader4096.setUniformf("bcount", 6f)//dither) batch.color = Color.WHITE batch.draw(img, 0f, 0f) + } + /*shapeRenderer.inUse { + shapeRenderer.rect(512f, 0f, 512f, 512f, Color.BLACK, Color.BLACK, Color.WHITE, Color.WHITE) + }*/ + batch.inUse { batch.shader = null batch.color = Color.WHITE batch.draw(img, img.width.toFloat(), 0f) - + batch.shader = null font.draw(batch, "Dither level: ${dither.toInt()}", 10f, Gdx.graphics.height - 30f) } } diff --git a/work_files/odd_numbered_bayer_matrix_gen.py b/work_files/odd_numbered_bayer_matrix_gen.py new file mode 100644 index 000000000..ff584de8f --- /dev/null +++ b/work_files/odd_numbered_bayer_matrix_gen.py @@ -0,0 +1,64 @@ +def printMatrix(mat): + for n in range(matrixSize): + outstr = "" + for k in range(matrixSize): + outstr += str(mat[n][k]) + + if (k < matrixSize - 1): + outstr += "," + + + print(outstr) + + + +matrixSize = 9 +# Matrix sizes I've tested: +# slanted diagonal (gud!): 7, 8, 9 +# ortho pattern: 3, 4, 6 +# ortho diagonal: 5 + + +matrix = [x[:] for x in [[-1] * matrixSize] * matrixSize] + + +# init matrix +cellX = 0 +cellY = (matrixSize >> 1) +for num in range(matrixSize * matrixSize): + if (matrix[cellY][cellX] == -1): + matrix[cellY][cellX] = num + else: + thefuck = matrix[cellY][cellX] + error("Matrix position ("+str(cellX)+", "+str(cellY)+"is occupied by "+thefuck) + + if (matrix[(cellY - 1) % matrixSize][(cellX + 1) % matrixSize] == -1): + cellX = (cellX + 1) % matrixSize + cellY = (cellY - 1) % matrixSize + else: + cellY = (cellY + 1) % matrixSize + + +# vertical shifts +for xpos in range(0, matrixSize): + lookup = [-1] * matrixSize + + for ycursor in range(matrixSize): + lookup[ycursor] = matrix[ycursor][xpos] + + for ycursor in range(matrixSize): + matrix[(ycursor - xpos) % matrixSize][xpos] = lookup[ycursor] + + +# horizontal shifts +for ypos in range(0, matrixSize): + shift = (matrixSize - 1) - ypos + 1 + lookup = list(matrix[ypos]) + + for xcursor in range(matrixSize): + matrix[ypos][(xcursor + shift) % matrixSize] = lookup[xcursor] + + + + +printMatrix(matrix) \ No newline at end of file diff --git a/work_files/the_accidental_bayer_matrix.xlsx b/work_files/the_accidental_bayer_matrix.xlsx new file mode 100644 index 000000000..19c431d44 Binary files /dev/null and b/work_files/the_accidental_bayer_matrix.xlsx differ