├── README.md ├── ScanlinesEffect.cs └── ScanlinesEffect.shader /README.md: -------------------------------------------------------------------------------- 1 | *Scanlines image effect for Unity* 2 | 3 | How to use: 4 | 5 | -Attach the ScanlinesEffect script to your camera. 6 | 7 | -Set the ScanlinesEffect shader in the shader property. 8 | 9 | -Tweak values. 10 | -------------------------------------------------------------------------------- /ScanlinesEffect.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using UnityEngine; 3 | 4 | [ExecuteInEditMode] 5 | [RequireComponent(typeof(Camera))] 6 | [AddComponentMenu("Image Effects/Camera/Scanlines Effect")] 7 | public class ScanlinesEffect : MonoBehaviour { 8 | public Shader shader; 9 | private Material _material; 10 | 11 | [Range(0, 10)] 12 | public float lineWidth = 2f; 13 | 14 | [Range(0, 1)] 15 | public float hardness = 0.9f; 16 | 17 | [Range(0, 1)] 18 | public float displacementSpeed = 0.1f; 19 | 20 | protected Material material { 21 | get { 22 | if (_material == null) { 23 | _material = new Material(shader); 24 | _material.hideFlags = HideFlags.HideAndDontSave; 25 | } 26 | return _material; 27 | } 28 | } 29 | 30 | private void OnRenderImage(RenderTexture source, RenderTexture destination) { 31 | if (shader == null) 32 | return; 33 | material.SetFloat("_LineWidth", lineWidth); 34 | material.SetFloat("_Hardness", hardness); 35 | material.SetFloat("_Speed", displacementSpeed); 36 | Graphics.Blit(source, destination, material, 0); 37 | } 38 | 39 | void OnDisable() { 40 | if (_material) { 41 | DestroyImmediate(_material); 42 | } 43 | } 44 | } -------------------------------------------------------------------------------- /ScanlinesEffect.shader: -------------------------------------------------------------------------------- 1 | Shader "Custom/Scanlines" { 2 | 3 | Properties { 4 | _MainTex("_Color", 2D) = "white" {} 5 | _LineWidth("Line Width", Float) = 4 6 | _Hardness("Hardness", Float) = 0.9 7 | _Speed("Displacement Speed", Range(0,1)) = 0.1 8 | } 9 | 10 | SubShader { 11 | 12 | Tags {"IgnoreProjector" = "True" "Queue" = "Overlay"} 13 | 14 | Pass { 15 | ZTest Always 16 | Cull Off 17 | ZWrite Off 18 | 19 | Fog{ Mode off } 20 | 21 | CGPROGRAM 22 | 23 | #pragma vertex vert 24 | #pragma fragment frag 25 | #pragma fragmentoption ARB_precision_hint_fastest 26 | #include "UnityCG.cginc" 27 | #pragma target 3.0 28 | 29 | struct v2f { 30 | float4 pos : POSITION; 31 | float2 uv : TEXCOORD0; 32 | float4 scr_pos : TEXCOORD1; 33 | }; 34 | 35 | uniform sampler2D _MainTex; 36 | uniform float _LineWidth; 37 | uniform float _Hardness; 38 | uniform float _Speed; 39 | 40 | v2f vert(appdata_img v) { 41 | v2f o; 42 | o.pos = UnityObjectToClipPos(v.vertex); 43 | o.uv = MultiplyUV(UNITY_MATRIX_TEXTURE0, v.texcoord); 44 | o.scr_pos = ComputeScreenPos(o.pos); 45 | 46 | return o; 47 | } 48 | 49 | half4 frag(v2f i) : COLOR { 50 | half4 color = tex2D(_MainTex, i.uv); 51 | fixed lineSize = _ScreenParams.y*0.005; 52 | float displacement = ((_Time.y*1000)*_Speed)%_ScreenParams.y; 53 | float ps = displacement+(i.scr_pos.y * _ScreenParams.y / i.scr_pos.w); 54 | 55 | return ((int)(ps / floor(_LineWidth*lineSize)) % 2 == 0) ? color : color * float4(_Hardness,_Hardness,_Hardness,1); 56 | } 57 | 58 | ENDCG 59 | } 60 | } 61 | FallBack "Diffuse" 62 | } --------------------------------------------------------------------------------