• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

190 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Where do I put this shader code? Getting weird error? 1 Answer

Fog not working in my Shader~ 0 Answers

Header for material properties in inspector? 2 Answers

Shader Color 1 Answer

Addition to Standard Shader stopping it from batching? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges