Skip to content

Instantly share code, notes, and snippets.

@CouriersRyan
Created June 12, 2023 23:23
Show Gist options
  • Select an option

  • Save CouriersRyan/eea13d99edeef77bacda7db1d3ada95b to your computer and use it in GitHub Desktop.

Select an option

Save CouriersRyan/eea13d99edeef77bacda7db1d3ada95b to your computer and use it in GitHub Desktop.
Post-processing Image Effect Shader for old digital screen.
Shader "custom/digitalScreen"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_scale ("noise scale", Range(2, 800)) = 50
_speed ("speed", Range(0, 10)) = 1
_contrast ("contrast", Range(1, 30)) = 25
}
SubShader
{
Cull Off ZWrite Off ZTest Always
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
float _scale;
float _speed;
float _contrast;
float rand (float2 uv) {
return frac(sin(dot(uv.xy, float2(12.9898, 78.233))) * 43758.5453123);
}
float noise (float2 uv) {
float2 ipos = floor(uv);
float2 fpos = frac(uv);
float o = rand(ipos);
float x = rand(ipos + float2(1, 0));
float y = rand(ipos + float2(0, 1));
float xy = rand(ipos + float2(1, 1));
float2 smooth = smoothstep(0, 1, fpos);
return lerp( lerp(o, x, smooth.x),
lerp(y, xy, smooth.x), smooth.y);
}
float fractal_noise (float2 uv) {
float n = 0;
// fractal noise is created by adding together "octaves" of a noise
// an octave is another noise value that is half the amplitude and double the frequency of the previously added noise
// below the uv is multiplied by a value double the previous. multiplying the uv changes the "frequency" or scale of the noise becuase it scales the underlying grid that is used to create the value noise
// the noise result from each line is multiplied by a value half of the previous value to change the "amplitude" or intensity or just how much that noise contributes to the overall resulting fractal noise.
n = (1 / 2.0) * noise( uv * 1);
n += (1 / 4.0) * noise( uv * 2);
n += (1 / 8.0) * noise( uv * 4);
n += (1 / 16.0) * noise( uv * 8);
return n;
}
struct MeshData
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct Interpolators
{
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
};
Interpolators vert (MeshData v)
{
Interpolators o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
sampler2D _MainTex;
float4 frag (Interpolators i) : SV_Target
{
// create a variable for our uvs
float2 uv = i.uv;
// create a separate uv variable we'll use as our coordinates to sample noise
float2 nUV = uv * _scale;
float2 mUV = nUV;
mUV.x *= 1.777778f;
// create discrete lines by rounding value
nUV.y = floor(nUV.y);
mUV.y = frac(mUV.y);
mUV.x = frac(mUV.x);
mUV -= 0.5f;
mUV = abs(mUV) * 2;
mUV = pow(mUV, 2);
mUV += 0.5f;
//mUV = 1 - mUV;
// the x component we'll use to sample the noise will change over time
nUV.x = _Time.y * _speed;
// sample fractal noise using nUV
float fn = fractal_noise(nUV);
// modify the uvs we'll use to sample the texture using the fractal noise
uv += float2(pow(fn, _contrast), 0);
uv.x = lerp(uv.x, uv.x + 0.005f, mUV.x);
uv.y = lerp(uv.y, uv.y + 0.005f, mUV.y);
// sample the texture
float3 color = tex2D(_MainTex, uv);
color = lerp(color, color*0.7f, mUV.x);
color = lerp(color, color*0.7f, mUV.y);
return float4(color, 1.0);
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment