-
Notifications
You must be signed in to change notification settings - Fork 78
/
GlitchEffect.cs
104 lines (84 loc) · 2.6 KB
/
GlitchEffect.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
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
*/
using UnityEngine;
[ExecuteInEditMode]
[AddComponentMenu("Image Effects/GlitchEffect")]
[RequireComponent(typeof(Camera))]
public class GlitchEffect : MonoBehaviour
{
public Texture2D displacementMap;
public Shader Shader;
[Header("Glitch Intensity")]
[Range(0, 1)]
public float intensity;
[Range(0, 1)]
public float flipIntensity;
[Range(0, 1)]
public float colorIntensity;
private float _glitchup;
private float _glitchdown;
private float flicker;
private float _glitchupTime = 0.05f;
private float _glitchdownTime = 0.05f;
private float _flickerTime = 0.5f;
private Material _material;
void Start()
{
_material = new Material(Shader);
}
// Called by camera to apply image effect
void OnRenderImage(RenderTexture source, RenderTexture destination)
{
_material.SetFloat("_Intensity", intensity);
_material.SetFloat("_ColorIntensity", colorIntensity);
_material.SetTexture("_DispTex", displacementMap);
flicker += Time.deltaTime * colorIntensity;
if (flicker > _flickerTime)
{
_material.SetFloat("filterRadius", Random.Range(-3f, 3f) * colorIntensity);
_material.SetVector("direction", Quaternion.AngleAxis(Random.Range(0, 360) * colorIntensity, Vector3.forward) * Vector4.one);
flicker = 0;
_flickerTime = Random.value;
}
if (colorIntensity == 0)
_material.SetFloat("filterRadius", 0);
_glitchup += Time.deltaTime * flipIntensity;
if (_glitchup > _glitchupTime)
{
if (Random.value < 0.1f * flipIntensity)
_material.SetFloat("flip_up", Random.Range(0, 1f) * flipIntensity);
else
_material.SetFloat("flip_up", 0);
_glitchup = 0;
_glitchupTime = Random.value / 10f;
}
if (flipIntensity == 0)
_material.SetFloat("flip_up", 0);
_glitchdown += Time.deltaTime * flipIntensity;
if (_glitchdown > _glitchdownTime)
{
if (Random.value < 0.1f * flipIntensity)
_material.SetFloat("flip_down", 1 - Random.Range(0, 1f) * flipIntensity);
else
_material.SetFloat("flip_down", 1);
_glitchdown = 0;
_glitchdownTime = Random.value / 10f;
}
if (flipIntensity == 0)
_material.SetFloat("flip_down", 1);
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);
}
}