Skip to content

Instantly share code, notes, and snippets.

@SvDvorak
Created February 1, 2017 20:20
Show Gist options
  • Save SvDvorak/72caef199a701b0c3ed67bfceac1c6cd to your computer and use it in GitHub Desktop.
Save SvDvorak/72caef199a701b0c3ed67bfceac1c6cd to your computer and use it in GitHub Desktop.
Shader "Custom/Pixelate" {
Properties
{
_PixelSize("Pixel Size", Float) = 10
_Snapping("Snapping", Int) = 48
_Color("Color", Color) = (1, 1, 1, 1)
}
SubShader
{
Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" }
Blend Off
Lighting Off
Fog{ Mode Off }
ZWrite Off
LOD 200
Cull Off
GrabPass{ "_GrabTexture" }
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata_t {
float4 vertex : POSITION;
float4 color : COLOR;
};
struct v2f
{
float4 pos : SV_POSITION;
float4 uv : TEXCOORD0;
float4 color : COLOR;
};
float _PixelSize;
int _Snapping;
float4 _Color;
v2f vert(appdata_t v)
{
v2f o;
float2 pixelRatio = _PixelSize / _ScreenParams.xy;
float4 snapToPixel = mul(UNITY_MATRIX_MVP, v.vertex);
float4 vertex = snapToPixel;
vertex.xyz = vertex.xyz / vertex.w;
vertex.x = floor(_Snapping * vertex.x) / _Snapping;
vertex.y = floor(_Snapping * vertex.y) / _Snapping;
vertex.xyz *= snapToPixel.w;
o.pos = vertex;
o.uv = ComputeGrabScreenPos(o.pos);
o.color = v.color;
return o;
}
sampler2D _GrabTexture;
float4 frag(v2f IN) : COLOR
{
float2 steppedUV = IN.uv.xy / IN.uv.w;
float2 pixelRatio = _PixelSize / _ScreenParams.xy;
steppedUV = round(steppedUV / pixelRatio) * pixelRatio;
float4 grabbedColor = tex2D(_GrabTexture, steppedUV);
return grabbedColor + _Color * _Color.w + IN.color * IN.color.w;
//return float4(grabbedColor.x * sin(IN.pos.x), grabbedColor.y, grabbedColor.z, grabbedColor.w);
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment