Skip to content

Commit

Permalink
- add two new 16-color palette presets: Macintosh and Windows
Browse files Browse the repository at this point in the history
- lots of tweaks and expansions
- it is now possible to change the palette square "size" - increasing the dither depth
  • Loading branch information
madame-rachelle committed Feb 9, 2022
1 parent 2b9407b commit 8f79968
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 41 deletions.
67 changes: 30 additions & 37 deletions 8bit.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,21 @@ vec4 paldownmix(vec4 c)
return texture(tclut, vec2(tx, ty));
}

vec4 egadownmix(vec4 c)
vec4 fbdownmix(vec4 c, sampler2D fblut)
{
float fdiff = 3.0;
float fattr = 0;
for (int i = 0; i < 16; i++)
{
vec3 lu = abs(pow(vec3(texture(egalut, vec2((i + 0.5) / 16, 0.5))), vec3(2.2)) - pow(vec3(c), vec3(2.2)));
vec3 lu = abs(pow(vec3(texture(fblut, vec2((i + 0.5) / 16, 0.5))), vec3(2.2)) - pow(vec3(c), vec3(2.2)));
float diff = lu.r + lu.g + lu.b;
if (fdiff > diff)
{
fdiff = diff;
fattr = float(i);
}
}
return texture(egalut, vec2((fattr + 0.5) / 16.0, 0.5));
return texture(fblut, vec2((fattr + 0.5) / 16.0, 0.5));
}

vec4 hiegadownmix(vec4 c)
Expand All @@ -74,25 +74,25 @@ vec4 downmix(vec4 c)
case 0:
return paldownmix(c);
case 1:
return egadownmix(c);
case 2:
return hiegadownmix(c);
case 2:
return fbdownmix(c, egalut);
case 3:
return fbdownmix(c, winlut);
case 4:
return fbdownmix(c, maclut);
}
}

vec4 dither1(vec4 c)
{
return downmix(clamp(c * vec4(vec3(2.0), 1.0) - downmix(c), vec4(0.0, 0.0, 0.0, 0.0), vec4(1.0, 1.0, 1.0, 1.0)));
}

vec4 dither2(vec4 c)
{
return downmix(clamp(c * vec4(vec3(3.0), 1.0) - dither1(c) - downmix(c), vec4(0.0, 0.0, 0.0, 0.0), vec4(1.0, 1.0, 1.0, 1.0)));
}

vec4 dither3(vec4 c)
vec4 dither(vec4 c, int count)
{
return downmix(clamp(c * vec4(vec3(4.0), 1.0) - dither2(c) - dither1(c) - downmix(c), vec4(0.0, 0.0, 0.0, 0.0), vec4(1.0, 1.0, 1.0, 1.0)));
vec4 r = c;
for (; count>=0; count--)
{
r = r + (c - downmix(clamp(r, vec4(0.0, 0.0, 0.0, 0.0), vec4(1.0, 1.0, 1.0, 1.0)))) * c_bias;
}
r = downmix(clamp(r, vec4(0.0, 0.0, 0.0, 0.0), vec4(1.0, 1.0, 1.0, 1.0)));
return r;
}

float brightness(vec3 c)
Expand All @@ -119,33 +119,26 @@ void main()
bool checker = ((int(txc.x) + int(txc.y)) & 1) == 1;

if (checker)
FragColor = dither1(c);
FragColor = dither(c, 1);
else
FragColor = downmix(c);
break;
case 3:
int pos = (int(txc.x) & 1) + (int(txc.y) & 1) * 2;
switch (pos)
{
default:
case 0:
FragColor = downmix(c);
break;
case 1:
FragColor = dither1(c);
break;
case 3:
FragColor = dither2(c);
break;
case 2:
FragColor = dither3(c);
break;
}
if ((int(txc.y) & 1) == 1)
txc.x = 1.0 - txc.x;

int pos = (int(txc.x) % c_sqsize) + (int(txc.y) % c_sqsize) * c_sqsize;

if (pos == 0)
FragColor = downmix(clamp(c, vec4(0.0, 0.0, 0.0, 1.0), vec4(1.0, 1.0, 1.0, 1.0)));
else
FragColor = dither(clamp(c, vec4(0.0, 0.0, 0.0, 1.0), vec4(1.0, 1.0, 1.0, 1.0)), pos);

break;
case 4:
vec4 o1 = c;
vec4 o2 = downmix(c);
vec4 o3 = dither1(c);
vec4 o3 = dither(c, 1);

float bri1 = max(brightness(vec3(o1)), 0.0001);
float bri2 = max(brightness(vec3(o2)), 0.0001);
Expand All @@ -161,7 +154,7 @@ void main()
FragColor = downmix(c) * bri1 / bri2;
else
{
vec4 o4 = (downmix(c) * dd3 + dither1(c) * dd2) / (dd2 + dd3);
vec4 o4 = (downmix(c) * dd3 + o3 * dd2) / (dd2 + dd3);
float bri4 = max(brightness(vec3(o4)), 0.0001);
FragColor = o4 * bri1 / bri4;
}
Expand Down
9 changes: 7 additions & 2 deletions gldefs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,20 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

// Lookup tables for Windows and Macintosh sourced from this wikipedia page:
// https://en.wikipedia.org/w/index.php?title=List_of_software_palettes&oldid=1053490836

hardwareshader postprocess screen
{
Name "8bit"
Shader "8bit.glsl" 330
Texture tclut "TCLUT8"
Texture egalut "egapal"
cvar_uniform int c_mode pal_mode 4
Texture maclut "macpal"
Texture winlut "winpal"
cvar_uniform int c_mode pal_mode 3
cvar_uniform int c_set pal_set 0
cvar_uniform float c_mixer c_mixer 0.5
cvar_uniform int c_sqsize pal_sqsize 2
cvar_uniform float c_bias pal_bias 0.9
Enabled
}
8 changes: 6 additions & 2 deletions menudef.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,16 @@ OptionValue "PalModes"
OptionValue "PalSets"
{
0, "Game-Specific"
1, "EGA 16-Color"
2, "EGA 64-Color"
1, "EGA 64-Color"
2, "EGA 16-Color"
3, "Windows 16-color"
4, "Macintosh 16-color"
}

AddOptionMenu VideoOptions
{
Option "Palettizing Mode", pal_mode, "PalModes"
Option "Palette Set", pal_set, "PalSets"
Slider "Palette Square Size", pal_sqsize, 2, 8, 1
slider "Palette Diffusion Bias", pal_bias, 0.5, 2.0, 0.1
}
Binary file added textures/macpal.bmp
Binary file not shown.
Binary file added textures/winpal.bmp
Binary file not shown.

0 comments on commit 8f79968

Please sign in to comment.