Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A glitch effect shader for Sprites in Unity3D
//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
_GlitchInterval ("Glitch interval time [seconds]", Float) = 0.16
_DispProbability ("Displacement Glitch Probability", Float) = 0.022
_DispIntensity ("Displacement Glitch Intensity", Float) = 0.09
_ColorProbability("Color Glitch Probability", Float) = 0.02
_ColorIntensity("Color Glitch Intensity", Float) = 0.07
[MaterialToggle] _WrapDispCoords ("Wrap disp glitch (off = clamp)", Float) = 1
[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;
//Takes two values and returns a pseudo-random number between 0 (included) and 1 (excluded)
//It samples the sin function, scales it up (presumably to increase floating point error) and then takes it's fraction part (to get value between 0 and 1)
float rand(float x, float y){
return frac(sin(x*12.9898 + y*78.233)*43758.5453);
}
float _GlitchInterval;
float _DispIntensity;
float _DispProbability;
float _ColorIntensity;
float _ColorProbability;
float _DispGlitchOn;
float _ColorGlitchOn;
float _WrapDispCoords;
fixed4 frag(v2f IN) : SV_Target
{
//This ensures that the shader only generates new random variables every [_GlitchInterval] seconds, e.g. every 0.5 seconds
//During each interval the value wether the glitch occurs and how much the sprites glitches stays the same
float intervalTime = floor(_Time.y / _GlitchInterval) * _GlitchInterval;
//Second value increased by arbitrary number just to get more possible different random values
float intervalTime2 = intervalTime + 2.793;
//These values depend on time and the x/y translation of that sprite (top right and middle right value in the transformation matrix are translation)
//The transformation matrix values are included so sprites with differen x/y values don't glitch at the same time
float timePositionVal = intervalTime + UNITY_MATRIX_MV[0][3] + UNITY_MATRIX_MV[1][3];
float timePositionVal2 = intervalTime2 + UNITY_MATRIX_MV[0][3] + UNITY_MATRIX_MV[1][3];
//Random chance that the displacement glich or color glitch occur
float dispGlitchRandom = rand(timePositionVal, -timePositionVal);
float colorGlitchRandom = rand(timePositionVal, timePositionVal);
//Precalculate color channel shift
float rShiftRandom = (rand(-timePositionVal, timePositionVal) - 0.5) * _ColorIntensity;
float gShiftRandom = (rand(-timePositionVal, -timePositionVal) - 0.5) * _ColorIntensity;
float bShiftRandom = (rand(-timePositionVal2, -timePositionVal2) - 0.5) * _ColorIntensity;
//For the displacement glitch, the sprite is divided into strips of 0.2 * sprite height (5 stripes)
//This value is the random offset each of the strip boundries get either up or down
//Without this, each strip would be exactly a 5th of the sprite height, with this their height is slightly randomised
float shiftLineOffset = float((rand(timePositionVal2, timePositionVal2) - 0.5) / 50);
//If the randomly rolled value is below the probability boundry and the displacement effect is turned on, apply the displacement effect
if(dispGlitchRandom < _DispProbability && _DispGlitchOn == 1){
IN.texcoord.x += (rand(floor(IN.texcoord.y / (0.2 + shiftLineOffset)) - timePositionVal, floor(IN.texcoord.y / (0.2 + shiftLineOffset)) + timePositionVal) - 0.5) * _DispIntensity;
//Prevent the texture coordinate from going into other parts of the texture, especially when using texture atlases
//Instead, loop the coordinate between 0 and 1
if(_WrapDispCoords == 1){
IN.texcoord.x = fmod(IN.texcoord.x, 1);
}
else{
IN.texcoord.x = clamp(IN.texcoord.x, 0, 1);
}
}
//Sample the texture at the normal position and at the shifted color channel positions
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 the randomly rolled value is below the probability boundry and the color effect is turned on, apply the color glitch effect
//Sets the output color to the shifted r,g,b channels and averages their alpha
if(colorGlitchRandom < _ColorProbability && _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;
}
//Apply tint and tint color alpha
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 _DispProbability;
float _GlitchInterval;
float _DispGlitchOn;
float _WrapDispCoords;
fixed4 frag(v2f IN) : SV_Target
{
float intervalTime = floor(_Time.y / _GlitchInterval) * _GlitchInterval;
float timePositionVal = float(intervalTime + UNITY_MATRIX_MV[0][3] + UNITY_MATRIX_MV[1][3]);
float timeRandom = rand(timePositionVal, -timePositionVal);
if(timeRandom < _DispProbability && _DispGlitchOn == 1){
IN.texcoord.x += (rand(floor(IN.texcoord.y / 0.2) - intervalTime, floor(IN.texcoord.y / 0.2) + intervalTime) - 0.5) * _DispIntensity;
if(_WrapDispCoords == 1){
IN.texcoord.x = fmod(IN.texcoord.x, 1);
}
else{
IN.texcoord.x = clamp(IN.texcoord.x, 0, 1);
}
}
fixed4 c = tex2D(_MainTex, IN.texcoord) * IN.color;
c.a *= IN.color.a;
c.rgb *= c.a;
return c;
}
ENDCG
}
}
}
@KeyMaster-
Copy link
Author

