Skip to content

Instantly share code, notes, and snippets.

@TheLouisHong
Created June 4, 2017 00:09
Show Gist options
  • Save TheLouisHong/819e05996548f5ea99a1c22e1546997f to your computer and use it in GitHub Desktop.
Save TheLouisHong/819e05996548f5ea99a1c22e1546997f to your computer and use it in GitHub Desktop.
Retry-gameboy looking image-fx in unity3d
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_5_4_OR_NEWER
[ImageEffectAllowedInSceneView]
#endif
[RequireComponent(typeof(Camera)), DisallowMultipleComponent, ExecuteInEditMode]
[AddComponentMenu("Effects/GameboyFX", -1)]
public class GameboyFX : MonoBehaviour {
private Material _material;
private Material material
{
get {
if (_material == null)
{
_material = new Material( Shader.Find("Hidden/GameboyFX") );
}
return _material;
}
}
// Postprocess the image
void OnRenderImage (RenderTexture source, RenderTexture destination)
{
Graphics.Blit (source, destination, material);
}
}
Shader "Hidden/GameboyFX"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
// No culling or depth
Cull Off ZWrite Off ZTest Always
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;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
sampler2D _MainTex;
fixed4 frag (v2f i) : SV_Target
{
// Size of pixel
const float pixel_size = 20;
const float contract = 1.5;
const float3 tint = float3(1.1, 1.4, 1);
float2 uv = i.uv;
float dx = pixel_size * (1.0/_ScreenParams.x);
float dy = pixel_size * (1.0/_ScreenParams.y);
float2 tc = float2(dx * floor(uv.x/dx), dy * floor(uv.y/dy));
float4 color = tex2D(_MainTex, tc);
color = (color - 0.5) * contract + 0.5;
float gray = (color.r + color.g + color.b) / 3.0;
color = float4(gray * tint, color.a);
return color;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment