Skip to content

Commit

Permalink
Add Game Boy palette shader
Browse files Browse the repository at this point in the history
  • Loading branch information
Monroe88 committed Jan 26, 2018
1 parent bee5b88 commit ff22e57
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 0 deletions.
9 changes: 9 additions & 0 deletions handheld/gb-palette-dmg.glslp
@@ -0,0 +1,9 @@
shaders = 1
shader0 = shaders/gb-palette/gb-palette.glsl //colorize image

scale_type0 = source
filter_linear0 = false

textures = COLOR_PALETTE
COLOR_PALETTE = shaders/gb-palette/resources/palette-dmg.png
COLOR_PALETTE_linear = false
9 changes: 9 additions & 0 deletions handheld/gb-palette-light.glslp
@@ -0,0 +1,9 @@
shaders = 1
shader0 = shaders/gb-palette/gb-palette.glsl //colorize image

scale_type0 = source
filter_linear0 = false

textures = COLOR_PALETTE
COLOR_PALETTE = shaders/gb-palette/resources/palette-light.png
COLOR_PALETTE_linear = false
9 changes: 9 additions & 0 deletions handheld/gb-palette-pocket.glslp
@@ -0,0 +1,9 @@
shaders = 1
shader0 = shaders/gb-palette/gb-palette.glsl //colorize image

scale_type0 = source
filter_linear0 = false

textures = COLOR_PALETTE
COLOR_PALETTE = shaders/gb-palette/resources/palette-pocket.png
COLOR_PALETTE_linear = false
98 changes: 98 additions & 0 deletions handheld/shaders/gb-palette/gb-palette.glsl
@@ -0,0 +1,98 @@
#if defined(VERTEX)

#if __VERSION__ >= 130
#define COMPAT_VARYING out
#define COMPAT_ATTRIBUTE in
#define COMPAT_TEXTURE texture
#else
#define COMPAT_VARYING varying
#define COMPAT_ATTRIBUTE attribute
#define COMPAT_TEXTURE texture2D
#endif

#ifdef GL_ES
#define COMPAT_PRECISION mediump
#else
#define COMPAT_PRECISION
#endif

COMPAT_ATTRIBUTE vec4 VertexCoord;
COMPAT_ATTRIBUTE vec4 COLOR;
COMPAT_ATTRIBUTE vec4 TexCoord;
COMPAT_VARYING vec4 COL0;
COMPAT_VARYING vec4 TEX0;

vec4 _oPosition1;
uniform mat4 MVPMatrix;
uniform COMPAT_PRECISION int FrameDirection;
uniform COMPAT_PRECISION int FrameCount;
uniform COMPAT_PRECISION vec2 OutputSize;
uniform COMPAT_PRECISION vec2 TextureSize;
uniform COMPAT_PRECISION vec2 InputSize;

// compatibility #defines
#define vTexCoord TEX0.xy
#define SourceSize vec4(TextureSize, 1.0 / TextureSize) //either TextureSize or InputSize
#define OutSize vec4(OutputSize, 1.0 / OutputSize)

void main()
{
gl_Position = MVPMatrix * VertexCoord;
TEX0.xy = TexCoord.xy;
}

#elif defined(FRAGMENT)

#if __VERSION__ >= 130
#define COMPAT_VARYING in
#define COMPAT_TEXTURE texture
out vec4 FragColor;
#else
#define COMPAT_VARYING varying
#define FragColor gl_FragColor
#define COMPAT_TEXTURE texture2D
#endif

#ifdef GL_ES
#ifdef GL_FRAGMENT_PRECISION_HIGH
precision highp float;
#else
precision mediump float;
#endif
#define COMPAT_PRECISION mediump
#else
#define COMPAT_PRECISION
#endif

uniform COMPAT_PRECISION int FrameDirection;
uniform COMPAT_PRECISION int FrameCount;
uniform COMPAT_PRECISION vec2 OutputSize;
uniform COMPAT_PRECISION vec2 TextureSize;
uniform COMPAT_PRECISION vec2 InputSize;
uniform sampler2D Texture;
uniform sampler2D COLOR_PALETTE;
COMPAT_VARYING vec4 TEX0;

// compatibility #defines
#define Source Texture
#define vTexCoord TEX0.xy

#define SourceSize vec4(TextureSize, 1.0 / TextureSize) //either TextureSize or InputSize
#define OutSize vec4(OutputSize, 1.0 / OutputSize)

void main()
{
//sample the input textures

vec4 out_color = COMPAT_TEXTURE(Source, vTexCoord.xy);

//input grayscale values:
//0.000 black - 0.333 medium gray - 0.667 light gray - 1.000 white
//multiply grayscale value by 3 to obtain int value in range [0, 3], acts as index for arrays storing custom palette UV coordinates and alpha value

vec2 palette_coordinate = vec2(0.5, (abs(1 - out_color.r) * 0.75) + 0.125); //directly map input grayscale value to color palette y coordinate
out_color = vec4(COMPAT_TEXTURE(COLOR_PALETTE, palette_coordinate).rgb, ceil(abs(1.0 - out_color.r))); //sample color from palette, alpha = 1 if color is not transparent

FragColor = out_color;
}
#endif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit ff22e57

Please sign in to comment.