Skip to content

Commit

Permalink
improve gb-camera.shader
Browse files Browse the repository at this point in the history
  • Loading branch information
exeldro committed Oct 24, 2023
1 parent b5f3b7c commit e0d9105
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions data/examples/gb-camera.shader
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ uniform float dither_factor<
float step = 0.01;
> = 0.8;

uniform bool alternative_bayer;

uniform float brightness<
string label = "Brightness";
string widget_type = "slider";
Expand Down Expand Up @@ -57,7 +59,7 @@ uniform float4 color_4 = {0.97, 0.94, 0.53, 1.0};
// quantize coords to low resolution
float2 pixelize(float2 uv, float2 pixelSize) {
float2 factor = pixelSize / uv_size;
return floor(uv / factor) * factor + factor / 2.0;
return floor(uv / factor) * factor;
}

float3 colorLUT(float3 color) {
Expand All @@ -84,22 +86,35 @@ float3 levels(float3 color, float brightness, float contrast, float gamma) {
// applies the dithering filter to a color map
float3 dither8x8(float2 coord, float3 color, float2 pixelSize) {
// reduces pixel space to the selected pixel size
float2 pixelCoord = floor((coord * uv_size) / pixelSize + 0.5);
float2 pixelCoord = floor((coord * uv_size) / pixelSize + float2(0.5, 0.5));

// applies the bayer matrix filter to the color map
pixelCoord = pixelCoord - 8.0 * floor(pixelCoord/8.0);
int index = int(pixelCoord.x + (pixelCoord.y * 8.0));
const int bayer8[64] = {
0, 32, 8, 40, 2, 34, 10, 42, /* 8x8 Bayer ordered dithering */
48, 16, 56, 24, 50, 18, 58, 26, /* pattern. Each input pixel */
12, 44, 4, 36, 14, 46, 6, 38, /* is scaled to the 0..63 range */
60, 28, 52, 20, 62, 30, 54, 22, /* before looking in this table */
3, 35, 11, 43, 1, 33, 9, 41, /* to determine the action. */
51, 19, 59, 27, 49, 17, 57, 25,
15, 47, 7, 39, 13, 45, 5, 37,
63, 31, 55, 23, 61, 29, 53, 21
};
float bayer = (bayer8[index]-31.0)/32.0;
float bayer;
if (alternative_bayer){
const int bayer8[64] = {
0, 32, 8, 40, 2, 34, 10, 42, /* 8x8 Bayer ordered dithering */
48, 16, 56, 24, 50, 18, 58, 26, /* pattern. Each input pixel */
12, 44, 4, 36, 14, 46, 6, 38, /* is scaled to the 0..63 range */
60, 28, 52, 20, 62, 30, 54, 22, /* before looking in this table */
3, 35, 11, 43, 1, 33, 9, 41, /* to determine the action. */
51, 19, 59, 27, 49, 17, 57, 25,
15, 47, 7, 39, 13, 45, 5, 37,
63, 31, 55, 23, 61, 29, 53, 21};
bayer = (bayer8[index]-31.0)/32.0;
} else {
const int bayer8[64] = {
0, 48, 12, 60, 3, 51, 15, 63,
32, 16, 44, 28, 35, 19, 47, 31,
8, 56, 4, 52, 11, 59, 7, 55,
40, 24, 36, 20, 43, 27, 39, 23,
2, 50, 14, 62, 1, 49, 13, 61,
34, 18, 46, 30, 33, 17, 45, 29,
10, 58, 6, 54, 9, 57, 5, 53,
42, 26, 38, 22, 41, 25, 37, 21};
bayer = (bayer8[index]-31.0)/32.0;
}
float3 bayerColor = (color + float3(bayer,bayer,bayer) * (dither_factor / 8.0));
// limits it to the selected palette
color = colorLUT(bayerColor);
Expand All @@ -110,6 +125,7 @@ float3 dither8x8(float2 coord, float3 color, float2 pixelSize) {
float4 mainImage(VertData v_in) : TARGET
{
float2 texcoord = pixelize(v_in.uv, float2(pixelSize,pixelSize));
texcoord = clamp(texcoord, 0.001, 1.0);
float4 c = image.Sample(textureSampler, texcoord);
float3 color = c.rgb;

Expand Down

0 comments on commit e0d9105

Please sign in to comment.