Skip to content

Instantly share code, notes, and snippets.

@lonewolfwilliams
Last active November 18, 2016 12:43
Show Gist options
  • Select an option

  • Save lonewolfwilliams/38fc0da9ea1c0657853d91e25468b96a to your computer and use it in GitHub Desktop.

Select an option

Save lonewolfwilliams/38fc0da9ea1c0657853d91e25468b96a to your computer and use it in GitHub Desktop.
Shader "Unlit/GBShader"
{
Properties
{
//to apply the filter to (could also be a rendertexture)
_MainTex ("Texture", 2D) = "white" {}
//a 256 x 256 'ramp' image with 4 shades of gray in (each 64 x 256)
_PalTex ("Pallete", 2D) = "white" {}
//a pow2 virtual resolution for the display
_Res ("Resolution", Float) = 64
//colour of LCD backlight
_BG ("backlight", Color) = (1,1,1,1)
//attenuation of fake lighting coming in from edges of screen
_Ntns ("backlight intensity curve", Float) = 2.2
//overall brightness (boost value)
_Brt ("brightness", Float) = 1.1
//the mix between the lighting from edges and 'ambient' light of the screen
_Mix ("ambient vs backlight", Float) = 0.5
}
SubShader
{
Tags { "RenderType"="Opaque" "ZWrite"="Off"}
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
sampler2D _PalTex;
float _Res;
fixed4 _BG;
float _Ntns;
float _Brt;
float _Mix;
float4 _MainTex_ST;
v2f vert (appdata v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
//pixelate (downsampling isn't necessary since we are reducing colours so drastically
fixed2 smp = floor(i.uv * _Res) / _Res;
//reduce colours
fixed4 col = tex2D(_MainTex, smp);
fixed av = (col.x + col.y + col.z) / 3.0;
fixed4 bw = tex2D(_PalTex, float2(0, av));
//lcd matrix (gaps)
fixed2 grd = abs(sin(i.uv * 3.14159 * _Res));
//backlight
fixed2 attn = pow(abs(0.5-i.uv) * 2, _Ntns);
fixed4 bg = _Mix + (attn.x + attn.y) * (1.0-_Mix);
//return (bg * bw - (grd.x * grd.y)) * _Brt * _BG;
return bg * bw * grd.x * grd.y * _Brt * _BG;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment