Skip to content

Instantly share code, notes, and snippets.

@NickDiMucci
Forked from KeyMaster-/spriteGlitch.shader
Created July 11, 2014 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NickDiMucci/4d3a65fd89994338a311 to your computer and use it in GitHub Desktop.
Save NickDiMucci/4d3a65fd89994338a311 to your computer and use it in GitHub Desktop.
//Copyright (c) 2014 Tilman Schmidt (@KeyMaster_)
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
Shader "Sprites/Glitch"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
_DispIntensity ("Displacement Glitch Intensity", Float) = 0.09
_DispFrequency ("Displacement Glitch Frequency", Float) = 0.022
_ColorIntensity("Color Glitch Intensity", Float) = 0.07
_ColorFrequency("Color Glitch Frequency", Float) = 0.02
[MaterialToggle] _DispGlitchOn ("Displacement Glitch On", Float) = 1
[MaterialToggle] _ColorGlitchOn ("Color Glitch On", Float) = 1
}
SubShader
{
Tags
{
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}
Cull Off
Lighting Off
ZWrite Off
Fog { Mode Off }
Blend One OneMinusSrcAlpha
Pass
{
CGPROGRAM
// Upgrade NOTE: excluded shader from Xbox360; has structs without semantics (struct v2f members pos)
#pragma exclude_renderers xbox360
#pragma vertex vert
#pragma fragment frag
#pragma target 3.0
#pragma multi_compile DUMMY PIXELSNAP_ON
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
half2 texcoord : TEXCOORD0;
};
fixed4 _Color;
v2f vert(appdata_t IN)
{
v2f OUT;
OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
OUT.texcoord = IN.texcoord;
OUT.color = IN.color * _Color;
#ifdef PIXELSNAP_ON
OUT.vertex = UnityPixelSnap (OUT.vertex);
#endif
return OUT;
}
sampler2D _MainTex;
float rand(float x, float y){
return frac(sin(x*12.9898 + y*78.233)*43758.5453);
}
float _DispIntensity;
float _DispFrequency;
float _ColorIntensity;
float _ColorFrequency;
float _DispGlitchOn;
float _ColorGlitchOn;
fixed4 frag(v2f IN) : SV_Target
{
float timeYIndependent = float(_Time.y + UNITY_MATRIX_MV[0][3] + UNITY_MATRIX_MV[1][3]);
float timeYIndependent2 = float(_Time.x + UNITY_MATRIX_MV[0][3] + UNITY_MATRIX_MV[1][3]);
float dispGlitchRandom = rand(timeYIndependent, -timeYIndependent);
float colorGlitchRandom = rand(timeYIndependent, timeYIndependent);
float rShiftRandom = (rand(-timeYIndependent, timeYIndependent) - 0.5) * _ColorIntensity;
float gShiftRandom = (rand(-timeYIndependent, -timeYIndependent) - 0.5) * _ColorIntensity;
float bShiftRandom = (rand(timeYIndependent, -timeYIndependent) - 0.5) * _ColorIntensity;
float shiftLineOffset = float((rand(timeYIndependent2, -timeYIndependent2) - 0.5) / 50);
if(dispGlitchRandom < _DispFrequency && _DispGlitchOn == 1){
IN.texcoord.x += (rand(floor(IN.texcoord.y / (0.2 + shiftLineOffset)) - _Time.x, floor(IN.texcoord.y / (0.2 + shiftLineOffset)) + _Time.x) - 0.5) * _DispIntensity;
}
fixed4 normalC = tex2D(_MainTex, IN.texcoord);
fixed4 rShifted = tex2D(_MainTex, float2(IN.texcoord.x + rShiftRandom, IN.texcoord.y + rShiftRandom));
fixed4 gShifted = tex2D(_MainTex, float2(IN.texcoord.x + gShiftRandom, IN.texcoord.y + gShiftRandom));
fixed4 bShifted = tex2D(_MainTex, float2(IN.texcoord.x + bShiftRandom, IN.texcoord.y + bShiftRandom));
fixed4 c = fixed4(0.0,0.0,0.0,0.0);
if(colorGlitchRandom < _ColorFrequency && _ColorGlitchOn== 1){
c.r = rShifted.r;
c.g = gShifted.g;
c.b = bShifted.b;
c.a = (rShifted.a + gShifted.a + bShifted.a) / 3;
}
else{
c = normalC;
}
c.rgb *= IN.color;
c.a *= IN.color.a;
c.rgb *= c.a;
return c;
}
ENDCG
}
}
SubShader
{
Tags
{
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}
Cull Off
Lighting Off
ZWrite Off
Fog { Mode Off }
Blend One OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile DUMMY PIXELSNAP_ON
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
half2 texcoord : TEXCOORD0;
};
fixed4 _Color;
v2f vert(appdata_t IN)
{
v2f OUT;
OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
OUT.texcoord = IN.texcoord;
OUT.color = IN.color * _Color;
#ifdef PIXELSNAP_ON
OUT.vertex = UnityPixelSnap (OUT.vertex);
#endif
return OUT;
}
sampler2D _MainTex;
float rand(float x, float y){
return frac(sin(x*12.9898 + y*78.233)*43758.5453);
}
float _DispIntensity;
float _DispFrequency;
fixed4 frag(v2f IN) : SV_Target
{
float timeYIndependent = float(_Time.y + UNITY_MATRIX_MV[0][3] + UNITY_MATRIX_MV[1][3]);
float timeRandom = rand(timeYIndependent, -timeYIndependent);
if(timeRandom < _DispFrequency){
IN.texcoord.x += (rand(floor(IN.texcoord.y / 0.2) - _Time.x, floor(IN.texcoord.y / 0.2) + _Time.x) - 0.5) * _DispIntensity;
}
fixed4 c = tex2D(_MainTex, IN.texcoord) * IN.color;
c.a *= IN.color.a;
c.rgb *= c.a;
return c;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment