1 Shader "Hidden/NoiseAndGrain" {
2     Properties {
3         _MainTex (
"Base (RGB)", 2D) = "white" {}
4         _NoiseTex (
"Noise (RGB)", 2D) = "white" {}
5     }
6     
7     CGINCLUDE
8
9         #include
"UnityCG.cginc"
10
11         sampler2D _MainTex;
12         sampler2D _NoiseTex;
13         float4 _NoiseTex_TexelSize;
14         
15         uniform float4 _MainTex_TexelSize;
16
17         uniform float3 _NoisePerChannel;
18         uniform float3 _NoiseTilingPerChannel;
19         uniform float3 _NoiseAmount;
20         uniform float3 _ThreshholdRGB;
21         uniform float3 _MidGrey;
22         
23         
struct v2f
24         {
25             float4 pos : SV_POSITION;
26             float2 uv_screen : TEXCOORD0;
27             float4 uvRg : TEXCOORD1;
28             float2 uvB : TEXCOORD2;
29         };
30         
31         
struct appdata_img2
32         {
33             float4 vertex : POSITION;
34             float2 texcoord : TEXCOORD0;
35             float2 texcoord1 : TEXCOORD1;
36         };
37
38         inline float3 Overlay(float3 m, float3 color) {
39             color = saturate(color);
40             float3 check = step(float3(
0.5,0.5,0.5), color.rgb);
41             float3 result = check * (float3(
1,1,1) - ((float3(1,1,1) - 2*(color.rgb-0.5)) * (1-m.rgb)));
42             result += (
1-check) * (2*color.rgb) * m.rgb;
43             
return result;
44         }
45                         
46         v2f vert (appdata_img2 v)
47         {
48             v2f o;
49             
50             o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
51             
52         #
if UNITY_UV_STARTS_AT_TOP
53             o.uv_screen = v.vertex.xyxy;
54             
if (_MainTex_TexelSize.y < 0)
55                 o.uv_screen.y =
1-o.uv_screen.y;
56         #
else
57                 o.uv_screen = v.vertex.xy;
58         #endif
59             
60             
// different tiling for 3 channels
61             o.uvRg = v.texcoord.xyxy + v.texcoord1.xyxy * _NoiseTilingPerChannel.rrgg * _NoiseTex_TexelSize.xyxy;
62             o.uvB = v.texcoord.xy + v.texcoord1.xy * _NoiseTilingPerChannel.bb * _NoiseTex_TexelSize.xy;
63
64             
return o;
65         }
66
67         float4 frag ( v2f i ) : SV_Target
68         {
69             float4 color = (tex2D (_MainTex, i.uv_screen.xy));
70             
71             
// black & white intensities
72             float2 blackWhiteCurve = Luminance(color.rgb) - _MidGrey.x;
// maybe tweak middle grey
73             blackWhiteCurve.xy = saturate(blackWhiteCurve.xy * _MidGrey.yz);
//float2(1.0/0.8, -1.0/0.2));
74
75             
float finalIntensity = _NoiseAmount.x + max(0.0f, dot(_NoiseAmount.zy, blackWhiteCurve.xy));
76             
77             
// fetching & scaling noise (COMPILER BUG WORKAROUND)
78             float3 m = float3(
0,0,0);
79             m += (tex2D(_NoiseTex, i.uvRg.xy) * float4(
1,0,0,0)).rgb;
80             m += (tex2D(_NoiseTex, i.uvRg.zw) * float4(
0,1,0,0)).rgb;
81             m += (tex2D(_NoiseTex, i.uvB.xy) * float4(
0,0,1,0)).rgb;
82
83             m = saturate(lerp(float3(
0.5,0.5,0.5), m, _NoisePerChannel.rgb * float3(finalIntensity,finalIntensity,finalIntensity) ));
84
85             
return float4(Overlay(m, color.rgb), color.a);
86         }
87
88         float4 fragTmp ( v2f i ) : SV_Target
89         {
90             float4 color = (tex2D (_MainTex, i.uv_screen.xy));
91             
92             
// black & white intensities
93             float2 blackWhiteCurve = Luminance(color.rgb) - _MidGrey.x;
// maybe tweak middle grey
94             blackWhiteCurve.xy = saturate(blackWhiteCurve.xy * _MidGrey.yz);
//float2(1.0/0.8, -1.0/0.2));
95
96             
float finalIntensity = _NoiseAmount.x + max(0.0f, dot(_NoiseAmount.zy, blackWhiteCurve.xy));
97             
98             
// fetching & scaling noise (COMPILER BUG WORKAROUND)
99             float3 m = float3(
0,0,0);
100             m += (tex2D(_NoiseTex, i.uvRg.xy) * float4(
1,0,0,0)).rgb;
101             m += (tex2D(_NoiseTex, i.uvRg.zw) * float4(
0,1,0,0)).rgb;
102             m += (tex2D(_NoiseTex, i.uvB.xy) * float4(
0,0,1,0)).rgb;
103
104             m = saturate(lerp(float3(
0.5,0.5,0.5), m, _NoisePerChannel.rgb * float3(finalIntensity,finalIntensity,finalIntensity)));
105
106             
return float4(m.rgb, color.a);
107         }
108
109         float4 fragOverlayBlend ( v2f i ) : SV_Target
110         {
111             float4 color = tex2D(_MainTex, i.uv_screen.xy);
112             float4 m = tex2D(_NoiseTex, i.uv_screen.xy);
113             
114             
return float4(Overlay(m, color.rgb), color.a);
115         }
116     
117     ENDCG
118     
119     SubShader {
120         ZTest Always Cull Off ZWrite Off Blend Off
121       
122         Pass {
123     
124         CGPROGRAM
125         
126         #pragma vertex vert
127         #pragma fragment frag
128         
129         ENDCG
130          
131         }
132
133         Pass {
134     
135         CGPROGRAM
136         
137         #pragma vertex vert
138         #pragma fragment fragOverlayBlend
139         
140         ENDCG
141          
142         }
143
144         Pass {
145     
146         CGPROGRAM
147         
148         #pragma vertex vert
149         #pragma fragment fragTmp
150         
151         ENDCG
152          
153         }
154     }
155     FallBack Off
156 }


Gõ tìm kiếm nhanh...