OldFilmEffectShader.shader 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. Shader "MyShaders/OldFilmEffectShader"
  2. {
  3. Properties
  4. {
  5. _MainTex ("Base (RGB)", 2D) = "white" {}
  6. _VignetteTex ("Vignette Texture", 2D) = "white"{}
  7. _ScratchesTex ("Scartches Texture", 2D) = "white"{}
  8. _DustTex ("Dust Texture", 2D) = "white"{}
  9. _SepiaColor ("Sepia Color", Color) = (1,1,1,1)
  10. _EffectAmount ("Old Film Effect Amount", Range(0,1)) = 1.0
  11. _VignetteAmount ("Vignette Opacity", Range(0,1)) = 1.0
  12. _ScratchesYSpeed ("Scratches Y Speed", Float) = 10.0
  13. _ScratchesXSpeed ("Scratches X Speed", Float) = 10.0
  14. _dustXSpeed ("Dust X Speed", Float) = 10.0
  15. _dustYSpeed ("Dust Y Speed", Float) = 10.0
  16. _RandomValue ("Random Value", Float) = 1.0
  17. _Contrast ("Contrast", Float) = 3.0
  18. _distortion ("Distortion", Float) = 0.2
  19. _cubicDistortion ("Cubic Distortion", Float) = 0.6
  20. _scale ("Scale (Zoom)", Float) = 0.8
  21. }
  22. SubShader
  23. {
  24. Pass
  25. {
  26. CGPROGRAM
  27. #pragma vertex vert_img
  28. #pragma fragment frag
  29. #pragma fragmentoption ARB_precision_hint_fastest
  30. #include "UnityCG.cginc"
  31. uniform sampler2D _MainTex;
  32. uniform sampler2D _VignetteTex;
  33. uniform sampler2D _ScratchesTex;
  34. uniform sampler2D _DustTex;
  35. fixed4 _SepiaColor;
  36. fixed _VignetteAmount;
  37. fixed _ScratchesYSpeed;
  38. fixed _ScratchesXSpeed;
  39. fixed _dustXSpeed;
  40. fixed _dustYSpeed;
  41. fixed _EffectAmount;
  42. fixed _RandomValue;
  43. fixed _Contrast;
  44. float _distortion;
  45. float _cubicDistortion;
  46. float _scale;
  47. float2 barrelDistortion(float2 coord)
  48. {
  49. float2 h = coord.xy - float2(0.5, 0.5);
  50. float r2 = h.x * h.x + h.y * h.y;
  51. float f = 1.0 + r2 * (_distortion + _cubicDistortion * sqrt(r2));
  52. return f * _scale * h + 0.5;
  53. }
  54. fixed4 frag(v2f_img i) : COLOR
  55. {
  56. //Get the colors from the RenderTexture and the uv's
  57. //from the v2f_img struct
  58. half2 distortedUV = barrelDistortion(i.uv);
  59. distortedUV = half2(i.uv.x, i.uv.y + (_RandomValue * _SinTime.z * 0.005));
  60. fixed4 renderTex = tex2D(_MainTex, i.uv);
  61. //Get the pixels from the Vignette Texture
  62. fixed4 vignetteTex = tex2D(_VignetteTex, i.uv);
  63. //Process the Scratches UV and pixels
  64. half2 scratchesUV = half2(i.uv.x + (_RandomValue * _SinTime.z * _ScratchesXSpeed),
  65. i.uv.y + (_Time.x * _ScratchesYSpeed));
  66. fixed4 scratchesTex = tex2D(_ScratchesTex, scratchesUV);
  67. //Process the Dust UV and pixels
  68. half2 dustUV = half2(i.uv.x + (_RandomValue * (_SinTime.z * _dustXSpeed)),
  69. i.uv.y + (_RandomValue * (_SinTime.z * _dustYSpeed)));
  70. fixed4 dustTex = tex2D(_DustTex, dustUV);
  71. // get the luminosity values from the render texture using the YIQ values.
  72. fixed lum = dot (fixed3(0.299, 0.587, 0.114), renderTex.rgb);
  73. //Add the constant color to the lum values
  74. fixed4 finalColor = lum + lerp(_SepiaColor, _SepiaColor +
  75. fixed4(0.1f,0.1f,0.1f,1.0f), _RandomValue);
  76. finalColor = pow(finalColor, _Contrast);
  77. //Create a constant white color we can use to adjust opacity of effects
  78. fixed3 constantWhite = fixed3(1,1,1);
  79. //Composite together the different layers to create finsl Screen Effect
  80. finalColor = lerp(finalColor, finalColor * vignetteTex, _VignetteAmount);
  81. finalColor.rgb *= lerp(scratchesTex, constantWhite, (_RandomValue));
  82. finalColor.rgb *= lerp(dustTex.rgb, constantWhite, (_RandomValue * _SinTime.z));
  83. finalColor = lerp(renderTex, finalColor, _EffectAmount);
  84. return finalColor;
  85. }
  86. ENDCG
  87. }
  88. }
  89. FallBack off
  90. }