- Home /
Question by
danelforty · Dec 01, 2019 at 09:41 AM ·
shadershadersshader programmingshader writing
CRT shader but NOT for camera
I found this amazing CRT shader but I want to modify it. This goes with a script in the camera but I dont want to apply the effect to the camera. I want to apply this on a quad and my objective is to affect what is in front of that quad. I dont know anything about shaders so, can anyone do this?
This is the shader:
Shader "PostEffects/CRT"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_NoiseX("NoiseX", Range(0, 1)) = 0
_Offset("Offset", Vector) = (0, 0, 0, 0)
_RGBNoise("RGBNoise", Range(0, 1)) = 0
_SinNoiseWidth("SineNoiseWidth", Float) = 1
_SinNoiseScale("SinNoiseScale", Float) = 1
_SinNoiseOffset("SinNoiseOffset", Float) = 1
_ScanLineTail("Tail", Float) = 0.5
_ScanLineSpeed("TailSpeed", Float) = 100
}
SubShader
{
// No culling or depth
Cull Off ZWrite Off ZTest Always
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 3.0
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
float rand(float2 co) {
return frac(sin(dot(co.xy, float2(12.9898, 78.233))) * 43758.5453);
}
float2 mod(float2 a, float2 b)
{
return a - floor(a / b) * b;
}
sampler2D _MainTex;
float _NoiseX;
float2 _Offset;
float _RGBNoise;
float _SinNoiseWidth;
float _SinNoiseScale;
float _SinNoiseOffset;
float _ScanLineTail;
float _ScanLineSpeed;
fixed4 frag (v2f i) : SV_Target
{
float2 inUV = i.uv;
float2 uv = i.uv - 0.5;
float vignet = length(uv);
uv /= 1 - vignet * 0.2;
float2 texUV = uv + 0.5;
if (max(abs(uv.y) - 0.5, abs(uv.x) - 0.5) > 0)
{
return float4(0, 0, 0, 1);
}
float3 col;
texUV.x += sin(texUV.y * _SinNoiseWidth + _SinNoiseOffset) * _SinNoiseScale;
texUV += _Offset;
texUV.x += (rand(floor(texUV.y * 500) + _Time.y) - 0.5) * _NoiseX;
texUV = mod(texUV, 1);
col.r = tex2D(_MainTex, texUV).r;
col.g = tex2D(_MainTex, texUV - float2(0.002, 0)).g;
col.b = tex2D(_MainTex, texUV - float2(0.004, 0)).b;
if (rand((rand(floor(texUV.y * 500) + _Time.y) - 0.5) + _Time.y) < _RGBNoise)
{
col.r = rand(uv + float2(123 + _Time.y, 0));
col.g = rand(uv + float2(123 + _Time.y, 1));
col.b = rand(uv + float2(123 + _Time.y, 2));
}
float floorX = fmod(inUV.x * _ScreenParams.x / 3, 1);
col.r *= floorX > 0.3333;
col.g *= floorX < 0.3333 || floorX > 0.6666;
col.b *= floorX < 0.6666;
float scanLineColor = sin(_Time.y * 10 + uv.y * 500) / 2 + 0.5;
col *= 0.5 + clamp(scanLineColor + 0.5, 0, 1) * 0.5;
float tail = clamp((frac(uv.y + _Time.y * _ScanLineSpeed) - 1 + _ScanLineTail) / min(_ScanLineTail, 1), 0, 1);
col *= tail;
col *= 1 - vignet * 1.3;
return float4(col, 1);
}
ENDCG
}
}
}
And this is the script that goes with it:
using UnityEngine;
using System.Collections;
[ExecuteInEditMode()]
public class CRT : MonoBehaviour {
[SerializeField]
Material material;
[SerializeField]
[Range(0, 1)]
float noiseX;
public float NoiseX { get { return noiseX; } set { noiseX = value; } }
[SerializeField]
[Range(0, 1)]
float rgbNoise;
public float RGBNoise { get { return rgbNoise; } set { rgbNoise = value; } }
[SerializeField]
[Range(0, 1)]
float sinNoiseScale;
public float SinNoiseScale { get { return sinNoiseScale; } set { sinNoiseScale = value; } }
[SerializeField]
[Range(0, 10)]
float sinNoiseWidth;
public float SinNoiseWidth { get { return sinNoiseWidth; } set { sinNoiseWidth = value; } }
[SerializeField]
float sinNoiseOffset;
public float SinNoiseOffset { get { return sinNoiseOffset; } set { sinNoiseOffset = value; } }
[SerializeField]
Vector2 offset;
public Vector2 Offset { get { return offset; } set { offset = value; } }
[SerializeField]
[Range(0, 2)]
float scanLineTail = 1.5f;
public float ScanLineTail { get { return scanLineTail; } set { scanLineTail = value; } }
[SerializeField]
[Range(-10, 10)]
float scanLineSpeed = 10;
public float ScanLineSpeed { get { return scanLineSpeed; } set { scanLineSpeed = value; } }
// Use this for initialization
void OnRenderImage(RenderTexture src, RenderTexture dest) {
material.SetFloat("_NoiseX", noiseX);
material.SetFloat("_RGBNoise", rgbNoise);
material.SetFloat("_SinNoiseScale", sinNoiseScale);
material.SetFloat("_SinNoiseWidth", sinNoiseWidth);
material.SetFloat("_SinNoiseOffset", sinNoiseOffset);
material.SetFloat("_ScanLineSpeed", scanLineSpeed);
material.SetFloat("_ScanLineTail", scanLineTail);
material.SetVector("_Offset", offset);
Graphics.Blit(src, dest, material);
}
}
Comment