KeyMaster- commented Jul 10, 2014

Glitch shader for sprites in Unity3D

Demo: http://imgur.com/fkGLhlw
Lets sprites glitch occasionally by either displacing strips of the sprite horizontally or displacing the color channels.

Features

  • Shader model 3
    • Horizontal displacement effect, shifting strips of the sprite left or right by a random amount
    • Color channel displacement along X and Y by a random amount
  • Shader model 2
    • Horizontal displacement effect only

Settings

  • Glitch Interval time [seconds]: Every "interval" seconds the glitch shader checks against the probability and determines wether the glitch effect occurs. If the glitch occurs, it will stay on for the length of one interval.
  • Displacement glitch probability: How likely it is in one frame for the displacement effect to occur.
    0 == Never, 1 == Always. Low value around 0.005 is recommended, depending on the glitch interval
  • Displacement glitch intensity: Maximum displacement of the strips.
    The value represents the "percentage" of the sprite width, e.g. 0.5 means half the sprite width in displacement at most
  • Color glitch probability: How likely it is in one frame for the color effect to occur.
    0 == Never, 1 == Always. Low value around 0.005 recommended
  • Color glitch intensity: How far the channels will be displaced maximum along both X and Y
    Again, a percentage of the sprite width and height, e.g. 0.5 means half the sprites width at most and half the sprites height at most
  • Wrap disp glitch: If on, the displaced texture coordinate wraps around. If off, it clamps to the edge.
  • Displacement glitch on: Switches the displacement effect on or off for all sprites using the shader
  • Color glitch on: Switches the color effect on or off for all sprites using the shader

Loading

@Techvania
Copy link

Techvania commented Jan 25, 2015

Hey! This is a really nice shader 😃 , however, I keep getting errors that say "Arithmetic instruction limit of 64 exceeded" since there are 73 instructions. Shader 3.0 is enabled in your script but for some reason it keeps kicking me to this error when compiling. Everything works fine - I just get this error on compile. Thoughts?

Loading

@Joutenheim
Copy link

Joutenheim commented Jul 15, 2016

Very nice! im wondering though if there is a way to add diffuse? im making a game where the sprite art is affected by point lights and such, giving it some light and dark areas

Loading

@ameturpoet
Copy link

ameturpoet commented Jun 17, 2017

Nice work! I'm having a bit of an issue with the shader, though. When I apply these shaders to a UI object, and then push that object beyond the bounds of a UI Mask, it does not hide as it normally does when it does not have a material attached. Is there any good fix for that?

Loading

@nightm4re94
Copy link

nightm4re94 commented Nov 16, 2017

Brilliant! thank you for this!

Loading

@metinevren
Copy link

metinevren commented Jan 7, 2018

I'm getting this warning. I don't know enough to edit the shader myself. Could you modify it?
"Shader warning in 'Sprites/Glitch': Use of UNITY_MATRIX_MV is detected. To transform a vertex into view space, consider using UnityObjectToViewPos for better performance."

Loading

@KeyMaster-
Copy link
Author

KeyMaster- commented Mar 20, 2018

Hi, since I don't use Unity myself anymore, I haven't kept this shader up to date, and I currently don't have the time to fix this.

However, after some brief googling, it seems like any use of UNITY_MATRIX_MVP should now use UnityObjectToClipPos and any use of UNITY_MATRIX_MV should now use UnityObjectToViewPos. Line 224 uses UNITY_MATRIX_MVP,
and I think OUT.vertex = UnityObjectToClipPos(IN.vertex.xyz); should be an equivalent line that can replace it.

As for the uses of UNITY_MATRIX_MV, (112 and 113, 249), they use the matrix to get the sprite's position in view space.
I think at both of those places, you could insert float3 offset = UnityObjectToViewPos(float3(0.0, 0.0, 0.0)); before the uses of the matrix, and then replace any instance of UNITY_MATRIX_MV[0][3] with offset.x and any instance of UNITY_MATRIX_MV[1][3] with offset.y.
See Unity Shaders builtin functions page for more info on those functions.

As I said I have no way of testing this right now, but I hope this will be of some use.

Loading

@metinevren
Copy link

metinevren commented Apr 11, 2018

Thank you, the warning went away, and it seems to be working. I didn't have in depth knowledge to adapt it, so thank you for the help. Other glitch effects effect whole screen, this one effects only the sprites so that makes it interesting to me. Thank you again.

Loading

@renanvalentin
Copy link

renanvalentin commented Apr 1, 2020

It works perfectly! Anyone knows how to extend this to support grayscale?

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment