Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save CouriersRyan/19311409f5232c624c6ac1269c0b04c3 to your computer and use it in GitHub Desktop.
Hologram, flickering screen glitch effect applied over an object.
Shader "Unlit/VertexGlitch"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Strength ("Strength", Range(0, 1)) = 1
_FresnelPower ("Fresnel Power", Range(0, 1)) = 1
}
SubShader
{
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
float _Strength;
float _FresnelPower;
struct appdata
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
float4 posWorld : TEXCOORD2;
float3 normal : TEXCOORD3;
float4 screenPos : TEXCOORD4;
};
float rand3D (float3 uv3)
{
return frac(sin(dot(uv3.xyz, float3(12.9898, 78.233, 84.394))) * 43758.5453123);
}
// modified to take in an extra coordinate z, representing time
float noise (float2 uv, float z) {
float2 ipos = floor(uv);
float2 fpos = frac(uv);
float iz = floor(z);
float fz = frac(z);
float o = rand3D(float3(ipos, iz));
float x = rand3D(float3(ipos, iz) + float3(1, 0, 0));
float y = rand3D(float3(ipos, iz) + float3(0, 1, 0));
float xy = rand3D(float3(ipos, iz) + float3(1, 1, 0));
float oz = rand3D(float3(ipos, iz) + float3(0, 0, 1));
float xz = rand3D(float3(ipos, iz) + float3(1, 0, 1));
float yz = rand3D(float3(ipos, iz) + float3(0, 1, 1));
float xyz = rand3D(float3(ipos, iz) + float3(1, 1, 1));
float2 smooth = smoothstep(0, 1, fpos);
float smoothz = smoothstep(0, 1, fz);
return lerp (
lerp(
lerp(o, x, smooth.x),
lerp(y, xy, smooth.x), smooth.y),
lerp(
lerp(oz, xz, smooth.x),
lerp(yz, xyz, smooth.x), smooth.y),
smoothz);
}
float fractal_noise (float2 uv, int n, float time) {
float fn = 0;
for(int j = 0; j < n; j++)
{
fn += (1.0 / pow(2, j + 1)) * noise(uv * pow(2, j), time * pow(2, j));
}
return fn;
}
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert (appdata v)
{
v2f o;
o.posWorld = mul(unity_ObjectToWorld, v.vertex);
o.vertex = o.posWorld;
o.vertex.x += _Strength * pow((rand3D(float3(floor(o.vertex.y * 10), floor(_Time.y * 0.5f), 0)) - 0.5f), 2);
o.vertex = UnityWorldToClipPos(o.vertex);
o.normal = UnityObjectToWorldNormal(v.normal);
o.screenPos = ComputeScreenPos(o.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
float3 normal = normalize(i.normal);
float2 uv = i.posWorld.xy * (1/i.posWorld.z);
uv = i.screenPos.xy / i.screenPos.w;
float2 mUV = uv * 10;
mUV.y = frac(mUV.y);
mUV.y -= 0.5f;
mUV.y = abs(mUV.y);
mUV.y = pow(mUV.y, 0.5);
// sample the texture
float3 lookDir = normalize(_WorldSpaceCameraPos - i.posWorld);
float fresnel = 1 - saturate(dot(lookDir, normal));
fresnel = pow(fresnel, _FresnelPower);
fixed4 col = tex2D(_MainTex, i.uv) * fresnel;
col.w = lerp(col.w, 0, mUV.y);
return col;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment