Skip to content

Instantly share code, notes, and snippets.

@tklee1975
Created April 27, 2019 06:52
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tklee1975/7402a5ed19d0bc7083f84b798bd2e618 to your computer and use it in GitHub Desktop.
Save tklee1975/7402a5ed19d0bc7083f84b798bd2e618 to your computer and use it in GitHub Desktop.
Simple Unity 2D Shader
Shader "Unlit/BlurShader"
{
Properties
{
[PerRendererData] _MainTex ("Texture", 2D) = "white" {}
_BlurSize ("Blur Size", Float) = 1.0
}
SubShader
{
Tags {"Queue"="Transparent" "RenderType"="Transparent" }
LOD 100
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float4 pos : SV_POSITION;
half2 uv[5]: TEXCOORD0;
};
sampler2D _MainTex;
float4 _MainTex_ST;
half4 _MainTex_TexelSize;
float _BlurSize;
v2f vert (appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
//o.uv = TRANSFORM_TEX(v.uv, _MainTex);
half2 uv = v.uv;
o.uv[0] = uv;
o.uv[1] = uv + float2(_MainTex_TexelSize.x * 1.0, 0.0) * _BlurSize;
o.uv[2] = uv - float2(_MainTex_TexelSize.x * 1.0, 0.0) * _BlurSize;
o.uv[3] = uv + float2(_MainTex_TexelSize.x * 2.0, 0.0) * _BlurSize;
o.uv[4] = uv - float2(_MainTex_TexelSize.x * 2.0, 0.0) * _BlurSize;
return o;
}
fixed4 frag(v2f i) : SV_Target {
float weight[3] = {0.4026, 0.2442, 0.0545};
fixed3 sum = tex2D(_MainTex, i.uv[0]).rgb * weight[0];
fixed a = tex2D(_MainTex, i.uv[0]).a;
for (int it = 1; it < 3; it++) {
sum += tex2D(_MainTex, i.uv[it*2-1]).rgb * weight[it];
sum += tex2D(_MainTex, i.uv[it*2]).rgb * weight[it];
}
return fixed4(sum, a);
}
ENDCG
}
}
}
Shader "Unlit/GrayscaleShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags {"Queue"="Transparent" "RenderType"="Transparent" }
LOD 100
Pass
{
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
fixed value = (col.x + col.y + col.z) / 3;
return fixed4(value, value, value, col.a);
//return col;
}
ENDCG
}
}
}
Shader "Unlit/MovingTileShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Speed ("Speed", Range(0,5.0)) = 1
}
SubShader
{
Tags { "RenderType"="Opaque" }
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;
float4 _MainTex_ST;
float _Speed;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
float4 finalTexColor(sampler2D tex, float4 st, float time, float2 uv)
{
float2 tiling = st.xy;
float2 offset = float2(time, time);
return tex2D(tex, uv * tiling + offset);
}
fixed4 frag (v2f i) : SV_Target
{
float time = _Time.y * _Speed;
float2 tiling = _MainTex_ST.xy;
float2 offset = float2(time, time);
return tex2D(_MainTex, i.uv * tiling + offset);
}
ENDCG
}
}
}
Shader "Unlit/ShiningShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Speed ("Wave Speed", Range(0.1, 80)) = 5
_BoomWeight ("Boom Weight", Range(0, 1)) = 0.5
}
SubShader
{
Tags {"Queue"="Transparent" "RenderType"="Transparent" }
LOD 100
Pass
{
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
// Upgrade NOTE: excluded shader from DX11; has structs without semantics (struct v2f members bloomWeight)
#pragma exclude_renderers d3d11
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float4 bloomColor : COLOR;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float _Speed;
float _BoomWeight;
v2f vert (appdata v)
{
v2f o;
// _Time is a data given by unity
float time = _Time * _Speed;
// the wave information.
float boomValue = (1 - sin(time)) * 0.5 * _BoomWeight; // RESULT: 0 ~ 1
o.bloomColor = float4(boomValue, boomValue, boomValue, 0);
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
col += i.bloomColor;
return col;
//return col;
}
ENDCG
}
}
}
Shader "Unlit/Unlit_TileShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
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;
float4 _MainTex_ST;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
// apply fog
// UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
}
}
Shader "Unlit/Unlit_Dither"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_DitherScale ("Dither Scale", Range(1, 100)) = 50
_DitherStrength ("Dither Strength", Range(1, 10)) = 3
}
SubShader
{
Tags { "RenderType"="Opaque" }
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;
float4 _MainTex_ST;
float _DitherScale;
float _DitherStrength;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
inline float Dither4x4(float2 uv, float resolution)
{
float pattern[16] = {0.0625,0.5625,0.1875, 0.6875,0.8125,0.3125,0.9375,0.4375,0.25,0.75,0.125,0.625,1.0,0.5,0.875,0.375};
int x = int(fmod(uv.x*resolution, 4));
int y = int(fmod(uv.y*resolution, 4));
int index = x + y * 4;
float limit = 0.0;
if (x < 4) {
limit = pattern[index];
}
return limit;
}
inline float4 FillPatternColor(fixed4 col, fixed steps, float lum, float limit)
{
// Calculate high/low color
col.rgb = col.rgb*steps;
fixed3 low = floor(col.rgb)/steps;
fixed3 high = ceil(col.rgb)/steps;
col.rgb = (lum < limit ? low : high);
return col;
}
inline float4 Dither4x4Color(float2 uv, fixed4 col, float resolution)
{
float lu = Luminance(col.rgb);
float limit = Dither4x4(uv, resolution);
return FillPatternColor(col, _DitherStrength, lu, limit);
}
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
// apply fog
// UNITY_APPLY_FOG(i.fogCoord, col);
return Dither4x4Color(i.uv, col, _DitherScale);
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment