TV Static Shader

Hello, everyone,

I’ve got a fun little TV Static shader that overlays on a mesh in screen space. You can customize a few parameters, such as color and blockiness. Perhaps it can be used for displaying characters and items yet to be unlocked?

//ddTVStatic shader: Daniel DeEntremont (dandeentremont@gmail.com)
//Applies a television static noise in screen space
//Options for noise colors, resolution, and scaling to camera

Shader "ddShaders/ddTVStatic"
{

    Properties
    {
        _ColorA ("Color A", Color) = (1,1,1,1)
        _ColorB ("Color B", Color) = (0,0,0,0)
       
        _ResX ("X Resolution", Float) = 100
        _ResY ("Y Resolution", Float) = 200
       
        _ScaleWithZoom("Scale With Cam Distance", Range(0,1)) = 1.0
       
    }

    SubShader
    {
        Pass
        {
            CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag
            #pragma target 3.0

            #include "UnityCG.cginc"
           
            //This produces random values between 0 and 1
            float rand(float2 co)
             {
                     return frac((sin( dot(co.xy , float2(12.345 * _Time.w, 67.890 * _Time.w) )) * 12345.67890+_Time.w));
             }
           
            fixed4 _ColorA;
            fixed4 _ColorB;
           
            float _ResX;
            float _ResY;
            float _ScaleWithZoom;
           
            struct vertexInput
            {
                float4 vertex : POSITION;
                float4 texcoord0 : TEXCOORD0;
                float4 texcoord2 : TEXCOORD2;
            };
           
            struct fragmentInput
            {
                float4 position : SV_POSITION;//SV_POSITION
                float4 texcoord0 : TEXCOORD0;
                float4 camDist : TEXCOORD2;
            };
           
            fragmentInput vert(vertexInput i)
            {
               
                fragmentInput o;
                UNITY_INITIALIZE_OUTPUT(fragmentInput, o); //5-28-2016 added to fix d3d11 errors
               
                o.position = mul (UNITY_MATRIX_MVP, i.vertex);
                o.texcoord0 = i.texcoord0;

               //get the model's origin, so we can calculate the distance to camera (and scale the noise accordingly)
                float4 modelOrigin = mul(_Object2World, float4(0.0, 0.0, 0.0, 1.0));
               
                o.camDist.x = distance(_WorldSpaceCameraPos.xyz, modelOrigin.xyz);
               
                o.camDist.x = lerp(1.0, o.camDist.x, _ScaleWithZoom);
               
                return o;
               
            }

            //5-28-2016 changed VPOS to SV_POSITION to get rid of duplicate semantic error
            fixed4 frag(float4 screenPos : SV_POSITION, fragmentInput i) : SV_Target
            {
               
                fixed4 sc = fixed4((screenPos.xy), 0.0, 1.0);
                sc *= 0.001;
               
                sc.xy -= 0.5;
                sc.xy *= i.camDist.xx;
                sc.xy += 0.5;

               //round the screen coordinates to give it a blocky appearance
                sc.x = round(sc.x*_ResX)/_ResX;
                sc.y = round(sc.y*_ResY)/_ResY;
               
               
               
                float noise = rand(sc.xy);
               
                float4 stat = lerp(_ColorA, _ColorB, noise.x);
               
               
                return fixed4(stat.xyz, 1.0);
            }

            ENDCG
        }
    }
}
15 Likes

well this doesn’t seem to work, thanks for nothing.

The shader works fine!

2 Likes

Hi, I’m new to shaders and I tried this in Unity 5.3.4f1 but I got the following error when it tried to compile:

Duplicate system value semantic definition: input semantic ‘VPOS’ and input semantic ‘SV_POSITION’
Compiling Vertex program
Platform defines: UNITY_NO_SCREENSPACE_SHADOWS UNITY_ENABLE_REFLECTION_BUFFERS UNITY_PBS_USE_BRDF1 UNITY_SPECCUBE_BOX_PROJECTION UNITY_SPECCUBE_BLENDING SHADER_API_DESKTOP

Any idea what I’m doing wrong?

Hi, KeithDuck

Sorry for replying so late!

Looks like I had a duplicate semantic value in there.
Changing “VPOS” to “SV_POSITION” fixed it.
I’ve updated the shader at the top.

Take care!

-Dan

4 Likes

Thanks!! Getting some cool effects with this :slight_smile:

Thanks Dan!

Thanks a lot !! But can you help to works on Dx9 ?

That’s perfect! It works great. :slight_smile:

Thank you!!!

This thread is a bit old, but still: Thank you, I was looking for something just like this. My shading skills are still a bit low, so this really helps me to get an understanding as well.

Is there a way to add alpha to this? possibly, to make the blacks transparent, so that i can play this over another object and make the stuff behind it look like it’s noisy? I’m trying to add a bit of a noisy bad quality camera look to a screen. Thanks!

i am also interested in an alpha setting

also curious - could this be applied to a 2d sprite renderer, and if so how?

Tested this in Unity 2017.4 LTS and it works but the object the material is applied on is transparent sort of. ie; you can see the objects behind the noisy objects. Can anyone fix that?