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