Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

TV Static Shader

Discussion in 'Made With Unity' started by dandeentremont, Sep 12, 2015.

  1. dandeentremont

    dandeentremont

    Joined:
    Jan 11, 2013
    Posts:
    24
    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?


    Code (CSharp):
    1. //ddTVStatic shader: Daniel DeEntremont (dandeentremont@gmail.com)
    2. //Applies a television static noise in screen space
    3. //Options for noise colors, resolution, and scaling to camera
    4.  
    5. Shader "ddShaders/ddTVStatic"
    6. {
    7.  
    8.     Properties
    9.     {
    10.         _ColorA ("Color A", Color) = (1,1,1,1)
    11.         _ColorB ("Color B", Color) = (0,0,0,0)
    12.        
    13.         _ResX ("X Resolution", Float) = 100
    14.         _ResY ("Y Resolution", Float) = 200
    15.        
    16.         _ScaleWithZoom("Scale With Cam Distance", Range(0,1)) = 1.0
    17.        
    18.     }
    19.  
    20.     SubShader
    21.     {
    22.         Pass
    23.         {
    24.             CGPROGRAM
    25.  
    26.             #pragma vertex vert
    27.             #pragma fragment frag
    28.             #pragma target 3.0
    29.  
    30.             #include "UnityCG.cginc"
    31.            
    32.             //This produces random values between 0 and 1
    33.             float rand(float2 co)
    34.              {
    35.                      return frac((sin( dot(co.xy , float2(12.345 * _Time.w, 67.890 * _Time.w) )) * 12345.67890+_Time.w));
    36.              }
    37.            
    38.             fixed4 _ColorA;
    39.             fixed4 _ColorB;
    40.            
    41.             float _ResX;
    42.             float _ResY;
    43.             float _ScaleWithZoom;
    44.            
    45.             struct vertexInput
    46.             {
    47.                 float4 vertex : POSITION;
    48.                 float4 texcoord0 : TEXCOORD0;
    49.                 float4 texcoord2 : TEXCOORD2;
    50.             };
    51.            
    52.             struct fragmentInput
    53.             {
    54.                 float4 position : SV_POSITION;//SV_POSITION
    55.                 float4 texcoord0 : TEXCOORD0;
    56.                 float4 camDist : TEXCOORD2;
    57.             };
    58.            
    59.             fragmentInput vert(vertexInput i)
    60.             {
    61.                
    62.                 fragmentInput o;
    63.                 UNITY_INITIALIZE_OUTPUT(fragmentInput, o); //5-28-2016 added to fix d3d11 errors
    64.                
    65.                 o.position = mul (UNITY_MATRIX_MVP, i.vertex);
    66.                 o.texcoord0 = i.texcoord0;
    67.  
    68.                //get the model's origin, so we can calculate the distance to camera (and scale the noise accordingly)
    69.                 float4 modelOrigin = mul(_Object2World, float4(0.0, 0.0, 0.0, 1.0));
    70.                
    71.                 o.camDist.x = distance(_WorldSpaceCameraPos.xyz, modelOrigin.xyz);
    72.                
    73.                 o.camDist.x = lerp(1.0, o.camDist.x, _ScaleWithZoom);
    74.                
    75.                 return o;
    76.                
    77.             }
    78.  
    79.             //5-28-2016 changed VPOS to SV_POSITION to get rid of duplicate semantic error
    80.             fixed4 frag(float4 screenPos : SV_POSITION, fragmentInput i) : SV_Target
    81.             {
    82.                
    83.                 fixed4 sc = fixed4((screenPos.xy), 0.0, 1.0);
    84.                 sc *= 0.001;
    85.                
    86.                 sc.xy -= 0.5;
    87.                 sc.xy *= i.camDist.xx;
    88.                 sc.xy += 0.5;
    89.  
    90.                //round the screen coordinates to give it a blocky appearance
    91.                 sc.x = round(sc.x*_ResX)/_ResX;
    92.                 sc.y = round(sc.y*_ResY)/_ResY;
    93.                
    94.                
    95.                
    96.                 float noise = rand(sc.xy);
    97.                
    98.                 float4 stat = lerp(_ColorA, _ColorB, noise.x);
    99.                
    100.                
    101.                 return fixed4(stat.xyz, 1.0);
    102.             }
    103.  
    104.             ENDCG
    105.         }
    106.     }
    107. }
    108.  
     
    Last edited: May 28, 2016
  2. guyo83

    guyo83

    Joined:
    Jul 3, 2012
    Posts:
    4
    well this doesn't seem to work, thanks for nothing.
     
  3. elias_t

    elias_t

    Joined:
    Sep 17, 2010
    Posts:
    1,366
    The shader works fine!
     
    Fattie and eduardopatricio like this.
  4. KeithDuck

    KeithDuck

    Joined:
    May 11, 2016
    Posts:
    4
    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?
     
  5. dandeentremont

    dandeentremont

    Joined:
    Jan 11, 2013
    Posts:
    24
    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
     
  6. liamjcarlisle

    liamjcarlisle

    Joined:
    Sep 6, 2016
    Posts:
    1
    Thanks!! Getting some cool effects with this :)
     
  7. jvrdzk

    jvrdzk

    Joined:
    Nov 21, 2016
    Posts:
    1
    Thanks Dan!
     
  8. NOoooRyuk

    NOoooRyuk

    Joined:
    Mar 7, 2017
    Posts:
    6
    Thanks a lot !! But can you help to works on Dx9 ?
     
  9. eduardopatricio

    eduardopatricio

    Joined:
    Jun 18, 2018
    Posts:
    2
    That's perfect! It works great. :)

    Thank you!!!
     
  10. Talis_Renston

    Talis_Renston

    Joined:
    Oct 24, 2016
    Posts:
    19
    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.
     
  11. RegahProd23

    RegahProd23

    Joined:
    Jul 22, 2019
    Posts:
    5
    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!
     
  12. vampire_computer_people

    vampire_computer_people

    Joined:
    Feb 1, 2020
    Posts:
    2
    i am also interested in an alpha setting

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

    TyroByte

    Joined:
    Mar 16, 2017
    Posts:
    25
    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?