Created
January 31, 2014 12:25
-
-
Save keiranlovett/8731159 to your computer and use it in GitHub Desktop.
Unity: Glitch Effect
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
[ExecuteInEditMode] | |
[AddComponentMenu("Image Effects/GlitchEffect")] | |
public class glitchEffect : ImageEffectBase { | |
public Texture2D displacementMap; | |
float glitchup, glitchdown, flicker, | |
glitchupTime = 0.05f, glitchdownTime = 0.05f, flickerTime = 0.5f; | |
public float intensity; | |
// Called by camera to apply image effect | |
void OnRenderImage (RenderTexture source, RenderTexture destination) { | |
material.SetFloat("_Intensity", intensity); | |
material.SetTexture("_DispTex", displacementMap); | |
glitchup += Time.deltaTime * intensity; | |
glitchdown += Time.deltaTime * intensity; | |
flicker += Time.deltaTime * intensity; | |
if(flicker > flickerTime){ | |
material.SetFloat("filterRadius", Random.Range(-3f, 3f) * intensity); | |
flicker = 0; | |
flickerTime = Random.value; | |
} | |
if(glitchup > glitchupTime){ | |
if(Random.value < 0.1f * intensity) | |
material.SetFloat("flip_up", Random.Range(0, 1f) * intensity); | |
else | |
material.SetFloat("flip_up", 0); | |
glitchup = 0; | |
glitchupTime = Random.value/10f; | |
} | |
if(glitchdown > glitchdownTime){ | |
if(Random.value < 0.1f * intensity) | |
material.SetFloat("flip_down", 1-Random.Range(0, 1f) * intensity); | |
else | |
material.SetFloat("flip_down", 1); | |
glitchdown = 0; | |
glitchdownTime = Random.value/10f; | |
} | |
if(Random.value < 0.05 * intensity){ | |
material.SetFloat("displace", Random.value * intensity); | |
material.SetFloat("scale", 1-Random.value * intensity); | |
}else | |
material.SetFloat("displace", 0); | |
Graphics.Blit (source, destination, material); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This work is licensed under a Creative Commons Attribution 3.0 Unported License. | |
// http://creativecommons.org/licenses/by/3.0/deed.en_GB | |
// | |
// You are free: | |
// | |
// to copy, distribute, display, and perform the work | |
// to make derivative works | |
// to make commercial use of the work | |
Shader "Hidden/GlitchShader" { | |
Properties { | |
_MainTex ("Base (RGB)", 2D) = "white" {} | |
_DispTex ("Base (RGB)", 2D) = "bump" {} | |
_Intensity ("Glitch Intensity", Range(0.1, 1.0)) = 1 | |
} | |
SubShader { | |
Pass { | |
ZTest Always Cull Off ZWrite Off | |
Fog { Mode off } | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#pragma fragmentoption ARB_precision_hint_fastest | |
#include "UnityCG.cginc" | |
uniform sampler2D _MainTex; | |
uniform sampler2D _DispTex; | |
float _Intensity; | |
float filterRadius; | |
float flip_up, flip_down; | |
float displace; | |
float scale; | |
struct v2f { | |
float4 pos : POSITION; | |
float2 uv : TEXCOORD0; | |
}; | |
v2f vert( appdata_img v ) | |
{ | |
v2f o; | |
o.pos = mul (UNITY_MATRIX_MVP, v.vertex); | |
o.uv = v.texcoord.xy; | |
return o; | |
} | |
half4 frag (v2f i) : COLOR | |
{ | |
half4 normal = tex2D (_DispTex, i.uv.xy * scale); | |
if(i.uv.y < flip_up) | |
i.uv.y = 1 - (i.uv.y + flip_up); | |
if(i.uv.y > flip_down) | |
i.uv.y = 1 - (i.uv.y - flip_down); | |
i.uv.xy += (normal.xy - 0.5) * displace * _Intensity; | |
half4 color = tex2D(_MainTex, i.uv.xy); | |
half4 redcolor = tex2D(_MainTex, i.uv.xy + 0.01 * filterRadius * _Intensity); | |
half4 greencolor = tex2D(_MainTex, i.uv.xy + 0.01 * filterRadius * _Intensity); | |
if(filterRadius > 0){ | |
color.r = redcolor.r * 1.2; | |
color.b = greencolor.b * 1.2; | |
}else{ | |
color.g = redcolor.b * 1.2; | |
color.r = greencolor.g * 1.2; | |
} | |
return color; | |
} | |
ENDCG | |
} | |
} | |
Fallback off | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment