[
  {
    "path": "README.md",
    "content": "# unity-vhsglitch\nglitched VHS post-processing shader for Unity3D\n\nThis work is licensed under a Creative Commons Attribution 3.0 Unported License.\nhttp://creativecommons.org/licenses/by/3.0/deed.en_GB\n\nYou are free:\n\nto copy, distribute, display, and perform the work\nto make derivative works\nto make commercial use of the work\n\n\nThis post-processing effect requires Unity Pro\n\nUSAGE:\nAdd VHSPostProcessEffect script to camera.\n\nVHS Glitched Video footage by Christopher Huppertz:\nhttps://www.youtube.com/watch?v=9eFVeErnUzg\n\nLicensed under: Creative Commons Attribution licence (reuse allowed)\n"
  },
  {
    "path": "Scripts/VHSPostProcessEffect.cs",
    "content": "using UnityEngine;\nusing UnityEngine.Video;\n\n[ExecuteInEditMode]\n[AddComponentMenu(\"Image Effects/GlitchEffect\")]\n[RequireComponent(typeof(Camera))]\n[RequireComponent(typeof(VideoPlayer))]\npublic class VHSPostProcessEffect : MonoBehaviour\n{\n\tpublic Shader shader;\n\tpublic VideoClip VHSClip;\n\n\tprivate float _yScanline;\n\tprivate float _xScanline;\n\tprivate Material _material = null;\n\tprivate VideoPlayer _player;\n\n\tvoid OnEnable()\n\t{\n\t\t_material = new Material(shader);\n\t\t_player = GetComponent<VideoPlayer>();\n\t\t_player.isLooping = true;\n\t\t_player.renderMode = VideoRenderMode.APIOnly;\n\t\t_player.audioOutputMode = VideoAudioOutputMode.None;\n\t\t_player.clip = VHSClip;\n\t\t_player.Play();\n\t}\n\n\tvoid OnRenderImage(RenderTexture source, RenderTexture destination)\n\t{\n\t\t_material.SetTexture(\"_VHSTex\", _player.texture);\n\n\t\t_yScanline += Time.deltaTime * 0.01f;\n\t\t_xScanline -= Time.deltaTime * 0.1f;\n\n\t\tif (_yScanline >= 1)\n\t\t{\n\t\t\t_yScanline = Random.value;\n\t\t}\n\t\tif (_xScanline <= 0 || Random.value < 0.05)\n\t\t{\n\t\t\t_xScanline = Random.value;\n\t\t}\n\t\t_material.SetFloat(\"_yScanline\", _yScanline);\n\t\t_material.SetFloat(\"_xScanline\", _xScanline);\n\t\tGraphics.Blit(source, destination, _material);\n\t}\n\n\tprotected void OnDisable()\n\t{\n\t\tif (_material)\n\t\t{\n\t\t\tDestroyImmediate(_material);\n\t\t}\n\t}\n}"
  },
  {
    "path": "Shaders/VHSPostProcessEffect.shader",
    "content": "﻿Shader \"Hidden/VHSPostProcessEffect\" {\n\tProperties {\n\t\t_MainTex (\"Base (RGB)\", 2D) = \"white\" {}\n\t\t_VHSTex (\"Base (RGB)\", 2D) = \"white\" {}\n\t}\n\n\tSubShader {\n\t\tPass {\n\t\t\tZTest Always Cull Off ZWrite Off\n\t\t\tFog { Mode off }\n\t\t\t\t\t\n\t\t\tCGPROGRAM\n\t\t\t#pragma vertex vert_img\n\t\t\t#pragma fragment frag\n\t\t\t#pragma fragmentoption ARB_precision_hint_fastest \n\t\t\t#include \"UnityCG.cginc\"\n\n\t\t\tuniform sampler2D _MainTex;\n\t\t\tuniform sampler2D _VHSTex;\n\t\t\t\n\t\t\tfloat _yScanline;\n\t\t\tfloat _xScanline;\n\t\t\tfloat rand(float3 co){\n\t\t\t     return frac(sin( dot(co.xyz ,float3(12.9898,78.233,45.5432) )) * 43758.5453);\n\t\t\t}\n \n\t\t\tfixed4 frag (v2f_img i) : COLOR{\n\t\t\t\tfixed4 vhs = tex2D (_VHSTex, i.uv);\n\t\t\t\t\n\t\t\t\tfloat dx = 1-abs(distance(i.uv.y, _xScanline));\n\t\t\t\tfloat dy = 1-abs(distance(i.uv.y, _yScanline));\n\t\t\t\t\n\t\t\t\t//float x = ((int)(i.uv.x*320))/320.0;\n\t\t\t\tdy = ((int)(dy*15))/15.0;\n\t\t\t\tdy = dy;\n\t\t\t\ti.uv.x += dy * 0.025 + rand(float3(dy,dy,dy)).r/500;//0.025;\n\t\t\t\t\n\t\t\t\tfloat white = (vhs.r+vhs.g+vhs.b)/3;\n\t\t\t\t\n\t\t\t\tif(dx > 0.99)\n\t\t\t\t\ti.uv.y = _xScanline;\n\t\t\t\t//i.uv.y = step(0.99, dy) * (_yScanline) + step(dy, 0.99) * i.uv.y;\n\t\t\t\t\n\t\t\t\ti.uv.x = i.uv.x % 1;\n\t\t\t\ti.uv.y = i.uv.y % 1;\n\t\t\t\t\n\t\t\t\tfixed4 c = tex2D (_MainTex, i.uv);\n\t\t\t\t\n\t\t\t\tfloat bleed = tex2D(_MainTex, i.uv + float2(0.01, 0)).r;\n\t\t\t\tbleed += tex2D(_MainTex, i.uv + float2(0.02, 0)).r;\n\t\t\t\tbleed += tex2D(_MainTex, i.uv + float2(0.01, 0.01)).r;\n\t\t\t\tbleed += tex2D(_MainTex, i.uv + float2(0.02, 0.02)).r;\n\t\t\t\tbleed /= 6;\n\t\t\t\t\n\t\t\t\tif(bleed > 0.1){\n\t\t\t\t\tvhs += fixed4(bleed * _xScanline, 0, 0, 0);\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\tfloat x = ((int)(i.uv.x*320))/320.0;\n\t\t\t\tfloat y = ((int)(i.uv.y*240))/240.0;\n\t\t\t\t\n\t\t\t\tc -= rand(float3(x, y, _xScanline)) * _xScanline / 5;\n\t\t\t\treturn c + vhs;\n\t\t\t}\n\t\t\tENDCG\n\t\t}\n\t}\nFallback off\n}"
  }
]