Skip to content

Instantly share code, notes, and snippets.

@pencilking2002
Created April 30, 2020 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pencilking2002/711cd24aa20466736cbcc684aec4ccbf to your computer and use it in GitHub Desktop.
Save pencilking2002/711cd24aa20466736cbcc684aec4ccbf to your computer and use it in GitHub Desktop.
Shader used in the Sleepwatchers game to display and distort a web cam feed
Shader "Unlit/CameraFeedShader"
{
Properties
{
_MainTex ("Main Texture", 2D) = "white" {}
_TintColor ("Tint Color", Color) = (1,1,1,1)
_SecondaryTex ("Secondary Texture", 2D) = "black" {}
_ScrollSpeed ("Scroll Speed", Float) = 8.0
_NoiseTex ("Noise", 2D) = "white" {}
_NoiseScrollSpeed("Noise Scroll Speed", Float) = 8.0
_StaticScrollSpeed("Static Scroll Speed", Float) = 8.0
_DimStrength ("Texture Strength", Float) = 10
_IsStatic ("Is Static Texture", Range(0,1)) = 1
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float2 uv2 : TEXCOORD2;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
sampler2D _NoiseTex;
sampler2D _SecondaryTex;
float4 _SecondaryTex_ST;
float4 _MainTex_ST;
half4 _TintColor;
half _ScrollSpeed;
half _NoiseScrollSpeed;
half _DimStrength;
float _IsStatic;
float _StaticScrollSpeed;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.uv2 = TRANSFORM_TEX(v.uv, _SecondaryTex);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// Sample our noise texture or we cam feed
fixed4 noise = tex2D(_NoiseTex, i.uv);
// This is where we use our IsStatic "bool" in a ternary operator. What this does is switch between menu and game mode.
// In the menu mode the main texture (Which at that point is a static tv texture) is animated
// in the horizontal and vertical directions. While in the game mode, the main texture is used but is also distorted by
// by our noise texture over time
// Notice the use of the frac() function, I am finding this to be one of the most useful shader functions,
// it essentially takes a number like 1.2 and spits out only the decimal, leaving you with 0.2
fixed4 col = _IsStatic > 0.8 ?
tex2D(_MainTex, float2(i.uv.x * _StaticScrollSpeed + frac(_Time.x/20), i.uv.y + frac(_Time.x/10))) * 1.5:
tex2D(_MainTex, i.uv + _Time.x * noise.r * _NoiseScrollSpeed);
// This is where the color of the scan lines get computed
// They are animated (moved) vertically
fixed4 secColor = tex2D(_SecondaryTex, half2(i.uv2.x, i.uv2.y + _Time.x * _ScrollSpeed));
// Applies the tint color to our static/cam feed
col.rgb *= _TintColor.rgb;
// Overlays the scan lines over our color so far
col += secColor/10;
// Modulates the brightness/dimness of our color
col.rgb /= _DimStrength;
// Makes sure our shader works with fog. Honestly this isn't doing anything right now, so ignore it :)
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment