Repository: staffantan/unity-vhsglitch Branch: master Commit: 01f8eeccd9c2 Files: 4 Total size: 3.5 KB Directory structure: gitextract_bsseorug/ ├── README.md ├── Scripts/ │ └── VHSPostProcessEffect.cs ├── Shaders/ │ └── VHSPostProcessEffect.shader └── VHSGlitch.unitypackage ================================================ FILE CONTENTS ================================================ ================================================ FILE: README.md ================================================ # unity-vhsglitch glitched VHS post-processing shader for Unity3D 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 This post-processing effect requires Unity Pro USAGE: Add VHSPostProcessEffect script to camera. VHS Glitched Video footage by Christopher Huppertz: https://www.youtube.com/watch?v=9eFVeErnUzg Licensed under: Creative Commons Attribution licence (reuse allowed) ================================================ FILE: Scripts/VHSPostProcessEffect.cs ================================================ using UnityEngine; using UnityEngine.Video; [ExecuteInEditMode] [AddComponentMenu("Image Effects/GlitchEffect")] [RequireComponent(typeof(Camera))] [RequireComponent(typeof(VideoPlayer))] public class VHSPostProcessEffect : MonoBehaviour { public Shader shader; public VideoClip VHSClip; private float _yScanline; private float _xScanline; private Material _material = null; private VideoPlayer _player; void OnEnable() { _material = new Material(shader); _player = GetComponent(); _player.isLooping = true; _player.renderMode = VideoRenderMode.APIOnly; _player.audioOutputMode = VideoAudioOutputMode.None; _player.clip = VHSClip; _player.Play(); } void OnRenderImage(RenderTexture source, RenderTexture destination) { _material.SetTexture("_VHSTex", _player.texture); _yScanline += Time.deltaTime * 0.01f; _xScanline -= Time.deltaTime * 0.1f; if (_yScanline >= 1) { _yScanline = Random.value; } if (_xScanline <= 0 || Random.value < 0.05) { _xScanline = Random.value; } _material.SetFloat("_yScanline", _yScanline); _material.SetFloat("_xScanline", _xScanline); Graphics.Blit(source, destination, _material); } protected void OnDisable() { if (_material) { DestroyImmediate(_material); } } } ================================================ FILE: Shaders/VHSPostProcessEffect.shader ================================================ Shader "Hidden/VHSPostProcessEffect" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} _VHSTex ("Base (RGB)", 2D) = "white" {} } SubShader { Pass { ZTest Always Cull Off ZWrite Off Fog { Mode off } CGPROGRAM #pragma vertex vert_img #pragma fragment frag #pragma fragmentoption ARB_precision_hint_fastest #include "UnityCG.cginc" uniform sampler2D _MainTex; uniform sampler2D _VHSTex; float _yScanline; float _xScanline; float rand(float3 co){ return frac(sin( dot(co.xyz ,float3(12.9898,78.233,45.5432) )) * 43758.5453); } fixed4 frag (v2f_img i) : COLOR{ fixed4 vhs = tex2D (_VHSTex, i.uv); float dx = 1-abs(distance(i.uv.y, _xScanline)); float dy = 1-abs(distance(i.uv.y, _yScanline)); //float x = ((int)(i.uv.x*320))/320.0; dy = ((int)(dy*15))/15.0; dy = dy; i.uv.x += dy * 0.025 + rand(float3(dy,dy,dy)).r/500;//0.025; float white = (vhs.r+vhs.g+vhs.b)/3; if(dx > 0.99) i.uv.y = _xScanline; //i.uv.y = step(0.99, dy) * (_yScanline) + step(dy, 0.99) * i.uv.y; i.uv.x = i.uv.x % 1; i.uv.y = i.uv.y % 1; fixed4 c = tex2D (_MainTex, i.uv); float bleed = tex2D(_MainTex, i.uv + float2(0.01, 0)).r; bleed += tex2D(_MainTex, i.uv + float2(0.02, 0)).r; bleed += tex2D(_MainTex, i.uv + float2(0.01, 0.01)).r; bleed += tex2D(_MainTex, i.uv + float2(0.02, 0.02)).r; bleed /= 6; if(bleed > 0.1){ vhs += fixed4(bleed * _xScanline, 0, 0, 0); } float x = ((int)(i.uv.x*320))/320.0; float y = ((int)(i.uv.y*240))/240.0; c -= rand(float3(x, y, _xScanline)) * _xScanline / 5; return c + vhs; } ENDCG } } Fallback off }