NoiseEffectShaderYUV.shader 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  2. Shader "Hidden/Noise Shader YUV" {
  3. Properties {
  4. _MainTex ("Base (RGB)", 2D) = "white" {}
  5. _GrainTex ("Base (RGB)", 2D) = "gray" {}
  6. _ScratchTex ("Base (RGB)", 2D) = "gray" {}
  7. }
  8. SubShader {
  9. Pass {
  10. ZTest Always Cull Off ZWrite Off
  11. Fog { Mode off }
  12. CGPROGRAM
  13. #pragma vertex vert
  14. #pragma fragment frag
  15. #pragma fragmentoption ARB_precision_hint_fastest
  16. #include "UnityCG.cginc"
  17. struct v2f {
  18. float4 pos : POSITION;
  19. float2 uv : TEXCOORD0;
  20. float2 uvg : TEXCOORD1; // grain
  21. float2 uvs : TEXCOORD2; // scratch
  22. };
  23. uniform sampler2D _MainTex;
  24. uniform sampler2D _GrainTex;
  25. uniform sampler2D _ScratchTex;
  26. uniform float4 _GrainOffsetScale;
  27. uniform float4 _ScratchOffsetScale;
  28. uniform fixed4 _Intensity; // x=grain, y=scratch
  29. v2f vert (appdata_img v)
  30. {
  31. v2f o;
  32. o.pos = UnityObjectToClipPos (v.vertex);
  33. o.uv = MultiplyUV (UNITY_MATRIX_TEXTURE0, v.texcoord);
  34. o.uvg = v.texcoord.xy * _GrainOffsetScale.zw + _GrainOffsetScale.xy;
  35. o.uvs = v.texcoord.xy * _ScratchOffsetScale.zw + _ScratchOffsetScale.xy;
  36. return o;
  37. }
  38. fixed4 frag (v2f i) : COLOR
  39. {
  40. fixed4 col = tex2D(_MainTex, i.uv);
  41. // convert to YUV
  42. fixed3 yuv;
  43. yuv.x = dot( col.rgb, half3(0.299,0.587,0.114) );
  44. yuv.y = (col.b-yuv.x)*0.492;
  45. yuv.z = (col.r-yuv.x)*0.877;
  46. // sample noise texture and do a signed add
  47. fixed3 grain = tex2D(_GrainTex, i.uvg).rgb * 2 - 1;
  48. yuv.rgb += grain * _Intensity.x;
  49. // convert back to rgb
  50. col.r = yuv.z * 1.140 + yuv.x;
  51. col.g = yuv.z * (-0.581) + yuv.y * (-0.395) + yuv.x;
  52. col.b = yuv.y * 2.032 + yuv.x;
  53. // sample scratch texture and add
  54. fixed3 scratch = tex2D(_ScratchTex, i.uvs).rgb * 2 - 1;
  55. col.rgb += scratch * _Intensity.y;
  56. return col;
  57. }
  58. ENDCG
  59. }
  60. }
  61. Fallback off
  62. }