Debian Codegraph @

examples/screen_effects_demo/data/screen_effects/film_grain.fs

raw
1 uniform sampler2D grainTexture;
2 uniform float time;
3
4 /* Larger values -> grain is more noticeable, 0 = no grain is visible */
5 const float strength = 0.10;
6 /* Larger values -> grain is "smaller dots" */
7 const float density = 5.0;
8
9 void main (void)
10 {
11 gl_FragColor = screenf_get_color(screenf_position());
12
13 /* Squeeze grainCoord into [0..1,0..1]. */
14 float screen_size = float(max(screen_width, screen_height));
15 vec2 grainCoord = density *
16 vec2(screen_position()) /
17 vec2(screen_size, screen_size);
18
19 /* Grain changes constanly, to make the noisy effect.
20 We could use a couple of textures (MovieTexture with noises),
21 but we can also instead add semi-random shift to grainCoord
22 to effectively get a different noise.
23
24 I experimented with changing noise smoothly,
25 e.g. in 0.25 of a second one noise changes into other.
26 But it looks bad. Simply using random independent noise slice
27 at each frame looks best. */
28 grainCoord += vec2(time * 789.0, time * 456.0);
29
30 float grain = strength * (
31 /* Take a couple of noise layers.
32 It's best to use somewhat distorted weights (not just 2, 4...)
33 for nice irregular but smooth effect. */
34 (texture2D(grainTexture, grainCoord / 1.1).r +
35 texture2D(grainTexture, grainCoord / 3.3).r)
36 - 1.0 /* -0.5 for each noise layer */
37 )
38 // on average, to not make the screen brighter
39 - strength / 2.0;
40
41 gl_FragColor.rgb += vec3(grain);
42 }